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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .procname = "speed_limit_min", |
| 102 | .data = &sysctl_speed_limit_min, |
| 103 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 104 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 105 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }, |
| 107 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .procname = "speed_limit_max", |
| 109 | .data = &sysctl_speed_limit_max, |
| 110 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 111 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 112 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 114 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static ctl_table raid_dir_table[] = { |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "raid", |
| 120 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 121 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .child = raid_table, |
| 123 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 124 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static ctl_table raid_root_table[] = { |
| 128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .procname = "dev", |
| 130 | .maxlen = 0, |
| 131 | .mode = 0555, |
| 132 | .child = raid_dir_table, |
| 133 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 134 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 137 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 139 | static int start_readonly; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 142 | * We have a system wide 'event count' that is incremented |
| 143 | * on any 'interesting' event, and readers of /proc/mdstat |
| 144 | * can use 'poll' or 'select' to find out when the event |
| 145 | * count increases. |
| 146 | * |
| 147 | * Events are: |
| 148 | * start array, stop array, error, add device, remove device, |
| 149 | * start build, activate spare |
| 150 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 151 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 152 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 153 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 154 | { |
| 155 | atomic_inc(&md_event_count); |
| 156 | wake_up(&md_event_waiters); |
| 157 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 159 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 160 | /* Alternate version that can be called from interrupts |
| 161 | * when calling sysfs_notify isn't needed. |
| 162 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 163 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 164 | { |
| 165 | atomic_inc(&md_event_count); |
| 166 | wake_up(&md_event_waiters); |
| 167 | } |
| 168 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * Enables to iterate over all existing md arrays |
| 171 | * all_mddevs_lock protects this list. |
| 172 | */ |
| 173 | static LIST_HEAD(all_mddevs); |
| 174 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | * iterates through all used mddevs in the system. |
| 179 | * We take care to grab the all_mddevs_lock whenever navigating |
| 180 | * the list, and to always hold a refcount when unlocked. |
| 181 | * Any code which breaks out of this loop while own |
| 182 | * a reference to the current mddev and must mddev_put it. |
| 183 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 184 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | \ |
| 186 | for (({ spin_lock(&all_mddevs_lock); \ |
| 187 | tmp = all_mddevs.next; \ |
| 188 | mddev = NULL;}); \ |
| 189 | ({ if (tmp != &all_mddevs) \ |
| 190 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 191 | spin_unlock(&all_mddevs_lock); \ |
| 192 | if (mddev) mddev_put(mddev); \ |
| 193 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 194 | tmp != &all_mddevs;}); \ |
| 195 | ({ spin_lock(&all_mddevs_lock); \ |
| 196 | tmp = tmp->next;}) \ |
| 197 | ) |
| 198 | |
| 199 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 200 | /* Rather than calling directly into the personality make_request function, |
| 201 | * IO requests come here first so that we can check if the device is |
| 202 | * being suspended pending a reconfiguration. |
| 203 | * We hold a refcount over the call to ->make_request. By the time that |
| 204 | * call has finished, the bio has been linked into some internal structure |
| 205 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 206 | */ |
| 207 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 209 | mddev_t *mddev = q->queuedata; |
| 210 | int rv; |
| 211 | if (mddev == NULL || mddev->pers == NULL) { |
| 212 | bio_io_error(bio); |
| 213 | return 0; |
| 214 | } |
| 215 | rcu_read_lock(); |
| 216 | if (mddev->suspended) { |
| 217 | DEFINE_WAIT(__wait); |
| 218 | for (;;) { |
| 219 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 220 | TASK_UNINTERRUPTIBLE); |
| 221 | if (!mddev->suspended) |
| 222 | break; |
| 223 | rcu_read_unlock(); |
| 224 | schedule(); |
| 225 | rcu_read_lock(); |
| 226 | } |
| 227 | finish_wait(&mddev->sb_wait, &__wait); |
| 228 | } |
| 229 | atomic_inc(&mddev->active_io); |
| 230 | rcu_read_unlock(); |
| 231 | rv = mddev->pers->make_request(q, bio); |
| 232 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 233 | wake_up(&mddev->sb_wait); |
| 234 | |
| 235 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 238 | static void mddev_suspend(mddev_t *mddev) |
| 239 | { |
| 240 | BUG_ON(mddev->suspended); |
| 241 | mddev->suspended = 1; |
| 242 | synchronize_rcu(); |
| 243 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 244 | mddev->pers->quiesce(mddev, 1); |
| 245 | md_unregister_thread(mddev->thread); |
| 246 | mddev->thread = NULL; |
| 247 | /* we now know that no code is executing in the personality module, |
| 248 | * except possibly the tail end of a ->bi_end_io function, but that |
| 249 | * is certain to complete before the module has a chance to get |
| 250 | * unloaded |
| 251 | */ |
| 252 | } |
| 253 | |
| 254 | static void mddev_resume(mddev_t *mddev) |
| 255 | { |
| 256 | mddev->suspended = 0; |
| 257 | wake_up(&mddev->sb_wait); |
| 258 | mddev->pers->quiesce(mddev, 0); |
| 259 | } |
| 260 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 261 | int mddev_congested(mddev_t *mddev, int bits) |
| 262 | { |
| 263 | return mddev->suspended; |
| 264 | } |
| 265 | EXPORT_SYMBOL(mddev_congested); |
| 266 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 269 | { |
| 270 | atomic_inc(&mddev->active); |
| 271 | return mddev; |
| 272 | } |
| 273 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 274 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | static void mddev_put(mddev_t *mddev) |
| 277 | { |
| 278 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 279 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 280 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 281 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 283 | if (mddev->gendisk) { |
| 284 | /* we did a probe so need to clean up. |
| 285 | * Call schedule_work inside the spinlock |
| 286 | * so that flush_scheduled_work() after |
| 287 | * mddev_find will succeed in waiting for the |
| 288 | * work to be done. |
| 289 | */ |
| 290 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 291 | schedule_work(&mddev->del_work); |
| 292 | } else |
| 293 | kfree(mddev); |
| 294 | } |
| 295 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static mddev_t * mddev_find(dev_t unit) |
| 299 | { |
| 300 | mddev_t *mddev, *new = NULL; |
| 301 | |
| 302 | retry: |
| 303 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 305 | if (unit) { |
| 306 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 307 | if (mddev->unit == unit) { |
| 308 | mddev_get(mddev); |
| 309 | spin_unlock(&all_mddevs_lock); |
| 310 | kfree(new); |
| 311 | return mddev; |
| 312 | } |
| 313 | |
| 314 | if (new) { |
| 315 | list_add(&new->all_mddevs, &all_mddevs); |
| 316 | spin_unlock(&all_mddevs_lock); |
| 317 | new->hold_active = UNTIL_IOCTL; |
| 318 | return new; |
| 319 | } |
| 320 | } else if (new) { |
| 321 | /* find an unused unit number */ |
| 322 | static int next_minor = 512; |
| 323 | int start = next_minor; |
| 324 | int is_free = 0; |
| 325 | int dev = 0; |
| 326 | while (!is_free) { |
| 327 | dev = MKDEV(MD_MAJOR, next_minor); |
| 328 | next_minor++; |
| 329 | if (next_minor > MINORMASK) |
| 330 | next_minor = 0; |
| 331 | if (next_minor == start) { |
| 332 | /* Oh dear, all in use. */ |
| 333 | spin_unlock(&all_mddevs_lock); |
| 334 | kfree(new); |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | is_free = 1; |
| 339 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 340 | if (mddev->unit == dev) { |
| 341 | is_free = 0; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | new->unit = dev; |
| 346 | new->md_minor = MINOR(dev); |
| 347 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | list_add(&new->all_mddevs, &all_mddevs); |
| 349 | spin_unlock(&all_mddevs_lock); |
| 350 | return new; |
| 351 | } |
| 352 | spin_unlock(&all_mddevs_lock); |
| 353 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 354 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (!new) |
| 356 | return NULL; |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | new->unit = unit; |
| 359 | if (MAJOR(unit) == MD_MAJOR) |
| 360 | new->md_minor = MINOR(unit); |
| 361 | else |
| 362 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 363 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 364 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 365 | mutex_init(&new->reconfig_mutex); |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 366 | mutex_init(&new->bitmap_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | INIT_LIST_HEAD(&new->disks); |
| 368 | INIT_LIST_HEAD(&new->all_mddevs); |
| 369 | init_timer(&new->safemode_timer); |
| 370 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 371 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 372 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 373 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 374 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 375 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 376 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 377 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 378 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 379 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | goto retry; |
| 382 | } |
| 383 | |
| 384 | static inline int mddev_lock(mddev_t * mddev) |
| 385 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 386 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 389 | static inline int mddev_is_locked(mddev_t *mddev) |
| 390 | { |
| 391 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 392 | } |
| 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | static inline int mddev_trylock(mddev_t * mddev) |
| 395 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 396 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static inline void mddev_unlock(mddev_t * mddev) |
| 400 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 401 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 403 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 406 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 408 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 410 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (rdev->desc_nr == nr) |
| 412 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 418 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | mdk_rdev_t *rdev; |
| 420 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 421 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (rdev->bdev->bd_dev == dev) |
| 423 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 428 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 429 | { |
| 430 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 431 | list_for_each_entry(pers, &pers_list, list) { |
| 432 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 433 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 434 | if (strcmp(pers->name, clevel)==0) |
| 435 | return pers; |
| 436 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 440 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 441 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 443 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 444 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 448 | { |
| 449 | if (rdev->sb_page) |
| 450 | MD_BUG(); |
| 451 | |
| 452 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 453 | if (!rdev->sb_page) { |
| 454 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 455 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 462 | { |
| 463 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 464 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | rdev->sb_loaded = 0; |
| 466 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 467 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 468 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 473 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 474 | { |
| 475 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 476 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 477 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 478 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 479 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 480 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 481 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 482 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 483 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 484 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 485 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 486 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 487 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 490 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 491 | { |
| 492 | struct bio *bio2 = bio->bi_private; |
| 493 | mdk_rdev_t *rdev = bio2->bi_private; |
| 494 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 495 | |
| 496 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 497 | error == -EOPNOTSUPP) { |
| 498 | unsigned long flags; |
| 499 | /* barriers don't appear to be supported :-( */ |
| 500 | set_bit(BarriersNotsupp, &rdev->flags); |
| 501 | mddev->barriers_work = 0; |
| 502 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 503 | bio2->bi_next = mddev->biolist; |
| 504 | mddev->biolist = bio2; |
| 505 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 506 | wake_up(&mddev->sb_wait); |
| 507 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 508 | } else { |
| 509 | bio_put(bio2); |
| 510 | bio->bi_private = rdev; |
| 511 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 512 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 513 | } |
| 514 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 515 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 516 | sector_t sector, int size, struct page *page) |
| 517 | { |
| 518 | /* write first size bytes of page to sector of rdev |
| 519 | * Increment mddev->pending_writes before returning |
| 520 | * and decrement it on completion, waking up sb_wait |
| 521 | * if zero is reached. |
| 522 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 523 | * |
| 524 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 525 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 526 | */ |
| 527 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 528 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 529 | |
| 530 | bio->bi_bdev = rdev->bdev; |
| 531 | bio->bi_sector = sector; |
| 532 | bio_add_page(bio, page, size, 0); |
| 533 | bio->bi_private = rdev; |
| 534 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 535 | bio->bi_rw = rw; |
| 536 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 537 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 538 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 539 | struct bio *rbio; |
| 540 | rw |= (1<<BIO_RW_BARRIER); |
| 541 | rbio = bio_clone(bio, GFP_NOIO); |
| 542 | rbio->bi_private = bio; |
| 543 | rbio->bi_end_io = super_written_barrier; |
| 544 | submit_bio(rw, rbio); |
| 545 | } else |
| 546 | submit_bio(rw, bio); |
| 547 | } |
| 548 | |
| 549 | void md_super_wait(mddev_t *mddev) |
| 550 | { |
| 551 | /* wait for all superblock writes that were scheduled to complete. |
| 552 | * if any had to be retried (due to BARRIER problems), retry them |
| 553 | */ |
| 554 | DEFINE_WAIT(wq); |
| 555 | for(;;) { |
| 556 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 557 | if (atomic_read(&mddev->pending_writes)==0) |
| 558 | break; |
| 559 | while (mddev->biolist) { |
| 560 | struct bio *bio; |
| 561 | spin_lock_irq(&mddev->write_lock); |
| 562 | bio = mddev->biolist; |
| 563 | mddev->biolist = bio->bi_next ; |
| 564 | bio->bi_next = NULL; |
| 565 | spin_unlock_irq(&mddev->write_lock); |
| 566 | submit_bio(bio->bi_rw, bio); |
| 567 | } |
| 568 | schedule(); |
| 569 | } |
| 570 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 571 | } |
| 572 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 573 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 578 | 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] | 579 | struct page *page, int rw) |
| 580 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 581 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | struct completion event; |
| 583 | int ret; |
| 584 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 585 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | bio->bi_bdev = bdev; |
| 588 | bio->bi_sector = sector; |
| 589 | bio_add_page(bio, page, size, 0); |
| 590 | init_completion(&event); |
| 591 | bio->bi_private = &event; |
| 592 | bio->bi_end_io = bi_complete; |
| 593 | submit_bio(rw, bio); |
| 594 | wait_for_completion(&event); |
| 595 | |
| 596 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 597 | bio_put(bio); |
| 598 | return ret; |
| 599 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 600 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 602 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
| 604 | char b[BDEVNAME_SIZE]; |
| 605 | if (!rdev->sb_page) { |
| 606 | MD_BUG(); |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | if (rdev->sb_loaded) |
| 610 | return 0; |
| 611 | |
| 612 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 613 | 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] | 614 | goto fail; |
| 615 | rdev->sb_loaded = 1; |
| 616 | return 0; |
| 617 | |
| 618 | fail: |
| 619 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 620 | bdevname(rdev->bdev,b)); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 625 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 626 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 627 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 628 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 629 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 633 | { |
| 634 | int ret; |
| 635 | mdp_super_t *tmp1, *tmp2; |
| 636 | |
| 637 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 638 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 639 | |
| 640 | if (!tmp1 || !tmp2) { |
| 641 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 642 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | goto abort; |
| 644 | } |
| 645 | |
| 646 | *tmp1 = *sb1; |
| 647 | *tmp2 = *sb2; |
| 648 | |
| 649 | /* |
| 650 | * nr_disks is not constant |
| 651 | */ |
| 652 | tmp1->nr_disks = 0; |
| 653 | tmp2->nr_disks = 0; |
| 654 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 655 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 657 | kfree(tmp1); |
| 658 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 662 | |
| 663 | static u32 md_csum_fold(u32 csum) |
| 664 | { |
| 665 | csum = (csum & 0xffff) + (csum >> 16); |
| 666 | return (csum & 0xffff) + (csum >> 16); |
| 667 | } |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 670 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 671 | u64 newcsum = 0; |
| 672 | u32 *sb32 = (u32*)sb; |
| 673 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | unsigned int disk_csum, csum; |
| 675 | |
| 676 | disk_csum = sb->sb_csum; |
| 677 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 678 | |
| 679 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 680 | newcsum += sb32[i]; |
| 681 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 682 | |
| 683 | |
| 684 | #ifdef CONFIG_ALPHA |
| 685 | /* This used to use csum_partial, which was wrong for several |
| 686 | * reasons including that different results are returned on |
| 687 | * different architectures. It isn't critical that we get exactly |
| 688 | * the same return value as before (we always csum_fold before |
| 689 | * testing, and that removes any differences). However as we |
| 690 | * know that csum_partial always returned a 16bit value on |
| 691 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 692 | */ |
| 693 | sb->sb_csum = md_csum_fold(disk_csum); |
| 694 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 696 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | return csum; |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /* |
| 702 | * Handle superblock details. |
| 703 | * We want to be able to handle multiple superblock formats |
| 704 | * so we have a common interface to them all, and an array of |
| 705 | * different handlers. |
| 706 | * We rely on user-space to write the initial superblock, and support |
| 707 | * reading and updating of superblocks. |
| 708 | * Interface methods are: |
| 709 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 710 | * loads and validates a superblock on dev. |
| 711 | * if refdev != NULL, compare superblocks on both devices |
| 712 | * Return: |
| 713 | * 0 - dev has a superblock that is compatible with refdev |
| 714 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 715 | * so dev should be used as the refdev in future |
| 716 | * -EINVAL superblock incompatible or invalid |
| 717 | * -othererror e.g. -EIO |
| 718 | * |
| 719 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 720 | * Verify that dev is acceptable into mddev. |
| 721 | * The first time, mddev->raid_disks will be 0, and data from |
| 722 | * dev should be merged in. Subsequent calls check that dev |
| 723 | * is new enough. Return 0 or -EINVAL |
| 724 | * |
| 725 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 726 | * Update the superblock for rdev with data in mddev |
| 727 | * This does not write to disc. |
| 728 | * |
| 729 | */ |
| 730 | |
| 731 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 732 | char *name; |
| 733 | struct module *owner; |
| 734 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 735 | int minor_version); |
| 736 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 737 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 738 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 739 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 743 | * Check that the given mddev has no bitmap. |
| 744 | * |
| 745 | * This function is called from the run method of all personalities that do not |
| 746 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 747 | * has a bitmap. Otherwise, it returns 0. |
| 748 | * |
| 749 | */ |
| 750 | int md_check_no_bitmap(mddev_t *mddev) |
| 751 | { |
| 752 | if (!mddev->bitmap_file && !mddev->bitmap_offset) |
| 753 | return 0; |
| 754 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 755 | mdname(mddev), mddev->pers->name); |
| 756 | return 1; |
| 757 | } |
| 758 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 759 | |
| 760 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | * load_super for 0.90.0 |
| 762 | */ |
| 763 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 764 | { |
| 765 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 766 | mdp_super_t *sb; |
| 767 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 770 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | * it's at the end of the disk. |
| 772 | * |
| 773 | * It also happens to be a multiple of 4Kb. |
| 774 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 775 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 777 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (ret) return ret; |
| 779 | |
| 780 | ret = -EINVAL; |
| 781 | |
| 782 | bdevname(rdev->bdev, b); |
| 783 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 784 | |
| 785 | if (sb->md_magic != MD_SB_MAGIC) { |
| 786 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 787 | b); |
| 788 | goto abort; |
| 789 | } |
| 790 | |
| 791 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 792 | sb->minor_version < 90 || |
| 793 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 795 | sb->major_version, sb->minor_version, |
| 796 | b); |
| 797 | goto abort; |
| 798 | } |
| 799 | |
| 800 | if (sb->raid_disks <= 0) |
| 801 | goto abort; |
| 802 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 803 | 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] | 804 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 805 | b); |
| 806 | goto abort; |
| 807 | } |
| 808 | |
| 809 | rdev->preferred_minor = sb->md_minor; |
| 810 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 811 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
| 813 | if (sb->level == LEVEL_MULTIPATH) |
| 814 | rdev->desc_nr = -1; |
| 815 | else |
| 816 | rdev->desc_nr = sb->this_disk.number; |
| 817 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 818 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 820 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | __u64 ev1, ev2; |
| 822 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 823 | if (!uuid_equal(refsb, sb)) { |
| 824 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 825 | b, bdevname(refdev->bdev,b2)); |
| 826 | goto abort; |
| 827 | } |
| 828 | if (!sb_equal(refsb, sb)) { |
| 829 | printk(KERN_WARNING "md: %s has same UUID" |
| 830 | " but different superblock to %s\n", |
| 831 | b, bdevname(refdev->bdev, b2)); |
| 832 | goto abort; |
| 833 | } |
| 834 | ev1 = md_event(sb); |
| 835 | ev2 = md_event(refsb); |
| 836 | if (ev1 > ev2) |
| 837 | ret = 1; |
| 838 | else |
| 839 | ret = 0; |
| 840 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 841 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 843 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 844 | /* "this cannot possibly happen" ... */ |
| 845 | ret = -EINVAL; |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | abort: |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * validate_super for 0.90.0 |
| 853 | */ |
| 854 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 855 | { |
| 856 | mdp_disk_t *desc; |
| 857 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 858 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 860 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 861 | clear_bit(Faulty, &rdev->flags); |
| 862 | clear_bit(In_sync, &rdev->flags); |
| 863 | clear_bit(WriteMostly, &rdev->flags); |
| 864 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (mddev->raid_disks == 0) { |
| 867 | mddev->major_version = 0; |
| 868 | mddev->minor_version = sb->minor_version; |
| 869 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 870 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 871 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | mddev->ctime = sb->ctime; |
| 873 | mddev->utime = sb->utime; |
| 874 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 875 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | mddev->layout = sb->layout; |
| 877 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 878 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 879 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 880 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 881 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 883 | if (mddev->minor_version >= 91) { |
| 884 | mddev->reshape_position = sb->reshape_position; |
| 885 | mddev->delta_disks = sb->delta_disks; |
| 886 | mddev->new_level = sb->new_level; |
| 887 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 888 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 889 | } else { |
| 890 | mddev->reshape_position = MaxSector; |
| 891 | mddev->delta_disks = 0; |
| 892 | mddev->new_level = mddev->level; |
| 893 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 894 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 898 | mddev->recovery_cp = MaxSector; |
| 899 | else { |
| 900 | if (sb->events_hi == sb->cp_events_hi && |
| 901 | sb->events_lo == sb->cp_events_lo) { |
| 902 | mddev->recovery_cp = sb->recovery_cp; |
| 903 | } else |
| 904 | mddev->recovery_cp = 0; |
| 905 | } |
| 906 | |
| 907 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 908 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 909 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 910 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 911 | |
| 912 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 913 | |
| 914 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 915 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 916 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 917 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 918 | } else if (mddev->pers == NULL) { |
| 919 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | ++ev1; |
| 921 | if (ev1 < mddev->events) |
| 922 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 923 | } else if (mddev->bitmap) { |
| 924 | /* if adding to array with a bitmap, then we can accept an |
| 925 | * older device ... but not too old. |
| 926 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 927 | if (ev1 < mddev->bitmap->events_cleared) |
| 928 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 929 | } else { |
| 930 | if (ev1 < mddev->events) |
| 931 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 932 | return 0; |
| 933 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | desc = sb->disks + rdev->desc_nr; |
| 937 | |
| 938 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 939 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 940 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 941 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 942 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 944 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 945 | /* active but not in sync implies recovery up to |
| 946 | * reshape position. We don't know exactly where |
| 947 | * that is, so set to zero for now */ |
| 948 | if (mddev->minor_version >= 91) { |
| 949 | rdev->recovery_offset = 0; |
| 950 | rdev->raid_disk = desc->raid_disk; |
| 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 953 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 954 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 955 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 956 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * sync_super for 0.90.0 |
| 962 | */ |
| 963 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 964 | { |
| 965 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | mdk_rdev_t *rdev2; |
| 967 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | /* make rdev->sb match mddev data.. |
| 971 | * |
| 972 | * 1/ zero out disks |
| 973 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 974 | * 3/ any empty disks < next_spare become removed |
| 975 | * |
| 976 | * disks[0] gets initialised to REMOVED because |
| 977 | * we cannot be sure from other fields if it has |
| 978 | * been initialised or not. |
| 979 | */ |
| 980 | int i; |
| 981 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 982 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 983 | rdev->sb_size = MD_SB_BYTES; |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 986 | |
| 987 | memset(sb, 0, sizeof(*sb)); |
| 988 | |
| 989 | sb->md_magic = MD_SB_MAGIC; |
| 990 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | sb->patch_version = mddev->patch_version; |
| 992 | sb->gvalid_words = 0; /* ignored */ |
| 993 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 994 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 995 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 996 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 997 | |
| 998 | sb->ctime = mddev->ctime; |
| 999 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1000 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | sb->raid_disks = mddev->raid_disks; |
| 1002 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1003 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | sb->utime = mddev->utime; |
| 1005 | sb->state = 0; |
| 1006 | sb->events_hi = (mddev->events>>32); |
| 1007 | sb->events_lo = (u32)mddev->events; |
| 1008 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1009 | if (mddev->reshape_position == MaxSector) |
| 1010 | sb->minor_version = 90; |
| 1011 | else { |
| 1012 | sb->minor_version = 91; |
| 1013 | sb->reshape_position = mddev->reshape_position; |
| 1014 | sb->new_level = mddev->new_level; |
| 1015 | sb->delta_disks = mddev->delta_disks; |
| 1016 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1017 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1018 | } |
| 1019 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (mddev->in_sync) |
| 1021 | { |
| 1022 | sb->recovery_cp = mddev->recovery_cp; |
| 1023 | sb->cp_events_hi = (mddev->events>>32); |
| 1024 | sb->cp_events_lo = (u32)mddev->events; |
| 1025 | if (mddev->recovery_cp == MaxSector) |
| 1026 | sb->state = (1<< MD_SB_CLEAN); |
| 1027 | } else |
| 1028 | sb->recovery_cp = 0; |
| 1029 | |
| 1030 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1031 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1033 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1034 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1037 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1039 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1040 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1041 | |
| 1042 | if (rdev2->raid_disk >= 0 && |
| 1043 | sb->minor_version >= 91) |
| 1044 | /* we have nowhere to store the recovery_offset, |
| 1045 | * but if it is not below the reshape_position, |
| 1046 | * we can piggy-back on that. |
| 1047 | */ |
| 1048 | is_active = 1; |
| 1049 | if (rdev2->raid_disk < 0 || |
| 1050 | test_bit(Faulty, &rdev2->flags)) |
| 1051 | is_active = 0; |
| 1052 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1053 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1055 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1056 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | d = &sb->disks[rdev2->desc_nr]; |
| 1058 | nr_disks++; |
| 1059 | d->number = rdev2->desc_nr; |
| 1060 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1061 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1062 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | d->raid_disk = rdev2->raid_disk; |
| 1064 | else |
| 1065 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1066 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1068 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1070 | if (test_bit(In_sync, &rdev2->flags)) |
| 1071 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | active++; |
| 1073 | working++; |
| 1074 | } else { |
| 1075 | d->state = 0; |
| 1076 | spare++; |
| 1077 | working++; |
| 1078 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1079 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1080 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1083 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1084 | mdp_disk_t *d = &sb->disks[i]; |
| 1085 | if (d->state == 0 && d->number == 0) { |
| 1086 | d->number = i; |
| 1087 | d->raid_disk = i; |
| 1088 | d->state = (1<<MD_DISK_REMOVED); |
| 1089 | d->state |= (1<<MD_DISK_FAULTY); |
| 1090 | failed++; |
| 1091 | } |
| 1092 | } |
| 1093 | sb->nr_disks = nr_disks; |
| 1094 | sb->active_disks = active; |
| 1095 | sb->working_disks = working; |
| 1096 | sb->failed_disks = failed; |
| 1097 | sb->spare_disks = spare; |
| 1098 | |
| 1099 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1100 | sb->sb_csum = calc_sb_csum(sb); |
| 1101 | } |
| 1102 | |
| 1103 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1104 | * rdev_size_change for 0.90.0 |
| 1105 | */ |
| 1106 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1107 | 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] | 1108 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1109 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1110 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1111 | if (rdev->mddev->bitmap_offset) |
| 1112 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1113 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1114 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1115 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1116 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1117 | rdev->sb_page); |
| 1118 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1119 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | * version 1 superblock |
| 1125 | */ |
| 1126 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1127 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1129 | __le32 disk_csum; |
| 1130 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | unsigned long long newcsum; |
| 1132 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1133 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | int i; |
| 1135 | |
| 1136 | disk_csum = sb->sb_csum; |
| 1137 | sb->sb_csum = 0; |
| 1138 | newcsum = 0; |
| 1139 | for (i=0; size>=4; size -= 4 ) |
| 1140 | newcsum += le32_to_cpu(*isuper++); |
| 1141 | |
| 1142 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1143 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1146 | sb->sb_csum = disk_csum; |
| 1147 | return cpu_to_le32(csum); |
| 1148 | } |
| 1149 | |
| 1150 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1151 | { |
| 1152 | struct mdp_superblock_1 *sb; |
| 1153 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1154 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1156 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1159 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | * It is always aligned to a 4K boundary and |
| 1161 | * depeding on minor_version, it can be: |
| 1162 | * 0: At least 8K, but less than 12K, from end of device |
| 1163 | * 1: At start of device |
| 1164 | * 2: 4K from start of device. |
| 1165 | */ |
| 1166 | switch(minor_version) { |
| 1167 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1168 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1169 | sb_start -= 8*2; |
| 1170 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1173 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1176 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | break; |
| 1178 | default: |
| 1179 | return -EINVAL; |
| 1180 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1181 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1183 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1184 | * and it is safe to read 4k, so we do that |
| 1185 | */ |
| 1186 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | if (ret) return ret; |
| 1188 | |
| 1189 | |
| 1190 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1191 | |
| 1192 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1193 | sb->major_version != cpu_to_le32(1) || |
| 1194 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1195 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1196 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | return -EINVAL; |
| 1198 | |
| 1199 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1200 | printk("md: invalid superblock checksum on %s\n", |
| 1201 | bdevname(rdev->bdev,b)); |
| 1202 | return -EINVAL; |
| 1203 | } |
| 1204 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1205 | printk("md: data_size too small on %s\n", |
| 1206 | bdevname(rdev->bdev,b)); |
| 1207 | return -EINVAL; |
| 1208 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | rdev->preferred_minor = 0xffff; |
| 1211 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1212 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1214 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1215 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1216 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1217 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1218 | |
| 1219 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1220 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1221 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1222 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1223 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1224 | rdev->desc_nr = -1; |
| 1225 | else |
| 1226 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1227 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1228 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1229 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1230 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | __u64 ev1, ev2; |
| 1232 | struct mdp_superblock_1 *refsb = |
| 1233 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1234 | |
| 1235 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1236 | sb->level != refsb->level || |
| 1237 | sb->layout != refsb->layout || |
| 1238 | sb->chunksize != refsb->chunksize) { |
| 1239 | printk(KERN_WARNING "md: %s has strangely different" |
| 1240 | " superblock to %s\n", |
| 1241 | bdevname(rdev->bdev,b), |
| 1242 | bdevname(refdev->bdev,b2)); |
| 1243 | return -EINVAL; |
| 1244 | } |
| 1245 | ev1 = le64_to_cpu(sb->events); |
| 1246 | ev2 = le64_to_cpu(refsb->events); |
| 1247 | |
| 1248 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1249 | ret = 1; |
| 1250 | else |
| 1251 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1253 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1254 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1255 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1257 | rdev->sectors = rdev->sb_start; |
| 1258 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1260 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1261 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1262 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1263 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1267 | { |
| 1268 | 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] | 1269 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1271 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1272 | clear_bit(Faulty, &rdev->flags); |
| 1273 | clear_bit(In_sync, &rdev->flags); |
| 1274 | clear_bit(WriteMostly, &rdev->flags); |
| 1275 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | if (mddev->raid_disks == 0) { |
| 1278 | mddev->major_version = 1; |
| 1279 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1280 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1281 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1283 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1284 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1285 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | mddev->layout = le32_to_cpu(sb->layout); |
| 1287 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1288 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1289 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1290 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1291 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1294 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1295 | |
| 1296 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1297 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1298 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1299 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1300 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1301 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1302 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1303 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1304 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1305 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1306 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1307 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1308 | } else { |
| 1309 | mddev->reshape_position = MaxSector; |
| 1310 | mddev->delta_disks = 0; |
| 1311 | mddev->new_level = mddev->level; |
| 1312 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1313 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1316 | } else if (mddev->pers == NULL) { |
| 1317 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | ++ev1; |
| 1319 | if (ev1 < mddev->events) |
| 1320 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1321 | } else if (mddev->bitmap) { |
| 1322 | /* If adding to array with a bitmap, then we can accept an |
| 1323 | * older device, but not too old. |
| 1324 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1325 | if (ev1 < mddev->bitmap->events_cleared) |
| 1326 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1327 | } else { |
| 1328 | if (ev1 < mddev->events) |
| 1329 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1330 | return 0; |
| 1331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1333 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1334 | if (rdev->desc_nr < 0 || |
| 1335 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1336 | role = 0xffff; |
| 1337 | rdev->desc_nr = -1; |
| 1338 | } else |
| 1339 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | switch(role) { |
| 1341 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | break; |
| 1343 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1344 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | break; |
| 1346 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1347 | if ((le32_to_cpu(sb->feature_map) & |
| 1348 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1349 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1350 | else |
| 1351 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | rdev->raid_disk = role; |
| 1353 | break; |
| 1354 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1355 | if (sb->devflags & WriteMostly1) |
| 1356 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1357 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1358 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1364 | { |
| 1365 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | mdk_rdev_t *rdev2; |
| 1367 | int max_dev, i; |
| 1368 | /* make rdev->sb match mddev and rdev data. */ |
| 1369 | |
| 1370 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1371 | |
| 1372 | sb->feature_map = 0; |
| 1373 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1374 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1376 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1377 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1378 | |
| 1379 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1380 | sb->events = cpu_to_le64(mddev->events); |
| 1381 | if (mddev->in_sync) |
| 1382 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1383 | else |
| 1384 | sb->resync_offset = cpu_to_le64(0); |
| 1385 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1386 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1387 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1388 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1389 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1390 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1391 | sb->level = cpu_to_le32(mddev->level); |
| 1392 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1393 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1394 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1395 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1396 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1397 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1398 | |
| 1399 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1400 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1401 | if (rdev->recovery_offset > 0) { |
| 1402 | sb->feature_map |= |
| 1403 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1404 | sb->recovery_offset = |
| 1405 | cpu_to_le64(rdev->recovery_offset); |
| 1406 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1409 | if (mddev->reshape_position != MaxSector) { |
| 1410 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1411 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1412 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1413 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1414 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1415 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1416 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1419 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | if (rdev2->desc_nr+1 > max_dev) |
| 1421 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1422 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1423 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1424 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1425 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1426 | rdev->sb_size = max_dev * 2 + 256; |
| 1427 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1428 | if (rdev->sb_size & bmask) |
| 1429 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | for (i=0; i<max_dev;i++) |
| 1432 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1433 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1434 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1436 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1438 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1440 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1441 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | else |
| 1443 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1444 | } |
| 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1447 | } |
| 1448 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1449 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1450 | 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] | 1451 | { |
| 1452 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1453 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1454 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1455 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1456 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1457 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1458 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1459 | max_sectors -= rdev->data_offset; |
| 1460 | if (!num_sectors || num_sectors > max_sectors) |
| 1461 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1462 | } else if (rdev->mddev->bitmap_offset) { |
| 1463 | /* minor version 0 with bitmap we can't move */ |
| 1464 | return 0; |
| 1465 | } else { |
| 1466 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1467 | sector_t sb_start; |
| 1468 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1469 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1470 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1471 | if (!num_sectors || num_sectors > max_sectors) |
| 1472 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1473 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1474 | } |
| 1475 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1476 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1477 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1478 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1479 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1480 | rdev->sb_page); |
| 1481 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1482 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1485 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | [0] = { |
| 1487 | .name = "0.90.0", |
| 1488 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1489 | .load_super = super_90_load, |
| 1490 | .validate_super = super_90_validate, |
| 1491 | .sync_super = super_90_sync, |
| 1492 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | }, |
| 1494 | [1] = { |
| 1495 | .name = "md-1", |
| 1496 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1497 | .load_super = super_1_load, |
| 1498 | .validate_super = super_1_validate, |
| 1499 | .sync_super = super_1_sync, |
| 1500 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | }, |
| 1502 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
| 1504 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1505 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1506 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1508 | rcu_read_lock(); |
| 1509 | rdev_for_each_rcu(rdev, mddev1) |
| 1510 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1511 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1512 | rdev2->bdev->bd_contains) { |
| 1513 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1514 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1515 | } |
| 1516 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static LIST_HEAD(pending_raid_disks); |
| 1521 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1522 | /* |
| 1523 | * Try to register data integrity profile for an mddev |
| 1524 | * |
| 1525 | * This is called when an array is started and after a disk has been kicked |
| 1526 | * from the array. It only succeeds if all working and active component devices |
| 1527 | * are integrity capable with matching profiles. |
| 1528 | */ |
| 1529 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1530 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1531 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1532 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1533 | if (list_empty(&mddev->disks)) |
| 1534 | return 0; /* nothing to do */ |
| 1535 | if (blk_get_integrity(mddev->gendisk)) |
| 1536 | return 0; /* already registered */ |
| 1537 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1538 | /* skip spares and non-functional disks */ |
| 1539 | if (test_bit(Faulty, &rdev->flags)) |
| 1540 | continue; |
| 1541 | if (rdev->raid_disk < 0) |
| 1542 | continue; |
| 1543 | /* |
| 1544 | * If at least one rdev is not integrity capable, we can not |
| 1545 | * enable data integrity for the md device. |
| 1546 | */ |
| 1547 | if (!bdev_get_integrity(rdev->bdev)) |
| 1548 | return -EINVAL; |
| 1549 | if (!reference) { |
| 1550 | /* Use the first rdev as the reference */ |
| 1551 | reference = rdev; |
| 1552 | continue; |
| 1553 | } |
| 1554 | /* does this rdev's profile match the reference profile? */ |
| 1555 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1556 | rdev->bdev->bd_disk) < 0) |
| 1557 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1558 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1559 | /* |
| 1560 | * All component devices are integrity capable and have matching |
| 1561 | * profiles, register the common profile for the md device. |
| 1562 | */ |
| 1563 | if (blk_integrity_register(mddev->gendisk, |
| 1564 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1565 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1566 | mdname(mddev)); |
| 1567 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1568 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1569 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1570 | mdname(mddev)); |
| 1571 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1572 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1573 | EXPORT_SYMBOL(md_integrity_register); |
| 1574 | |
| 1575 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1576 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1577 | { |
| 1578 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1579 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1580 | |
| 1581 | if (!bi_mddev) /* nothing to do */ |
| 1582 | return; |
| 1583 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1584 | return; |
| 1585 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1586 | rdev->bdev->bd_disk) >= 0) |
| 1587 | return; |
| 1588 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1589 | blk_integrity_unregister(mddev->gendisk); |
| 1590 | } |
| 1591 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1594 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1595 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1596 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1597 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1598 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
| 1600 | if (rdev->mddev) { |
| 1601 | MD_BUG(); |
| 1602 | return -EINVAL; |
| 1603 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1604 | |
| 1605 | /* prevent duplicates */ |
| 1606 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1607 | return -EEXIST; |
| 1608 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1609 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1610 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1611 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1612 | if (mddev->pers) { |
| 1613 | /* Cannot change size, so fail |
| 1614 | * If mddev->level <= 0, then we don't care |
| 1615 | * about aligning sizes (e.g. linear) |
| 1616 | */ |
| 1617 | if (mddev->level > 0) |
| 1618 | return -ENOSPC; |
| 1619 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1620 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | /* Verify rdev->desc_nr is unique. |
| 1624 | * If it is -1, assign a free number, else |
| 1625 | * check number is not in use |
| 1626 | */ |
| 1627 | if (rdev->desc_nr < 0) { |
| 1628 | int choice = 0; |
| 1629 | if (mddev->pers) choice = mddev->raid_disks; |
| 1630 | while (find_rdev_nr(mddev, choice)) |
| 1631 | choice++; |
| 1632 | rdev->desc_nr = choice; |
| 1633 | } else { |
| 1634 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1635 | return -EBUSY; |
| 1636 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1637 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1638 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1639 | mdname(mddev), mddev->max_disks); |
| 1640 | return -EBUSY; |
| 1641 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1642 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1643 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1644 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1647 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1648 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1649 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1650 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1651 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1652 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1653 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1654 | kobject_del(&rdev->kobj); |
| 1655 | goto fail; |
| 1656 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1657 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1658 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1659 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1660 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1661 | |
| 1662 | /* May as well allow recovery to be retried once */ |
| 1663 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1666 | |
| 1667 | fail: |
| 1668 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1669 | b, mdname(mddev)); |
| 1670 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1673 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1674 | { |
| 1675 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1676 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1677 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1681 | { |
| 1682 | char b[BDEVNAME_SIZE]; |
| 1683 | if (!rdev->mddev) { |
| 1684 | MD_BUG(); |
| 1685 | return; |
| 1686 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1687 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1688 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1690 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1691 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1692 | sysfs_put(rdev->sysfs_state); |
| 1693 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1694 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1695 | * writing to 'remove' to "dev/state". We also need |
| 1696 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1697 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1698 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1699 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1700 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1701 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * prevent the device from being mounted, repartitioned or |
| 1706 | * otherwise reused by a RAID array (or any other kernel |
| 1707 | * subsystem), by bd_claiming the device. |
| 1708 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1709 | 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] | 1710 | { |
| 1711 | int err = 0; |
| 1712 | struct block_device *bdev; |
| 1713 | char b[BDEVNAME_SIZE]; |
| 1714 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1715 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | if (IS_ERR(bdev)) { |
| 1717 | printk(KERN_ERR "md: could not open %s.\n", |
| 1718 | __bdevname(dev, b)); |
| 1719 | return PTR_ERR(bdev); |
| 1720 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1721 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | if (err) { |
| 1723 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1724 | bdevname(bdev, b)); |
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 | return err; |
| 1727 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1728 | if (!shared) |
| 1729 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | rdev->bdev = bdev; |
| 1731 | return err; |
| 1732 | } |
| 1733 | |
| 1734 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1735 | { |
| 1736 | struct block_device *bdev = rdev->bdev; |
| 1737 | rdev->bdev = NULL; |
| 1738 | if (!bdev) |
| 1739 | MD_BUG(); |
| 1740 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1741 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | void md_autodetect_dev(dev_t dev); |
| 1745 | |
| 1746 | static void export_rdev(mdk_rdev_t * rdev) |
| 1747 | { |
| 1748 | char b[BDEVNAME_SIZE]; |
| 1749 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1750 | bdevname(rdev->bdev,b)); |
| 1751 | if (rdev->mddev) |
| 1752 | MD_BUG(); |
| 1753 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1755 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1756 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | #endif |
| 1758 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1759 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1763 | { |
| 1764 | unbind_rdev_from_array(rdev); |
| 1765 | export_rdev(rdev); |
| 1766 | } |
| 1767 | |
| 1768 | static void export_array(mddev_t *mddev) |
| 1769 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1770 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1772 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | if (!rdev->mddev) { |
| 1774 | MD_BUG(); |
| 1775 | continue; |
| 1776 | } |
| 1777 | kick_rdev_from_array(rdev); |
| 1778 | } |
| 1779 | if (!list_empty(&mddev->disks)) |
| 1780 | MD_BUG(); |
| 1781 | mddev->raid_disks = 0; |
| 1782 | mddev->major_version = 0; |
| 1783 | } |
| 1784 | |
| 1785 | static void print_desc(mdp_disk_t *desc) |
| 1786 | { |
| 1787 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1788 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1789 | } |
| 1790 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1791 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | { |
| 1793 | int i; |
| 1794 | |
| 1795 | printk(KERN_INFO |
| 1796 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1797 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1798 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1799 | sb->ctime); |
| 1800 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1801 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1802 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1803 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1804 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1805 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1806 | sb->failed_disks, sb->spare_disks, |
| 1807 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1808 | |
| 1809 | printk(KERN_INFO); |
| 1810 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1811 | mdp_disk_t *desc; |
| 1812 | |
| 1813 | desc = sb->disks + i; |
| 1814 | if (desc->number || desc->major || desc->minor || |
| 1815 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1816 | printk(" D %2d: ", i); |
| 1817 | print_desc(desc); |
| 1818 | } |
| 1819 | } |
| 1820 | printk(KERN_INFO "md: THIS: "); |
| 1821 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1824 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1825 | { |
| 1826 | __u8 *uuid; |
| 1827 | |
| 1828 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1829 | printk(KERN_INFO |
| 1830 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1831 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1832 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1833 | le32_to_cpu(sb->major_version), |
| 1834 | le32_to_cpu(sb->feature_map), |
| 1835 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1836 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1837 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1838 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1839 | sb->set_name, |
| 1840 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1841 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1842 | |
| 1843 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1844 | printk(KERN_INFO |
| 1845 | "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] | 1846 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1847 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1848 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1849 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1850 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1851 | le32_to_cpu(sb->level), |
| 1852 | (unsigned long long)le64_to_cpu(sb->size), |
| 1853 | le32_to_cpu(sb->raid_disks), |
| 1854 | le32_to_cpu(sb->layout), |
| 1855 | le32_to_cpu(sb->chunksize), |
| 1856 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1857 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1858 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1859 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1860 | le32_to_cpu(sb->dev_number), |
| 1861 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1862 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1863 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1864 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1865 | sb->devflags, |
| 1866 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1867 | (unsigned long long)le64_to_cpu(sb->events), |
| 1868 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1869 | le32_to_cpu(sb->sb_csum), |
| 1870 | le32_to_cpu(sb->max_dev) |
| 1871 | ); |
| 1872 | } |
| 1873 | |
| 1874 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | { |
| 1876 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1877 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1878 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1879 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1880 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1882 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1883 | switch (major_version) { |
| 1884 | case 0: |
| 1885 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1886 | break; |
| 1887 | case 1: |
| 1888 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1889 | break; |
| 1890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | } else |
| 1892 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1893 | } |
| 1894 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1895 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1897 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | mdk_rdev_t *rdev; |
| 1899 | mddev_t *mddev; |
| 1900 | char b[BDEVNAME_SIZE]; |
| 1901 | |
| 1902 | printk("\n"); |
| 1903 | printk("md: **********************************\n"); |
| 1904 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1905 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1906 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1908 | if (mddev->bitmap) |
| 1909 | bitmap_print_sb(mddev->bitmap); |
| 1910 | else |
| 1911 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1912 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1914 | printk("\n"); |
| 1915 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1916 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1917 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | } |
| 1919 | printk("md: **********************************\n"); |
| 1920 | printk("\n"); |
| 1921 | } |
| 1922 | |
| 1923 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1924 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1926 | /* Update each superblock (in-memory image), but |
| 1927 | * if we are allowed to, skip spares which already |
| 1928 | * have the right event counter, or have one earlier |
| 1929 | * (which would mean they aren't being marked as dirty |
| 1930 | * with the rest of the array) |
| 1931 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 1934 | /* First make sure individual recovery_offsets are correct */ |
| 1935 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1936 | if (rdev->raid_disk >= 0 && |
| 1937 | !test_bit(In_sync, &rdev->flags) && |
| 1938 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 1939 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1940 | |
| 1941 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1942 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1943 | if (rdev->sb_events == mddev->events || |
| 1944 | (nospares && |
| 1945 | rdev->raid_disk < 0 && |
| 1946 | (rdev->sb_events&1)==0 && |
| 1947 | rdev->sb_events+1 == mddev->events)) { |
| 1948 | /* Don't update this superblock */ |
| 1949 | rdev->sb_loaded = 2; |
| 1950 | } else { |
| 1951 | super_types[mddev->major_version]. |
| 1952 | sync_super(mddev, rdev); |
| 1953 | rdev->sb_loaded = 1; |
| 1954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1958 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1961 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1962 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1964 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1965 | if (mddev->external) |
| 1966 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1968 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1969 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1970 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1971 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1972 | force_change = 1; |
| 1973 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1974 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1975 | * though if events isn't the right even/odd, we will have to do |
| 1976 | * spares after all |
| 1977 | */ |
| 1978 | nospares = 1; |
| 1979 | if (force_change) |
| 1980 | nospares = 0; |
| 1981 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1982 | /* If the array is degraded, then skipping spares is both |
| 1983 | * dangerous and fairly pointless. |
| 1984 | * Dangerous because a device that was removed from the array |
| 1985 | * might have a event_count that still looks up-to-date, |
| 1986 | * so it can be re-added without a resync. |
| 1987 | * Pointless because if there are any spares to skip, |
| 1988 | * then a recovery will happen and soon that array won't |
| 1989 | * be degraded any more and the spare can go back to sleep then. |
| 1990 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1991 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1992 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1993 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1994 | |
| 1995 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1996 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1997 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1998 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1999 | && (mddev->events & 1) |
| 2000 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2001 | mddev->events--; |
| 2002 | else { |
| 2003 | /* otherwise we have to go forward and ... */ |
| 2004 | mddev->events ++; |
| 2005 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2006 | /* .. if the array isn't clean, an 'even' event must also go |
| 2007 | * to spares. */ |
| 2008 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2009 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2010 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2011 | /* otherwise an 'odd' event must go to spares */ |
| 2012 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2013 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2014 | } |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | |
| 2017 | if (!mddev->events) { |
| 2018 | /* |
| 2019 | * oops, this 64-bit counter should never wrap. |
| 2020 | * Either we are in around ~1 trillion A.C., assuming |
| 2021 | * 1 reboot per second, or we have a bug: |
| 2022 | */ |
| 2023 | MD_BUG(); |
| 2024 | mddev->events --; |
| 2025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | /* |
| 2028 | * do not write anything to disk if using |
| 2029 | * nonpersistent superblocks |
| 2030 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2031 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2032 | if (!mddev->external) |
| 2033 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2034 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2035 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2036 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2038 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2039 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2040 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
| 2042 | dprintk(KERN_INFO |
| 2043 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2044 | mdname(mddev),mddev->in_sync); |
| 2045 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2046 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2047 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | char b[BDEVNAME_SIZE]; |
| 2049 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2050 | if (rdev->sb_loaded != 1) |
| 2051 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2052 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | dprintk("(skipping faulty "); |
| 2054 | |
| 2055 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2056 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2057 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2058 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2059 | rdev->sb_page); |
| 2060 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2061 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2062 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2063 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | } else |
| 2066 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2067 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | /* only need to write one superblock... */ |
| 2069 | break; |
| 2070 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2071 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2072 | /* 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] | 2073 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2074 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2075 | if (mddev->in_sync != sync_req || |
| 2076 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2077 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2078 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2079 | goto repeat; |
| 2080 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2081 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2082 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2083 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2084 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2085 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2089 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2090 | * We want to accept with case. For this we use cmd_match. |
| 2091 | */ |
| 2092 | static int cmd_match(const char *cmd, const char *str) |
| 2093 | { |
| 2094 | /* See if cmd, written into a sysfs file, matches |
| 2095 | * str. They must either be the same, or cmd can |
| 2096 | * have a trailing newline |
| 2097 | */ |
| 2098 | while (*cmd && *str && *cmd == *str) { |
| 2099 | cmd++; |
| 2100 | str++; |
| 2101 | } |
| 2102 | if (*cmd == '\n') |
| 2103 | cmd++; |
| 2104 | if (*str || *cmd) |
| 2105 | return 0; |
| 2106 | return 1; |
| 2107 | } |
| 2108 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2109 | struct rdev_sysfs_entry { |
| 2110 | struct attribute attr; |
| 2111 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2112 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2113 | }; |
| 2114 | |
| 2115 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2116 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2117 | { |
| 2118 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2119 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2120 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2121 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2122 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2123 | sep = ","; |
| 2124 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2125 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2126 | len += sprintf(page+len, "%sin_sync",sep); |
| 2127 | sep = ","; |
| 2128 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2129 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2130 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2131 | sep = ","; |
| 2132 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2133 | if (test_bit(Blocked, &rdev->flags)) { |
| 2134 | len += sprintf(page+len, "%sblocked", sep); |
| 2135 | sep = ","; |
| 2136 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2137 | if (!test_bit(Faulty, &rdev->flags) && |
| 2138 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2139 | len += sprintf(page+len, "%sspare", sep); |
| 2140 | sep = ","; |
| 2141 | } |
| 2142 | return len+sprintf(page+len, "\n"); |
| 2143 | } |
| 2144 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2145 | static ssize_t |
| 2146 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2147 | { |
| 2148 | /* can write |
| 2149 | * faulty - simulates and error |
| 2150 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2151 | * writemostly - sets write_mostly |
| 2152 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2153 | * blocked - sets the Blocked flag |
| 2154 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2155 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2156 | */ |
| 2157 | int err = -EINVAL; |
| 2158 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2159 | md_error(rdev->mddev, rdev); |
| 2160 | err = 0; |
| 2161 | } else if (cmd_match(buf, "remove")) { |
| 2162 | if (rdev->raid_disk >= 0) |
| 2163 | err = -EBUSY; |
| 2164 | else { |
| 2165 | mddev_t *mddev = rdev->mddev; |
| 2166 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2167 | if (mddev->pers) |
| 2168 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2169 | md_new_event(mddev); |
| 2170 | err = 0; |
| 2171 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2172 | } else if (cmd_match(buf, "writemostly")) { |
| 2173 | set_bit(WriteMostly, &rdev->flags); |
| 2174 | err = 0; |
| 2175 | } else if (cmd_match(buf, "-writemostly")) { |
| 2176 | clear_bit(WriteMostly, &rdev->flags); |
| 2177 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2178 | } else if (cmd_match(buf, "blocked")) { |
| 2179 | set_bit(Blocked, &rdev->flags); |
| 2180 | err = 0; |
| 2181 | } else if (cmd_match(buf, "-blocked")) { |
| 2182 | clear_bit(Blocked, &rdev->flags); |
| 2183 | wake_up(&rdev->blocked_wait); |
| 2184 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2185 | md_wakeup_thread(rdev->mddev->thread); |
| 2186 | |
| 2187 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2188 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2189 | set_bit(In_sync, &rdev->flags); |
| 2190 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2191 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2192 | if (!err && rdev->sysfs_state) |
| 2193 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2194 | return err ? err : len; |
| 2195 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2196 | static struct rdev_sysfs_entry rdev_state = |
| 2197 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2198 | |
| 2199 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2200 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2201 | { |
| 2202 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2203 | } |
| 2204 | |
| 2205 | static ssize_t |
| 2206 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2207 | { |
| 2208 | char *e; |
| 2209 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2210 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2211 | atomic_set(&rdev->corrected_errors, n); |
| 2212 | return len; |
| 2213 | } |
| 2214 | return -EINVAL; |
| 2215 | } |
| 2216 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2217 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2218 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2219 | static ssize_t |
| 2220 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2221 | { |
| 2222 | if (rdev->raid_disk < 0) |
| 2223 | return sprintf(page, "none\n"); |
| 2224 | else |
| 2225 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2226 | } |
| 2227 | |
| 2228 | static ssize_t |
| 2229 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2230 | { |
| 2231 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2232 | int err; |
| 2233 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2234 | int slot = simple_strtoul(buf, &e, 10); |
| 2235 | if (strncmp(buf, "none", 4)==0) |
| 2236 | slot = -1; |
| 2237 | else if (e==buf || (*e && *e!= '\n')) |
| 2238 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2239 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2240 | /* Setting 'slot' on an active array requires also |
| 2241 | * updating the 'rd%d' link, and communicating |
| 2242 | * with the personality with ->hot_*_disk. |
| 2243 | * For now we only support removing |
| 2244 | * failed/spare devices. This normally happens automatically, |
| 2245 | * but not when the metadata is externally managed. |
| 2246 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2247 | if (rdev->raid_disk == -1) |
| 2248 | return -EEXIST; |
| 2249 | /* personality does all needed checks */ |
| 2250 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2251 | return -EINVAL; |
| 2252 | err = rdev->mddev->pers-> |
| 2253 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2254 | if (err) |
| 2255 | return err; |
| 2256 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2257 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2258 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2259 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2260 | } else if (rdev->mddev->pers) { |
| 2261 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2262 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2263 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2264 | */ |
| 2265 | |
| 2266 | if (rdev->raid_disk != -1) |
| 2267 | return -EBUSY; |
| 2268 | |
| 2269 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2270 | return -EINVAL; |
| 2271 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2272 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2273 | if (rdev2->raid_disk == slot) |
| 2274 | return -EEXIST; |
| 2275 | |
| 2276 | rdev->raid_disk = slot; |
| 2277 | if (test_bit(In_sync, &rdev->flags)) |
| 2278 | rdev->saved_raid_disk = slot; |
| 2279 | else |
| 2280 | rdev->saved_raid_disk = -1; |
| 2281 | err = rdev->mddev->pers-> |
| 2282 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2283 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2284 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2285 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2286 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2287 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2288 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2289 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2290 | printk(KERN_WARNING |
| 2291 | "md: cannot register " |
| 2292 | "%s for %s\n", |
| 2293 | nm, mdname(rdev->mddev)); |
| 2294 | |
| 2295 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2296 | } else { |
| 2297 | if (slot >= rdev->mddev->raid_disks) |
| 2298 | return -ENOSPC; |
| 2299 | rdev->raid_disk = slot; |
| 2300 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2301 | clear_bit(Faulty, &rdev->flags); |
| 2302 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2303 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2304 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2305 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2306 | return len; |
| 2307 | } |
| 2308 | |
| 2309 | |
| 2310 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2311 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2312 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2313 | static ssize_t |
| 2314 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2315 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2316 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | static ssize_t |
| 2320 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2321 | { |
| 2322 | char *e; |
| 2323 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2324 | if (e==buf || (*e && *e != '\n')) |
| 2325 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2326 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2327 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2328 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2329 | /* Must set offset before size, so overlap checks |
| 2330 | * can be sane */ |
| 2331 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2332 | rdev->data_offset = offset; |
| 2333 | return len; |
| 2334 | } |
| 2335 | |
| 2336 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2337 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2338 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2339 | static ssize_t |
| 2340 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2341 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2342 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2343 | } |
| 2344 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2345 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2346 | { |
| 2347 | /* check if two start/length pairs overlap */ |
| 2348 | if (s1+l1 <= s2) |
| 2349 | return 0; |
| 2350 | if (s2+l2 <= s1) |
| 2351 | return 0; |
| 2352 | return 1; |
| 2353 | } |
| 2354 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2355 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2356 | { |
| 2357 | unsigned long long blocks; |
| 2358 | sector_t new; |
| 2359 | |
| 2360 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2361 | return -EINVAL; |
| 2362 | |
| 2363 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2364 | return -EINVAL; /* sector conversion overflow */ |
| 2365 | |
| 2366 | new = blocks * 2; |
| 2367 | if (new != blocks * 2) |
| 2368 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2369 | |
| 2370 | *sectors = new; |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2374 | static ssize_t |
| 2375 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2376 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2377 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2378 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2379 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2380 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2381 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2382 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2383 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2384 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2385 | sectors = super_types[my_mddev->major_version]. |
| 2386 | rdev_size_change(rdev, sectors); |
| 2387 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2388 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2389 | } else if (!sectors) |
| 2390 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2391 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2392 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2393 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2394 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2395 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2396 | rdev->sectors = sectors; |
| 2397 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2398 | /* need to check that all other rdevs with the same ->bdev |
| 2399 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2400 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2401 | * we have to change it back, we will have the lock again. |
| 2402 | */ |
| 2403 | mddev_t *mddev; |
| 2404 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2405 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2406 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2407 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2408 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2409 | mdk_rdev_t *rdev2; |
| 2410 | |
| 2411 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2412 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2413 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2414 | (rdev->bdev == rdev2->bdev && |
| 2415 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2416 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2417 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2418 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2419 | overlap = 1; |
| 2420 | break; |
| 2421 | } |
| 2422 | mddev_unlock(mddev); |
| 2423 | if (overlap) { |
| 2424 | mddev_put(mddev); |
| 2425 | break; |
| 2426 | } |
| 2427 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2428 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2429 | if (overlap) { |
| 2430 | /* Someone else could have slipped in a size |
| 2431 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2432 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2433 | * safe, and trust userspace not to race with |
| 2434 | * itself |
| 2435 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2436 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2437 | return -EBUSY; |
| 2438 | } |
| 2439 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2440 | return len; |
| 2441 | } |
| 2442 | |
| 2443 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2444 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2445 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2446 | static struct attribute *rdev_default_attrs[] = { |
| 2447 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2448 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2449 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2450 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2451 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2452 | NULL, |
| 2453 | }; |
| 2454 | static ssize_t |
| 2455 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2456 | { |
| 2457 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2458 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2459 | mddev_t *mddev = rdev->mddev; |
| 2460 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2461 | |
| 2462 | if (!entry->show) |
| 2463 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2464 | |
| 2465 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2466 | if (!rv) { |
| 2467 | if (rdev->mddev == NULL) |
| 2468 | rv = -EBUSY; |
| 2469 | else |
| 2470 | rv = entry->show(rdev, page); |
| 2471 | mddev_unlock(mddev); |
| 2472 | } |
| 2473 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | static ssize_t |
| 2477 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2478 | const char *page, size_t length) |
| 2479 | { |
| 2480 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2481 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2482 | ssize_t rv; |
| 2483 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2484 | |
| 2485 | if (!entry->store) |
| 2486 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2487 | if (!capable(CAP_SYS_ADMIN)) |
| 2488 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2489 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2490 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2491 | if (rdev->mddev == NULL) |
| 2492 | rv = -EBUSY; |
| 2493 | else |
| 2494 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2495 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2496 | } |
| 2497 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | static void rdev_free(struct kobject *ko) |
| 2501 | { |
| 2502 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2503 | kfree(rdev); |
| 2504 | } |
| 2505 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2506 | .show = rdev_attr_show, |
| 2507 | .store = rdev_attr_store, |
| 2508 | }; |
| 2509 | static struct kobj_type rdev_ktype = { |
| 2510 | .release = rdev_free, |
| 2511 | .sysfs_ops = &rdev_sysfs_ops, |
| 2512 | .default_attrs = rdev_default_attrs, |
| 2513 | }; |
| 2514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | /* |
| 2516 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2517 | * |
| 2518 | * mark the device faulty if: |
| 2519 | * |
| 2520 | * - the device is nonexistent (zero size) |
| 2521 | * - the device has no valid superblock |
| 2522 | * |
| 2523 | * a faulty rdev _never_ has rdev->sb set. |
| 2524 | */ |
| 2525 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2526 | { |
| 2527 | char b[BDEVNAME_SIZE]; |
| 2528 | int err; |
| 2529 | mdk_rdev_t *rdev; |
| 2530 | sector_t size; |
| 2531 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2532 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | if (!rdev) { |
| 2534 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2535 | return ERR_PTR(-ENOMEM); |
| 2536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | if ((err = alloc_disk_sb(rdev))) |
| 2539 | goto abort_free; |
| 2540 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2541 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | if (err) |
| 2543 | goto abort_free; |
| 2544 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2545 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2548 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2549 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2550 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2552 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2554 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2555 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2558 | if (!size) { |
| 2559 | printk(KERN_WARNING |
| 2560 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2561 | bdevname(rdev->bdev,b)); |
| 2562 | err = -EINVAL; |
| 2563 | goto abort_free; |
| 2564 | } |
| 2565 | |
| 2566 | if (super_format >= 0) { |
| 2567 | err = super_types[super_format]. |
| 2568 | load_super(rdev, NULL, super_minor); |
| 2569 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2570 | printk(KERN_WARNING |
| 2571 | "md: %s does not have a valid v%d.%d " |
| 2572 | "superblock, not importing!\n", |
| 2573 | bdevname(rdev->bdev,b), |
| 2574 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | goto abort_free; |
| 2576 | } |
| 2577 | if (err < 0) { |
| 2578 | printk(KERN_WARNING |
| 2579 | "md: could not read %s's sb, not importing!\n", |
| 2580 | bdevname(rdev->bdev,b)); |
| 2581 | goto abort_free; |
| 2582 | } |
| 2583 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2586 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | |
| 2588 | return rdev; |
| 2589 | |
| 2590 | abort_free: |
| 2591 | if (rdev->sb_page) { |
| 2592 | if (rdev->bdev) |
| 2593 | unlock_rdev(rdev); |
| 2594 | free_disk_sb(rdev); |
| 2595 | } |
| 2596 | kfree(rdev); |
| 2597 | return ERR_PTR(err); |
| 2598 | } |
| 2599 | |
| 2600 | /* |
| 2601 | * Check a full RAID array for plausibility |
| 2602 | */ |
| 2603 | |
| 2604 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2605 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | { |
| 2607 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2608 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | char b[BDEVNAME_SIZE]; |
| 2610 | |
| 2611 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2612 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | switch (super_types[mddev->major_version]. |
| 2614 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2615 | case 1: |
| 2616 | freshest = rdev; |
| 2617 | break; |
| 2618 | case 0: |
| 2619 | break; |
| 2620 | default: |
| 2621 | printk( KERN_ERR \ |
| 2622 | "md: fatal superblock inconsistency in %s" |
| 2623 | " -- removing from array\n", |
| 2624 | bdevname(rdev->bdev,b)); |
| 2625 | kick_rdev_from_array(rdev); |
| 2626 | } |
| 2627 | |
| 2628 | |
| 2629 | super_types[mddev->major_version]. |
| 2630 | validate_super(mddev, freshest); |
| 2631 | |
| 2632 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2633 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2634 | if (rdev->desc_nr >= mddev->max_disks || |
| 2635 | i > mddev->max_disks) { |
| 2636 | printk(KERN_WARNING |
| 2637 | "md: %s: %s: only %d devices permitted\n", |
| 2638 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2639 | mddev->max_disks); |
| 2640 | kick_rdev_from_array(rdev); |
| 2641 | continue; |
| 2642 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | if (rdev != freshest) |
| 2644 | if (super_types[mddev->major_version]. |
| 2645 | validate_super(mddev, rdev)) { |
| 2646 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2647 | " from array!\n", |
| 2648 | bdevname(rdev->bdev,b)); |
| 2649 | kick_rdev_from_array(rdev); |
| 2650 | continue; |
| 2651 | } |
| 2652 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2653 | rdev->desc_nr = i++; |
| 2654 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2655 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2656 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2657 | rdev->raid_disk = -1; |
| 2658 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | } |
| 2662 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2663 | static void md_safemode_timeout(unsigned long data); |
| 2664 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2665 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2666 | safe_delay_show(mddev_t *mddev, char *page) |
| 2667 | { |
| 2668 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2669 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2670 | } |
| 2671 | static ssize_t |
| 2672 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2673 | { |
| 2674 | int scale=1; |
| 2675 | int dot=0; |
| 2676 | int i; |
| 2677 | unsigned long msec; |
| 2678 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2679 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2680 | /* remove a period, and count digits after it */ |
| 2681 | if (len >= sizeof(buf)) |
| 2682 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2683 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2684 | for (i=0; i<len; i++) { |
| 2685 | if (dot) { |
| 2686 | if (isdigit(buf[i])) { |
| 2687 | buf[i-1] = buf[i]; |
| 2688 | scale *= 10; |
| 2689 | } |
| 2690 | buf[i] = 0; |
| 2691 | } else if (buf[i] == '.') { |
| 2692 | dot=1; |
| 2693 | buf[i] = 0; |
| 2694 | } |
| 2695 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2696 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2697 | return -EINVAL; |
| 2698 | msec = (msec * 1000) / scale; |
| 2699 | if (msec == 0) |
| 2700 | mddev->safemode_delay = 0; |
| 2701 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2702 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2703 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2704 | if (mddev->safemode_delay == 0) |
| 2705 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2706 | if (mddev->safemode_delay < old_delay) |
| 2707 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2708 | } |
| 2709 | return len; |
| 2710 | } |
| 2711 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2712 | __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] | 2713 | |
| 2714 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2715 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2716 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2717 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2718 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2719 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2720 | else if (mddev->clevel[0]) |
| 2721 | return sprintf(page, "%s\n", mddev->clevel); |
| 2722 | else if (mddev->level != LEVEL_NONE) |
| 2723 | return sprintf(page, "%d\n", mddev->level); |
| 2724 | else |
| 2725 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2728 | static ssize_t |
| 2729 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2730 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2731 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2732 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2733 | struct mdk_personality *pers; |
| 2734 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2735 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2736 | |
| 2737 | if (mddev->pers == NULL) { |
| 2738 | if (len == 0) |
| 2739 | return 0; |
| 2740 | if (len >= sizeof(mddev->clevel)) |
| 2741 | return -ENOSPC; |
| 2742 | strncpy(mddev->clevel, buf, len); |
| 2743 | if (mddev->clevel[len-1] == '\n') |
| 2744 | len--; |
| 2745 | mddev->clevel[len] = 0; |
| 2746 | mddev->level = LEVEL_NONE; |
| 2747 | return rv; |
| 2748 | } |
| 2749 | |
| 2750 | /* request to change the personality. Need to ensure: |
| 2751 | * - array is not engaged in resync/recovery/reshape |
| 2752 | * - old personality can be suspended |
| 2753 | * - new personality will access other array. |
| 2754 | */ |
| 2755 | |
| 2756 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2757 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2758 | |
| 2759 | if (!mddev->pers->quiesce) { |
| 2760 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2761 | mdname(mddev), mddev->pers->name); |
| 2762 | return -EINVAL; |
| 2763 | } |
| 2764 | |
| 2765 | /* Now find the new personality */ |
| 2766 | if (len == 0 || len >= sizeof(level)) |
| 2767 | return -EINVAL; |
| 2768 | strncpy(level, buf, len); |
| 2769 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2770 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2771 | level[len] = 0; |
| 2772 | |
| 2773 | request_module("md-%s", level); |
| 2774 | spin_lock(&pers_lock); |
| 2775 | pers = find_pers(LEVEL_NONE, level); |
| 2776 | if (!pers || !try_module_get(pers->owner)) { |
| 2777 | spin_unlock(&pers_lock); |
| 2778 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | spin_unlock(&pers_lock); |
| 2782 | |
| 2783 | if (pers == mddev->pers) { |
| 2784 | /* Nothing to do! */ |
| 2785 | module_put(pers->owner); |
| 2786 | return rv; |
| 2787 | } |
| 2788 | if (!pers->takeover) { |
| 2789 | module_put(pers->owner); |
| 2790 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2791 | mdname(mddev), level); |
| 2792 | return -EINVAL; |
| 2793 | } |
| 2794 | |
| 2795 | /* ->takeover must set new_* and/or delta_disks |
| 2796 | * if it succeeds, and may set them when it fails. |
| 2797 | */ |
| 2798 | priv = pers->takeover(mddev); |
| 2799 | if (IS_ERR(priv)) { |
| 2800 | mddev->new_level = mddev->level; |
| 2801 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2802 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2803 | mddev->raid_disks -= mddev->delta_disks; |
| 2804 | mddev->delta_disks = 0; |
| 2805 | module_put(pers->owner); |
| 2806 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2807 | mdname(mddev), level); |
| 2808 | return PTR_ERR(priv); |
| 2809 | } |
| 2810 | |
| 2811 | /* Looks like we have a winner */ |
| 2812 | mddev_suspend(mddev); |
| 2813 | mddev->pers->stop(mddev); |
| 2814 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2815 | /* Invalidate devices that are now superfluous */ |
| 2816 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2817 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2818 | rdev->raid_disk = -1; |
| 2819 | clear_bit(In_sync, &rdev->flags); |
| 2820 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2821 | mddev->pers = pers; |
| 2822 | mddev->private = priv; |
| 2823 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2824 | mddev->level = mddev->new_level; |
| 2825 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2826 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2827 | mddev->delta_disks = 0; |
| 2828 | pers->run(mddev); |
| 2829 | mddev_resume(mddev); |
| 2830 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2831 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2832 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2833 | return rv; |
| 2834 | } |
| 2835 | |
| 2836 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2837 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2838 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2839 | |
| 2840 | static ssize_t |
| 2841 | layout_show(mddev_t *mddev, char *page) |
| 2842 | { |
| 2843 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2844 | if (mddev->reshape_position != MaxSector && |
| 2845 | mddev->layout != mddev->new_layout) |
| 2846 | return sprintf(page, "%d (%d)\n", |
| 2847 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2848 | return sprintf(page, "%d\n", mddev->layout); |
| 2849 | } |
| 2850 | |
| 2851 | static ssize_t |
| 2852 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2853 | { |
| 2854 | char *e; |
| 2855 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2856 | |
| 2857 | if (!*buf || (*e && *e != '\n')) |
| 2858 | return -EINVAL; |
| 2859 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2860 | if (mddev->pers) { |
| 2861 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2862 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2863 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2864 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2865 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2866 | if (err) { |
| 2867 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2868 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2869 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2870 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2871 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2872 | if (mddev->reshape_position == MaxSector) |
| 2873 | mddev->layout = n; |
| 2874 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2875 | return len; |
| 2876 | } |
| 2877 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2878 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2879 | |
| 2880 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2881 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2882 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2883 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2884 | if (mddev->raid_disks == 0) |
| 2885 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2886 | if (mddev->reshape_position != MaxSector && |
| 2887 | mddev->delta_disks != 0) |
| 2888 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2889 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2890 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2891 | } |
| 2892 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2893 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2894 | |
| 2895 | static ssize_t |
| 2896 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2897 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2898 | char *e; |
| 2899 | int rv = 0; |
| 2900 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2901 | |
| 2902 | if (!*buf || (*e && *e != '\n')) |
| 2903 | return -EINVAL; |
| 2904 | |
| 2905 | if (mddev->pers) |
| 2906 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2907 | else if (mddev->reshape_position != MaxSector) { |
| 2908 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2909 | mddev->delta_disks = n - olddisks; |
| 2910 | mddev->raid_disks = n; |
| 2911 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2912 | mddev->raid_disks = n; |
| 2913 | return rv ? rv : len; |
| 2914 | } |
| 2915 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2916 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2917 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2918 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2919 | chunk_size_show(mddev_t *mddev, char *page) |
| 2920 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2921 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2922 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2923 | return sprintf(page, "%d (%d)\n", |
| 2924 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2925 | mddev->chunk_sectors << 9); |
| 2926 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | static ssize_t |
| 2930 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2931 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2932 | char *e; |
| 2933 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2934 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2935 | if (!*buf || (*e && *e != '\n')) |
| 2936 | return -EINVAL; |
| 2937 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2938 | if (mddev->pers) { |
| 2939 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2940 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2941 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2942 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2943 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2944 | if (err) { |
| 2945 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2946 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2947 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2948 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2949 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2950 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2951 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2952 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2953 | return len; |
| 2954 | } |
| 2955 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2956 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2957 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2958 | static ssize_t |
| 2959 | resync_start_show(mddev_t *mddev, char *page) |
| 2960 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2961 | if (mddev->recovery_cp == MaxSector) |
| 2962 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2963 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2964 | } |
| 2965 | |
| 2966 | static ssize_t |
| 2967 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2968 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2969 | char *e; |
| 2970 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2971 | |
| 2972 | if (mddev->pers) |
| 2973 | return -EBUSY; |
| 2974 | if (!*buf || (*e && *e != '\n')) |
| 2975 | return -EINVAL; |
| 2976 | |
| 2977 | mddev->recovery_cp = n; |
| 2978 | return len; |
| 2979 | } |
| 2980 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2981 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2982 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2983 | /* |
| 2984 | * The array state can be: |
| 2985 | * |
| 2986 | * clear |
| 2987 | * No devices, no size, no level |
| 2988 | * Equivalent to STOP_ARRAY ioctl |
| 2989 | * inactive |
| 2990 | * May have some settings, but array is not active |
| 2991 | * all IO results in error |
| 2992 | * When written, doesn't tear down array, but just stops it |
| 2993 | * suspended (not supported yet) |
| 2994 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2995 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2996 | * readonly |
| 2997 | * no resync can happen. no superblocks get written. |
| 2998 | * write requests fail |
| 2999 | * read-auto |
| 3000 | * like readonly, but behaves like 'clean' on a write request. |
| 3001 | * |
| 3002 | * clean - no pending writes, but otherwise active. |
| 3003 | * When written to inactive array, starts without resync |
| 3004 | * If a write request arrives then |
| 3005 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3006 | * if not known, block and switch to write-pending |
| 3007 | * If written to an active array that has pending writes, then fails. |
| 3008 | * active |
| 3009 | * fully active: IO and resync can be happening. |
| 3010 | * When written to inactive array, starts with resync |
| 3011 | * |
| 3012 | * write-pending |
| 3013 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3014 | * |
| 3015 | * active-idle |
| 3016 | * like active, but no writes have been seen for a while (100msec). |
| 3017 | * |
| 3018 | */ |
| 3019 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3020 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3021 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3022 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3023 | "write-pending", "active-idle", NULL }; |
| 3024 | |
| 3025 | static int match_word(const char *word, char **list) |
| 3026 | { |
| 3027 | int n; |
| 3028 | for (n=0; list[n]; n++) |
| 3029 | if (cmd_match(word, list[n])) |
| 3030 | break; |
| 3031 | return n; |
| 3032 | } |
| 3033 | |
| 3034 | static ssize_t |
| 3035 | array_state_show(mddev_t *mddev, char *page) |
| 3036 | { |
| 3037 | enum array_state st = inactive; |
| 3038 | |
| 3039 | if (mddev->pers) |
| 3040 | switch(mddev->ro) { |
| 3041 | case 1: |
| 3042 | st = readonly; |
| 3043 | break; |
| 3044 | case 2: |
| 3045 | st = read_auto; |
| 3046 | break; |
| 3047 | case 0: |
| 3048 | if (mddev->in_sync) |
| 3049 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3050 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3051 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3052 | else if (mddev->safemode) |
| 3053 | st = active_idle; |
| 3054 | else |
| 3055 | st = active; |
| 3056 | } |
| 3057 | else { |
| 3058 | if (list_empty(&mddev->disks) && |
| 3059 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3060 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3061 | st = clear; |
| 3062 | else |
| 3063 | st = inactive; |
| 3064 | } |
| 3065 | return sprintf(page, "%s\n", array_states[st]); |
| 3066 | } |
| 3067 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3068 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3069 | static int do_md_run(mddev_t * mddev); |
| 3070 | static int restart_array(mddev_t *mddev); |
| 3071 | |
| 3072 | static ssize_t |
| 3073 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3074 | { |
| 3075 | int err = -EINVAL; |
| 3076 | enum array_state st = match_word(buf, array_states); |
| 3077 | switch(st) { |
| 3078 | case bad_word: |
| 3079 | break; |
| 3080 | case clear: |
| 3081 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3082 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3083 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3084 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3085 | break; |
| 3086 | case inactive: |
| 3087 | /* stopping an active array */ |
| 3088 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3089 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3090 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3091 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3092 | } else |
| 3093 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3094 | break; |
| 3095 | case suspended: |
| 3096 | break; /* not supported yet */ |
| 3097 | case readonly: |
| 3098 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3099 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3100 | else { |
| 3101 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3102 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3103 | err = do_md_run(mddev); |
| 3104 | } |
| 3105 | break; |
| 3106 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3107 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3108 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3109 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3110 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3111 | err = restart_array(mddev); |
| 3112 | if (err == 0) { |
| 3113 | mddev->ro = 2; |
| 3114 | set_disk_ro(mddev->gendisk, 0); |
| 3115 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3116 | } else { |
| 3117 | mddev->ro = 2; |
| 3118 | err = do_md_run(mddev); |
| 3119 | } |
| 3120 | break; |
| 3121 | case clean: |
| 3122 | if (mddev->pers) { |
| 3123 | restart_array(mddev); |
| 3124 | spin_lock_irq(&mddev->write_lock); |
| 3125 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3126 | if (mddev->in_sync == 0) { |
| 3127 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3128 | if (mddev->safemode == 1) |
| 3129 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3130 | if (mddev->persistent) |
| 3131 | set_bit(MD_CHANGE_CLEAN, |
| 3132 | &mddev->flags); |
| 3133 | } |
| 3134 | err = 0; |
| 3135 | } else |
| 3136 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3137 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3138 | } else |
| 3139 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3140 | break; |
| 3141 | case active: |
| 3142 | if (mddev->pers) { |
| 3143 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3144 | if (mddev->external) |
| 3145 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3146 | wake_up(&mddev->sb_wait); |
| 3147 | err = 0; |
| 3148 | } else { |
| 3149 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3150 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3151 | err = do_md_run(mddev); |
| 3152 | } |
| 3153 | break; |
| 3154 | case write_pending: |
| 3155 | case active_idle: |
| 3156 | /* these cannot be set */ |
| 3157 | break; |
| 3158 | } |
| 3159 | if (err) |
| 3160 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3161 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3162 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3163 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3164 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3165 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3166 | static struct md_sysfs_entry md_array_state = |
| 3167 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3168 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3169 | static ssize_t |
| 3170 | null_show(mddev_t *mddev, char *page) |
| 3171 | { |
| 3172 | return -EINVAL; |
| 3173 | } |
| 3174 | |
| 3175 | static ssize_t |
| 3176 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3177 | { |
| 3178 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3179 | /* The new device is added to the array. |
| 3180 | * If the array has a persistent superblock, we read the |
| 3181 | * superblock to initialise info and check validity. |
| 3182 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3183 | * which mainly checks size. |
| 3184 | */ |
| 3185 | char *e; |
| 3186 | int major = simple_strtoul(buf, &e, 10); |
| 3187 | int minor; |
| 3188 | dev_t dev; |
| 3189 | mdk_rdev_t *rdev; |
| 3190 | int err; |
| 3191 | |
| 3192 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3193 | return -EINVAL; |
| 3194 | minor = simple_strtoul(e+1, &e, 10); |
| 3195 | if (*e && *e != '\n') |
| 3196 | return -EINVAL; |
| 3197 | dev = MKDEV(major, minor); |
| 3198 | if (major != MAJOR(dev) || |
| 3199 | minor != MINOR(dev)) |
| 3200 | return -EOVERFLOW; |
| 3201 | |
| 3202 | |
| 3203 | if (mddev->persistent) { |
| 3204 | rdev = md_import_device(dev, mddev->major_version, |
| 3205 | mddev->minor_version); |
| 3206 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3207 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3208 | mdk_rdev_t, same_set); |
| 3209 | err = super_types[mddev->major_version] |
| 3210 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3211 | if (err < 0) |
| 3212 | goto out; |
| 3213 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3214 | } else if (mddev->external) |
| 3215 | rdev = md_import_device(dev, -2, -1); |
| 3216 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3217 | rdev = md_import_device(dev, -1, -1); |
| 3218 | |
| 3219 | if (IS_ERR(rdev)) |
| 3220 | return PTR_ERR(rdev); |
| 3221 | err = bind_rdev_to_array(rdev, mddev); |
| 3222 | out: |
| 3223 | if (err) |
| 3224 | export_rdev(rdev); |
| 3225 | return err ? err : len; |
| 3226 | } |
| 3227 | |
| 3228 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3229 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3230 | |
| 3231 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3232 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3233 | { |
| 3234 | char *end; |
| 3235 | unsigned long chunk, end_chunk; |
| 3236 | |
| 3237 | if (!mddev->bitmap) |
| 3238 | goto out; |
| 3239 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3240 | while (*buf) { |
| 3241 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3242 | if (buf == end) break; |
| 3243 | if (*end == '-') { /* range */ |
| 3244 | buf = end + 1; |
| 3245 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3246 | if (buf == end) break; |
| 3247 | } |
| 3248 | if (*end && !isspace(*end)) break; |
| 3249 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3250 | buf = end; |
| 3251 | while (isspace(*buf)) buf++; |
| 3252 | } |
| 3253 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3254 | out: |
| 3255 | return len; |
| 3256 | } |
| 3257 | |
| 3258 | static struct md_sysfs_entry md_bitmap = |
| 3259 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3260 | |
| 3261 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3262 | size_show(mddev_t *mddev, char *page) |
| 3263 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3264 | return sprintf(page, "%llu\n", |
| 3265 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3266 | } |
| 3267 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3268 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3269 | |
| 3270 | static ssize_t |
| 3271 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3272 | { |
| 3273 | /* If array is inactive, we can reduce the component size, but |
| 3274 | * not increase it (except from 0). |
| 3275 | * If array is active, we can try an on-line resize |
| 3276 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3277 | sector_t sectors; |
| 3278 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3279 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3280 | if (err < 0) |
| 3281 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3282 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3283 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3284 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3285 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3286 | if (mddev->dev_sectors == 0 || |
| 3287 | mddev->dev_sectors > sectors) |
| 3288 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3289 | else |
| 3290 | err = -ENOSPC; |
| 3291 | } |
| 3292 | return err ? err : len; |
| 3293 | } |
| 3294 | |
| 3295 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3296 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3297 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3298 | |
| 3299 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3300 | * This is one of |
| 3301 | * 'none' for arrays with no metadata (good luck...) |
| 3302 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3303 | * or N.M for internally known formats |
| 3304 | */ |
| 3305 | static ssize_t |
| 3306 | metadata_show(mddev_t *mddev, char *page) |
| 3307 | { |
| 3308 | if (mddev->persistent) |
| 3309 | return sprintf(page, "%d.%d\n", |
| 3310 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3311 | else if (mddev->external) |
| 3312 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3313 | else |
| 3314 | return sprintf(page, "none\n"); |
| 3315 | } |
| 3316 | |
| 3317 | static ssize_t |
| 3318 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3319 | { |
| 3320 | int major, minor; |
| 3321 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3322 | /* Changing the details of 'external' metadata is |
| 3323 | * always permitted. Otherwise there must be |
| 3324 | * no devices attached to the array. |
| 3325 | */ |
| 3326 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3327 | ; |
| 3328 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3329 | return -EBUSY; |
| 3330 | |
| 3331 | if (cmd_match(buf, "none")) { |
| 3332 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3333 | mddev->external = 0; |
| 3334 | mddev->major_version = 0; |
| 3335 | mddev->minor_version = 90; |
| 3336 | return len; |
| 3337 | } |
| 3338 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3339 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3340 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3341 | namelen = sizeof(mddev->metadata_type)-1; |
| 3342 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3343 | mddev->metadata_type[namelen] = 0; |
| 3344 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3345 | mddev->metadata_type[--namelen] = 0; |
| 3346 | mddev->persistent = 0; |
| 3347 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3348 | mddev->major_version = 0; |
| 3349 | mddev->minor_version = 90; |
| 3350 | return len; |
| 3351 | } |
| 3352 | major = simple_strtoul(buf, &e, 10); |
| 3353 | if (e==buf || *e != '.') |
| 3354 | return -EINVAL; |
| 3355 | buf = e+1; |
| 3356 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3357 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3358 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3359 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3360 | return -ENOENT; |
| 3361 | mddev->major_version = major; |
| 3362 | mddev->minor_version = minor; |
| 3363 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3364 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3365 | return len; |
| 3366 | } |
| 3367 | |
| 3368 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3369 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3370 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3371 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3372 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3373 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3374 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3375 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3376 | type = "frozen"; |
| 3377 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3378 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3379 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3380 | type = "reshape"; |
| 3381 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3382 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3383 | type = "resync"; |
| 3384 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3385 | type = "check"; |
| 3386 | else |
| 3387 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3388 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3389 | type = "recover"; |
| 3390 | } |
| 3391 | return sprintf(page, "%s\n", type); |
| 3392 | } |
| 3393 | |
| 3394 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3395 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3396 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3397 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3398 | return -EINVAL; |
| 3399 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3400 | if (cmd_match(page, "frozen")) |
| 3401 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3402 | else |
| 3403 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3404 | |
| 3405 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3406 | if (mddev->sync_thread) { |
| 3407 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3408 | md_unregister_thread(mddev->sync_thread); |
| 3409 | mddev->sync_thread = NULL; |
| 3410 | mddev->recovery = 0; |
| 3411 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3412 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3413 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3414 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3415 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3416 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3417 | else if (cmd_match(page, "recover")) { |
| 3418 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3419 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3420 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3421 | int err; |
| 3422 | if (mddev->pers->start_reshape == NULL) |
| 3423 | return -EINVAL; |
| 3424 | err = mddev->pers->start_reshape(mddev); |
| 3425 | if (err) |
| 3426 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3427 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3428 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3429 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3430 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3431 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3432 | return -EINVAL; |
| 3433 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3434 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3435 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3436 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3437 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3438 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3439 | return len; |
| 3440 | } |
| 3441 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3442 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3443 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3444 | { |
| 3445 | return sprintf(page, "%llu\n", |
| 3446 | (unsigned long long) mddev->resync_mismatches); |
| 3447 | } |
| 3448 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3449 | static struct md_sysfs_entry md_scan_mode = |
| 3450 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3451 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3452 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3453 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3454 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3455 | static ssize_t |
| 3456 | sync_min_show(mddev_t *mddev, char *page) |
| 3457 | { |
| 3458 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3459 | mddev->sync_speed_min ? "local": "system"); |
| 3460 | } |
| 3461 | |
| 3462 | static ssize_t |
| 3463 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3464 | { |
| 3465 | int min; |
| 3466 | char *e; |
| 3467 | if (strncmp(buf, "system", 6)==0) { |
| 3468 | mddev->sync_speed_min = 0; |
| 3469 | return len; |
| 3470 | } |
| 3471 | min = simple_strtoul(buf, &e, 10); |
| 3472 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3473 | return -EINVAL; |
| 3474 | mddev->sync_speed_min = min; |
| 3475 | return len; |
| 3476 | } |
| 3477 | |
| 3478 | static struct md_sysfs_entry md_sync_min = |
| 3479 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3480 | |
| 3481 | static ssize_t |
| 3482 | sync_max_show(mddev_t *mddev, char *page) |
| 3483 | { |
| 3484 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3485 | mddev->sync_speed_max ? "local": "system"); |
| 3486 | } |
| 3487 | |
| 3488 | static ssize_t |
| 3489 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3490 | { |
| 3491 | int max; |
| 3492 | char *e; |
| 3493 | if (strncmp(buf, "system", 6)==0) { |
| 3494 | mddev->sync_speed_max = 0; |
| 3495 | return len; |
| 3496 | } |
| 3497 | max = simple_strtoul(buf, &e, 10); |
| 3498 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3499 | return -EINVAL; |
| 3500 | mddev->sync_speed_max = max; |
| 3501 | return len; |
| 3502 | } |
| 3503 | |
| 3504 | static struct md_sysfs_entry md_sync_max = |
| 3505 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3506 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3507 | static ssize_t |
| 3508 | degraded_show(mddev_t *mddev, char *page) |
| 3509 | { |
| 3510 | return sprintf(page, "%d\n", mddev->degraded); |
| 3511 | } |
| 3512 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3513 | |
| 3514 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3515 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3516 | { |
| 3517 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3518 | } |
| 3519 | |
| 3520 | static ssize_t |
| 3521 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3522 | { |
| 3523 | long n; |
| 3524 | |
| 3525 | if (strict_strtol(buf, 10, &n)) |
| 3526 | return -EINVAL; |
| 3527 | |
| 3528 | if (n != 0 && n != 1) |
| 3529 | return -EINVAL; |
| 3530 | |
| 3531 | mddev->parallel_resync = n; |
| 3532 | |
| 3533 | if (mddev->sync_thread) |
| 3534 | wake_up(&resync_wait); |
| 3535 | |
| 3536 | return len; |
| 3537 | } |
| 3538 | |
| 3539 | /* force parallel resync, even with shared block devices */ |
| 3540 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3541 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3542 | sync_force_parallel_show, sync_force_parallel_store); |
| 3543 | |
| 3544 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3545 | sync_speed_show(mddev_t *mddev, char *page) |
| 3546 | { |
| 3547 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3548 | if (mddev->curr_resync == 0) |
| 3549 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3550 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3551 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3552 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3553 | db = resync - mddev->resync_mark_cnt; |
| 3554 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3555 | } |
| 3556 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3557 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3558 | |
| 3559 | static ssize_t |
| 3560 | sync_completed_show(mddev_t *mddev, char *page) |
| 3561 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3562 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3563 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3564 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3565 | return sprintf(page, "none\n"); |
| 3566 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3567 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3568 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3569 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3570 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3571 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3572 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3573 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3574 | } |
| 3575 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3576 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3577 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3578 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3579 | min_sync_show(mddev_t *mddev, char *page) |
| 3580 | { |
| 3581 | return sprintf(page, "%llu\n", |
| 3582 | (unsigned long long)mddev->resync_min); |
| 3583 | } |
| 3584 | static ssize_t |
| 3585 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3586 | { |
| 3587 | unsigned long long min; |
| 3588 | if (strict_strtoull(buf, 10, &min)) |
| 3589 | return -EINVAL; |
| 3590 | if (min > mddev->resync_max) |
| 3591 | return -EINVAL; |
| 3592 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3593 | return -EBUSY; |
| 3594 | |
| 3595 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3596 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3597 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3598 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3599 | return -EINVAL; |
| 3600 | } |
| 3601 | mddev->resync_min = min; |
| 3602 | |
| 3603 | return len; |
| 3604 | } |
| 3605 | |
| 3606 | static struct md_sysfs_entry md_min_sync = |
| 3607 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3608 | |
| 3609 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3610 | max_sync_show(mddev_t *mddev, char *page) |
| 3611 | { |
| 3612 | if (mddev->resync_max == MaxSector) |
| 3613 | return sprintf(page, "max\n"); |
| 3614 | else |
| 3615 | return sprintf(page, "%llu\n", |
| 3616 | (unsigned long long)mddev->resync_max); |
| 3617 | } |
| 3618 | static ssize_t |
| 3619 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3620 | { |
| 3621 | if (strncmp(buf, "max", 3) == 0) |
| 3622 | mddev->resync_max = MaxSector; |
| 3623 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3624 | unsigned long long max; |
| 3625 | if (strict_strtoull(buf, 10, &max)) |
| 3626 | return -EINVAL; |
| 3627 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3628 | return -EINVAL; |
| 3629 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3630 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3631 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3632 | return -EBUSY; |
| 3633 | |
| 3634 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3635 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3636 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3637 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3638 | return -EINVAL; |
| 3639 | } |
| 3640 | mddev->resync_max = max; |
| 3641 | } |
| 3642 | wake_up(&mddev->recovery_wait); |
| 3643 | return len; |
| 3644 | } |
| 3645 | |
| 3646 | static struct md_sysfs_entry md_max_sync = |
| 3647 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3648 | |
| 3649 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3650 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3651 | { |
| 3652 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3653 | } |
| 3654 | |
| 3655 | static ssize_t |
| 3656 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3657 | { |
| 3658 | char *e; |
| 3659 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3660 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3661 | if (mddev->pers == NULL || |
| 3662 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3663 | return -EINVAL; |
| 3664 | if (buf == e || (*e && *e != '\n')) |
| 3665 | return -EINVAL; |
| 3666 | if (new >= mddev->suspend_hi || |
| 3667 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3668 | mddev->suspend_lo = new; |
| 3669 | mddev->pers->quiesce(mddev, 2); |
| 3670 | return len; |
| 3671 | } else |
| 3672 | return -EINVAL; |
| 3673 | } |
| 3674 | static struct md_sysfs_entry md_suspend_lo = |
| 3675 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3676 | |
| 3677 | |
| 3678 | static ssize_t |
| 3679 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3680 | { |
| 3681 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3682 | } |
| 3683 | |
| 3684 | static ssize_t |
| 3685 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3686 | { |
| 3687 | char *e; |
| 3688 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3689 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3690 | if (mddev->pers == NULL || |
| 3691 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3692 | return -EINVAL; |
| 3693 | if (buf == e || (*e && *e != '\n')) |
| 3694 | return -EINVAL; |
| 3695 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3696 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3697 | mddev->suspend_hi = new; |
| 3698 | mddev->pers->quiesce(mddev, 1); |
| 3699 | mddev->pers->quiesce(mddev, 0); |
| 3700 | return len; |
| 3701 | } else |
| 3702 | return -EINVAL; |
| 3703 | } |
| 3704 | static struct md_sysfs_entry md_suspend_hi = |
| 3705 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3706 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3707 | static ssize_t |
| 3708 | reshape_position_show(mddev_t *mddev, char *page) |
| 3709 | { |
| 3710 | if (mddev->reshape_position != MaxSector) |
| 3711 | return sprintf(page, "%llu\n", |
| 3712 | (unsigned long long)mddev->reshape_position); |
| 3713 | strcpy(page, "none\n"); |
| 3714 | return 5; |
| 3715 | } |
| 3716 | |
| 3717 | static ssize_t |
| 3718 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3719 | { |
| 3720 | char *e; |
| 3721 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3722 | if (mddev->pers) |
| 3723 | return -EBUSY; |
| 3724 | if (buf == e || (*e && *e != '\n')) |
| 3725 | return -EINVAL; |
| 3726 | mddev->reshape_position = new; |
| 3727 | mddev->delta_disks = 0; |
| 3728 | mddev->new_level = mddev->level; |
| 3729 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3730 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3731 | return len; |
| 3732 | } |
| 3733 | |
| 3734 | static struct md_sysfs_entry md_reshape_position = |
| 3735 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3736 | reshape_position_store); |
| 3737 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3738 | static ssize_t |
| 3739 | array_size_show(mddev_t *mddev, char *page) |
| 3740 | { |
| 3741 | if (mddev->external_size) |
| 3742 | return sprintf(page, "%llu\n", |
| 3743 | (unsigned long long)mddev->array_sectors/2); |
| 3744 | else |
| 3745 | return sprintf(page, "default\n"); |
| 3746 | } |
| 3747 | |
| 3748 | static ssize_t |
| 3749 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3750 | { |
| 3751 | sector_t sectors; |
| 3752 | |
| 3753 | if (strncmp(buf, "default", 7) == 0) { |
| 3754 | if (mddev->pers) |
| 3755 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3756 | else |
| 3757 | sectors = mddev->array_sectors; |
| 3758 | |
| 3759 | mddev->external_size = 0; |
| 3760 | } else { |
| 3761 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3762 | return -EINVAL; |
| 3763 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3764 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3765 | |
| 3766 | mddev->external_size = 1; |
| 3767 | } |
| 3768 | |
| 3769 | mddev->array_sectors = sectors; |
| 3770 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3771 | if (mddev->pers) |
| 3772 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3773 | |
| 3774 | return len; |
| 3775 | } |
| 3776 | |
| 3777 | static struct md_sysfs_entry md_array_size = |
| 3778 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3779 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3780 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3781 | static struct attribute *md_default_attrs[] = { |
| 3782 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3783 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3784 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3785 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3786 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3787 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3788 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3789 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3790 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3791 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3792 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3793 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3794 | NULL, |
| 3795 | }; |
| 3796 | |
| 3797 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3798 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3799 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3800 | &md_sync_min.attr, |
| 3801 | &md_sync_max.attr, |
| 3802 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3803 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3804 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3805 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3806 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3807 | &md_suspend_lo.attr, |
| 3808 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3809 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3810 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3811 | NULL, |
| 3812 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3813 | static struct attribute_group md_redundancy_group = { |
| 3814 | .name = NULL, |
| 3815 | .attrs = md_redundancy_attrs, |
| 3816 | }; |
| 3817 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3818 | |
| 3819 | static ssize_t |
| 3820 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3821 | { |
| 3822 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3823 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3824 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3825 | |
| 3826 | if (!entry->show) |
| 3827 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3828 | rv = mddev_lock(mddev); |
| 3829 | if (!rv) { |
| 3830 | rv = entry->show(mddev, page); |
| 3831 | mddev_unlock(mddev); |
| 3832 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3833 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | static ssize_t |
| 3837 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3838 | const char *page, size_t length) |
| 3839 | { |
| 3840 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3841 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3842 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3843 | |
| 3844 | if (!entry->store) |
| 3845 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3846 | if (!capable(CAP_SYS_ADMIN)) |
| 3847 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3848 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3849 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3850 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3851 | if (!rv) { |
| 3852 | rv = entry->store(mddev, page, length); |
| 3853 | mddev_unlock(mddev); |
| 3854 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3855 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | static void md_free(struct kobject *ko) |
| 3859 | { |
| 3860 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3861 | |
| 3862 | if (mddev->sysfs_state) |
| 3863 | sysfs_put(mddev->sysfs_state); |
| 3864 | |
| 3865 | if (mddev->gendisk) { |
| 3866 | del_gendisk(mddev->gendisk); |
| 3867 | put_disk(mddev->gendisk); |
| 3868 | } |
| 3869 | if (mddev->queue) |
| 3870 | blk_cleanup_queue(mddev->queue); |
| 3871 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3872 | kfree(mddev); |
| 3873 | } |
| 3874 | |
| 3875 | static struct sysfs_ops md_sysfs_ops = { |
| 3876 | .show = md_attr_show, |
| 3877 | .store = md_attr_store, |
| 3878 | }; |
| 3879 | static struct kobj_type md_ktype = { |
| 3880 | .release = md_free, |
| 3881 | .sysfs_ops = &md_sysfs_ops, |
| 3882 | .default_attrs = md_default_attrs, |
| 3883 | }; |
| 3884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | int mdp_major = 0; |
| 3886 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3887 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3888 | { |
| 3889 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3890 | |
| 3891 | if (mddev->private == &md_redundancy_group) { |
| 3892 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3893 | if (mddev->sysfs_action) |
| 3894 | sysfs_put(mddev->sysfs_action); |
| 3895 | mddev->sysfs_action = NULL; |
| 3896 | mddev->private = NULL; |
| 3897 | } |
| 3898 | kobject_del(&mddev->kobj); |
| 3899 | kobject_put(&mddev->kobj); |
| 3900 | } |
| 3901 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3902 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3904 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | mddev_t *mddev = mddev_find(dev); |
| 3906 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3907 | int partitioned; |
| 3908 | int shift; |
| 3909 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3910 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | |
| 3912 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3913 | return -ENODEV; |
| 3914 | |
| 3915 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3916 | shift = partitioned ? MdpMinorShift : 0; |
| 3917 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3919 | /* wait for any previous instance if this device |
| 3920 | * to be completed removed (mddev_delayed_delete). |
| 3921 | */ |
| 3922 | flush_scheduled_work(); |
| 3923 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3924 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3925 | error = -EEXIST; |
| 3926 | if (mddev->gendisk) |
| 3927 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3928 | |
| 3929 | if (name) { |
| 3930 | /* Need to ensure that 'name' is not a duplicate. |
| 3931 | */ |
| 3932 | mddev_t *mddev2; |
| 3933 | spin_lock(&all_mddevs_lock); |
| 3934 | |
| 3935 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3936 | if (mddev2->gendisk && |
| 3937 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3938 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3939 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3940 | } |
| 3941 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3943 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3944 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3945 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3946 | if (!mddev->queue) |
| 3947 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3948 | mddev->queue->queuedata = mddev; |
| 3949 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3950 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3951 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3952 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3953 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | disk = alloc_disk(1 << shift); |
| 3956 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3957 | blk_cleanup_queue(mddev->queue); |
| 3958 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3959 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3961 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3963 | if (name) |
| 3964 | strcpy(disk->disk_name, name); |
| 3965 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3967 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | disk->fops = &md_fops; |
| 3970 | disk->private_data = mddev; |
| 3971 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3972 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3973 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3974 | * remove it now. |
| 3975 | */ |
| 3976 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | add_disk(disk); |
| 3978 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3979 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3980 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3981 | if (error) { |
| 3982 | /* This isn't possible, but as kobject_init_and_add is marked |
| 3983 | * __must_check, we must do something with the result |
| 3984 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3985 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3986 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3987 | error = 0; |
| 3988 | } |
| 3989 | abort: |
| 3990 | mutex_unlock(&disks_mutex); |
| 3991 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3992 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3993 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3994 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3995 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3996 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4000 | { |
| 4001 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | return NULL; |
| 4003 | } |
| 4004 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4005 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4006 | { |
| 4007 | /* val must be "md_*" where * is not all digits. |
| 4008 | * We allocate an array with a large free minor number, and |
| 4009 | * set the name to val. val must not already be an active name. |
| 4010 | */ |
| 4011 | int len = strlen(val); |
| 4012 | char buf[DISK_NAME_LEN]; |
| 4013 | |
| 4014 | while (len && val[len-1] == '\n') |
| 4015 | len--; |
| 4016 | if (len >= DISK_NAME_LEN) |
| 4017 | return -E2BIG; |
| 4018 | strlcpy(buf, val, len+1); |
| 4019 | if (strncmp(buf, "md_", 3) != 0) |
| 4020 | return -EINVAL; |
| 4021 | return md_alloc(0, buf); |
| 4022 | } |
| 4023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | static void md_safemode_timeout(unsigned long data) |
| 4025 | { |
| 4026 | mddev_t *mddev = (mddev_t *) data; |
| 4027 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4028 | if (!atomic_read(&mddev->writes_pending)) { |
| 4029 | mddev->safemode = 1; |
| 4030 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4031 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | md_wakeup_thread(mddev->thread); |
| 4034 | } |
| 4035 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4036 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | |
| 4038 | static int do_md_run(mddev_t * mddev) |
| 4039 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4040 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | mdk_rdev_t *rdev; |
| 4042 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4043 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4045 | if (list_empty(&mddev->disks)) |
| 4046 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | |
| 4049 | if (mddev->pers) |
| 4050 | return -EBUSY; |
| 4051 | |
| 4052 | /* |
| 4053 | * Analyze all RAID superblock(s) |
| 4054 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4055 | if (!mddev->raid_disks) { |
| 4056 | if (!mddev->persistent) |
| 4057 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4058 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4061 | if (mddev->level != LEVEL_NONE) |
| 4062 | request_module("md-level-%d", mddev->level); |
| 4063 | else if (mddev->clevel[0]) |
| 4064 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | |
| 4066 | /* |
| 4067 | * Drop all container device buffers, from now on |
| 4068 | * the only valid external interface is through the md |
| 4069 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4071 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4072 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | continue; |
| 4074 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4075 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4076 | |
| 4077 | /* perform some consistency tests on the device. |
| 4078 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4079 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4080 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4081 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4082 | if (mddev->dev_sectors && |
| 4083 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4084 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4085 | printk("md: %s: data overlaps metadata\n", |
| 4086 | mdname(mddev)); |
| 4087 | return -EINVAL; |
| 4088 | } |
| 4089 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4090 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4091 | > rdev->data_offset) { |
| 4092 | printk("md: %s: metadata overlaps data\n", |
| 4093 | mdname(mddev)); |
| 4094 | return -EINVAL; |
| 4095 | } |
| 4096 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4097 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | md_probe(mddev->unit, NULL, NULL); |
| 4101 | disk = mddev->gendisk; |
| 4102 | if (!disk) |
| 4103 | return -ENOMEM; |
| 4104 | |
| 4105 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4106 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4107 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4109 | if (mddev->level != LEVEL_NONE) |
| 4110 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4111 | mddev->level); |
| 4112 | else |
| 4113 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4114 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | return -EINVAL; |
| 4116 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4117 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4119 | if (mddev->level != pers->level) { |
| 4120 | mddev->level = pers->level; |
| 4121 | mddev->new_level = pers->level; |
| 4122 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4123 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4125 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4126 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4127 | /* This personality cannot handle reshaping... */ |
| 4128 | mddev->pers = NULL; |
| 4129 | module_put(pers->owner); |
| 4130 | return -EINVAL; |
| 4131 | } |
| 4132 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4133 | if (pers->sync_request) { |
| 4134 | /* Warn if this is a potentially silly |
| 4135 | * configuration. |
| 4136 | */ |
| 4137 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4138 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4139 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4140 | |
| 4141 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4142 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4143 | if (rdev < rdev2 && |
| 4144 | rdev->bdev->bd_contains == |
| 4145 | rdev2->bdev->bd_contains) { |
| 4146 | printk(KERN_WARNING |
| 4147 | "%s: WARNING: %s appears to be" |
| 4148 | " on the same physical disk as" |
| 4149 | " %s.\n", |
| 4150 | mdname(mddev), |
| 4151 | bdevname(rdev->bdev,b), |
| 4152 | bdevname(rdev2->bdev,b2)); |
| 4153 | warned = 1; |
| 4154 | } |
| 4155 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4156 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4157 | if (warned) |
| 4158 | printk(KERN_WARNING |
| 4159 | "True protection against single-disk" |
| 4160 | " failure might be compromised.\n"); |
| 4161 | } |
| 4162 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4163 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4164 | /* may be over-ridden by personality */ |
| 4165 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4166 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4167 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4168 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4170 | if (start_readonly) |
| 4171 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4172 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4173 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4174 | if (err) |
| 4175 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4176 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4177 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4178 | " but 'external_size' not in effect?\n", __func__); |
| 4179 | printk(KERN_ERR |
| 4180 | "md: invalid array_size %llu > default size %llu\n", |
| 4181 | (unsigned long long)mddev->array_sectors / 2, |
| 4182 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4183 | err = -EINVAL; |
| 4184 | mddev->pers->stop(mddev); |
| 4185 | } |
| 4186 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4187 | err = bitmap_create(mddev); |
| 4188 | if (err) { |
| 4189 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4190 | mdname(mddev), err); |
| 4191 | mddev->pers->stop(mddev); |
| 4192 | } |
| 4193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | module_put(mddev->pers->owner); |
| 4196 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4197 | bitmap_destroy(mddev); |
| 4198 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4200 | if (mddev->pers->sync_request) { |
| 4201 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4202 | printk(KERN_WARNING |
| 4203 | "md: cannot register extra attributes for %s\n", |
| 4204 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4205 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4206 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4207 | mddev->ro = 0; |
| 4208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | atomic_set(&mddev->writes_pending,0); |
| 4210 | mddev->safemode = 0; |
| 4211 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4212 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4213 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4215 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4216 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4217 | if (rdev->raid_disk >= 0) { |
| 4218 | char nm[20]; |
| 4219 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4220 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4221 | printk("md: cannot register %s for %s\n", |
| 4222 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
| 4225 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4226 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4227 | if (mddev->flags) |
| 4228 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4230 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4232 | /* If there is a partially-recovered drive we need to |
| 4233 | * start recovery here. If we leave it to md_check_recovery, |
| 4234 | * it will remove the drives and not do the right thing |
| 4235 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4236 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4237 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4238 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4239 | if (rdev->raid_disk >= 0 && |
| 4240 | !test_bit(In_sync, &rdev->flags) && |
| 4241 | !test_bit(Faulty, &rdev->flags)) |
| 4242 | /* complete an interrupted recovery */ |
| 4243 | spares++; |
| 4244 | if (spares && mddev->pers->sync_request) { |
| 4245 | mddev->recovery = 0; |
| 4246 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4247 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4248 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4249 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4250 | if (!mddev->sync_thread) { |
| 4251 | printk(KERN_ERR "%s: could not start resync" |
| 4252 | " thread...\n", |
| 4253 | mdname(mddev)); |
| 4254 | /* leave the spares where they are, it shouldn't hurt */ |
| 4255 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4256 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4259 | md_wakeup_thread(mddev->thread); |
| 4260 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4261 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4262 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4263 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4264 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4265 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4266 | if (mddev->sysfs_action) |
| 4267 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4268 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4269 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | return 0; |
| 4271 | } |
| 4272 | |
| 4273 | static int restart_array(mddev_t *mddev) |
| 4274 | { |
| 4275 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4277 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4279 | return -ENXIO; |
| 4280 | if (!mddev->pers) |
| 4281 | return -EINVAL; |
| 4282 | if (!mddev->ro) |
| 4283 | return -EBUSY; |
| 4284 | mddev->safemode = 0; |
| 4285 | mddev->ro = 0; |
| 4286 | set_disk_ro(disk, 0); |
| 4287 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4288 | mdname(mddev)); |
| 4289 | /* Kick recovery or resync if necessary */ |
| 4290 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4291 | md_wakeup_thread(mddev->thread); |
| 4292 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4293 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4297 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4298 | * to the file ourselves */ |
| 4299 | static int deny_bitmap_write_access(struct file * file) |
| 4300 | { |
| 4301 | struct inode *inode = file->f_mapping->host; |
| 4302 | |
| 4303 | spin_lock(&inode->i_lock); |
| 4304 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4305 | spin_unlock(&inode->i_lock); |
| 4306 | return -ETXTBSY; |
| 4307 | } |
| 4308 | atomic_set(&inode->i_writecount, -1); |
| 4309 | spin_unlock(&inode->i_lock); |
| 4310 | |
| 4311 | return 0; |
| 4312 | } |
| 4313 | |
| 4314 | static void restore_bitmap_write_access(struct file *file) |
| 4315 | { |
| 4316 | struct inode *inode = file->f_mapping->host; |
| 4317 | |
| 4318 | spin_lock(&inode->i_lock); |
| 4319 | atomic_set(&inode->i_writecount, 1); |
| 4320 | spin_unlock(&inode->i_lock); |
| 4321 | } |
| 4322 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4323 | /* mode: |
| 4324 | * 0 - completely stop and dis-assemble array |
| 4325 | * 1 - switch to readonly |
| 4326 | * 2 - stop but do not disassemble array |
| 4327 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4328 | 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] | 4329 | { |
| 4330 | int err = 0; |
| 4331 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4332 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4334 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4335 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4336 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4337 | err = -EBUSY; |
| 4338 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
| 4340 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4341 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4343 | md_unregister_thread(mddev->sync_thread); |
| 4344 | mddev->sync_thread = NULL; |
| 4345 | } |
| 4346 | |
| 4347 | del_timer_sync(&mddev->safemode_timer); |
| 4348 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4349 | switch(mode) { |
| 4350 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4352 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | goto out; |
| 4354 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4355 | break; |
| 4356 | case 0: /* disassemble */ |
| 4357 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4358 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4359 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | if (mddev->ro) |
| 4361 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4364 | mddev->queue->merge_bvec_fn = NULL; |
| 4365 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4366 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4368 | if (mddev->pers->sync_request) |
| 4369 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4371 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4372 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4373 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4374 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4375 | if (rdev->raid_disk >= 0) { |
| 4376 | char nm[20]; |
| 4377 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4378 | sysfs_remove_link(&mddev->kobj, nm); |
| 4379 | } |
| 4380 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4381 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4382 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | if (mddev->ro) |
| 4385 | mddev->ro = 0; |
| 4386 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4387 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | /* mark array as shutdown cleanly */ |
| 4389 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4390 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4392 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4394 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4395 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4397 | out: |
| 4398 | mutex_unlock(&mddev->open_mutex); |
| 4399 | if (err) |
| 4400 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | /* |
| 4402 | * Free resources if final stop |
| 4403 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4404 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4407 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4408 | bitmap_destroy(mddev); |
| 4409 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4410 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4411 | fput(mddev->bitmap_file); |
| 4412 | mddev->bitmap_file = NULL; |
| 4413 | } |
| 4414 | mddev->bitmap_offset = 0; |
| 4415 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4416 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4417 | flush_scheduled_work(); |
| 4418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | export_array(mddev); |
| 4420 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4421 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4422 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4423 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4424 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4425 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4426 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4427 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4428 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4429 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4430 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4431 | mddev->level = LEVEL_NONE; |
| 4432 | mddev->clevel[0] = 0; |
| 4433 | mddev->flags = 0; |
| 4434 | mddev->ro = 0; |
| 4435 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4436 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4437 | mddev->ctime = mddev->utime = 0; |
| 4438 | mddev->layout = 0; |
| 4439 | mddev->max_disks = 0; |
| 4440 | mddev->events = 0; |
| 4441 | mddev->delta_disks = 0; |
| 4442 | mddev->new_level = LEVEL_NONE; |
| 4443 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4444 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4445 | mddev->curr_resync = 0; |
| 4446 | mddev->resync_mismatches = 0; |
| 4447 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4448 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4449 | mddev->recovery = 0; |
| 4450 | mddev->in_sync = 0; |
| 4451 | mddev->changed = 0; |
| 4452 | mddev->degraded = 0; |
| 4453 | mddev->barriers_work = 0; |
| 4454 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4455 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4456 | if (mddev->hold_active == UNTIL_STOP) |
| 4457 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4458 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4459 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4461 | mdname(mddev)); |
| 4462 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4463 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4464 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4465 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | return err; |
| 4467 | } |
| 4468 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4469 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | static void autorun_array(mddev_t *mddev) |
| 4471 | { |
| 4472 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | int err; |
| 4474 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4475 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | |
| 4478 | printk(KERN_INFO "md: running: "); |
| 4479 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4480 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | char b[BDEVNAME_SIZE]; |
| 4482 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4483 | } |
| 4484 | printk("\n"); |
| 4485 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4486 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | if (err) { |
| 4488 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4489 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | /* |
| 4494 | * lets try to run arrays based on all disks that have arrived |
| 4495 | * until now. (those are in pending_raid_disks) |
| 4496 | * |
| 4497 | * the method: pick the first pending disk, collect all disks with |
| 4498 | * the same UUID, remove all from the pending list and put them into |
| 4499 | * the 'same_array' list. Then order this list based on superblock |
| 4500 | * update time (freshest comes first), kick out 'old' disks and |
| 4501 | * compare superblocks. If everything's fine then run it. |
| 4502 | * |
| 4503 | * If "unit" is allocated, then bump its reference count |
| 4504 | */ |
| 4505 | static void autorun_devices(int part) |
| 4506 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4507 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | mddev_t *mddev; |
| 4509 | char b[BDEVNAME_SIZE]; |
| 4510 | |
| 4511 | printk(KERN_INFO "md: autorun ...\n"); |
| 4512 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4513 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4515 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | rdev0 = list_entry(pending_raid_disks.next, |
| 4517 | mdk_rdev_t, same_set); |
| 4518 | |
| 4519 | printk(KERN_INFO "md: considering %s ...\n", |
| 4520 | bdevname(rdev0->bdev,b)); |
| 4521 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4522 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4524 | printk(KERN_INFO "md: adding %s ...\n", |
| 4525 | bdevname(rdev->bdev,b)); |
| 4526 | list_move(&rdev->same_set, &candidates); |
| 4527 | } |
| 4528 | /* |
| 4529 | * now we have a set of devices, with all of them having |
| 4530 | * mostly sane superblocks. It's time to allocate the |
| 4531 | * mddev. |
| 4532 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4533 | if (part) { |
| 4534 | dev = MKDEV(mdp_major, |
| 4535 | rdev0->preferred_minor << MdpMinorShift); |
| 4536 | unit = MINOR(dev) >> MdpMinorShift; |
| 4537 | } else { |
| 4538 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4539 | unit = MINOR(dev); |
| 4540 | } |
| 4541 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4543 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4544 | break; |
| 4545 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | |
| 4547 | md_probe(dev, NULL, NULL); |
| 4548 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4549 | if (!mddev || !mddev->gendisk) { |
| 4550 | if (mddev) |
| 4551 | mddev_put(mddev); |
| 4552 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | "md: cannot allocate memory for md drive.\n"); |
| 4554 | break; |
| 4555 | } |
| 4556 | if (mddev_lock(mddev)) |
| 4557 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4558 | mdname(mddev)); |
| 4559 | else if (mddev->raid_disks || mddev->major_version |
| 4560 | || !list_empty(&mddev->disks)) { |
| 4561 | printk(KERN_WARNING |
| 4562 | "md: %s already running, cannot run %s\n", |
| 4563 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4564 | mddev_unlock(mddev); |
| 4565 | } else { |
| 4566 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4567 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4568 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | list_del_init(&rdev->same_set); |
| 4570 | if (bind_rdev_to_array(rdev, mddev)) |
| 4571 | export_rdev(rdev); |
| 4572 | } |
| 4573 | autorun_array(mddev); |
| 4574 | mddev_unlock(mddev); |
| 4575 | } |
| 4576 | /* on success, candidates will be empty, on error |
| 4577 | * it won't... |
| 4578 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4579 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4580 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | mddev_put(mddev); |
| 4584 | } |
| 4585 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4586 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4587 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | static int get_version(void __user * arg) |
| 4590 | { |
| 4591 | mdu_version_t ver; |
| 4592 | |
| 4593 | ver.major = MD_MAJOR_VERSION; |
| 4594 | ver.minor = MD_MINOR_VERSION; |
| 4595 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4596 | |
| 4597 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4598 | return -EFAULT; |
| 4599 | |
| 4600 | return 0; |
| 4601 | } |
| 4602 | |
| 4603 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4604 | { |
| 4605 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4606 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4609 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4610 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4612 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | failed++; |
| 4614 | else { |
| 4615 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4616 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4617 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | else |
| 4619 | spare++; |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | info.major_version = mddev->major_version; |
| 4624 | info.minor_version = mddev->minor_version; |
| 4625 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4626 | info.ctime = mddev->ctime; |
| 4627 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4628 | info.size = mddev->dev_sectors / 2; |
| 4629 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4630 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | info.nr_disks = nr; |
| 4632 | info.raid_disks = mddev->raid_disks; |
| 4633 | info.md_minor = mddev->md_minor; |
| 4634 | info.not_persistent= !mddev->persistent; |
| 4635 | |
| 4636 | info.utime = mddev->utime; |
| 4637 | info.state = 0; |
| 4638 | if (mddev->in_sync) |
| 4639 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4640 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4641 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4642 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | info.working_disks = working; |
| 4644 | info.failed_disks = failed; |
| 4645 | info.spare_disks = spare; |
| 4646 | |
| 4647 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4648 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | |
| 4650 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4651 | return -EFAULT; |
| 4652 | |
| 4653 | return 0; |
| 4654 | } |
| 4655 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4656 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4657 | { |
| 4658 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4659 | char *ptr, *buf = NULL; |
| 4660 | int err = -ENOMEM; |
| 4661 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4662 | if (md_allow_write(mddev)) |
| 4663 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4664 | else |
| 4665 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4666 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4667 | if (!file) |
| 4668 | goto out; |
| 4669 | |
| 4670 | /* bitmap disabled, zero the first byte and copy out */ |
| 4671 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4672 | file->pathname[0] = '\0'; |
| 4673 | goto copy_out; |
| 4674 | } |
| 4675 | |
| 4676 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4677 | if (!buf) |
| 4678 | goto out; |
| 4679 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4680 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4681 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4682 | goto out; |
| 4683 | |
| 4684 | strcpy(file->pathname, ptr); |
| 4685 | |
| 4686 | copy_out: |
| 4687 | err = 0; |
| 4688 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4689 | err = -EFAULT; |
| 4690 | out: |
| 4691 | kfree(buf); |
| 4692 | kfree(file); |
| 4693 | return err; |
| 4694 | } |
| 4695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4697 | { |
| 4698 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | mdk_rdev_t *rdev; |
| 4700 | |
| 4701 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4702 | return -EFAULT; |
| 4703 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4704 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | if (rdev) { |
| 4706 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4707 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4708 | info.raid_disk = rdev->raid_disk; |
| 4709 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4710 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4712 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4714 | info.state |= (1<<MD_DISK_SYNC); |
| 4715 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4716 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4717 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | } else { |
| 4719 | info.major = info.minor = 0; |
| 4720 | info.raid_disk = -1; |
| 4721 | info.state = (1<<MD_DISK_REMOVED); |
| 4722 | } |
| 4723 | |
| 4724 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4725 | return -EFAULT; |
| 4726 | |
| 4727 | return 0; |
| 4728 | } |
| 4729 | |
| 4730 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4731 | { |
| 4732 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4733 | mdk_rdev_t *rdev; |
| 4734 | dev_t dev = MKDEV(info->major,info->minor); |
| 4735 | |
| 4736 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4737 | return -EOVERFLOW; |
| 4738 | |
| 4739 | if (!mddev->raid_disks) { |
| 4740 | int err; |
| 4741 | /* expecting a device which has a superblock */ |
| 4742 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4743 | if (IS_ERR(rdev)) { |
| 4744 | printk(KERN_WARNING |
| 4745 | "md: md_import_device returned %ld\n", |
| 4746 | PTR_ERR(rdev)); |
| 4747 | return PTR_ERR(rdev); |
| 4748 | } |
| 4749 | if (!list_empty(&mddev->disks)) { |
| 4750 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4751 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4752 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4754 | if (err < 0) { |
| 4755 | printk(KERN_WARNING |
| 4756 | "md: %s has different UUID to %s\n", |
| 4757 | bdevname(rdev->bdev,b), |
| 4758 | bdevname(rdev0->bdev,b2)); |
| 4759 | export_rdev(rdev); |
| 4760 | return -EINVAL; |
| 4761 | } |
| 4762 | } |
| 4763 | err = bind_rdev_to_array(rdev, mddev); |
| 4764 | if (err) |
| 4765 | export_rdev(rdev); |
| 4766 | return err; |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * add_new_disk can be used once the array is assembled |
| 4771 | * to add "hot spares". They must already have a superblock |
| 4772 | * written |
| 4773 | */ |
| 4774 | if (mddev->pers) { |
| 4775 | int err; |
| 4776 | if (!mddev->pers->hot_add_disk) { |
| 4777 | printk(KERN_WARNING |
| 4778 | "%s: personality does not support diskops!\n", |
| 4779 | mdname(mddev)); |
| 4780 | return -EINVAL; |
| 4781 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4782 | if (mddev->persistent) |
| 4783 | rdev = md_import_device(dev, mddev->major_version, |
| 4784 | mddev->minor_version); |
| 4785 | else |
| 4786 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | if (IS_ERR(rdev)) { |
| 4788 | printk(KERN_WARNING |
| 4789 | "md: md_import_device returned %ld\n", |
| 4790 | PTR_ERR(rdev)); |
| 4791 | return PTR_ERR(rdev); |
| 4792 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4793 | /* set save_raid_disk if appropriate */ |
| 4794 | if (!mddev->persistent) { |
| 4795 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4796 | info->raid_disk < mddev->raid_disks) |
| 4797 | rdev->raid_disk = info->raid_disk; |
| 4798 | else |
| 4799 | rdev->raid_disk = -1; |
| 4800 | } else |
| 4801 | super_types[mddev->major_version]. |
| 4802 | validate_super(mddev, rdev); |
| 4803 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4804 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4805 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4806 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4807 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4808 | else |
| 4809 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | rdev->raid_disk = -1; |
| 4812 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4813 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4814 | /* If there is hot_add_disk but no hot_remove_disk |
| 4815 | * then added disks for geometry changes, |
| 4816 | * and should be added immediately. |
| 4817 | */ |
| 4818 | super_types[mddev->major_version]. |
| 4819 | validate_super(mddev, rdev); |
| 4820 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4821 | if (err) |
| 4822 | unbind_rdev_from_array(rdev); |
| 4823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | if (err) |
| 4825 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4826 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4827 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4828 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4829 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4830 | if (mddev->degraded) |
| 4831 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4832 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4833 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | return err; |
| 4835 | } |
| 4836 | |
| 4837 | /* otherwise, add_new_disk is only allowed |
| 4838 | * for major_version==0 superblocks |
| 4839 | */ |
| 4840 | if (mddev->major_version != 0) { |
| 4841 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4842 | mdname(mddev)); |
| 4843 | return -EINVAL; |
| 4844 | } |
| 4845 | |
| 4846 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4847 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4848 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | if (IS_ERR(rdev)) { |
| 4850 | printk(KERN_WARNING |
| 4851 | "md: error, md_import_device() returned %ld\n", |
| 4852 | PTR_ERR(rdev)); |
| 4853 | return PTR_ERR(rdev); |
| 4854 | } |
| 4855 | rdev->desc_nr = info->number; |
| 4856 | if (info->raid_disk < mddev->raid_disks) |
| 4857 | rdev->raid_disk = info->raid_disk; |
| 4858 | else |
| 4859 | rdev->raid_disk = -1; |
| 4860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4862 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4863 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4865 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4866 | set_bit(WriteMostly, &rdev->flags); |
| 4867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | if (!mddev->persistent) { |
| 4869 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4870 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4872 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4873 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4875 | err = bind_rdev_to_array(rdev, mddev); |
| 4876 | if (err) { |
| 4877 | export_rdev(rdev); |
| 4878 | return err; |
| 4879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4886 | { |
| 4887 | char b[BDEVNAME_SIZE]; |
| 4888 | mdk_rdev_t *rdev; |
| 4889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | rdev = find_rdev(mddev, dev); |
| 4891 | if (!rdev) |
| 4892 | return -ENXIO; |
| 4893 | |
| 4894 | if (rdev->raid_disk >= 0) |
| 4895 | goto busy; |
| 4896 | |
| 4897 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4898 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4899 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | |
| 4901 | return 0; |
| 4902 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4903 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4905 | return -EBUSY; |
| 4906 | } |
| 4907 | |
| 4908 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4909 | { |
| 4910 | char b[BDEVNAME_SIZE]; |
| 4911 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | mdk_rdev_t *rdev; |
| 4913 | |
| 4914 | if (!mddev->pers) |
| 4915 | return -ENODEV; |
| 4916 | |
| 4917 | if (mddev->major_version != 0) { |
| 4918 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4919 | " version-0 superblocks.\n", |
| 4920 | mdname(mddev)); |
| 4921 | return -EINVAL; |
| 4922 | } |
| 4923 | if (!mddev->pers->hot_add_disk) { |
| 4924 | printk(KERN_WARNING |
| 4925 | "%s: personality does not support diskops!\n", |
| 4926 | mdname(mddev)); |
| 4927 | return -EINVAL; |
| 4928 | } |
| 4929 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4930 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | if (IS_ERR(rdev)) { |
| 4932 | printk(KERN_WARNING |
| 4933 | "md: error, md_import_device() returned %ld\n", |
| 4934 | PTR_ERR(rdev)); |
| 4935 | return -EINVAL; |
| 4936 | } |
| 4937 | |
| 4938 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4939 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4941 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4943 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4945 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | printk(KERN_WARNING |
| 4947 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4948 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4949 | err = -EINVAL; |
| 4950 | goto abort_export; |
| 4951 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4952 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4954 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4955 | err = bind_rdev_to_array(rdev, mddev); |
| 4956 | if (err) |
| 4957 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | |
| 4959 | /* |
| 4960 | * The rest should better be atomic, we can have disk failures |
| 4961 | * noticed in interrupt contexts ... |
| 4962 | */ |
| 4963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | rdev->raid_disk = -1; |
| 4965 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4966 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
| 4968 | /* |
| 4969 | * Kick recovery, maybe this spare has to be added to the |
| 4970 | * array immediately. |
| 4971 | */ |
| 4972 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4973 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4974 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | return 0; |
| 4976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | abort_export: |
| 4978 | export_rdev(rdev); |
| 4979 | return err; |
| 4980 | } |
| 4981 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4982 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4983 | { |
| 4984 | int err; |
| 4985 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4986 | if (mddev->pers) { |
| 4987 | if (!mddev->pers->quiesce) |
| 4988 | return -EBUSY; |
| 4989 | if (mddev->recovery || mddev->sync_thread) |
| 4990 | return -EBUSY; |
| 4991 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4992 | } |
| 4993 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4994 | |
| 4995 | if (fd >= 0) { |
| 4996 | if (mddev->bitmap) |
| 4997 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4998 | mddev->bitmap_file = fget(fd); |
| 4999 | |
| 5000 | if (mddev->bitmap_file == NULL) { |
| 5001 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5002 | mdname(mddev)); |
| 5003 | return -EBADF; |
| 5004 | } |
| 5005 | |
| 5006 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 5007 | if (err) { |
| 5008 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5009 | mdname(mddev)); |
| 5010 | fput(mddev->bitmap_file); |
| 5011 | mddev->bitmap_file = NULL; |
| 5012 | return err; |
| 5013 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 5014 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5015 | } else if (mddev->bitmap == NULL) |
| 5016 | return -ENOENT; /* cannot remove what isn't there */ |
| 5017 | err = 0; |
| 5018 | if (mddev->pers) { |
| 5019 | mddev->pers->quiesce(mddev, 1); |
| 5020 | if (fd >= 0) |
| 5021 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5022 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5023 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5024 | fd = -1; /* make sure to put the file */ |
| 5025 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5026 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5027 | } |
| 5028 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5029 | if (mddev->bitmap_file) { |
| 5030 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5031 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5032 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5033 | mddev->bitmap_file = NULL; |
| 5034 | } |
| 5035 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5036 | return err; |
| 5037 | } |
| 5038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | /* |
| 5040 | * set_array_info is used two different ways |
| 5041 | * The original usage is when creating a new array. |
| 5042 | * In this usage, raid_disks is > 0 and it together with |
| 5043 | * level, size, not_persistent,layout,chunksize determine the |
| 5044 | * shape of the array. |
| 5045 | * This will always create an array with a type-0.90.0 superblock. |
| 5046 | * The newer usage is when assembling an array. |
| 5047 | * In this case raid_disks will be 0, and the major_version field is |
| 5048 | * use to determine which style super-blocks are to be found on the devices. |
| 5049 | * The minor and patch _version numbers are also kept incase the |
| 5050 | * super_block handler wishes to interpret them. |
| 5051 | */ |
| 5052 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5053 | { |
| 5054 | |
| 5055 | if (info->raid_disks == 0) { |
| 5056 | /* just setting version number for superblock loading */ |
| 5057 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5058 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | super_types[info->major_version].name == NULL) { |
| 5060 | /* maybe try to auto-load a module? */ |
| 5061 | printk(KERN_INFO |
| 5062 | "md: superblock version %d not known\n", |
| 5063 | info->major_version); |
| 5064 | return -EINVAL; |
| 5065 | } |
| 5066 | mddev->major_version = info->major_version; |
| 5067 | mddev->minor_version = info->minor_version; |
| 5068 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5069 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | return 0; |
| 5071 | } |
| 5072 | mddev->major_version = MD_MAJOR_VERSION; |
| 5073 | mddev->minor_version = MD_MINOR_VERSION; |
| 5074 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5075 | mddev->ctime = get_seconds(); |
| 5076 | |
| 5077 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5078 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5079 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | mddev->raid_disks = info->raid_disks; |
| 5081 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5082 | * openned |
| 5083 | */ |
| 5084 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5085 | mddev->recovery_cp = MaxSector; |
| 5086 | else |
| 5087 | mddev->recovery_cp = 0; |
| 5088 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5089 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | |
| 5091 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5092 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | |
| 5094 | mddev->max_disks = MD_SB_DISKS; |
| 5095 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5096 | if (mddev->persistent) |
| 5097 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5098 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5100 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5101 | mddev->bitmap_offset = 0; |
| 5102 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5103 | mddev->reshape_position = MaxSector; |
| 5104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | /* |
| 5106 | * Generate a 128 bit UUID |
| 5107 | */ |
| 5108 | get_random_bytes(mddev->uuid, 16); |
| 5109 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5110 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5111 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5112 | mddev->new_layout = mddev->layout; |
| 5113 | mddev->delta_disks = 0; |
| 5114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | return 0; |
| 5116 | } |
| 5117 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5118 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5119 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5120 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5121 | |
| 5122 | if (mddev->external_size) |
| 5123 | return; |
| 5124 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5125 | mddev->array_sectors = array_sectors; |
| 5126 | } |
| 5127 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5128 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5129 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5130 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5131 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5132 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5133 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5134 | |
| 5135 | if (mddev->pers->resize == NULL) |
| 5136 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5137 | /* The "num_sectors" is the number of sectors of each device that |
| 5138 | * is used. This can only make sense for arrays with redundancy. |
| 5139 | * linear and raid0 always use whatever space is available. We can only |
| 5140 | * consider changing this number if no resync or reconstruction is |
| 5141 | * 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] | 5142 | * 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] | 5143 | * of each device. If num_sectors is zero, we find the largest size |
| 5144 | * that fits. |
| 5145 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5146 | */ |
| 5147 | if (mddev->sync_thread) |
| 5148 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5149 | if (mddev->bitmap) |
| 5150 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5151 | * grow, and re-add. |
| 5152 | */ |
| 5153 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5154 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5155 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5156 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5157 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5158 | num_sectors = avail; |
| 5159 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5160 | return -ENOSPC; |
| 5161 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5162 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5163 | if (!rv) |
| 5164 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5165 | return rv; |
| 5166 | } |
| 5167 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5168 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5169 | { |
| 5170 | int rv; |
| 5171 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5172 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5173 | return -EINVAL; |
| 5174 | if (raid_disks <= 0 || |
| 5175 | raid_disks >= mddev->max_disks) |
| 5176 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5177 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5178 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5179 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5180 | |
| 5181 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5182 | return rv; |
| 5183 | } |
| 5184 | |
| 5185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | /* |
| 5187 | * update_array_info is used to change the configuration of an |
| 5188 | * on-line array. |
| 5189 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5190 | * fields in the info are checked against the array. |
| 5191 | * Any differences that cannot be handled will cause an error. |
| 5192 | * Normally, only one change can be managed at a time. |
| 5193 | */ |
| 5194 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5195 | { |
| 5196 | int rv = 0; |
| 5197 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5198 | int state = 0; |
| 5199 | |
| 5200 | /* calculate expected state,ignoring low bits */ |
| 5201 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5202 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | |
| 5204 | if (mddev->major_version != info->major_version || |
| 5205 | mddev->minor_version != info->minor_version || |
| 5206 | /* mddev->patch_version != info->patch_version || */ |
| 5207 | mddev->ctime != info->ctime || |
| 5208 | mddev->level != info->level || |
| 5209 | /* mddev->layout != info->layout || */ |
| 5210 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5211 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5212 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5213 | ((state^info->state) & 0xfffffe00) |
| 5214 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | return -EINVAL; |
| 5216 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5217 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5218 | cnt++; |
| 5219 | if (mddev->raid_disks != info->raid_disks) |
| 5220 | cnt++; |
| 5221 | if (mddev->layout != info->layout) |
| 5222 | cnt++; |
| 5223 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5224 | cnt++; |
| 5225 | if (cnt == 0) |
| 5226 | return 0; |
| 5227 | if (cnt > 1) |
| 5228 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | |
| 5230 | if (mddev->layout != info->layout) { |
| 5231 | /* Change layout |
| 5232 | * we don't need to do anything at the md level, the |
| 5233 | * personality will take care of it all. |
| 5234 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5235 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5237 | else { |
| 5238 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5239 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5240 | if (rv) |
| 5241 | mddev->new_layout = mddev->layout; |
| 5242 | return rv; |
| 5243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5245 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5246 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5248 | if (mddev->raid_disks != info->raid_disks) |
| 5249 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5251 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5252 | if (mddev->pers->quiesce == NULL) |
| 5253 | return -EINVAL; |
| 5254 | if (mddev->recovery || mddev->sync_thread) |
| 5255 | return -EBUSY; |
| 5256 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5257 | /* add the bitmap */ |
| 5258 | if (mddev->bitmap) |
| 5259 | return -EEXIST; |
| 5260 | if (mddev->default_bitmap_offset == 0) |
| 5261 | return -EINVAL; |
| 5262 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5263 | mddev->pers->quiesce(mddev, 1); |
| 5264 | rv = bitmap_create(mddev); |
| 5265 | if (rv) |
| 5266 | bitmap_destroy(mddev); |
| 5267 | mddev->pers->quiesce(mddev, 0); |
| 5268 | } else { |
| 5269 | /* remove the bitmap */ |
| 5270 | if (!mddev->bitmap) |
| 5271 | return -ENOENT; |
| 5272 | if (mddev->bitmap->file) |
| 5273 | return -EINVAL; |
| 5274 | mddev->pers->quiesce(mddev, 1); |
| 5275 | bitmap_destroy(mddev); |
| 5276 | mddev->pers->quiesce(mddev, 0); |
| 5277 | mddev->bitmap_offset = 0; |
| 5278 | } |
| 5279 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5280 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | return rv; |
| 5282 | } |
| 5283 | |
| 5284 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5285 | { |
| 5286 | mdk_rdev_t *rdev; |
| 5287 | |
| 5288 | if (mddev->pers == NULL) |
| 5289 | return -ENODEV; |
| 5290 | |
| 5291 | rdev = find_rdev(mddev, dev); |
| 5292 | if (!rdev) |
| 5293 | return -ENODEV; |
| 5294 | |
| 5295 | md_error(mddev, rdev); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5299 | /* |
| 5300 | * We have a problem here : there is no easy way to give a CHS |
| 5301 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5302 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5303 | * dosfs just mad... ;-) |
| 5304 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5305 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5306 | { |
| 5307 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5308 | |
| 5309 | geo->heads = 2; |
| 5310 | geo->sectors = 4; |
| 5311 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5315 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | unsigned int cmd, unsigned long arg) |
| 5317 | { |
| 5318 | int err = 0; |
| 5319 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | mddev_t *mddev = NULL; |
| 5321 | |
| 5322 | if (!capable(CAP_SYS_ADMIN)) |
| 5323 | return -EACCES; |
| 5324 | |
| 5325 | /* |
| 5326 | * Commands dealing with the RAID driver but not any |
| 5327 | * particular array: |
| 5328 | */ |
| 5329 | switch (cmd) |
| 5330 | { |
| 5331 | case RAID_VERSION: |
| 5332 | err = get_version(argp); |
| 5333 | goto done; |
| 5334 | |
| 5335 | case PRINT_RAID_DEBUG: |
| 5336 | err = 0; |
| 5337 | md_print_devices(); |
| 5338 | goto done; |
| 5339 | |
| 5340 | #ifndef MODULE |
| 5341 | case RAID_AUTORUN: |
| 5342 | err = 0; |
| 5343 | autostart_arrays(arg); |
| 5344 | goto done; |
| 5345 | #endif |
| 5346 | default:; |
| 5347 | } |
| 5348 | |
| 5349 | /* |
| 5350 | * Commands creating/starting a new array: |
| 5351 | */ |
| 5352 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5353 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
| 5355 | if (!mddev) { |
| 5356 | BUG(); |
| 5357 | goto abort; |
| 5358 | } |
| 5359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | err = mddev_lock(mddev); |
| 5361 | if (err) { |
| 5362 | printk(KERN_INFO |
| 5363 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5364 | err, cmd); |
| 5365 | goto abort; |
| 5366 | } |
| 5367 | |
| 5368 | switch (cmd) |
| 5369 | { |
| 5370 | case SET_ARRAY_INFO: |
| 5371 | { |
| 5372 | mdu_array_info_t info; |
| 5373 | if (!arg) |
| 5374 | memset(&info, 0, sizeof(info)); |
| 5375 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5376 | err = -EFAULT; |
| 5377 | goto abort_unlock; |
| 5378 | } |
| 5379 | if (mddev->pers) { |
| 5380 | err = update_array_info(mddev, &info); |
| 5381 | if (err) { |
| 5382 | printk(KERN_WARNING "md: couldn't update" |
| 5383 | " array info. %d\n", err); |
| 5384 | goto abort_unlock; |
| 5385 | } |
| 5386 | goto done_unlock; |
| 5387 | } |
| 5388 | if (!list_empty(&mddev->disks)) { |
| 5389 | printk(KERN_WARNING |
| 5390 | "md: array %s already has disks!\n", |
| 5391 | mdname(mddev)); |
| 5392 | err = -EBUSY; |
| 5393 | goto abort_unlock; |
| 5394 | } |
| 5395 | if (mddev->raid_disks) { |
| 5396 | printk(KERN_WARNING |
| 5397 | "md: array %s already initialised!\n", |
| 5398 | mdname(mddev)); |
| 5399 | err = -EBUSY; |
| 5400 | goto abort_unlock; |
| 5401 | } |
| 5402 | err = set_array_info(mddev, &info); |
| 5403 | if (err) { |
| 5404 | printk(KERN_WARNING "md: couldn't set" |
| 5405 | " array info. %d\n", err); |
| 5406 | goto abort_unlock; |
| 5407 | } |
| 5408 | } |
| 5409 | goto done_unlock; |
| 5410 | |
| 5411 | default:; |
| 5412 | } |
| 5413 | |
| 5414 | /* |
| 5415 | * Commands querying/configuring an existing array: |
| 5416 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5417 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5418 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5419 | if ((!mddev->raid_disks && !mddev->external) |
| 5420 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5421 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5422 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | err = -ENODEV; |
| 5424 | goto abort_unlock; |
| 5425 | } |
| 5426 | |
| 5427 | /* |
| 5428 | * Commands even a read-only array can execute: |
| 5429 | */ |
| 5430 | switch (cmd) |
| 5431 | { |
| 5432 | case GET_ARRAY_INFO: |
| 5433 | err = get_array_info(mddev, argp); |
| 5434 | goto done_unlock; |
| 5435 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5436 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5437 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5438 | goto done_unlock; |
| 5439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | case GET_DISK_INFO: |
| 5441 | err = get_disk_info(mddev, argp); |
| 5442 | goto done_unlock; |
| 5443 | |
| 5444 | case RESTART_ARRAY_RW: |
| 5445 | err = restart_array(mddev); |
| 5446 | goto done_unlock; |
| 5447 | |
| 5448 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5449 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | goto done_unlock; |
| 5451 | |
| 5452 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5453 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | goto done_unlock; |
| 5455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | /* |
| 5459 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5460 | * superblock, so we do not allow them on read-only arrays. |
| 5461 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5462 | * here and hit the 'default' below, so only disallow |
| 5463 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5465 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5466 | if (mddev->ro == 2) { |
| 5467 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5468 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5469 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5470 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5471 | } else { |
| 5472 | err = -EROFS; |
| 5473 | goto abort_unlock; |
| 5474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | switch (cmd) |
| 5478 | { |
| 5479 | case ADD_NEW_DISK: |
| 5480 | { |
| 5481 | mdu_disk_info_t info; |
| 5482 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5483 | err = -EFAULT; |
| 5484 | else |
| 5485 | err = add_new_disk(mddev, &info); |
| 5486 | goto done_unlock; |
| 5487 | } |
| 5488 | |
| 5489 | case HOT_REMOVE_DISK: |
| 5490 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5491 | goto done_unlock; |
| 5492 | |
| 5493 | case HOT_ADD_DISK: |
| 5494 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5495 | goto done_unlock; |
| 5496 | |
| 5497 | case SET_DISK_FAULTY: |
| 5498 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5499 | goto done_unlock; |
| 5500 | |
| 5501 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5502 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | goto done_unlock; |
| 5504 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5505 | case SET_BITMAP_FILE: |
| 5506 | err = set_bitmap_file(mddev, (int)arg); |
| 5507 | goto done_unlock; |
| 5508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | err = -EINVAL; |
| 5511 | goto abort_unlock; |
| 5512 | } |
| 5513 | |
| 5514 | done_unlock: |
| 5515 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5516 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5517 | err != -EINVAL) |
| 5518 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | mddev_unlock(mddev); |
| 5520 | |
| 5521 | return err; |
| 5522 | done: |
| 5523 | if (err) |
| 5524 | MD_BUG(); |
| 5525 | abort: |
| 5526 | return err; |
| 5527 | } |
| 5528 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5529 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | { |
| 5531 | /* |
| 5532 | * Succeed if we can lock the mddev, which confirms that |
| 5533 | * it isn't being stopped right now. |
| 5534 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5535 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | int err; |
| 5537 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5538 | if (mddev->gendisk != bdev->bd_disk) { |
| 5539 | /* we are racing with mddev_put which is discarding this |
| 5540 | * bd_disk. |
| 5541 | */ |
| 5542 | mddev_put(mddev); |
| 5543 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5544 | flush_scheduled_work(); |
| 5545 | /* Then retry the open from the top */ |
| 5546 | return -ERESTARTSYS; |
| 5547 | } |
| 5548 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5549 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5550 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | goto out; |
| 5552 | |
| 5553 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5554 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5555 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5557 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | out: |
| 5559 | return err; |
| 5560 | } |
| 5561 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5562 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5564 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5566 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5567 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | mddev_put(mddev); |
| 5569 | |
| 5570 | return 0; |
| 5571 | } |
| 5572 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5573 | static int md_media_changed(struct gendisk *disk) |
| 5574 | { |
| 5575 | mddev_t *mddev = disk->private_data; |
| 5576 | |
| 5577 | return mddev->changed; |
| 5578 | } |
| 5579 | |
| 5580 | static int md_revalidate(struct gendisk *disk) |
| 5581 | { |
| 5582 | mddev_t *mddev = disk->private_data; |
| 5583 | |
| 5584 | mddev->changed = 0; |
| 5585 | return 0; |
| 5586 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5587 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | { |
| 5589 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5590 | .open = md_open, |
| 5591 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5592 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5593 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5594 | .media_changed = md_media_changed, |
| 5595 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | }; |
| 5597 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5598 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | { |
| 5600 | mdk_thread_t *thread = arg; |
| 5601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | /* |
| 5603 | * md_thread is a 'system-thread', it's priority should be very |
| 5604 | * high. We avoid resource deadlocks individually in each |
| 5605 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5606 | * the very same RT priority as kswapd, thus we will never get |
| 5607 | * into a priority inversion deadlock. |
| 5608 | * |
| 5609 | * we definitely have to have equal or higher priority than |
| 5610 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5611 | * many dirty RAID5 blocks. |
| 5612 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5614 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5615 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5617 | /* We need to wait INTERRUPTIBLE so that |
| 5618 | * we don't add to the load-average. |
| 5619 | * That means we need to be sure no signals are |
| 5620 | * pending |
| 5621 | */ |
| 5622 | if (signal_pending(current)) |
| 5623 | flush_signals(current); |
| 5624 | |
| 5625 | wait_event_interruptible_timeout |
| 5626 | (thread->wqueue, |
| 5627 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5628 | || kthread_should_stop(), |
| 5629 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | |
| 5631 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5632 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5633 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | return 0; |
| 5637 | } |
| 5638 | |
| 5639 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5640 | { |
| 5641 | if (thread) { |
| 5642 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5643 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5644 | wake_up(&thread->wqueue); |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5649 | const char *name) |
| 5650 | { |
| 5651 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5653 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | if (!thread) |
| 5655 | return NULL; |
| 5656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | init_waitqueue_head(&thread->wqueue); |
| 5658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | thread->run = run; |
| 5660 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5661 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5662 | thread->tsk = kthread_run(md_thread, thread, |
| 5663 | "%s_%s", |
| 5664 | mdname(thread->mddev), |
| 5665 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5666 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | kfree(thread); |
| 5668 | return NULL; |
| 5669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | return thread; |
| 5671 | } |
| 5672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | void md_unregister_thread(mdk_thread_t *thread) |
| 5674 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5675 | if (!thread) |
| 5676 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5677 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5678 | |
| 5679 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | kfree(thread); |
| 5681 | } |
| 5682 | |
| 5683 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5684 | { |
| 5685 | if (!mddev) { |
| 5686 | MD_BUG(); |
| 5687 | return; |
| 5688 | } |
| 5689 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5690 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5692 | |
| 5693 | if (mddev->external) |
| 5694 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5695 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5697 | mdname(mddev), |
| 5698 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5699 | __builtin_return_address(0),__builtin_return_address(1), |
| 5700 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5701 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5702 | if (!mddev->pers) |
| 5703 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | if (!mddev->pers->error_handler) |
| 5705 | return; |
| 5706 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5707 | if (mddev->degraded) |
| 5708 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5709 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5711 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5712 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5713 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | } |
| 5715 | |
| 5716 | /* seq_file implementation /proc/mdstat */ |
| 5717 | |
| 5718 | static void status_unused(struct seq_file *seq) |
| 5719 | { |
| 5720 | int i = 0; |
| 5721 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
| 5723 | seq_printf(seq, "unused devices: "); |
| 5724 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5725 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | char b[BDEVNAME_SIZE]; |
| 5727 | i++; |
| 5728 | seq_printf(seq, "%s ", |
| 5729 | bdevname(rdev->bdev,b)); |
| 5730 | } |
| 5731 | if (!i) |
| 5732 | seq_printf(seq, "<none>"); |
| 5733 | |
| 5734 | seq_printf(seq, "\n"); |
| 5735 | } |
| 5736 | |
| 5737 | |
| 5738 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5739 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5740 | sector_t max_sectors, resync, res; |
| 5741 | unsigned long dt, db; |
| 5742 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5743 | int scale; |
| 5744 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5746 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | |
| 5748 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5749 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5751 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | |
| 5753 | /* |
| 5754 | * Should not happen. |
| 5755 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5756 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | MD_BUG(); |
| 5758 | return; |
| 5759 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5760 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5761 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5762 | * u32, as those are the requirements for sector_div. |
| 5763 | * Thus 'scale' must be at least 10 |
| 5764 | */ |
| 5765 | scale = 10; |
| 5766 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5767 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5768 | scale++; |
| 5769 | } |
| 5770 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5771 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5772 | |
| 5773 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5775 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | seq_printf(seq, "["); |
| 5777 | for (i = 0; i < x; i++) |
| 5778 | seq_printf(seq, "="); |
| 5779 | seq_printf(seq, ">"); |
| 5780 | for (i = 0; i < y; i++) |
| 5781 | seq_printf(seq, "."); |
| 5782 | seq_printf(seq, "] "); |
| 5783 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5784 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5785 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5786 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5787 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5788 | "check" : |
| 5789 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5790 | "resync" : "recovery"))), |
| 5791 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5792 | (unsigned long long) resync/2, |
| 5793 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | |
| 5795 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | * dt: time from mark until now |
| 5797 | * db: blocks written from mark until now |
| 5798 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5799 | * |
| 5800 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5801 | * So we divide before multiply in case it is 32bit and close |
| 5802 | * to the limit. |
| 5803 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5804 | * near the end of resync when the number of remaining sectors |
| 5805 | * is close to 'db'. |
| 5806 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5807 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | */ |
| 5809 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5810 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5811 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5812 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5814 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5815 | sector_div(rt, db/32+1); |
| 5816 | rt *= dt; |
| 5817 | rt >>= 5; |
| 5818 | |
| 5819 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5820 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5822 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5826 | { |
| 5827 | struct list_head *tmp; |
| 5828 | loff_t l = *pos; |
| 5829 | mddev_t *mddev; |
| 5830 | |
| 5831 | if (l >= 0x10000) |
| 5832 | return NULL; |
| 5833 | if (!l--) |
| 5834 | /* header */ |
| 5835 | return (void*)1; |
| 5836 | |
| 5837 | spin_lock(&all_mddevs_lock); |
| 5838 | list_for_each(tmp,&all_mddevs) |
| 5839 | if (!l--) { |
| 5840 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5841 | mddev_get(mddev); |
| 5842 | spin_unlock(&all_mddevs_lock); |
| 5843 | return mddev; |
| 5844 | } |
| 5845 | spin_unlock(&all_mddevs_lock); |
| 5846 | if (!l--) |
| 5847 | return (void*)2;/* tail */ |
| 5848 | return NULL; |
| 5849 | } |
| 5850 | |
| 5851 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5852 | { |
| 5853 | struct list_head *tmp; |
| 5854 | mddev_t *next_mddev, *mddev = v; |
| 5855 | |
| 5856 | ++*pos; |
| 5857 | if (v == (void*)2) |
| 5858 | return NULL; |
| 5859 | |
| 5860 | spin_lock(&all_mddevs_lock); |
| 5861 | if (v == (void*)1) |
| 5862 | tmp = all_mddevs.next; |
| 5863 | else |
| 5864 | tmp = mddev->all_mddevs.next; |
| 5865 | if (tmp != &all_mddevs) |
| 5866 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5867 | else { |
| 5868 | next_mddev = (void*)2; |
| 5869 | *pos = 0x10000; |
| 5870 | } |
| 5871 | spin_unlock(&all_mddevs_lock); |
| 5872 | |
| 5873 | if (v != (void*)1) |
| 5874 | mddev_put(mddev); |
| 5875 | return next_mddev; |
| 5876 | |
| 5877 | } |
| 5878 | |
| 5879 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5880 | { |
| 5881 | mddev_t *mddev = v; |
| 5882 | |
| 5883 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5884 | mddev_put(mddev); |
| 5885 | } |
| 5886 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5887 | struct mdstat_info { |
| 5888 | int event; |
| 5889 | }; |
| 5890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5892 | { |
| 5893 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5894 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5896 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5897 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | |
| 5899 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5900 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | seq_printf(seq, "Personalities : "); |
| 5902 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5903 | list_for_each_entry(pers, &pers_list, list) |
| 5904 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | |
| 5906 | spin_unlock(&pers_lock); |
| 5907 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5908 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | return 0; |
| 5910 | } |
| 5911 | if (v == (void*)2) { |
| 5912 | status_unused(seq); |
| 5913 | return 0; |
| 5914 | } |
| 5915 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5916 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5920 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5921 | mddev->pers ? "" : "in"); |
| 5922 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5923 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5925 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5926 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | seq_printf(seq, " %s", mddev->pers->name); |
| 5928 | } |
| 5929 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5930 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5931 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | char b[BDEVNAME_SIZE]; |
| 5933 | seq_printf(seq, " %s[%d]", |
| 5934 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5935 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5936 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5937 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | seq_printf(seq, "(F)"); |
| 5939 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5940 | } else if (rdev->raid_disk < 0) |
| 5941 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5942 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | if (!list_empty(&mddev->disks)) { |
| 5946 | if (mddev->pers) |
| 5947 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5948 | (unsigned long long) |
| 5949 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | else |
| 5951 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5952 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5954 | if (mddev->persistent) { |
| 5955 | if (mddev->major_version != 0 || |
| 5956 | mddev->minor_version != 90) { |
| 5957 | seq_printf(seq," super %d.%d", |
| 5958 | mddev->major_version, |
| 5959 | mddev->minor_version); |
| 5960 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5961 | } else if (mddev->external) |
| 5962 | seq_printf(seq, " super external:%s", |
| 5963 | mddev->metadata_type); |
| 5964 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5965 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | |
| 5967 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5968 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5970 | if (mddev->pers->sync_request) { |
| 5971 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5972 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5973 | seq_printf(seq, "\n "); |
| 5974 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5975 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5976 | else if (mddev->recovery_cp < MaxSector) |
| 5977 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5978 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5979 | } else |
| 5980 | seq_printf(seq, "\n "); |
| 5981 | |
| 5982 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5983 | unsigned long chunk_kb; |
| 5984 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5985 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5986 | chunk_kb = bitmap->chunksize >> 10; |
| 5987 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5988 | "%lu%s chunk", |
| 5989 | bitmap->pages - bitmap->missing_pages, |
| 5990 | bitmap->pages, |
| 5991 | (bitmap->pages - bitmap->missing_pages) |
| 5992 | << (PAGE_SHIFT - 10), |
| 5993 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5994 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5995 | if (bitmap->file) { |
| 5996 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5997 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5998 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5999 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6000 | seq_printf(seq, "\n"); |
| 6001 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | seq_printf(seq, "\n"); |
| 6005 | } |
| 6006 | mddev_unlock(mddev); |
| 6007 | |
| 6008 | return 0; |
| 6009 | } |
| 6010 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6011 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | .start = md_seq_start, |
| 6013 | .next = md_seq_next, |
| 6014 | .stop = md_seq_stop, |
| 6015 | .show = md_seq_show, |
| 6016 | }; |
| 6017 | |
| 6018 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6019 | { |
| 6020 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6021 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6022 | if (mi == NULL) |
| 6023 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | |
| 6025 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6026 | if (error) |
| 6027 | kfree(mi); |
| 6028 | else { |
| 6029 | struct seq_file *p = file->private_data; |
| 6030 | p->private = mi; |
| 6031 | mi->event = atomic_read(&md_event_count); |
| 6032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | return error; |
| 6034 | } |
| 6035 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6036 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6037 | { |
| 6038 | struct seq_file *m = filp->private_data; |
| 6039 | struct mdstat_info *mi = m->private; |
| 6040 | int mask; |
| 6041 | |
| 6042 | poll_wait(filp, &md_event_waiters, wait); |
| 6043 | |
| 6044 | /* always allow read */ |
| 6045 | mask = POLLIN | POLLRDNORM; |
| 6046 | |
| 6047 | if (mi->event != atomic_read(&md_event_count)) |
| 6048 | mask |= POLLERR | POLLPRI; |
| 6049 | return mask; |
| 6050 | } |
| 6051 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6052 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6053 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | .open = md_seq_open, |
| 6055 | .read = seq_read, |
| 6056 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6057 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6058 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | }; |
| 6060 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6061 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6064 | list_add_tail(&p->list, &pers_list); |
| 6065 | 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] | 6066 | spin_unlock(&pers_lock); |
| 6067 | return 0; |
| 6068 | } |
| 6069 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6070 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6072 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6074 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | spin_unlock(&pers_lock); |
| 6076 | return 0; |
| 6077 | } |
| 6078 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6079 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | { |
| 6081 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6083 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
| 6085 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6086 | rcu_read_lock(); |
| 6087 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6089 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6090 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6091 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6092 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6093 | * as sync_io is counted when a request starts, and |
| 6094 | * disk_stats is counted when it completes. |
| 6095 | * So resync activity will cause curr_events to be smaller than |
| 6096 | * when there was no such activity. |
| 6097 | * non-sync IO will cause disk_stat to increase without |
| 6098 | * increasing sync_io so curr_events will (eventually) |
| 6099 | * be larger than it was before. Once it becomes |
| 6100 | * substantially larger, the test below will cause |
| 6101 | * the array to appear non-idle, and resync will slow |
| 6102 | * down. |
| 6103 | * If there is a lot of outstanding resync activity when |
| 6104 | * we set last_event to curr_events, then all that activity |
| 6105 | * completing might cause the array to appear non-idle |
| 6106 | * and resync will be slowed down even though there might |
| 6107 | * not have been non-resync activity. This will only |
| 6108 | * happen once though. 'last_events' will soon reflect |
| 6109 | * the state where there is little or no outstanding |
| 6110 | * resync requests, and further resync activity will |
| 6111 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6112 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6114 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | rdev->last_events = curr_events; |
| 6116 | idle = 0; |
| 6117 | } |
| 6118 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6119 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | return idle; |
| 6121 | } |
| 6122 | |
| 6123 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6124 | { |
| 6125 | /* another "blocks" (512byte) blocks have been synced */ |
| 6126 | atomic_sub(blocks, &mddev->recovery_active); |
| 6127 | wake_up(&mddev->recovery_wait); |
| 6128 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6129 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | md_wakeup_thread(mddev->thread); |
| 6131 | // stop recovery, signal do_sync .... |
| 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6136 | /* md_write_start(mddev, bi) |
| 6137 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6138 | * in superblock) before writing, schedule a superblock update |
| 6139 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6140 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6141 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6143 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6144 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6145 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6146 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6147 | BUG_ON(mddev->ro == 1); |
| 6148 | if (mddev->ro == 2) { |
| 6149 | /* need to switch to read/write */ |
| 6150 | mddev->ro = 0; |
| 6151 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6152 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6153 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6154 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6155 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6156 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6157 | if (mddev->safemode == 1) |
| 6158 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6159 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6160 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6161 | if (mddev->in_sync) { |
| 6162 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6163 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6164 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6165 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6166 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6167 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6168 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6169 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6170 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6171 | wait_event(mddev->sb_wait, |
| 6172 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6173 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | void md_write_end(mddev_t *mddev) |
| 6177 | { |
| 6178 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6179 | if (mddev->safemode == 2) |
| 6180 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6181 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6183 | } |
| 6184 | } |
| 6185 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6186 | /* md_allow_write(mddev) |
| 6187 | * Calling this ensures that the array is marked 'active' so that writes |
| 6188 | * may proceed without blocking. It is important to call this before |
| 6189 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6190 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6191 | * |
| 6192 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6193 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6194 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6195 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6196 | { |
| 6197 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6198 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6199 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6200 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6201 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6202 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6203 | |
| 6204 | spin_lock_irq(&mddev->write_lock); |
| 6205 | if (mddev->in_sync) { |
| 6206 | mddev->in_sync = 0; |
| 6207 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6208 | if (mddev->safemode_delay && |
| 6209 | mddev->safemode == 0) |
| 6210 | mddev->safemode = 1; |
| 6211 | spin_unlock_irq(&mddev->write_lock); |
| 6212 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6213 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6214 | } else |
| 6215 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6216 | |
| 6217 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6218 | return -EAGAIN; |
| 6219 | else |
| 6220 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6221 | } |
| 6222 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | #define SYNC_MARKS 10 |
| 6225 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6226 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | { |
| 6228 | mddev_t *mddev2; |
| 6229 | unsigned int currspeed = 0, |
| 6230 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6231 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | unsigned long mark[SYNC_MARKS]; |
| 6233 | sector_t mark_cnt[SYNC_MARKS]; |
| 6234 | int last_mark,m; |
| 6235 | struct list_head *tmp; |
| 6236 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6237 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6238 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6239 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | |
| 6241 | /* just incase thread restarts... */ |
| 6242 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6243 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6244 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6245 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6247 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6248 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6249 | desc = "data-check"; |
| 6250 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6251 | desc = "requested-resync"; |
| 6252 | else |
| 6253 | desc = "resync"; |
| 6254 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6255 | desc = "reshape"; |
| 6256 | else |
| 6257 | desc = "recovery"; |
| 6258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | /* we overload curr_resync somewhat here. |
| 6260 | * 0 == not engaged in resync at all |
| 6261 | * 2 == checking that there is no conflict with another sync |
| 6262 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6263 | * commense |
| 6264 | * other == active in resync - this many blocks |
| 6265 | * |
| 6266 | * Before starting a resync we must have set curr_resync to |
| 6267 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6268 | * less than ours. When we find one that is the same or higher |
| 6269 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6270 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6271 | * This will mean we have to start checking from the beginning again. |
| 6272 | * |
| 6273 | */ |
| 6274 | |
| 6275 | do { |
| 6276 | mddev->curr_resync = 2; |
| 6277 | |
| 6278 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6279 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6280 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | goto skip; |
| 6282 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6283 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | if (mddev2 == mddev) |
| 6285 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6286 | if (!mddev->parallel_resync |
| 6287 | && mddev2->curr_resync |
| 6288 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | DEFINE_WAIT(wq); |
| 6290 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6291 | /* arbitrarily yield */ |
| 6292 | mddev->curr_resync = 1; |
| 6293 | wake_up(&resync_wait); |
| 6294 | } |
| 6295 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6296 | /* no need to wait here, we can wait the next |
| 6297 | * time 'round when curr_resync == 2 |
| 6298 | */ |
| 6299 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6300 | /* We need to wait 'interruptible' so as not to |
| 6301 | * contribute to the load average, and not to |
| 6302 | * be caught by 'softlockup' |
| 6303 | */ |
| 6304 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6305 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6306 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6307 | printk(KERN_INFO "md: delaying %s of %s" |
| 6308 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6310 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6312 | if (signal_pending(current)) |
| 6313 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | schedule(); |
| 6315 | finish_wait(&resync_wait, &wq); |
| 6316 | goto try_again; |
| 6317 | } |
| 6318 | finish_wait(&resync_wait, &wq); |
| 6319 | } |
| 6320 | } |
| 6321 | } while (mddev->curr_resync < 2); |
| 6322 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6323 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6324 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6326 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | */ |
| 6328 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6329 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6330 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6331 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6332 | j = mddev->resync_min; |
| 6333 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6334 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6335 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6336 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6337 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6338 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6340 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6341 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6342 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6343 | if (rdev->raid_disk >= 0 && |
| 6344 | !test_bit(Faulty, &rdev->flags) && |
| 6345 | !test_bit(In_sync, &rdev->flags) && |
| 6346 | rdev->recovery_offset < j) |
| 6347 | j = rdev->recovery_offset; |
| 6348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6349 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6350 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6351 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6352 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6353 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6354 | "(but not more than %d KB/sec) for %s.\n", |
| 6355 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6357 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6358 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6359 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6361 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6362 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | } |
| 6364 | last_mark = 0; |
| 6365 | mddev->resync_mark = mark[last_mark]; |
| 6366 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6367 | |
| 6368 | /* |
| 6369 | * Tune reconstruction: |
| 6370 | */ |
| 6371 | window = 32*(PAGE_SIZE/512); |
| 6372 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6373 | window/2,(unsigned long long) max_sectors/2); |
| 6374 | |
| 6375 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | last_check = 0; |
| 6377 | |
| 6378 | if (j>2) { |
| 6379 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6380 | "md: resuming %s of %s from checkpoint.\n", |
| 6381 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | mddev->curr_resync = j; |
| 6383 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame^] | 6384 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | |
| 6386 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6387 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6389 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6390 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6391 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6392 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6393 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6394 | > (max_sectors >> 4)) || |
| 6395 | (j - mddev->curr_resync_completed)*2 |
| 6396 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6397 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6398 | /* time to update curr_resync_completed */ |
| 6399 | blk_unplug(mddev->queue); |
| 6400 | wait_event(mddev->recovery_wait, |
| 6401 | atomic_read(&mddev->recovery_active) == 0); |
| 6402 | mddev->curr_resync_completed = |
| 6403 | mddev->curr_resync; |
| 6404 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6405 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6406 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6407 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6408 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6409 | /* As this condition is controlled by user-space, |
| 6410 | * we can block indefinitely, so use '_interruptible' |
| 6411 | * to avoid triggering warnings. |
| 6412 | */ |
| 6413 | flush_signals(current); /* just in case */ |
| 6414 | wait_event_interruptible(mddev->recovery_wait, |
| 6415 | mddev->resync_max > j |
| 6416 | || kthread_should_stop()); |
| 6417 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6418 | |
| 6419 | if (kthread_should_stop()) |
| 6420 | goto interrupted; |
| 6421 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6422 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6423 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6424 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6425 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | goto out; |
| 6427 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6428 | |
| 6429 | if (!skipped) { /* actual IO requested */ |
| 6430 | io_sectors += sectors; |
| 6431 | atomic_add(sectors, &mddev->recovery_active); |
| 6432 | } |
| 6433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | j += sectors; |
| 6435 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6436 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6437 | if (last_check == 0) |
| 6438 | /* this is the earliers that rebuilt will be |
| 6439 | * visible in /proc/mdstat |
| 6440 | */ |
| 6441 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6442 | |
| 6443 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | continue; |
| 6445 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6446 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6448 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | break; |
| 6450 | |
| 6451 | repeat: |
| 6452 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6453 | /* step marks */ |
| 6454 | int next = (last_mark+1) % SYNC_MARKS; |
| 6455 | |
| 6456 | mddev->resync_mark = mark[next]; |
| 6457 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6458 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6459 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | last_mark = next; |
| 6461 | } |
| 6462 | |
| 6463 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6464 | if (kthread_should_stop()) |
| 6465 | goto interrupted; |
| 6466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
| 6468 | /* |
| 6469 | * this loop exits only if either when we are slower than |
| 6470 | * the 'hard' speed limit, or the system was IO-idle for |
| 6471 | * a jiffy. |
| 6472 | * the system might be non-idle CPU-wise, but we only care |
| 6473 | * about not overloading the IO subsystem. (things like an |
| 6474 | * e2fsck being done on the RAID array should execute fast) |
| 6475 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6476 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | cond_resched(); |
| 6478 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6479 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6480 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6482 | if (currspeed > speed_min(mddev)) { |
| 6483 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6484 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6485 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | goto repeat; |
| 6487 | } |
| 6488 | } |
| 6489 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6490 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | /* |
| 6492 | * this also signals 'finished resyncing' to md_stop |
| 6493 | */ |
| 6494 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6495 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | |
| 6497 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6498 | |
| 6499 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6500 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6502 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6503 | mddev->curr_resync > 2) { |
| 6504 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6505 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6506 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6507 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6508 | "md: checkpointing %s of %s.\n", |
| 6509 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6510 | mddev->recovery_cp = mddev->curr_resync; |
| 6511 | } |
| 6512 | } else |
| 6513 | mddev->recovery_cp = MaxSector; |
| 6514 | } else { |
| 6515 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6516 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6517 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6518 | if (rdev->raid_disk >= 0 && |
| 6519 | !test_bit(Faulty, &rdev->flags) && |
| 6520 | !test_bit(In_sync, &rdev->flags) && |
| 6521 | rdev->recovery_offset < mddev->curr_resync) |
| 6522 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6525 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6528 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6529 | /* We completed so min/max setting can be forgotten if used. */ |
| 6530 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6531 | mddev->resync_min = 0; |
| 6532 | mddev->resync_max = MaxSector; |
| 6533 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6534 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame^] | 6536 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6537 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6538 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | wake_up(&resync_wait); |
| 6540 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6541 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6542 | return; |
| 6543 | |
| 6544 | interrupted: |
| 6545 | /* |
| 6546 | * got a signal, exit. |
| 6547 | */ |
| 6548 | printk(KERN_INFO |
| 6549 | "md: md_do_sync() got signal ... exiting\n"); |
| 6550 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6551 | goto out; |
| 6552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6554 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | |
| 6556 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6557 | static int remove_and_add_spares(mddev_t *mddev) |
| 6558 | { |
| 6559 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6560 | int spares = 0; |
| 6561 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6562 | mddev->curr_resync_completed = 0; |
| 6563 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6564 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6565 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6566 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6567 | (test_bit(Faulty, &rdev->flags) || |
| 6568 | ! test_bit(In_sync, &rdev->flags)) && |
| 6569 | atomic_read(&rdev->nr_pending)==0) { |
| 6570 | if (mddev->pers->hot_remove_disk( |
| 6571 | mddev, rdev->raid_disk)==0) { |
| 6572 | char nm[20]; |
| 6573 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6574 | sysfs_remove_link(&mddev->kobj, nm); |
| 6575 | rdev->raid_disk = -1; |
| 6576 | } |
| 6577 | } |
| 6578 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6579 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6580 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6581 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6582 | !test_bit(In_sync, &rdev->flags) && |
| 6583 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6584 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6585 | if (rdev->raid_disk < 0 |
| 6586 | && !test_bit(Faulty, &rdev->flags)) { |
| 6587 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6588 | if (mddev->pers-> |
| 6589 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6590 | char nm[20]; |
| 6591 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6592 | if (sysfs_create_link(&mddev->kobj, |
| 6593 | &rdev->kobj, nm)) |
| 6594 | printk(KERN_WARNING |
| 6595 | "md: cannot register " |
| 6596 | "%s for %s\n", |
| 6597 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6598 | spares++; |
| 6599 | md_new_event(mddev); |
| 6600 | } else |
| 6601 | break; |
| 6602 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6603 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6604 | } |
| 6605 | return spares; |
| 6606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | /* |
| 6608 | * This routine is regularly called by all per-raid-array threads to |
| 6609 | * deal with generic issues like resync and super-block update. |
| 6610 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6611 | * need this as they never do any recovery or update the superblock. |
| 6612 | * |
| 6613 | * It does not do any resync itself, but rather "forks" off other threads |
| 6614 | * to do that as needed. |
| 6615 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6616 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6617 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | * and wakeups up this thread which will reap the thread and finish up. |
| 6619 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6620 | * |
| 6621 | * The overall approach is: |
| 6622 | * 1/ if the superblock needs updating, update it. |
| 6623 | * 2/ If a recovery thread is running, don't do anything else. |
| 6624 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6625 | * 4/ If there are any faulty devices, remove them. |
| 6626 | * 5/ If array is degraded, try to add spares devices |
| 6627 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6628 | */ |
| 6629 | void md_check_recovery(mddev_t *mddev) |
| 6630 | { |
| 6631 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | |
| 6633 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6634 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6635 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | |
| 6637 | if (mddev->ro) |
| 6638 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6639 | |
| 6640 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6641 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6642 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6643 | mdname(mddev)); |
| 6644 | mddev->safemode = 2; |
| 6645 | } |
| 6646 | flush_signals(current); |
| 6647 | } |
| 6648 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6649 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6650 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6652 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6654 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6655 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6656 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6657 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | )) |
| 6659 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6660 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6661 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6662 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6663 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6664 | if (mddev->ro) { |
| 6665 | /* Only thing we do on a ro array is remove |
| 6666 | * failed devices. |
| 6667 | */ |
| 6668 | remove_and_add_spares(mddev); |
| 6669 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6670 | goto unlock; |
| 6671 | } |
| 6672 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6673 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6674 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6675 | spin_lock_irq(&mddev->write_lock); |
| 6676 | if (mddev->safemode && |
| 6677 | !atomic_read(&mddev->writes_pending) && |
| 6678 | !mddev->in_sync && |
| 6679 | mddev->recovery_cp == MaxSector) { |
| 6680 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6681 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6682 | if (mddev->persistent) |
| 6683 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6684 | } |
| 6685 | if (mddev->safemode == 1) |
| 6686 | mddev->safemode = 0; |
| 6687 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6688 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6689 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6690 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6691 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6692 | if (mddev->flags) |
| 6693 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6694 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6695 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6696 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6697 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6698 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6701 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6702 | /* resync/recovery still happening */ |
| 6703 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6704 | goto unlock; |
| 6705 | } |
| 6706 | if (mddev->sync_thread) { |
| 6707 | /* resync has finished, collect result */ |
| 6708 | md_unregister_thread(mddev->sync_thread); |
| 6709 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6710 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6711 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | /* success...*/ |
| 6713 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6714 | if (mddev->pers->spare_active(mddev)) |
| 6715 | sysfs_notify(&mddev->kobj, NULL, |
| 6716 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6718 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6719 | mddev->pers->finish_reshape) |
| 6720 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6721 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6722 | |
| 6723 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6724 | * information must be scrapped |
| 6725 | */ |
| 6726 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6727 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6728 | rdev->saved_raid_disk = -1; |
| 6729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | mddev->recovery = 0; |
| 6731 | /* flag recovery needed just to double check */ |
| 6732 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6733 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6734 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | goto unlock; |
| 6736 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6737 | /* Set RUNNING before clearing NEEDED to avoid |
| 6738 | * any transients in the value of "sync_action". |
| 6739 | */ |
| 6740 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6741 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6742 | /* Clear some bits that don't mean anything, but |
| 6743 | * might be left set |
| 6744 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6745 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6746 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6748 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6749 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | /* no recovery is running. |
| 6751 | * remove any failed drives, then |
| 6752 | * add spares if possible. |
| 6753 | * Spare are also removed and re-added, to allow |
| 6754 | * the personality to fail the re-add. |
| 6755 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6757 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6758 | if (mddev->pers->check_reshape == NULL || |
| 6759 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6760 | /* Cannot proceed */ |
| 6761 | goto unlock; |
| 6762 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6763 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6764 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6765 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6766 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6767 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6768 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6769 | } else if (mddev->recovery_cp < MaxSector) { |
| 6770 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6771 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6772 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6773 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6777 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6778 | /* We are adding a device or devices to an array |
| 6779 | * which has the bitmap stored on all devices. |
| 6780 | * So make sure all bitmap pages get written |
| 6781 | */ |
| 6782 | bitmap_write_all(mddev->bitmap); |
| 6783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6785 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6786 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | if (!mddev->sync_thread) { |
| 6788 | printk(KERN_ERR "%s: could not start resync" |
| 6789 | " thread...\n", |
| 6790 | mdname(mddev)); |
| 6791 | /* leave the spares where they are, it shouldn't hurt */ |
| 6792 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6793 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6795 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6796 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6797 | } |
| 6798 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6799 | if (!mddev->sync_thread) { |
| 6800 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6801 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6802 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6803 | if (mddev->sysfs_action) |
| 6804 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | mddev_unlock(mddev); |
| 6807 | } |
| 6808 | } |
| 6809 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6810 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6811 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6812 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6813 | wait_event_timeout(rdev->blocked_wait, |
| 6814 | !test_bit(Blocked, &rdev->flags), |
| 6815 | msecs_to_jiffies(5000)); |
| 6816 | rdev_dec_pending(rdev, mddev); |
| 6817 | } |
| 6818 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6819 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6820 | static int md_notify_reboot(struct notifier_block *this, |
| 6821 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | { |
| 6823 | struct list_head *tmp; |
| 6824 | mddev_t *mddev; |
| 6825 | |
| 6826 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6827 | |
| 6828 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6829 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6830 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6831 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6832 | /* Force a switch to readonly even array |
| 6833 | * appears to still be in use. Hence |
| 6834 | * the '100'. |
| 6835 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6836 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6837 | mddev_unlock(mddev); |
| 6838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | /* |
| 6840 | * certain more exotic SCSI devices are known to be |
| 6841 | * volatile wrt too early system reboots. While the |
| 6842 | * right place to handle this issue is the given |
| 6843 | * driver, we do want to have a safe RAID driver ... |
| 6844 | */ |
| 6845 | mdelay(1000*1); |
| 6846 | } |
| 6847 | return NOTIFY_DONE; |
| 6848 | } |
| 6849 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6850 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | .notifier_call = md_notify_reboot, |
| 6852 | .next = NULL, |
| 6853 | .priority = INT_MAX, /* before any real devices */ |
| 6854 | }; |
| 6855 | |
| 6856 | static void md_geninit(void) |
| 6857 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6859 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6860 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | } |
| 6862 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6863 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6865 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | return -1; |
| 6867 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6868 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | return -1; |
| 6870 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6871 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6872 | md_probe, NULL, NULL); |
| 6873 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | md_probe, NULL, NULL); |
| 6875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6877 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | |
| 6879 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6880 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | |
| 6884 | #ifndef MODULE |
| 6885 | |
| 6886 | /* |
| 6887 | * Searches all registered partitions for autorun RAID arrays |
| 6888 | * at boot time. |
| 6889 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6890 | |
| 6891 | static LIST_HEAD(all_detected_devices); |
| 6892 | struct detected_devices_node { |
| 6893 | struct list_head list; |
| 6894 | dev_t dev; |
| 6895 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | |
| 6897 | void md_autodetect_dev(dev_t dev) |
| 6898 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6899 | struct detected_devices_node *node_detected_dev; |
| 6900 | |
| 6901 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6902 | if (node_detected_dev) { |
| 6903 | node_detected_dev->dev = dev; |
| 6904 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6905 | } else { |
| 6906 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6907 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | |
| 6912 | static void autostart_arrays(int part) |
| 6913 | { |
| 6914 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6915 | struct detected_devices_node *node_detected_dev; |
| 6916 | dev_t dev; |
| 6917 | int i_scanned, i_passed; |
| 6918 | |
| 6919 | i_scanned = 0; |
| 6920 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
| 6922 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6923 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6924 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6925 | i_scanned++; |
| 6926 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6927 | struct detected_devices_node, list); |
| 6928 | list_del(&node_detected_dev->list); |
| 6929 | dev = node_detected_dev->dev; |
| 6930 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6931 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | if (IS_ERR(rdev)) |
| 6933 | continue; |
| 6934 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6935 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6936 | MD_BUG(); |
| 6937 | continue; |
| 6938 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6939 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6941 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6943 | |
| 6944 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6945 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
| 6947 | autorun_devices(part); |
| 6948 | } |
| 6949 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6950 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | |
| 6952 | static __exit void md_exit(void) |
| 6953 | { |
| 6954 | mddev_t *mddev; |
| 6955 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6956 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6957 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6958 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6960 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | unregister_blkdev(mdp_major, "mdp"); |
| 6962 | unregister_reboot_notifier(&md_notifier); |
| 6963 | unregister_sysctl_table(raid_table_header); |
| 6964 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6965 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6967 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6968 | } |
| 6969 | } |
| 6970 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6971 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | module_exit(md_exit) |
| 6973 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6974 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6975 | { |
| 6976 | return sprintf(buffer, "%d", start_readonly); |
| 6977 | } |
| 6978 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6979 | { |
| 6980 | char *e; |
| 6981 | int num = simple_strtoul(val, &e, 10); |
| 6982 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6983 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6984 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6985 | } |
| 6986 | return -EINVAL; |
| 6987 | } |
| 6988 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6989 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6990 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6991 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6992 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | EXPORT_SYMBOL(register_md_personality); |
| 6995 | EXPORT_SYMBOL(unregister_md_personality); |
| 6996 | EXPORT_SYMBOL(md_error); |
| 6997 | EXPORT_SYMBOL(md_done_sync); |
| 6998 | EXPORT_SYMBOL(md_write_start); |
| 6999 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | EXPORT_SYMBOL(md_register_thread); |
| 7001 | EXPORT_SYMBOL(md_unregister_thread); |
| 7002 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | EXPORT_SYMBOL(md_check_recovery); |
| 7004 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7005 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7006 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |