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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 37 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/sysctl.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 | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define MAJOR_NR MD_MAJOR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* 63 partitions with the alternate major number (mdp) */ |
| 52 | #define MdpMinorShift 6 |
| 53 | |
| 54 | #define DEBUG 0 |
| 55 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 56 | |
| 57 | |
| 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 62 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(pers_lock); |
| 64 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 65 | static void md_print_devices(void); |
| 66 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 67 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 73 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 74 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 75 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 77 | * speed limit - in case reconstruction slows down your system despite |
| 78 | * idle IO detection. |
| 79 | * |
| 80 | * 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] | 81 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | static int sysctl_speed_limit_min = 1000; |
| 85 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 86 | static inline int speed_min(mddev_t *mddev) |
| 87 | { |
| 88 | return mddev->sync_speed_min ? |
| 89 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 90 | } |
| 91 | |
| 92 | static inline int speed_max(mddev_t *mddev) |
| 93 | { |
| 94 | return mddev->sync_speed_max ? |
| 95 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | static struct ctl_table_header *raid_table_header; |
| 99 | |
| 100 | static ctl_table raid_table[] = { |
| 101 | { |
| 102 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 103 | .procname = "speed_limit_min", |
| 104 | .data = &sysctl_speed_limit_min, |
| 105 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 106 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .proc_handler = &proc_dointvec, |
| 108 | }, |
| 109 | { |
| 110 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 111 | .procname = "speed_limit_max", |
| 112 | .data = &sysctl_speed_limit_max, |
| 113 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 114 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .proc_handler = &proc_dointvec, |
| 116 | }, |
| 117 | { .ctl_name = 0 } |
| 118 | }; |
| 119 | |
| 120 | static ctl_table raid_dir_table[] = { |
| 121 | { |
| 122 | .ctl_name = DEV_RAID, |
| 123 | .procname = "raid", |
| 124 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 125 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .child = raid_table, |
| 127 | }, |
| 128 | { .ctl_name = 0 } |
| 129 | }; |
| 130 | |
| 131 | static ctl_table raid_root_table[] = { |
| 132 | { |
| 133 | .ctl_name = CTL_DEV, |
| 134 | .procname = "dev", |
| 135 | .maxlen = 0, |
| 136 | .mode = 0555, |
| 137 | .child = raid_dir_table, |
| 138 | }, |
| 139 | { .ctl_name = 0 } |
| 140 | }; |
| 141 | |
| 142 | static struct block_device_operations md_fops; |
| 143 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 144 | static int start_readonly; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 147 | * We have a system wide 'event count' that is incremented |
| 148 | * on any 'interesting' event, and readers of /proc/mdstat |
| 149 | * can use 'poll' or 'select' to find out when the event |
| 150 | * count increases. |
| 151 | * |
| 152 | * Events are: |
| 153 | * start array, stop array, error, add device, remove device, |
| 154 | * start build, activate spare |
| 155 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 156 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 158 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 159 | { |
| 160 | atomic_inc(&md_event_count); |
| 161 | wake_up(&md_event_waiters); |
| 162 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 164 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 165 | /* Alternate version that can be called from interrupts |
| 166 | * when calling sysfs_notify isn't needed. |
| 167 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 168 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
| 172 | } |
| 173 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 174 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * Enables to iterate over all existing md arrays |
| 176 | * all_mddevs_lock protects this list. |
| 177 | */ |
| 178 | static LIST_HEAD(all_mddevs); |
| 179 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * iterates through all used mddevs in the system. |
| 184 | * We take care to grab the all_mddevs_lock whenever navigating |
| 185 | * the list, and to always hold a refcount when unlocked. |
| 186 | * Any code which breaks out of this loop while own |
| 187 | * a reference to the current mddev and must mddev_put it. |
| 188 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 189 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | \ |
| 191 | for (({ spin_lock(&all_mddevs_lock); \ |
| 192 | tmp = all_mddevs.next; \ |
| 193 | mddev = NULL;}); \ |
| 194 | ({ if (tmp != &all_mddevs) \ |
| 195 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 196 | spin_unlock(&all_mddevs_lock); \ |
| 197 | if (mddev) mddev_put(mddev); \ |
| 198 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 199 | tmp != &all_mddevs;}); \ |
| 200 | ({ spin_lock(&all_mddevs_lock); \ |
| 201 | tmp = tmp->next;}) \ |
| 202 | ) |
| 203 | |
| 204 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 205 | static int md_fail_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 207 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 212 | { |
| 213 | atomic_inc(&mddev->active); |
| 214 | return mddev; |
| 215 | } |
| 216 | |
| 217 | static void mddev_put(mddev_t *mddev) |
| 218 | { |
| 219 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 220 | return; |
| 221 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 222 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 223 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 224 | blk_cleanup_queue(mddev->queue); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 225 | if (mddev->sysfs_state) |
| 226 | sysfs_put(mddev->sysfs_state); |
| 227 | mddev->sysfs_state = NULL; |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 228 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 229 | } else |
| 230 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static mddev_t * mddev_find(dev_t unit) |
| 234 | { |
| 235 | mddev_t *mddev, *new = NULL; |
| 236 | |
| 237 | retry: |
| 238 | spin_lock(&all_mddevs_lock); |
| 239 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 240 | if (mddev->unit == unit) { |
| 241 | mddev_get(mddev); |
| 242 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 243 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return mddev; |
| 245 | } |
| 246 | |
| 247 | if (new) { |
| 248 | list_add(&new->all_mddevs, &all_mddevs); |
| 249 | spin_unlock(&all_mddevs_lock); |
| 250 | return new; |
| 251 | } |
| 252 | spin_unlock(&all_mddevs_lock); |
| 253 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 254 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (!new) |
| 256 | return NULL; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | new->unit = unit; |
| 259 | if (MAJOR(unit) == MD_MAJOR) |
| 260 | new->md_minor = MINOR(unit); |
| 261 | else |
| 262 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 263 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 264 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | INIT_LIST_HEAD(&new->disks); |
| 266 | INIT_LIST_HEAD(&new->all_mddevs); |
| 267 | init_timer(&new->safemode_timer); |
| 268 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 269 | atomic_set(&new->openers, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 270 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 271 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 272 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 273 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 274 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 275 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 276 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 279 | if (!new->queue) { |
| 280 | kfree(new); |
| 281 | return NULL; |
| 282 | } |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 283 | /* Can be unlocked because the queue is new: no concurrency */ |
| 284 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | blk_queue_make_request(new->queue, md_fail_request); |
| 287 | |
| 288 | goto retry; |
| 289 | } |
| 290 | |
| 291 | static inline int mddev_lock(mddev_t * mddev) |
| 292 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 293 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | static inline int mddev_trylock(mddev_t * mddev) |
| 297 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 298 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static inline void mddev_unlock(mddev_t * mddev) |
| 302 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 303 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 305 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 308 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | mdk_rdev_t * rdev; |
| 311 | struct list_head *tmp; |
| 312 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 313 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (rdev->desc_nr == nr) |
| 315 | return rdev; |
| 316 | } |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 321 | { |
| 322 | struct list_head *tmp; |
| 323 | mdk_rdev_t *rdev; |
| 324 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 325 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (rdev->bdev->bd_dev == dev) |
| 327 | return rdev; |
| 328 | } |
| 329 | return NULL; |
| 330 | } |
| 331 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 332 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 333 | { |
| 334 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 335 | list_for_each_entry(pers, &pers_list, list) { |
| 336 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 337 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 338 | if (strcmp(pers->name, clevel)==0) |
| 339 | return pers; |
| 340 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 344 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 345 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 347 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 348 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 351 | static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 353 | sector_t num_sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | if (chunk_size) |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 356 | num_sectors &= ~((sector_t)chunk_size/512 - 1); |
| 357 | return num_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 361 | { |
| 362 | if (rdev->sb_page) |
| 363 | MD_BUG(); |
| 364 | |
| 365 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 366 | if (!rdev->sb_page) { |
| 367 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 368 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 375 | { |
| 376 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 377 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | rdev->sb_loaded = 0; |
| 379 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 380 | rdev->sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | rdev->size = 0; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 386 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 387 | { |
| 388 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 389 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 390 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 391 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 392 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 393 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 394 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 395 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 396 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 397 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 398 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 399 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 400 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 401 | } |
| 402 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 403 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 404 | { |
| 405 | struct bio *bio2 = bio->bi_private; |
| 406 | mdk_rdev_t *rdev = bio2->bi_private; |
| 407 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 408 | |
| 409 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 410 | error == -EOPNOTSUPP) { |
| 411 | unsigned long flags; |
| 412 | /* barriers don't appear to be supported :-( */ |
| 413 | set_bit(BarriersNotsupp, &rdev->flags); |
| 414 | mddev->barriers_work = 0; |
| 415 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 416 | bio2->bi_next = mddev->biolist; |
| 417 | mddev->biolist = bio2; |
| 418 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 419 | wake_up(&mddev->sb_wait); |
| 420 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 421 | } else { |
| 422 | bio_put(bio2); |
| 423 | bio->bi_private = rdev; |
| 424 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 425 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 426 | } |
| 427 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 428 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 429 | sector_t sector, int size, struct page *page) |
| 430 | { |
| 431 | /* write first size bytes of page to sector of rdev |
| 432 | * Increment mddev->pending_writes before returning |
| 433 | * and decrement it on completion, waking up sb_wait |
| 434 | * if zero is reached. |
| 435 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 436 | * |
| 437 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 438 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 439 | */ |
| 440 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 441 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 442 | |
| 443 | bio->bi_bdev = rdev->bdev; |
| 444 | bio->bi_sector = sector; |
| 445 | bio_add_page(bio, page, size, 0); |
| 446 | bio->bi_private = rdev; |
| 447 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 448 | bio->bi_rw = rw; |
| 449 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 450 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 451 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 452 | struct bio *rbio; |
| 453 | rw |= (1<<BIO_RW_BARRIER); |
| 454 | rbio = bio_clone(bio, GFP_NOIO); |
| 455 | rbio->bi_private = bio; |
| 456 | rbio->bi_end_io = super_written_barrier; |
| 457 | submit_bio(rw, rbio); |
| 458 | } else |
| 459 | submit_bio(rw, bio); |
| 460 | } |
| 461 | |
| 462 | void md_super_wait(mddev_t *mddev) |
| 463 | { |
| 464 | /* wait for all superblock writes that were scheduled to complete. |
| 465 | * if any had to be retried (due to BARRIER problems), retry them |
| 466 | */ |
| 467 | DEFINE_WAIT(wq); |
| 468 | for(;;) { |
| 469 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 470 | if (atomic_read(&mddev->pending_writes)==0) |
| 471 | break; |
| 472 | while (mddev->biolist) { |
| 473 | struct bio *bio; |
| 474 | spin_lock_irq(&mddev->write_lock); |
| 475 | bio = mddev->biolist; |
| 476 | mddev->biolist = bio->bi_next ; |
| 477 | bio->bi_next = NULL; |
| 478 | spin_unlock_irq(&mddev->write_lock); |
| 479 | submit_bio(bio->bi_rw, bio); |
| 480 | } |
| 481 | schedule(); |
| 482 | } |
| 483 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 484 | } |
| 485 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 486 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 491 | 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] | 492 | struct page *page, int rw) |
| 493 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 494 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | struct completion event; |
| 496 | int ret; |
| 497 | |
| 498 | rw |= (1 << BIO_RW_SYNC); |
| 499 | |
| 500 | bio->bi_bdev = bdev; |
| 501 | bio->bi_sector = sector; |
| 502 | bio_add_page(bio, page, size, 0); |
| 503 | init_completion(&event); |
| 504 | bio->bi_private = &event; |
| 505 | bio->bi_end_io = bi_complete; |
| 506 | submit_bio(rw, bio); |
| 507 | wait_for_completion(&event); |
| 508 | |
| 509 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 510 | bio_put(bio); |
| 511 | return ret; |
| 512 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 515 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | char b[BDEVNAME_SIZE]; |
| 518 | if (!rdev->sb_page) { |
| 519 | MD_BUG(); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | if (rdev->sb_loaded) |
| 523 | return 0; |
| 524 | |
| 525 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 526 | 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] | 527 | goto fail; |
| 528 | rdev->sb_loaded = 1; |
| 529 | return 0; |
| 530 | |
| 531 | fail: |
| 532 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 533 | bdevname(rdev->bdev,b)); |
| 534 | return -EINVAL; |
| 535 | } |
| 536 | |
| 537 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 538 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 539 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 540 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 541 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 542 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 546 | { |
| 547 | int ret; |
| 548 | mdp_super_t *tmp1, *tmp2; |
| 549 | |
| 550 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 551 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 552 | |
| 553 | if (!tmp1 || !tmp2) { |
| 554 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 555 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | goto abort; |
| 557 | } |
| 558 | |
| 559 | *tmp1 = *sb1; |
| 560 | *tmp2 = *sb2; |
| 561 | |
| 562 | /* |
| 563 | * nr_disks is not constant |
| 564 | */ |
| 565 | tmp1->nr_disks = 0; |
| 566 | tmp2->nr_disks = 0; |
| 567 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 568 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 570 | kfree(tmp1); |
| 571 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return ret; |
| 573 | } |
| 574 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 575 | |
| 576 | static u32 md_csum_fold(u32 csum) |
| 577 | { |
| 578 | csum = (csum & 0xffff) + (csum >> 16); |
| 579 | return (csum & 0xffff) + (csum >> 16); |
| 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 583 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 584 | u64 newcsum = 0; |
| 585 | u32 *sb32 = (u32*)sb; |
| 586 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | unsigned int disk_csum, csum; |
| 588 | |
| 589 | disk_csum = sb->sb_csum; |
| 590 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 591 | |
| 592 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 593 | newcsum += sb32[i]; |
| 594 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 595 | |
| 596 | |
| 597 | #ifdef CONFIG_ALPHA |
| 598 | /* This used to use csum_partial, which was wrong for several |
| 599 | * reasons including that different results are returned on |
| 600 | * different architectures. It isn't critical that we get exactly |
| 601 | * the same return value as before (we always csum_fold before |
| 602 | * testing, and that removes any differences). However as we |
| 603 | * know that csum_partial always returned a 16bit value on |
| 604 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 605 | */ |
| 606 | sb->sb_csum = md_csum_fold(disk_csum); |
| 607 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 609 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return csum; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | /* |
| 615 | * Handle superblock details. |
| 616 | * We want to be able to handle multiple superblock formats |
| 617 | * so we have a common interface to them all, and an array of |
| 618 | * different handlers. |
| 619 | * We rely on user-space to write the initial superblock, and support |
| 620 | * reading and updating of superblocks. |
| 621 | * Interface methods are: |
| 622 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 623 | * loads and validates a superblock on dev. |
| 624 | * if refdev != NULL, compare superblocks on both devices |
| 625 | * Return: |
| 626 | * 0 - dev has a superblock that is compatible with refdev |
| 627 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 628 | * so dev should be used as the refdev in future |
| 629 | * -EINVAL superblock incompatible or invalid |
| 630 | * -othererror e.g. -EIO |
| 631 | * |
| 632 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 633 | * Verify that dev is acceptable into mddev. |
| 634 | * The first time, mddev->raid_disks will be 0, and data from |
| 635 | * dev should be merged in. Subsequent calls check that dev |
| 636 | * is new enough. Return 0 or -EINVAL |
| 637 | * |
| 638 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 639 | * Update the superblock for rdev with data in mddev |
| 640 | * This does not write to disc. |
| 641 | * |
| 642 | */ |
| 643 | |
| 644 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 645 | char *name; |
| 646 | struct module *owner; |
| 647 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 648 | int minor_version); |
| 649 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 650 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 651 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 652 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | /* |
| 656 | * load_super for 0.90.0 |
| 657 | */ |
| 658 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 659 | { |
| 660 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 661 | mdp_super_t *sb; |
| 662 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 665 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | * it's at the end of the disk. |
| 667 | * |
| 668 | * It also happens to be a multiple of 4Kb. |
| 669 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 670 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 672 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (ret) return ret; |
| 674 | |
| 675 | ret = -EINVAL; |
| 676 | |
| 677 | bdevname(rdev->bdev, b); |
| 678 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 679 | |
| 680 | if (sb->md_magic != MD_SB_MAGIC) { |
| 681 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 682 | b); |
| 683 | goto abort; |
| 684 | } |
| 685 | |
| 686 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 687 | sb->minor_version < 90 || |
| 688 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 690 | sb->major_version, sb->minor_version, |
| 691 | b); |
| 692 | goto abort; |
| 693 | } |
| 694 | |
| 695 | if (sb->raid_disks <= 0) |
| 696 | goto abort; |
| 697 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 698 | 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] | 699 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 700 | b); |
| 701 | goto abort; |
| 702 | } |
| 703 | |
| 704 | rdev->preferred_minor = sb->md_minor; |
| 705 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 706 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 708 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 709 | if (sb->level != 1 && sb->level != 4 |
| 710 | && sb->level != 5 && sb->level != 6 |
| 711 | && sb->level != 10) { |
| 712 | /* FIXME use a better test */ |
| 713 | printk(KERN_WARNING |
| 714 | "md: bitmaps not supported for this level.\n"); |
| 715 | goto abort; |
| 716 | } |
| 717 | } |
| 718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (sb->level == LEVEL_MULTIPATH) |
| 720 | rdev->desc_nr = -1; |
| 721 | else |
| 722 | rdev->desc_nr = sb->this_disk.number; |
| 723 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 724 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 726 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | __u64 ev1, ev2; |
| 728 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 729 | if (!uuid_equal(refsb, sb)) { |
| 730 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 731 | b, bdevname(refdev->bdev,b2)); |
| 732 | goto abort; |
| 733 | } |
| 734 | if (!sb_equal(refsb, sb)) { |
| 735 | printk(KERN_WARNING "md: %s has same UUID" |
| 736 | " but different superblock to %s\n", |
| 737 | b, bdevname(refdev->bdev, b2)); |
| 738 | goto abort; |
| 739 | } |
| 740 | ev1 = md_event(sb); |
| 741 | ev2 = md_event(refsb); |
| 742 | if (ev1 > ev2) |
| 743 | ret = 1; |
| 744 | else |
| 745 | ret = 0; |
| 746 | } |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 747 | rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 749 | if (rdev->size < sb->size && sb->level > 1) |
| 750 | /* "this cannot possibly happen" ... */ |
| 751 | ret = -EINVAL; |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | abort: |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * validate_super for 0.90.0 |
| 759 | */ |
| 760 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 761 | { |
| 762 | mdp_disk_t *desc; |
| 763 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 764 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 766 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 767 | clear_bit(Faulty, &rdev->flags); |
| 768 | clear_bit(In_sync, &rdev->flags); |
| 769 | clear_bit(WriteMostly, &rdev->flags); |
| 770 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if (mddev->raid_disks == 0) { |
| 773 | mddev->major_version = 0; |
| 774 | mddev->minor_version = sb->minor_version; |
| 775 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 776 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | mddev->chunk_size = sb->chunk_size; |
| 778 | mddev->ctime = sb->ctime; |
| 779 | mddev->utime = sb->utime; |
| 780 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 781 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | mddev->layout = sb->layout; |
| 783 | mddev->raid_disks = sb->raid_disks; |
| 784 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 785 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 786 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 787 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 789 | if (mddev->minor_version >= 91) { |
| 790 | mddev->reshape_position = sb->reshape_position; |
| 791 | mddev->delta_disks = sb->delta_disks; |
| 792 | mddev->new_level = sb->new_level; |
| 793 | mddev->new_layout = sb->new_layout; |
| 794 | mddev->new_chunk = sb->new_chunk; |
| 795 | } else { |
| 796 | mddev->reshape_position = MaxSector; |
| 797 | mddev->delta_disks = 0; |
| 798 | mddev->new_level = mddev->level; |
| 799 | mddev->new_layout = mddev->layout; |
| 800 | mddev->new_chunk = mddev->chunk_size; |
| 801 | } |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 804 | mddev->recovery_cp = MaxSector; |
| 805 | else { |
| 806 | if (sb->events_hi == sb->cp_events_hi && |
| 807 | sb->events_lo == sb->cp_events_lo) { |
| 808 | mddev->recovery_cp = sb->recovery_cp; |
| 809 | } else |
| 810 | mddev->recovery_cp = 0; |
| 811 | } |
| 812 | |
| 813 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 814 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 815 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 816 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 817 | |
| 818 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 819 | |
| 820 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 821 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 822 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 823 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 824 | } else if (mddev->pers == NULL) { |
| 825 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | ++ev1; |
| 827 | if (ev1 < mddev->events) |
| 828 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 829 | } else if (mddev->bitmap) { |
| 830 | /* if adding to array with a bitmap, then we can accept an |
| 831 | * older device ... but not too old. |
| 832 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 833 | if (ev1 < mddev->bitmap->events_cleared) |
| 834 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 835 | } else { |
| 836 | if (ev1 < mddev->events) |
| 837 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 838 | return 0; |
| 839 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | desc = sb->disks + rdev->desc_nr; |
| 843 | |
| 844 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 845 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 846 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 847 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 848 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | rdev->raid_disk = desc->raid_disk; |
| 850 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 851 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 852 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 853 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 854 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * sync_super for 0.90.0 |
| 860 | */ |
| 861 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 862 | { |
| 863 | mdp_super_t *sb; |
| 864 | struct list_head *tmp; |
| 865 | mdk_rdev_t *rdev2; |
| 866 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
| 869 | /* make rdev->sb match mddev data.. |
| 870 | * |
| 871 | * 1/ zero out disks |
| 872 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 873 | * 3/ any empty disks < next_spare become removed |
| 874 | * |
| 875 | * disks[0] gets initialised to REMOVED because |
| 876 | * we cannot be sure from other fields if it has |
| 877 | * been initialised or not. |
| 878 | */ |
| 879 | int i; |
| 880 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 881 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 882 | rdev->sb_size = MD_SB_BYTES; |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 885 | |
| 886 | memset(sb, 0, sizeof(*sb)); |
| 887 | |
| 888 | sb->md_magic = MD_SB_MAGIC; |
| 889 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | sb->patch_version = mddev->patch_version; |
| 891 | sb->gvalid_words = 0; /* ignored */ |
| 892 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 893 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 894 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 895 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 896 | |
| 897 | sb->ctime = mddev->ctime; |
| 898 | sb->level = mddev->level; |
| 899 | sb->size = mddev->size; |
| 900 | sb->raid_disks = mddev->raid_disks; |
| 901 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 902 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | sb->utime = mddev->utime; |
| 904 | sb->state = 0; |
| 905 | sb->events_hi = (mddev->events>>32); |
| 906 | sb->events_lo = (u32)mddev->events; |
| 907 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 908 | if (mddev->reshape_position == MaxSector) |
| 909 | sb->minor_version = 90; |
| 910 | else { |
| 911 | sb->minor_version = 91; |
| 912 | sb->reshape_position = mddev->reshape_position; |
| 913 | sb->new_level = mddev->new_level; |
| 914 | sb->delta_disks = mddev->delta_disks; |
| 915 | sb->new_layout = mddev->new_layout; |
| 916 | sb->new_chunk = mddev->new_chunk; |
| 917 | } |
| 918 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (mddev->in_sync) |
| 920 | { |
| 921 | sb->recovery_cp = mddev->recovery_cp; |
| 922 | sb->cp_events_hi = (mddev->events>>32); |
| 923 | sb->cp_events_lo = (u32)mddev->events; |
| 924 | if (mddev->recovery_cp == MaxSector) |
| 925 | sb->state = (1<< MD_SB_CLEAN); |
| 926 | } else |
| 927 | sb->recovery_cp = 0; |
| 928 | |
| 929 | sb->layout = mddev->layout; |
| 930 | sb->chunk_size = mddev->chunk_size; |
| 931 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 932 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 933 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 936 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 938 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 939 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 940 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 941 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 943 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 944 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | d = &sb->disks[rdev2->desc_nr]; |
| 946 | nr_disks++; |
| 947 | d->number = rdev2->desc_nr; |
| 948 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 949 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 950 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 951 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | d->raid_disk = rdev2->raid_disk; |
| 953 | else |
| 954 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 955 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 957 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | d->state = (1<<MD_DISK_ACTIVE); |
| 959 | d->state |= (1<<MD_DISK_SYNC); |
| 960 | active++; |
| 961 | working++; |
| 962 | } else { |
| 963 | d->state = 0; |
| 964 | spare++; |
| 965 | working++; |
| 966 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 967 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 968 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 971 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 972 | mdp_disk_t *d = &sb->disks[i]; |
| 973 | if (d->state == 0 && d->number == 0) { |
| 974 | d->number = i; |
| 975 | d->raid_disk = i; |
| 976 | d->state = (1<<MD_DISK_REMOVED); |
| 977 | d->state |= (1<<MD_DISK_FAULTY); |
| 978 | failed++; |
| 979 | } |
| 980 | } |
| 981 | sb->nr_disks = nr_disks; |
| 982 | sb->active_disks = active; |
| 983 | sb->working_disks = working; |
| 984 | sb->failed_disks = failed; |
| 985 | sb->spare_disks = spare; |
| 986 | |
| 987 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 988 | sb->sb_csum = calc_sb_csum(sb); |
| 989 | } |
| 990 | |
| 991 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 992 | * rdev_size_change for 0.90.0 |
| 993 | */ |
| 994 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 995 | 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] | 996 | { |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 997 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 998 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 999 | if (rdev->mddev->bitmap_offset) |
| 1000 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1001 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1002 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1003 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1004 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1005 | rdev->sb_page); |
| 1006 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1007 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | * version 1 superblock |
| 1013 | */ |
| 1014 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1015 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1017 | __le32 disk_csum; |
| 1018 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | unsigned long long newcsum; |
| 1020 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1021 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | int i; |
| 1023 | |
| 1024 | disk_csum = sb->sb_csum; |
| 1025 | sb->sb_csum = 0; |
| 1026 | newcsum = 0; |
| 1027 | for (i=0; size>=4; size -= 4 ) |
| 1028 | newcsum += le32_to_cpu(*isuper++); |
| 1029 | |
| 1030 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1031 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1034 | sb->sb_csum = disk_csum; |
| 1035 | return cpu_to_le32(csum); |
| 1036 | } |
| 1037 | |
| 1038 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1039 | { |
| 1040 | struct mdp_superblock_1 *sb; |
| 1041 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1042 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1044 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1047 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | * It is always aligned to a 4K boundary and |
| 1049 | * depeding on minor_version, it can be: |
| 1050 | * 0: At least 8K, but less than 12K, from end of device |
| 1051 | * 1: At start of device |
| 1052 | * 2: 4K from start of device. |
| 1053 | */ |
| 1054 | switch(minor_version) { |
| 1055 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1056 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1057 | sb_start -= 8*2; |
| 1058 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | break; |
| 1060 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1061 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | break; |
| 1063 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1064 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | break; |
| 1066 | default: |
| 1067 | return -EINVAL; |
| 1068 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1069 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1071 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1072 | * and it is safe to read 4k, so we do that |
| 1073 | */ |
| 1074 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (ret) return ret; |
| 1076 | |
| 1077 | |
| 1078 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1079 | |
| 1080 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1081 | sb->major_version != cpu_to_le32(1) || |
| 1082 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1083 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1084 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | return -EINVAL; |
| 1086 | |
| 1087 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1088 | printk("md: invalid superblock checksum on %s\n", |
| 1089 | bdevname(rdev->bdev,b)); |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1093 | printk("md: data_size too small on %s\n", |
| 1094 | bdevname(rdev->bdev,b)); |
| 1095 | return -EINVAL; |
| 1096 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1097 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1098 | if (sb->level != cpu_to_le32(1) && |
| 1099 | sb->level != cpu_to_le32(4) && |
| 1100 | sb->level != cpu_to_le32(5) && |
| 1101 | sb->level != cpu_to_le32(6) && |
| 1102 | sb->level != cpu_to_le32(10)) { |
| 1103 | printk(KERN_WARNING |
| 1104 | "md: bitmaps not supported for this level.\n"); |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | } |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | rdev->preferred_minor = 0xffff; |
| 1110 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1111 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1113 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1114 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1115 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1116 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1117 | |
| 1118 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1119 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1120 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1121 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1122 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1123 | rdev->desc_nr = -1; |
| 1124 | else |
| 1125 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1126 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1127 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1128 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1129 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | __u64 ev1, ev2; |
| 1131 | struct mdp_superblock_1 *refsb = |
| 1132 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1133 | |
| 1134 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1135 | sb->level != refsb->level || |
| 1136 | sb->layout != refsb->layout || |
| 1137 | sb->chunksize != refsb->chunksize) { |
| 1138 | printk(KERN_WARNING "md: %s has strangely different" |
| 1139 | " superblock to %s\n", |
| 1140 | bdevname(rdev->bdev,b), |
| 1141 | bdevname(refdev->bdev,b2)); |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | ev1 = le64_to_cpu(sb->events); |
| 1145 | ev2 = le64_to_cpu(refsb->events); |
| 1146 | |
| 1147 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1148 | ret = 1; |
| 1149 | else |
| 1150 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1152 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1154 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1155 | rdev->size = rdev->sb_start / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1157 | return -EINVAL; |
| 1158 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1159 | if (le32_to_cpu(sb->chunksize)) |
| 1160 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1161 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1162 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1163 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1164 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1168 | { |
| 1169 | 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] | 1170 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1172 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1173 | clear_bit(Faulty, &rdev->flags); |
| 1174 | clear_bit(In_sync, &rdev->flags); |
| 1175 | clear_bit(WriteMostly, &rdev->flags); |
| 1176 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (mddev->raid_disks == 0) { |
| 1179 | mddev->major_version = 1; |
| 1180 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1181 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1183 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1184 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1185 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1186 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | mddev->layout = le32_to_cpu(sb->layout); |
| 1188 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1189 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1190 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1191 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1192 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
| 1194 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1195 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1196 | |
| 1197 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1198 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1199 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1200 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1201 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1202 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1203 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1204 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1205 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1206 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1207 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1208 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1209 | } else { |
| 1210 | mddev->reshape_position = MaxSector; |
| 1211 | mddev->delta_disks = 0; |
| 1212 | mddev->new_level = mddev->level; |
| 1213 | mddev->new_layout = mddev->layout; |
| 1214 | mddev->new_chunk = mddev->chunk_size; |
| 1215 | } |
| 1216 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1217 | } else if (mddev->pers == NULL) { |
| 1218 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | ++ev1; |
| 1220 | if (ev1 < mddev->events) |
| 1221 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1222 | } else if (mddev->bitmap) { |
| 1223 | /* If adding to array with a bitmap, then we can accept an |
| 1224 | * older device, but not too old. |
| 1225 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1226 | if (ev1 < mddev->bitmap->events_cleared) |
| 1227 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1228 | } else { |
| 1229 | if (ev1 < mddev->events) |
| 1230 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1231 | return 0; |
| 1232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1234 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1236 | switch(role) { |
| 1237 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | break; |
| 1239 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1240 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | break; |
| 1242 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1243 | if ((le32_to_cpu(sb->feature_map) & |
| 1244 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1245 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1246 | else |
| 1247 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | rdev->raid_disk = role; |
| 1249 | break; |
| 1250 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1251 | if (sb->devflags & WriteMostly1) |
| 1252 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1253 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1254 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1260 | { |
| 1261 | struct mdp_superblock_1 *sb; |
| 1262 | struct list_head *tmp; |
| 1263 | mdk_rdev_t *rdev2; |
| 1264 | int max_dev, i; |
| 1265 | /* make rdev->sb match mddev and rdev data. */ |
| 1266 | |
| 1267 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1268 | |
| 1269 | sb->feature_map = 0; |
| 1270 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1271 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1273 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1274 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1275 | |
| 1276 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1277 | sb->events = cpu_to_le64(mddev->events); |
| 1278 | if (mddev->in_sync) |
| 1279 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1280 | else |
| 1281 | sb->resync_offset = cpu_to_le64(0); |
| 1282 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1283 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1284 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1285 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1286 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1287 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1288 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1289 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1290 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1291 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (rdev->raid_disk >= 0 && |
| 1294 | !test_bit(In_sync, &rdev->flags) && |
| 1295 | rdev->recovery_offset > 0) { |
| 1296 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1297 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1298 | } |
| 1299 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1300 | if (mddev->reshape_position != MaxSector) { |
| 1301 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1302 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1303 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1304 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1305 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1306 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1307 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | max_dev = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1310 | rdev_for_each(rdev2, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | if (rdev2->desc_nr+1 > max_dev) |
| 1312 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1313 | |
| 1314 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1315 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | for (i=0; i<max_dev;i++) |
| 1317 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1318 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1319 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1321 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1323 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1325 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1326 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | else |
| 1328 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1329 | } |
| 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1332 | } |
| 1333 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1334 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1335 | 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] | 1336 | { |
| 1337 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1338 | sector_t max_sectors; |
| 1339 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1340 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1341 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1342 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1343 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1344 | max_sectors -= rdev->data_offset; |
| 1345 | if (!num_sectors || num_sectors > max_sectors) |
| 1346 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1347 | } else if (rdev->mddev->bitmap_offset) { |
| 1348 | /* minor version 0 with bitmap we can't move */ |
| 1349 | return 0; |
| 1350 | } else { |
| 1351 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1352 | sector_t sb_start; |
| 1353 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1354 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1355 | max_sectors = rdev->size * 2 + sb_start - rdev->sb_start; |
| 1356 | if (!num_sectors || num_sectors > max_sectors) |
| 1357 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1358 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1359 | } |
| 1360 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1361 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1362 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1363 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | rdev->sb_page); |
| 1366 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1367 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1370 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | [0] = { |
| 1372 | .name = "0.90.0", |
| 1373 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1374 | .load_super = super_90_load, |
| 1375 | .validate_super = super_90_validate, |
| 1376 | .sync_super = super_90_sync, |
| 1377 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | }, |
| 1379 | [1] = { |
| 1380 | .name = "md-1", |
| 1381 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1382 | .load_super = super_1_load, |
| 1383 | .validate_super = super_1_validate, |
| 1384 | .sync_super = super_1_sync, |
| 1385 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | }, |
| 1387 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1390 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1391 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1393 | rcu_read_lock(); |
| 1394 | rdev_for_each_rcu(rdev, mddev1) |
| 1395 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1396 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1397 | rdev2->bdev->bd_contains) { |
| 1398 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1399 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1400 | } |
| 1401 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | static LIST_HEAD(pending_raid_disks); |
| 1406 | |
| 1407 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1408 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1409 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1410 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1411 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1412 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | if (rdev->mddev) { |
| 1415 | MD_BUG(); |
| 1416 | return -EINVAL; |
| 1417 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1418 | |
| 1419 | /* prevent duplicates */ |
| 1420 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1421 | return -EEXIST; |
| 1422 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1423 | /* make sure rdev->size exceeds mddev->size */ |
| 1424 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1425 | if (mddev->pers) { |
| 1426 | /* Cannot change size, so fail |
| 1427 | * If mddev->level <= 0, then we don't care |
| 1428 | * about aligning sizes (e.g. linear) |
| 1429 | */ |
| 1430 | if (mddev->level > 0) |
| 1431 | return -ENOSPC; |
| 1432 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1433 | mddev->size = rdev->size; |
| 1434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
| 1436 | /* Verify rdev->desc_nr is unique. |
| 1437 | * If it is -1, assign a free number, else |
| 1438 | * check number is not in use |
| 1439 | */ |
| 1440 | if (rdev->desc_nr < 0) { |
| 1441 | int choice = 0; |
| 1442 | if (mddev->pers) choice = mddev->raid_disks; |
| 1443 | while (find_rdev_nr(mddev, choice)) |
| 1444 | choice++; |
| 1445 | rdev->desc_nr = choice; |
| 1446 | } else { |
| 1447 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1448 | return -EBUSY; |
| 1449 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1450 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1451 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1452 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1455 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1456 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1457 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1458 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1459 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1460 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1461 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1462 | kobject_del(&rdev->kobj); |
| 1463 | goto fail; |
| 1464 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1465 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1466 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1467 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1468 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1470 | |
| 1471 | fail: |
| 1472 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1473 | b, mdname(mddev)); |
| 1474 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1477 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1478 | { |
| 1479 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1480 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1481 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1485 | { |
| 1486 | char b[BDEVNAME_SIZE]; |
| 1487 | if (!rdev->mddev) { |
| 1488 | MD_BUG(); |
| 1489 | return; |
| 1490 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1491 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1492 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1494 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1495 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1496 | sysfs_put(rdev->sysfs_state); |
| 1497 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1498 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1499 | * writing to 'remove' to "dev/state". We also need |
| 1500 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1501 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1502 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1503 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1504 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1505 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /* |
| 1509 | * prevent the device from being mounted, repartitioned or |
| 1510 | * otherwise reused by a RAID array (or any other kernel |
| 1511 | * subsystem), by bd_claiming the device. |
| 1512 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1513 | 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] | 1514 | { |
| 1515 | int err = 0; |
| 1516 | struct block_device *bdev; |
| 1517 | char b[BDEVNAME_SIZE]; |
| 1518 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1519 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | if (IS_ERR(bdev)) { |
| 1521 | printk(KERN_ERR "md: could not open %s.\n", |
| 1522 | __bdevname(dev, b)); |
| 1523 | return PTR_ERR(bdev); |
| 1524 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1525 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | if (err) { |
| 1527 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1528 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1529 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | return err; |
| 1531 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1532 | if (!shared) |
| 1533 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | rdev->bdev = bdev; |
| 1535 | return err; |
| 1536 | } |
| 1537 | |
| 1538 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1539 | { |
| 1540 | struct block_device *bdev = rdev->bdev; |
| 1541 | rdev->bdev = NULL; |
| 1542 | if (!bdev) |
| 1543 | MD_BUG(); |
| 1544 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1545 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | void md_autodetect_dev(dev_t dev); |
| 1549 | |
| 1550 | static void export_rdev(mdk_rdev_t * rdev) |
| 1551 | { |
| 1552 | char b[BDEVNAME_SIZE]; |
| 1553 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1554 | bdevname(rdev->bdev,b)); |
| 1555 | if (rdev->mddev) |
| 1556 | MD_BUG(); |
| 1557 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1559 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1560 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | #endif |
| 1562 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1563 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1567 | { |
| 1568 | unbind_rdev_from_array(rdev); |
| 1569 | export_rdev(rdev); |
| 1570 | } |
| 1571 | |
| 1572 | static void export_array(mddev_t *mddev) |
| 1573 | { |
| 1574 | struct list_head *tmp; |
| 1575 | mdk_rdev_t *rdev; |
| 1576 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1577 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | if (!rdev->mddev) { |
| 1579 | MD_BUG(); |
| 1580 | continue; |
| 1581 | } |
| 1582 | kick_rdev_from_array(rdev); |
| 1583 | } |
| 1584 | if (!list_empty(&mddev->disks)) |
| 1585 | MD_BUG(); |
| 1586 | mddev->raid_disks = 0; |
| 1587 | mddev->major_version = 0; |
| 1588 | } |
| 1589 | |
| 1590 | static void print_desc(mdp_disk_t *desc) |
| 1591 | { |
| 1592 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1593 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1594 | } |
| 1595 | |
| 1596 | static void print_sb(mdp_super_t *sb) |
| 1597 | { |
| 1598 | int i; |
| 1599 | |
| 1600 | printk(KERN_INFO |
| 1601 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1602 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1603 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1604 | sb->ctime); |
| 1605 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1606 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1607 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1608 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1609 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1610 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1611 | sb->failed_disks, sb->spare_disks, |
| 1612 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1613 | |
| 1614 | printk(KERN_INFO); |
| 1615 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1616 | mdp_disk_t *desc; |
| 1617 | |
| 1618 | desc = sb->disks + i; |
| 1619 | if (desc->number || desc->major || desc->minor || |
| 1620 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1621 | printk(" D %2d: ", i); |
| 1622 | print_desc(desc); |
| 1623 | } |
| 1624 | } |
| 1625 | printk(KERN_INFO "md: THIS: "); |
| 1626 | print_desc(&sb->this_disk); |
| 1627 | |
| 1628 | } |
| 1629 | |
| 1630 | static void print_rdev(mdk_rdev_t *rdev) |
| 1631 | { |
| 1632 | char b[BDEVNAME_SIZE]; |
| 1633 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1634 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1635 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1636 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | if (rdev->sb_loaded) { |
| 1638 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1639 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1640 | } else |
| 1641 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1642 | } |
| 1643 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1644 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | { |
| 1646 | struct list_head *tmp, *tmp2; |
| 1647 | mdk_rdev_t *rdev; |
| 1648 | mddev_t *mddev; |
| 1649 | char b[BDEVNAME_SIZE]; |
| 1650 | |
| 1651 | printk("\n"); |
| 1652 | printk("md: **********************************\n"); |
| 1653 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1654 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1655 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1657 | if (mddev->bitmap) |
| 1658 | bitmap_print_sb(mddev->bitmap); |
| 1659 | else |
| 1660 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1661 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1663 | printk("\n"); |
| 1664 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1665 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | print_rdev(rdev); |
| 1667 | } |
| 1668 | printk("md: **********************************\n"); |
| 1669 | printk("\n"); |
| 1670 | } |
| 1671 | |
| 1672 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1673 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1675 | /* Update each superblock (in-memory image), but |
| 1676 | * if we are allowed to, skip spares which already |
| 1677 | * have the right event counter, or have one earlier |
| 1678 | * (which would mean they aren't being marked as dirty |
| 1679 | * with the rest of the array) |
| 1680 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | mdk_rdev_t *rdev; |
| 1682 | struct list_head *tmp; |
| 1683 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1684 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1685 | if (rdev->sb_events == mddev->events || |
| 1686 | (nospares && |
| 1687 | rdev->raid_disk < 0 && |
| 1688 | (rdev->sb_events&1)==0 && |
| 1689 | rdev->sb_events+1 == mddev->events)) { |
| 1690 | /* Don't update this superblock */ |
| 1691 | rdev->sb_loaded = 2; |
| 1692 | } else { |
| 1693 | super_types[mddev->major_version]. |
| 1694 | sync_super(mddev, rdev); |
| 1695 | rdev->sb_loaded = 1; |
| 1696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1700 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | struct list_head *tmp; |
| 1703 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1704 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1705 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1707 | if (mddev->external) |
| 1708 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1710 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1711 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1712 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1713 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1714 | force_change = 1; |
| 1715 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1716 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1717 | * though if events isn't the right even/odd, we will have to do |
| 1718 | * spares after all |
| 1719 | */ |
| 1720 | nospares = 1; |
| 1721 | if (force_change) |
| 1722 | nospares = 0; |
| 1723 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1724 | /* If the array is degraded, then skipping spares is both |
| 1725 | * dangerous and fairly pointless. |
| 1726 | * Dangerous because a device that was removed from the array |
| 1727 | * might have a event_count that still looks up-to-date, |
| 1728 | * so it can be re-added without a resync. |
| 1729 | * Pointless because if there are any spares to skip, |
| 1730 | * then a recovery will happen and soon that array won't |
| 1731 | * be degraded any more and the spare can go back to sleep then. |
| 1732 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1733 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1734 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1735 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1737 | |
| 1738 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1739 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1740 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1741 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1742 | && (mddev->events & 1) |
| 1743 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1744 | mddev->events--; |
| 1745 | else { |
| 1746 | /* otherwise we have to go forward and ... */ |
| 1747 | mddev->events ++; |
| 1748 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1749 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1750 | if ((mddev->events&1)==0) { |
| 1751 | mddev->events++; |
| 1752 | nospares = 0; |
| 1753 | } |
| 1754 | } else { |
| 1755 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1756 | if ((mddev->events&1)) { |
| 1757 | mddev->events++; |
| 1758 | nospares = 0; |
| 1759 | } |
| 1760 | } |
| 1761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
| 1763 | if (!mddev->events) { |
| 1764 | /* |
| 1765 | * oops, this 64-bit counter should never wrap. |
| 1766 | * Either we are in around ~1 trillion A.C., assuming |
| 1767 | * 1 reboot per second, or we have a bug: |
| 1768 | */ |
| 1769 | MD_BUG(); |
| 1770 | mddev->events --; |
| 1771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
| 1773 | /* |
| 1774 | * do not write anything to disk if using |
| 1775 | * nonpersistent superblocks |
| 1776 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1777 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1778 | if (!mddev->external) |
| 1779 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1780 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1781 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1782 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1784 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1785 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1786 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | dprintk(KERN_INFO |
| 1789 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1790 | mdname(mddev),mddev->in_sync); |
| 1791 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1792 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1793 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | char b[BDEVNAME_SIZE]; |
| 1795 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1796 | if (rdev->sb_loaded != 1) |
| 1797 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1798 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | dprintk("(skipping faulty "); |
| 1800 | |
| 1801 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1802 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1803 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1804 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1805 | rdev->sb_page); |
| 1806 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1807 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1808 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1809 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | } else |
| 1812 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1813 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | /* only need to write one superblock... */ |
| 1815 | break; |
| 1816 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1817 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1818 | /* 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] | 1819 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1820 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1821 | if (mddev->in_sync != sync_req || |
| 1822 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1823 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1824 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1825 | goto repeat; |
| 1826 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1827 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1828 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1829 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 1833 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1834 | * We want to accept with case. For this we use cmd_match. |
| 1835 | */ |
| 1836 | static int cmd_match(const char *cmd, const char *str) |
| 1837 | { |
| 1838 | /* See if cmd, written into a sysfs file, matches |
| 1839 | * str. They must either be the same, or cmd can |
| 1840 | * have a trailing newline |
| 1841 | */ |
| 1842 | while (*cmd && *str && *cmd == *str) { |
| 1843 | cmd++; |
| 1844 | str++; |
| 1845 | } |
| 1846 | if (*cmd == '\n') |
| 1847 | cmd++; |
| 1848 | if (*str || *cmd) |
| 1849 | return 0; |
| 1850 | return 1; |
| 1851 | } |
| 1852 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1853 | struct rdev_sysfs_entry { |
| 1854 | struct attribute attr; |
| 1855 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1856 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1857 | }; |
| 1858 | |
| 1859 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1860 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1861 | { |
| 1862 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1863 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1864 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1865 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1866 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1867 | sep = ","; |
| 1868 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1869 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1870 | len += sprintf(page+len, "%sin_sync",sep); |
| 1871 | sep = ","; |
| 1872 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1873 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1874 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1875 | sep = ","; |
| 1876 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1877 | if (test_bit(Blocked, &rdev->flags)) { |
| 1878 | len += sprintf(page+len, "%sblocked", sep); |
| 1879 | sep = ","; |
| 1880 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1881 | if (!test_bit(Faulty, &rdev->flags) && |
| 1882 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1883 | len += sprintf(page+len, "%sspare", sep); |
| 1884 | sep = ","; |
| 1885 | } |
| 1886 | return len+sprintf(page+len, "\n"); |
| 1887 | } |
| 1888 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1889 | static ssize_t |
| 1890 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1891 | { |
| 1892 | /* can write |
| 1893 | * faulty - simulates and error |
| 1894 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1895 | * writemostly - sets write_mostly |
| 1896 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1897 | * blocked - sets the Blocked flag |
| 1898 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1899 | */ |
| 1900 | int err = -EINVAL; |
| 1901 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1902 | md_error(rdev->mddev, rdev); |
| 1903 | err = 0; |
| 1904 | } else if (cmd_match(buf, "remove")) { |
| 1905 | if (rdev->raid_disk >= 0) |
| 1906 | err = -EBUSY; |
| 1907 | else { |
| 1908 | mddev_t *mddev = rdev->mddev; |
| 1909 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1910 | if (mddev->pers) |
| 1911 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1912 | md_new_event(mddev); |
| 1913 | err = 0; |
| 1914 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1915 | } else if (cmd_match(buf, "writemostly")) { |
| 1916 | set_bit(WriteMostly, &rdev->flags); |
| 1917 | err = 0; |
| 1918 | } else if (cmd_match(buf, "-writemostly")) { |
| 1919 | clear_bit(WriteMostly, &rdev->flags); |
| 1920 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1921 | } else if (cmd_match(buf, "blocked")) { |
| 1922 | set_bit(Blocked, &rdev->flags); |
| 1923 | err = 0; |
| 1924 | } else if (cmd_match(buf, "-blocked")) { |
| 1925 | clear_bit(Blocked, &rdev->flags); |
| 1926 | wake_up(&rdev->blocked_wait); |
| 1927 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1928 | md_wakeup_thread(rdev->mddev->thread); |
| 1929 | |
| 1930 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1931 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1932 | if (!err && rdev->sysfs_state) |
| 1933 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1934 | return err ? err : len; |
| 1935 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1936 | static struct rdev_sysfs_entry rdev_state = |
| 1937 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1938 | |
| 1939 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1940 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1941 | { |
| 1942 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1943 | } |
| 1944 | |
| 1945 | static ssize_t |
| 1946 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1947 | { |
| 1948 | char *e; |
| 1949 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1950 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1951 | atomic_set(&rdev->corrected_errors, n); |
| 1952 | return len; |
| 1953 | } |
| 1954 | return -EINVAL; |
| 1955 | } |
| 1956 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1957 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1958 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1959 | static ssize_t |
| 1960 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1961 | { |
| 1962 | if (rdev->raid_disk < 0) |
| 1963 | return sprintf(page, "none\n"); |
| 1964 | else |
| 1965 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1966 | } |
| 1967 | |
| 1968 | static ssize_t |
| 1969 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1970 | { |
| 1971 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1972 | int err; |
| 1973 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1974 | int slot = simple_strtoul(buf, &e, 10); |
| 1975 | if (strncmp(buf, "none", 4)==0) |
| 1976 | slot = -1; |
| 1977 | else if (e==buf || (*e && *e!= '\n')) |
| 1978 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1979 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1980 | /* Setting 'slot' on an active array requires also |
| 1981 | * updating the 'rd%d' link, and communicating |
| 1982 | * with the personality with ->hot_*_disk. |
| 1983 | * For now we only support removing |
| 1984 | * failed/spare devices. This normally happens automatically, |
| 1985 | * but not when the metadata is externally managed. |
| 1986 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1987 | if (rdev->raid_disk == -1) |
| 1988 | return -EEXIST; |
| 1989 | /* personality does all needed checks */ |
| 1990 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1991 | return -EINVAL; |
| 1992 | err = rdev->mddev->pers-> |
| 1993 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 1994 | if (err) |
| 1995 | return err; |
| 1996 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1997 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 1998 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1999 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2000 | } else if (rdev->mddev->pers) { |
| 2001 | mdk_rdev_t *rdev2; |
| 2002 | struct list_head *tmp; |
| 2003 | /* Activating a spare .. or possibly reactivating |
| 2004 | * if we every get bitmaps working here. |
| 2005 | */ |
| 2006 | |
| 2007 | if (rdev->raid_disk != -1) |
| 2008 | return -EBUSY; |
| 2009 | |
| 2010 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2011 | return -EINVAL; |
| 2012 | |
| 2013 | rdev_for_each(rdev2, tmp, rdev->mddev) |
| 2014 | if (rdev2->raid_disk == slot) |
| 2015 | return -EEXIST; |
| 2016 | |
| 2017 | rdev->raid_disk = slot; |
| 2018 | if (test_bit(In_sync, &rdev->flags)) |
| 2019 | rdev->saved_raid_disk = slot; |
| 2020 | else |
| 2021 | rdev->saved_raid_disk = -1; |
| 2022 | err = rdev->mddev->pers-> |
| 2023 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2024 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2025 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2026 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2027 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2028 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2029 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2030 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2031 | printk(KERN_WARNING |
| 2032 | "md: cannot register " |
| 2033 | "%s for %s\n", |
| 2034 | nm, mdname(rdev->mddev)); |
| 2035 | |
| 2036 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2037 | } else { |
| 2038 | if (slot >= rdev->mddev->raid_disks) |
| 2039 | return -ENOSPC; |
| 2040 | rdev->raid_disk = slot; |
| 2041 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2042 | clear_bit(Faulty, &rdev->flags); |
| 2043 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2044 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2045 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2046 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2047 | return len; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2052 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2053 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2054 | static ssize_t |
| 2055 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2056 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2057 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | static ssize_t |
| 2061 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2062 | { |
| 2063 | char *e; |
| 2064 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2065 | if (e==buf || (*e && *e != '\n')) |
| 2066 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2067 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2068 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2069 | if (rdev->size && rdev->mddev->external) |
| 2070 | /* Must set offset before size, so overlap checks |
| 2071 | * can be sane */ |
| 2072 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2073 | rdev->data_offset = offset; |
| 2074 | return len; |
| 2075 | } |
| 2076 | |
| 2077 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2078 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2079 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2080 | static ssize_t |
| 2081 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2082 | { |
| 2083 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2084 | } |
| 2085 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2086 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2087 | { |
| 2088 | /* check if two start/length pairs overlap */ |
| 2089 | if (s1+l1 <= s2) |
| 2090 | return 0; |
| 2091 | if (s2+l2 <= s1) |
| 2092 | return 0; |
| 2093 | return 1; |
| 2094 | } |
| 2095 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2096 | static ssize_t |
| 2097 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2098 | { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2099 | unsigned long long size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2100 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2101 | mddev_t *my_mddev = rdev->mddev; |
| 2102 | |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2103 | if (strict_strtoull(buf, 10, &size) < 0) |
| 2104 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2105 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2106 | if (my_mddev->persistent) { |
| 2107 | size = super_types[my_mddev->major_version]. |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2108 | rdev_size_change(rdev, size * 2); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2109 | if (!size) |
| 2110 | return -EBUSY; |
| 2111 | } else if (!size) { |
| 2112 | size = (rdev->bdev->bd_inode->i_size >> 10); |
| 2113 | size -= rdev->data_offset/2; |
| 2114 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2115 | } |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2116 | if (size < my_mddev->size) |
| 2117 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2118 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2119 | rdev->size = size; |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2120 | if (size > oldsize && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2121 | /* need to check that all other rdevs with the same ->bdev |
| 2122 | * do not overlap. We need to unlock the mddev to avoid |
| 2123 | * a deadlock. We have already changed rdev->size, and if |
| 2124 | * we have to change it back, we will have the lock again. |
| 2125 | */ |
| 2126 | mddev_t *mddev; |
| 2127 | int overlap = 0; |
| 2128 | struct list_head *tmp, *tmp2; |
| 2129 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2130 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2131 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2132 | mdk_rdev_t *rdev2; |
| 2133 | |
| 2134 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2135 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2136 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2137 | (rdev->bdev == rdev2->bdev && |
| 2138 | rdev != rdev2 && |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2139 | overlaps(rdev->data_offset, rdev->size * 2, |
| 2140 | rdev2->data_offset, |
| 2141 | rdev2->size * 2))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2142 | overlap = 1; |
| 2143 | break; |
| 2144 | } |
| 2145 | mddev_unlock(mddev); |
| 2146 | if (overlap) { |
| 2147 | mddev_put(mddev); |
| 2148 | break; |
| 2149 | } |
| 2150 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2151 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2152 | if (overlap) { |
| 2153 | /* Someone else could have slipped in a size |
| 2154 | * change here, but doing so is just silly. |
| 2155 | * We put oldsize back because we *know* it is |
| 2156 | * safe, and trust userspace not to race with |
| 2157 | * itself |
| 2158 | */ |
| 2159 | rdev->size = oldsize; |
| 2160 | return -EBUSY; |
| 2161 | } |
| 2162 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2163 | return len; |
| 2164 | } |
| 2165 | |
| 2166 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2167 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2168 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2169 | static struct attribute *rdev_default_attrs[] = { |
| 2170 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2171 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2172 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2173 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2174 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2175 | NULL, |
| 2176 | }; |
| 2177 | static ssize_t |
| 2178 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2179 | { |
| 2180 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2181 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2182 | mddev_t *mddev = rdev->mddev; |
| 2183 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2184 | |
| 2185 | if (!entry->show) |
| 2186 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2187 | |
| 2188 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2189 | if (!rv) { |
| 2190 | if (rdev->mddev == NULL) |
| 2191 | rv = -EBUSY; |
| 2192 | else |
| 2193 | rv = entry->show(rdev, page); |
| 2194 | mddev_unlock(mddev); |
| 2195 | } |
| 2196 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | static ssize_t |
| 2200 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2201 | const char *page, size_t length) |
| 2202 | { |
| 2203 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2204 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2205 | ssize_t rv; |
| 2206 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2207 | |
| 2208 | if (!entry->store) |
| 2209 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2210 | if (!capable(CAP_SYS_ADMIN)) |
| 2211 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2212 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2213 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2214 | if (rdev->mddev == NULL) |
| 2215 | rv = -EBUSY; |
| 2216 | else |
| 2217 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2218 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2219 | } |
| 2220 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | static void rdev_free(struct kobject *ko) |
| 2224 | { |
| 2225 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2226 | kfree(rdev); |
| 2227 | } |
| 2228 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2229 | .show = rdev_attr_show, |
| 2230 | .store = rdev_attr_store, |
| 2231 | }; |
| 2232 | static struct kobj_type rdev_ktype = { |
| 2233 | .release = rdev_free, |
| 2234 | .sysfs_ops = &rdev_sysfs_ops, |
| 2235 | .default_attrs = rdev_default_attrs, |
| 2236 | }; |
| 2237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | /* |
| 2239 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2240 | * |
| 2241 | * mark the device faulty if: |
| 2242 | * |
| 2243 | * - the device is nonexistent (zero size) |
| 2244 | * - the device has no valid superblock |
| 2245 | * |
| 2246 | * a faulty rdev _never_ has rdev->sb set. |
| 2247 | */ |
| 2248 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2249 | { |
| 2250 | char b[BDEVNAME_SIZE]; |
| 2251 | int err; |
| 2252 | mdk_rdev_t *rdev; |
| 2253 | sector_t size; |
| 2254 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2255 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | if (!rdev) { |
| 2257 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2258 | return ERR_PTR(-ENOMEM); |
| 2259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
| 2261 | if ((err = alloc_disk_sb(rdev))) |
| 2262 | goto abort_free; |
| 2263 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2264 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | if (err) |
| 2266 | goto abort_free; |
| 2267 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2268 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2271 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2272 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2273 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2275 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2277 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2278 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
| 2280 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2281 | if (!size) { |
| 2282 | printk(KERN_WARNING |
| 2283 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2284 | bdevname(rdev->bdev,b)); |
| 2285 | err = -EINVAL; |
| 2286 | goto abort_free; |
| 2287 | } |
| 2288 | |
| 2289 | if (super_format >= 0) { |
| 2290 | err = super_types[super_format]. |
| 2291 | load_super(rdev, NULL, super_minor); |
| 2292 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2293 | printk(KERN_WARNING |
| 2294 | "md: %s does not have a valid v%d.%d " |
| 2295 | "superblock, not importing!\n", |
| 2296 | bdevname(rdev->bdev,b), |
| 2297 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | goto abort_free; |
| 2299 | } |
| 2300 | if (err < 0) { |
| 2301 | printk(KERN_WARNING |
| 2302 | "md: could not read %s's sb, not importing!\n", |
| 2303 | bdevname(rdev->bdev,b)); |
| 2304 | goto abort_free; |
| 2305 | } |
| 2306 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2309 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
| 2311 | return rdev; |
| 2312 | |
| 2313 | abort_free: |
| 2314 | if (rdev->sb_page) { |
| 2315 | if (rdev->bdev) |
| 2316 | unlock_rdev(rdev); |
| 2317 | free_disk_sb(rdev); |
| 2318 | } |
| 2319 | kfree(rdev); |
| 2320 | return ERR_PTR(err); |
| 2321 | } |
| 2322 | |
| 2323 | /* |
| 2324 | * Check a full RAID array for plausibility |
| 2325 | */ |
| 2326 | |
| 2327 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2328 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | { |
| 2330 | int i; |
| 2331 | struct list_head *tmp; |
| 2332 | mdk_rdev_t *rdev, *freshest; |
| 2333 | char b[BDEVNAME_SIZE]; |
| 2334 | |
| 2335 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2336 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | switch (super_types[mddev->major_version]. |
| 2338 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2339 | case 1: |
| 2340 | freshest = rdev; |
| 2341 | break; |
| 2342 | case 0: |
| 2343 | break; |
| 2344 | default: |
| 2345 | printk( KERN_ERR \ |
| 2346 | "md: fatal superblock inconsistency in %s" |
| 2347 | " -- removing from array\n", |
| 2348 | bdevname(rdev->bdev,b)); |
| 2349 | kick_rdev_from_array(rdev); |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | super_types[mddev->major_version]. |
| 2354 | validate_super(mddev, freshest); |
| 2355 | |
| 2356 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2357 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (rdev != freshest) |
| 2359 | if (super_types[mddev->major_version]. |
| 2360 | validate_super(mddev, rdev)) { |
| 2361 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2362 | " from array!\n", |
| 2363 | bdevname(rdev->bdev,b)); |
| 2364 | kick_rdev_from_array(rdev); |
| 2365 | continue; |
| 2366 | } |
| 2367 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2368 | rdev->desc_nr = i++; |
| 2369 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2370 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2371 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2372 | rdev->raid_disk = -1; |
| 2373 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | if (mddev->recovery_cp != MaxSector && |
| 2380 | mddev->level >= 1) |
| 2381 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2382 | " -- starting background reconstruction\n", |
| 2383 | mdname(mddev)); |
| 2384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2387 | static void md_safemode_timeout(unsigned long data); |
| 2388 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2389 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2390 | safe_delay_show(mddev_t *mddev, char *page) |
| 2391 | { |
| 2392 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2393 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2394 | } |
| 2395 | static ssize_t |
| 2396 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2397 | { |
| 2398 | int scale=1; |
| 2399 | int dot=0; |
| 2400 | int i; |
| 2401 | unsigned long msec; |
| 2402 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2403 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2404 | /* remove a period, and count digits after it */ |
| 2405 | if (len >= sizeof(buf)) |
| 2406 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2407 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2408 | for (i=0; i<len; i++) { |
| 2409 | if (dot) { |
| 2410 | if (isdigit(buf[i])) { |
| 2411 | buf[i-1] = buf[i]; |
| 2412 | scale *= 10; |
| 2413 | } |
| 2414 | buf[i] = 0; |
| 2415 | } else if (buf[i] == '.') { |
| 2416 | dot=1; |
| 2417 | buf[i] = 0; |
| 2418 | } |
| 2419 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2420 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2421 | return -EINVAL; |
| 2422 | msec = (msec * 1000) / scale; |
| 2423 | if (msec == 0) |
| 2424 | mddev->safemode_delay = 0; |
| 2425 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2426 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2427 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2428 | if (mddev->safemode_delay == 0) |
| 2429 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2430 | if (mddev->safemode_delay < old_delay) |
| 2431 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2432 | } |
| 2433 | return len; |
| 2434 | } |
| 2435 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2436 | __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] | 2437 | |
| 2438 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2439 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2440 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2441 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2442 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2443 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2444 | else if (mddev->clevel[0]) |
| 2445 | return sprintf(page, "%s\n", mddev->clevel); |
| 2446 | else if (mddev->level != LEVEL_NONE) |
| 2447 | return sprintf(page, "%d\n", mddev->level); |
| 2448 | else |
| 2449 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2452 | static ssize_t |
| 2453 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2454 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2455 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2456 | if (mddev->pers) |
| 2457 | return -EBUSY; |
| 2458 | if (len == 0) |
| 2459 | return 0; |
| 2460 | if (len >= sizeof(mddev->clevel)) |
| 2461 | return -ENOSPC; |
| 2462 | strncpy(mddev->clevel, buf, len); |
| 2463 | if (mddev->clevel[len-1] == '\n') |
| 2464 | len--; |
| 2465 | mddev->clevel[len] = 0; |
| 2466 | mddev->level = LEVEL_NONE; |
| 2467 | return rv; |
| 2468 | } |
| 2469 | |
| 2470 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2471 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2472 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2473 | |
| 2474 | static ssize_t |
| 2475 | layout_show(mddev_t *mddev, char *page) |
| 2476 | { |
| 2477 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2478 | if (mddev->reshape_position != MaxSector && |
| 2479 | mddev->layout != mddev->new_layout) |
| 2480 | return sprintf(page, "%d (%d)\n", |
| 2481 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2482 | return sprintf(page, "%d\n", mddev->layout); |
| 2483 | } |
| 2484 | |
| 2485 | static ssize_t |
| 2486 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2487 | { |
| 2488 | char *e; |
| 2489 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2490 | |
| 2491 | if (!*buf || (*e && *e != '\n')) |
| 2492 | return -EINVAL; |
| 2493 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2494 | if (mddev->pers) |
| 2495 | return -EBUSY; |
| 2496 | if (mddev->reshape_position != MaxSector) |
| 2497 | mddev->new_layout = n; |
| 2498 | else |
| 2499 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2500 | return len; |
| 2501 | } |
| 2502 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2503 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2504 | |
| 2505 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2506 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2507 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2508 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2509 | if (mddev->raid_disks == 0) |
| 2510 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2511 | if (mddev->reshape_position != MaxSector && |
| 2512 | mddev->delta_disks != 0) |
| 2513 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2514 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2515 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2516 | } |
| 2517 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2518 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2519 | |
| 2520 | static ssize_t |
| 2521 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2522 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2523 | char *e; |
| 2524 | int rv = 0; |
| 2525 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2526 | |
| 2527 | if (!*buf || (*e && *e != '\n')) |
| 2528 | return -EINVAL; |
| 2529 | |
| 2530 | if (mddev->pers) |
| 2531 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2532 | else if (mddev->reshape_position != MaxSector) { |
| 2533 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2534 | mddev->delta_disks = n - olddisks; |
| 2535 | mddev->raid_disks = n; |
| 2536 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2537 | mddev->raid_disks = n; |
| 2538 | return rv ? rv : len; |
| 2539 | } |
| 2540 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2541 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2542 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2543 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2544 | chunk_size_show(mddev_t *mddev, char *page) |
| 2545 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2546 | if (mddev->reshape_position != MaxSector && |
| 2547 | mddev->chunk_size != mddev->new_chunk) |
| 2548 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2549 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2550 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2551 | } |
| 2552 | |
| 2553 | static ssize_t |
| 2554 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2555 | { |
| 2556 | /* can only set chunk_size if array is not yet active */ |
| 2557 | char *e; |
| 2558 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2559 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2560 | if (!*buf || (*e && *e != '\n')) |
| 2561 | return -EINVAL; |
| 2562 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2563 | if (mddev->pers) |
| 2564 | return -EBUSY; |
| 2565 | else if (mddev->reshape_position != MaxSector) |
| 2566 | mddev->new_chunk = n; |
| 2567 | else |
| 2568 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2569 | return len; |
| 2570 | } |
| 2571 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2572 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2573 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2574 | static ssize_t |
| 2575 | resync_start_show(mddev_t *mddev, char *page) |
| 2576 | { |
| 2577 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2578 | } |
| 2579 | |
| 2580 | static ssize_t |
| 2581 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2582 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2583 | char *e; |
| 2584 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2585 | |
| 2586 | if (mddev->pers) |
| 2587 | return -EBUSY; |
| 2588 | if (!*buf || (*e && *e != '\n')) |
| 2589 | return -EINVAL; |
| 2590 | |
| 2591 | mddev->recovery_cp = n; |
| 2592 | return len; |
| 2593 | } |
| 2594 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2595 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2596 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2597 | /* |
| 2598 | * The array state can be: |
| 2599 | * |
| 2600 | * clear |
| 2601 | * No devices, no size, no level |
| 2602 | * Equivalent to STOP_ARRAY ioctl |
| 2603 | * inactive |
| 2604 | * May have some settings, but array is not active |
| 2605 | * all IO results in error |
| 2606 | * When written, doesn't tear down array, but just stops it |
| 2607 | * suspended (not supported yet) |
| 2608 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2609 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2610 | * readonly |
| 2611 | * no resync can happen. no superblocks get written. |
| 2612 | * write requests fail |
| 2613 | * read-auto |
| 2614 | * like readonly, but behaves like 'clean' on a write request. |
| 2615 | * |
| 2616 | * clean - no pending writes, but otherwise active. |
| 2617 | * When written to inactive array, starts without resync |
| 2618 | * If a write request arrives then |
| 2619 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2620 | * if not known, block and switch to write-pending |
| 2621 | * If written to an active array that has pending writes, then fails. |
| 2622 | * active |
| 2623 | * fully active: IO and resync can be happening. |
| 2624 | * When written to inactive array, starts with resync |
| 2625 | * |
| 2626 | * write-pending |
| 2627 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2628 | * |
| 2629 | * active-idle |
| 2630 | * like active, but no writes have been seen for a while (100msec). |
| 2631 | * |
| 2632 | */ |
| 2633 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2634 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2635 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2636 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2637 | "write-pending", "active-idle", NULL }; |
| 2638 | |
| 2639 | static int match_word(const char *word, char **list) |
| 2640 | { |
| 2641 | int n; |
| 2642 | for (n=0; list[n]; n++) |
| 2643 | if (cmd_match(word, list[n])) |
| 2644 | break; |
| 2645 | return n; |
| 2646 | } |
| 2647 | |
| 2648 | static ssize_t |
| 2649 | array_state_show(mddev_t *mddev, char *page) |
| 2650 | { |
| 2651 | enum array_state st = inactive; |
| 2652 | |
| 2653 | if (mddev->pers) |
| 2654 | switch(mddev->ro) { |
| 2655 | case 1: |
| 2656 | st = readonly; |
| 2657 | break; |
| 2658 | case 2: |
| 2659 | st = read_auto; |
| 2660 | break; |
| 2661 | case 0: |
| 2662 | if (mddev->in_sync) |
| 2663 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2664 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2665 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2666 | else if (mddev->safemode) |
| 2667 | st = active_idle; |
| 2668 | else |
| 2669 | st = active; |
| 2670 | } |
| 2671 | else { |
| 2672 | if (list_empty(&mddev->disks) && |
| 2673 | mddev->raid_disks == 0 && |
| 2674 | mddev->size == 0) |
| 2675 | st = clear; |
| 2676 | else |
| 2677 | st = inactive; |
| 2678 | } |
| 2679 | return sprintf(page, "%s\n", array_states[st]); |
| 2680 | } |
| 2681 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2682 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2683 | static int do_md_run(mddev_t * mddev); |
| 2684 | static int restart_array(mddev_t *mddev); |
| 2685 | |
| 2686 | static ssize_t |
| 2687 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2688 | { |
| 2689 | int err = -EINVAL; |
| 2690 | enum array_state st = match_word(buf, array_states); |
| 2691 | switch(st) { |
| 2692 | case bad_word: |
| 2693 | break; |
| 2694 | case clear: |
| 2695 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2696 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2697 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2698 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2699 | break; |
| 2700 | case inactive: |
| 2701 | /* stopping an active array */ |
| 2702 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2703 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2704 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2705 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2706 | } else |
| 2707 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2708 | break; |
| 2709 | case suspended: |
| 2710 | break; /* not supported yet */ |
| 2711 | case readonly: |
| 2712 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2713 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2714 | else { |
| 2715 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2716 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2717 | err = do_md_run(mddev); |
| 2718 | } |
| 2719 | break; |
| 2720 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2721 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2722 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2723 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2724 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2725 | err = restart_array(mddev); |
| 2726 | if (err == 0) { |
| 2727 | mddev->ro = 2; |
| 2728 | set_disk_ro(mddev->gendisk, 0); |
| 2729 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2730 | } else { |
| 2731 | mddev->ro = 2; |
| 2732 | err = do_md_run(mddev); |
| 2733 | } |
| 2734 | break; |
| 2735 | case clean: |
| 2736 | if (mddev->pers) { |
| 2737 | restart_array(mddev); |
| 2738 | spin_lock_irq(&mddev->write_lock); |
| 2739 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2740 | if (mddev->in_sync == 0) { |
| 2741 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2742 | if (mddev->safemode == 1) |
| 2743 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2744 | if (mddev->persistent) |
| 2745 | set_bit(MD_CHANGE_CLEAN, |
| 2746 | &mddev->flags); |
| 2747 | } |
| 2748 | err = 0; |
| 2749 | } else |
| 2750 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2751 | spin_unlock_irq(&mddev->write_lock); |
| 2752 | } else { |
| 2753 | mddev->ro = 0; |
| 2754 | mddev->recovery_cp = MaxSector; |
| 2755 | err = do_md_run(mddev); |
| 2756 | } |
| 2757 | break; |
| 2758 | case active: |
| 2759 | if (mddev->pers) { |
| 2760 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2761 | if (mddev->external) |
| 2762 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2763 | wake_up(&mddev->sb_wait); |
| 2764 | err = 0; |
| 2765 | } else { |
| 2766 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2767 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2768 | err = do_md_run(mddev); |
| 2769 | } |
| 2770 | break; |
| 2771 | case write_pending: |
| 2772 | case active_idle: |
| 2773 | /* these cannot be set */ |
| 2774 | break; |
| 2775 | } |
| 2776 | if (err) |
| 2777 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2778 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 2779 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2780 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2781 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2782 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2783 | static struct md_sysfs_entry md_array_state = |
| 2784 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2785 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2786 | static ssize_t |
| 2787 | null_show(mddev_t *mddev, char *page) |
| 2788 | { |
| 2789 | return -EINVAL; |
| 2790 | } |
| 2791 | |
| 2792 | static ssize_t |
| 2793 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2794 | { |
| 2795 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2796 | /* The new device is added to the array. |
| 2797 | * If the array has a persistent superblock, we read the |
| 2798 | * superblock to initialise info and check validity. |
| 2799 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2800 | * which mainly checks size. |
| 2801 | */ |
| 2802 | char *e; |
| 2803 | int major = simple_strtoul(buf, &e, 10); |
| 2804 | int minor; |
| 2805 | dev_t dev; |
| 2806 | mdk_rdev_t *rdev; |
| 2807 | int err; |
| 2808 | |
| 2809 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2810 | return -EINVAL; |
| 2811 | minor = simple_strtoul(e+1, &e, 10); |
| 2812 | if (*e && *e != '\n') |
| 2813 | return -EINVAL; |
| 2814 | dev = MKDEV(major, minor); |
| 2815 | if (major != MAJOR(dev) || |
| 2816 | minor != MINOR(dev)) |
| 2817 | return -EOVERFLOW; |
| 2818 | |
| 2819 | |
| 2820 | if (mddev->persistent) { |
| 2821 | rdev = md_import_device(dev, mddev->major_version, |
| 2822 | mddev->minor_version); |
| 2823 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2824 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2825 | mdk_rdev_t, same_set); |
| 2826 | err = super_types[mddev->major_version] |
| 2827 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2828 | if (err < 0) |
| 2829 | goto out; |
| 2830 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2831 | } else if (mddev->external) |
| 2832 | rdev = md_import_device(dev, -2, -1); |
| 2833 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2834 | rdev = md_import_device(dev, -1, -1); |
| 2835 | |
| 2836 | if (IS_ERR(rdev)) |
| 2837 | return PTR_ERR(rdev); |
| 2838 | err = bind_rdev_to_array(rdev, mddev); |
| 2839 | out: |
| 2840 | if (err) |
| 2841 | export_rdev(rdev); |
| 2842 | return err ? err : len; |
| 2843 | } |
| 2844 | |
| 2845 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2846 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2847 | |
| 2848 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2849 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2850 | { |
| 2851 | char *end; |
| 2852 | unsigned long chunk, end_chunk; |
| 2853 | |
| 2854 | if (!mddev->bitmap) |
| 2855 | goto out; |
| 2856 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2857 | while (*buf) { |
| 2858 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2859 | if (buf == end) break; |
| 2860 | if (*end == '-') { /* range */ |
| 2861 | buf = end + 1; |
| 2862 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2863 | if (buf == end) break; |
| 2864 | } |
| 2865 | if (*end && !isspace(*end)) break; |
| 2866 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2867 | buf = end; |
| 2868 | while (isspace(*buf)) buf++; |
| 2869 | } |
| 2870 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2871 | out: |
| 2872 | return len; |
| 2873 | } |
| 2874 | |
| 2875 | static struct md_sysfs_entry md_bitmap = |
| 2876 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2877 | |
| 2878 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2879 | size_show(mddev_t *mddev, char *page) |
| 2880 | { |
| 2881 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2882 | } |
| 2883 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2884 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2885 | |
| 2886 | static ssize_t |
| 2887 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2888 | { |
| 2889 | /* If array is inactive, we can reduce the component size, but |
| 2890 | * not increase it (except from 0). |
| 2891 | * If array is active, we can try an on-line resize |
| 2892 | */ |
| 2893 | char *e; |
| 2894 | int err = 0; |
| 2895 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2896 | if (!*buf || *buf == '\n' || |
| 2897 | (*e && *e != '\n')) |
| 2898 | return -EINVAL; |
| 2899 | |
| 2900 | if (mddev->pers) { |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2901 | err = update_size(mddev, size * 2); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2902 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2903 | } else { |
| 2904 | if (mddev->size == 0 || |
| 2905 | mddev->size > size) |
| 2906 | mddev->size = size; |
| 2907 | else |
| 2908 | err = -ENOSPC; |
| 2909 | } |
| 2910 | return err ? err : len; |
| 2911 | } |
| 2912 | |
| 2913 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2914 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2915 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2916 | |
| 2917 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2918 | * This is one of |
| 2919 | * 'none' for arrays with no metadata (good luck...) |
| 2920 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2921 | * or N.M for internally known formats |
| 2922 | */ |
| 2923 | static ssize_t |
| 2924 | metadata_show(mddev_t *mddev, char *page) |
| 2925 | { |
| 2926 | if (mddev->persistent) |
| 2927 | return sprintf(page, "%d.%d\n", |
| 2928 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2929 | else if (mddev->external) |
| 2930 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2931 | else |
| 2932 | return sprintf(page, "none\n"); |
| 2933 | } |
| 2934 | |
| 2935 | static ssize_t |
| 2936 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2937 | { |
| 2938 | int major, minor; |
| 2939 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2940 | /* Changing the details of 'external' metadata is |
| 2941 | * always permitted. Otherwise there must be |
| 2942 | * no devices attached to the array. |
| 2943 | */ |
| 2944 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 2945 | ; |
| 2946 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2947 | return -EBUSY; |
| 2948 | |
| 2949 | if (cmd_match(buf, "none")) { |
| 2950 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2951 | mddev->external = 0; |
| 2952 | mddev->major_version = 0; |
| 2953 | mddev->minor_version = 90; |
| 2954 | return len; |
| 2955 | } |
| 2956 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2957 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2958 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2959 | namelen = sizeof(mddev->metadata_type)-1; |
| 2960 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2961 | mddev->metadata_type[namelen] = 0; |
| 2962 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2963 | mddev->metadata_type[--namelen] = 0; |
| 2964 | mddev->persistent = 0; |
| 2965 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2966 | mddev->major_version = 0; |
| 2967 | mddev->minor_version = 90; |
| 2968 | return len; |
| 2969 | } |
| 2970 | major = simple_strtoul(buf, &e, 10); |
| 2971 | if (e==buf || *e != '.') |
| 2972 | return -EINVAL; |
| 2973 | buf = e+1; |
| 2974 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2975 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2976 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2977 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2978 | return -ENOENT; |
| 2979 | mddev->major_version = major; |
| 2980 | mddev->minor_version = minor; |
| 2981 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2982 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2983 | return len; |
| 2984 | } |
| 2985 | |
| 2986 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2987 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2988 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2989 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2990 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2991 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2992 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2993 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2994 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2995 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2996 | type = "reshape"; |
| 2997 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2998 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2999 | type = "resync"; |
| 3000 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3001 | type = "check"; |
| 3002 | else |
| 3003 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3004 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3005 | type = "recover"; |
| 3006 | } |
| 3007 | return sprintf(page, "%s\n", type); |
| 3008 | } |
| 3009 | |
| 3010 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3011 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3012 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3013 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3014 | return -EINVAL; |
| 3015 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3016 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3017 | if (mddev->sync_thread) { |
| 3018 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3019 | md_unregister_thread(mddev->sync_thread); |
| 3020 | mddev->sync_thread = NULL; |
| 3021 | mddev->recovery = 0; |
| 3022 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3023 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3024 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3025 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3026 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3027 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3028 | else if (cmd_match(page, "recover")) { |
| 3029 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3030 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3031 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3032 | int err; |
| 3033 | if (mddev->pers->start_reshape == NULL) |
| 3034 | return -EINVAL; |
| 3035 | err = mddev->pers->start_reshape(mddev); |
| 3036 | if (err) |
| 3037 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3038 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3039 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3040 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3041 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3042 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3043 | return -EINVAL; |
| 3044 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3045 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3046 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3047 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3048 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 3049 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3050 | return len; |
| 3051 | } |
| 3052 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3053 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3054 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3055 | { |
| 3056 | return sprintf(page, "%llu\n", |
| 3057 | (unsigned long long) mddev->resync_mismatches); |
| 3058 | } |
| 3059 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3060 | static struct md_sysfs_entry md_scan_mode = |
| 3061 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3062 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3063 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3064 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3065 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3066 | static ssize_t |
| 3067 | sync_min_show(mddev_t *mddev, char *page) |
| 3068 | { |
| 3069 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3070 | mddev->sync_speed_min ? "local": "system"); |
| 3071 | } |
| 3072 | |
| 3073 | static ssize_t |
| 3074 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3075 | { |
| 3076 | int min; |
| 3077 | char *e; |
| 3078 | if (strncmp(buf, "system", 6)==0) { |
| 3079 | mddev->sync_speed_min = 0; |
| 3080 | return len; |
| 3081 | } |
| 3082 | min = simple_strtoul(buf, &e, 10); |
| 3083 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3084 | return -EINVAL; |
| 3085 | mddev->sync_speed_min = min; |
| 3086 | return len; |
| 3087 | } |
| 3088 | |
| 3089 | static struct md_sysfs_entry md_sync_min = |
| 3090 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3091 | |
| 3092 | static ssize_t |
| 3093 | sync_max_show(mddev_t *mddev, char *page) |
| 3094 | { |
| 3095 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3096 | mddev->sync_speed_max ? "local": "system"); |
| 3097 | } |
| 3098 | |
| 3099 | static ssize_t |
| 3100 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3101 | { |
| 3102 | int max; |
| 3103 | char *e; |
| 3104 | if (strncmp(buf, "system", 6)==0) { |
| 3105 | mddev->sync_speed_max = 0; |
| 3106 | return len; |
| 3107 | } |
| 3108 | max = simple_strtoul(buf, &e, 10); |
| 3109 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3110 | return -EINVAL; |
| 3111 | mddev->sync_speed_max = max; |
| 3112 | return len; |
| 3113 | } |
| 3114 | |
| 3115 | static struct md_sysfs_entry md_sync_max = |
| 3116 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3117 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3118 | static ssize_t |
| 3119 | degraded_show(mddev_t *mddev, char *page) |
| 3120 | { |
| 3121 | return sprintf(page, "%d\n", mddev->degraded); |
| 3122 | } |
| 3123 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3124 | |
| 3125 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3126 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3127 | { |
| 3128 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3129 | } |
| 3130 | |
| 3131 | static ssize_t |
| 3132 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3133 | { |
| 3134 | long n; |
| 3135 | |
| 3136 | if (strict_strtol(buf, 10, &n)) |
| 3137 | return -EINVAL; |
| 3138 | |
| 3139 | if (n != 0 && n != 1) |
| 3140 | return -EINVAL; |
| 3141 | |
| 3142 | mddev->parallel_resync = n; |
| 3143 | |
| 3144 | if (mddev->sync_thread) |
| 3145 | wake_up(&resync_wait); |
| 3146 | |
| 3147 | return len; |
| 3148 | } |
| 3149 | |
| 3150 | /* force parallel resync, even with shared block devices */ |
| 3151 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3152 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3153 | sync_force_parallel_show, sync_force_parallel_store); |
| 3154 | |
| 3155 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3156 | sync_speed_show(mddev_t *mddev, char *page) |
| 3157 | { |
| 3158 | unsigned long resync, dt, db; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3159 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3160 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3161 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3162 | db = resync - mddev->resync_mark_cnt; |
| 3163 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3164 | } |
| 3165 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3166 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3167 | |
| 3168 | static ssize_t |
| 3169 | sync_completed_show(mddev_t *mddev, char *page) |
| 3170 | { |
| 3171 | unsigned long max_blocks, resync; |
| 3172 | |
| 3173 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3174 | max_blocks = mddev->resync_max_sectors; |
| 3175 | else |
| 3176 | max_blocks = mddev->size << 1; |
| 3177 | |
| 3178 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3179 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3180 | } |
| 3181 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3182 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3183 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3184 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3185 | min_sync_show(mddev_t *mddev, char *page) |
| 3186 | { |
| 3187 | return sprintf(page, "%llu\n", |
| 3188 | (unsigned long long)mddev->resync_min); |
| 3189 | } |
| 3190 | static ssize_t |
| 3191 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3192 | { |
| 3193 | unsigned long long min; |
| 3194 | if (strict_strtoull(buf, 10, &min)) |
| 3195 | return -EINVAL; |
| 3196 | if (min > mddev->resync_max) |
| 3197 | return -EINVAL; |
| 3198 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3199 | return -EBUSY; |
| 3200 | |
| 3201 | /* Must be a multiple of chunk_size */ |
| 3202 | if (mddev->chunk_size) { |
| 3203 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3204 | return -EINVAL; |
| 3205 | } |
| 3206 | mddev->resync_min = min; |
| 3207 | |
| 3208 | return len; |
| 3209 | } |
| 3210 | |
| 3211 | static struct md_sysfs_entry md_min_sync = |
| 3212 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3213 | |
| 3214 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3215 | max_sync_show(mddev_t *mddev, char *page) |
| 3216 | { |
| 3217 | if (mddev->resync_max == MaxSector) |
| 3218 | return sprintf(page, "max\n"); |
| 3219 | else |
| 3220 | return sprintf(page, "%llu\n", |
| 3221 | (unsigned long long)mddev->resync_max); |
| 3222 | } |
| 3223 | static ssize_t |
| 3224 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3225 | { |
| 3226 | if (strncmp(buf, "max", 3) == 0) |
| 3227 | mddev->resync_max = MaxSector; |
| 3228 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3229 | unsigned long long max; |
| 3230 | if (strict_strtoull(buf, 10, &max)) |
| 3231 | return -EINVAL; |
| 3232 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3233 | return -EINVAL; |
| 3234 | if (max < mddev->resync_max && |
| 3235 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3236 | return -EBUSY; |
| 3237 | |
| 3238 | /* Must be a multiple of chunk_size */ |
| 3239 | if (mddev->chunk_size) { |
| 3240 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3241 | return -EINVAL; |
| 3242 | } |
| 3243 | mddev->resync_max = max; |
| 3244 | } |
| 3245 | wake_up(&mddev->recovery_wait); |
| 3246 | return len; |
| 3247 | } |
| 3248 | |
| 3249 | static struct md_sysfs_entry md_max_sync = |
| 3250 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3251 | |
| 3252 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3253 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3254 | { |
| 3255 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3256 | } |
| 3257 | |
| 3258 | static ssize_t |
| 3259 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3260 | { |
| 3261 | char *e; |
| 3262 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3263 | |
| 3264 | if (mddev->pers->quiesce == NULL) |
| 3265 | return -EINVAL; |
| 3266 | if (buf == e || (*e && *e != '\n')) |
| 3267 | return -EINVAL; |
| 3268 | if (new >= mddev->suspend_hi || |
| 3269 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3270 | mddev->suspend_lo = new; |
| 3271 | mddev->pers->quiesce(mddev, 2); |
| 3272 | return len; |
| 3273 | } else |
| 3274 | return -EINVAL; |
| 3275 | } |
| 3276 | static struct md_sysfs_entry md_suspend_lo = |
| 3277 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3278 | |
| 3279 | |
| 3280 | static ssize_t |
| 3281 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3282 | { |
| 3283 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3284 | } |
| 3285 | |
| 3286 | static ssize_t |
| 3287 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3288 | { |
| 3289 | char *e; |
| 3290 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3291 | |
| 3292 | if (mddev->pers->quiesce == NULL) |
| 3293 | return -EINVAL; |
| 3294 | if (buf == e || (*e && *e != '\n')) |
| 3295 | return -EINVAL; |
| 3296 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3297 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3298 | mddev->suspend_hi = new; |
| 3299 | mddev->pers->quiesce(mddev, 1); |
| 3300 | mddev->pers->quiesce(mddev, 0); |
| 3301 | return len; |
| 3302 | } else |
| 3303 | return -EINVAL; |
| 3304 | } |
| 3305 | static struct md_sysfs_entry md_suspend_hi = |
| 3306 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3307 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3308 | static ssize_t |
| 3309 | reshape_position_show(mddev_t *mddev, char *page) |
| 3310 | { |
| 3311 | if (mddev->reshape_position != MaxSector) |
| 3312 | return sprintf(page, "%llu\n", |
| 3313 | (unsigned long long)mddev->reshape_position); |
| 3314 | strcpy(page, "none\n"); |
| 3315 | return 5; |
| 3316 | } |
| 3317 | |
| 3318 | static ssize_t |
| 3319 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3320 | { |
| 3321 | char *e; |
| 3322 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3323 | if (mddev->pers) |
| 3324 | return -EBUSY; |
| 3325 | if (buf == e || (*e && *e != '\n')) |
| 3326 | return -EINVAL; |
| 3327 | mddev->reshape_position = new; |
| 3328 | mddev->delta_disks = 0; |
| 3329 | mddev->new_level = mddev->level; |
| 3330 | mddev->new_layout = mddev->layout; |
| 3331 | mddev->new_chunk = mddev->chunk_size; |
| 3332 | return len; |
| 3333 | } |
| 3334 | |
| 3335 | static struct md_sysfs_entry md_reshape_position = |
| 3336 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3337 | reshape_position_store); |
| 3338 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3339 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3340 | static struct attribute *md_default_attrs[] = { |
| 3341 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3342 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3343 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3344 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3345 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3346 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3347 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3348 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3349 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3350 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3351 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3352 | NULL, |
| 3353 | }; |
| 3354 | |
| 3355 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3356 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3357 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3358 | &md_sync_min.attr, |
| 3359 | &md_sync_max.attr, |
| 3360 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3361 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3362 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3363 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3364 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3365 | &md_suspend_lo.attr, |
| 3366 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3367 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3368 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3369 | NULL, |
| 3370 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3371 | static struct attribute_group md_redundancy_group = { |
| 3372 | .name = NULL, |
| 3373 | .attrs = md_redundancy_attrs, |
| 3374 | }; |
| 3375 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3376 | |
| 3377 | static ssize_t |
| 3378 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3379 | { |
| 3380 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3381 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3382 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3383 | |
| 3384 | if (!entry->show) |
| 3385 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3386 | rv = mddev_lock(mddev); |
| 3387 | if (!rv) { |
| 3388 | rv = entry->show(mddev, page); |
| 3389 | mddev_unlock(mddev); |
| 3390 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3391 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | static ssize_t |
| 3395 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3396 | const char *page, size_t length) |
| 3397 | { |
| 3398 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3399 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3400 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3401 | |
| 3402 | if (!entry->store) |
| 3403 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3404 | if (!capable(CAP_SYS_ADMIN)) |
| 3405 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3406 | rv = mddev_lock(mddev); |
| 3407 | if (!rv) { |
| 3408 | rv = entry->store(mddev, page, length); |
| 3409 | mddev_unlock(mddev); |
| 3410 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3411 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | static void md_free(struct kobject *ko) |
| 3415 | { |
| 3416 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3417 | kfree(mddev); |
| 3418 | } |
| 3419 | |
| 3420 | static struct sysfs_ops md_sysfs_ops = { |
| 3421 | .show = md_attr_show, |
| 3422 | .store = md_attr_store, |
| 3423 | }; |
| 3424 | static struct kobj_type md_ktype = { |
| 3425 | .release = md_free, |
| 3426 | .sysfs_ops = &md_sysfs_ops, |
| 3427 | .default_attrs = md_default_attrs, |
| 3428 | }; |
| 3429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | int mdp_major = 0; |
| 3431 | |
| 3432 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3433 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3434 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | mddev_t *mddev = mddev_find(dev); |
| 3436 | struct gendisk *disk; |
| 3437 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3438 | int shift = partitioned ? MdpMinorShift : 0; |
| 3439 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3440 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | |
| 3442 | if (!mddev) |
| 3443 | return NULL; |
| 3444 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3445 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3447 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | mddev_put(mddev); |
| 3449 | return NULL; |
| 3450 | } |
| 3451 | disk = alloc_disk(1 << shift); |
| 3452 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3453 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | mddev_put(mddev); |
| 3455 | return NULL; |
| 3456 | } |
| 3457 | disk->major = MAJOR(dev); |
| 3458 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3459 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3461 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | disk->fops = &md_fops; |
| 3464 | disk->private_data = mddev; |
| 3465 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3466 | /* Allow extended partitions. This makes the |
| 3467 | * 'mdp' device redundant, but we can really |
| 3468 | * remove it now. |
| 3469 | */ |
| 3470 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | add_disk(disk); |
| 3472 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3473 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3474 | &disk_to_dev(disk)->kobj, "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3475 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3476 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3477 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3478 | disk->disk_name); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3479 | else { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3480 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3481 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | return NULL; |
| 3484 | } |
| 3485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | static void md_safemode_timeout(unsigned long data) |
| 3487 | { |
| 3488 | mddev_t *mddev = (mddev_t *) data; |
| 3489 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3490 | if (!atomic_read(&mddev->writes_pending)) { |
| 3491 | mddev->safemode = 1; |
| 3492 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3493 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | md_wakeup_thread(mddev->thread); |
| 3496 | } |
| 3497 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3498 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | |
| 3500 | static int do_md_run(mddev_t * mddev) |
| 3501 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3502 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | int chunk_size; |
| 3504 | struct list_head *tmp; |
| 3505 | mdk_rdev_t *rdev; |
| 3506 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3507 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | char b[BDEVNAME_SIZE]; |
| 3509 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3510 | if (list_empty(&mddev->disks)) |
| 3511 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | |
| 3514 | if (mddev->pers) |
| 3515 | return -EBUSY; |
| 3516 | |
| 3517 | /* |
| 3518 | * Analyze all RAID superblock(s) |
| 3519 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3520 | if (!mddev->raid_disks) { |
| 3521 | if (!mddev->persistent) |
| 3522 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3523 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | |
| 3526 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3528 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3530 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3531 | chunk_size, MAX_CHUNK_SIZE); |
| 3532 | return -EINVAL; |
| 3533 | } |
| 3534 | /* |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3535 | * chunk-size has to be a power of 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | */ |
| 3537 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3538 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | return -EINVAL; |
| 3540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | |
| 3542 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3543 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3544 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | continue; |
| 3546 | if (rdev->size < chunk_size / 1024) { |
| 3547 | printk(KERN_WARNING |
| 3548 | "md: Dev %s smaller than chunk_size:" |
| 3549 | " %lluk < %dk\n", |
| 3550 | bdevname(rdev->bdev,b), |
| 3551 | (unsigned long long)rdev->size, |
| 3552 | chunk_size / 1024); |
| 3553 | return -EINVAL; |
| 3554 | } |
| 3555 | } |
| 3556 | } |
| 3557 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3558 | if (mddev->level != LEVEL_NONE) |
| 3559 | request_module("md-level-%d", mddev->level); |
| 3560 | else if (mddev->clevel[0]) |
| 3561 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | |
| 3563 | /* |
| 3564 | * Drop all container device buffers, from now on |
| 3565 | * the only valid external interface is through the md |
| 3566 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3568 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3569 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | continue; |
| 3571 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3572 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3573 | |
| 3574 | /* perform some consistency tests on the device. |
| 3575 | * We don't want the data to overlap the metadata, |
| 3576 | * Internal Bitmap issues has handled elsewhere. |
| 3577 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3578 | if (rdev->data_offset < rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3579 | if (mddev->size && |
| 3580 | rdev->data_offset + mddev->size*2 |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3581 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3582 | printk("md: %s: data overlaps metadata\n", |
| 3583 | mdname(mddev)); |
| 3584 | return -EINVAL; |
| 3585 | } |
| 3586 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3587 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3588 | > rdev->data_offset) { |
| 3589 | printk("md: %s: metadata overlaps data\n", |
| 3590 | mdname(mddev)); |
| 3591 | return -EINVAL; |
| 3592 | } |
| 3593 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 3594 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | md_probe(mddev->unit, NULL, NULL); |
| 3598 | disk = mddev->gendisk; |
| 3599 | if (!disk) |
| 3600 | return -ENOMEM; |
| 3601 | |
| 3602 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3603 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3604 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3606 | if (mddev->level != LEVEL_NONE) |
| 3607 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3608 | mddev->level); |
| 3609 | else |
| 3610 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3611 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | return -EINVAL; |
| 3613 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3614 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3616 | mddev->level = pers->level; |
| 3617 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3619 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3620 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3621 | /* This personality cannot handle reshaping... */ |
| 3622 | mddev->pers = NULL; |
| 3623 | module_put(pers->owner); |
| 3624 | return -EINVAL; |
| 3625 | } |
| 3626 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3627 | if (pers->sync_request) { |
| 3628 | /* Warn if this is a potentially silly |
| 3629 | * configuration. |
| 3630 | */ |
| 3631 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3632 | mdk_rdev_t *rdev2; |
| 3633 | struct list_head *tmp2; |
| 3634 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3635 | rdev_for_each(rdev, tmp, mddev) { |
| 3636 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3637 | if (rdev < rdev2 && |
| 3638 | rdev->bdev->bd_contains == |
| 3639 | rdev2->bdev->bd_contains) { |
| 3640 | printk(KERN_WARNING |
| 3641 | "%s: WARNING: %s appears to be" |
| 3642 | " on the same physical disk as" |
| 3643 | " %s.\n", |
| 3644 | mdname(mddev), |
| 3645 | bdevname(rdev->bdev,b), |
| 3646 | bdevname(rdev2->bdev,b2)); |
| 3647 | warned = 1; |
| 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | if (warned) |
| 3652 | printk(KERN_WARNING |
| 3653 | "True protection against single-disk" |
| 3654 | " failure might be compromised.\n"); |
| 3655 | } |
| 3656 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3657 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3659 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3660 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3662 | if (start_readonly) |
| 3663 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3664 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3665 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 3666 | if (err) |
| 3667 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3668 | else if (mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3669 | err = bitmap_create(mddev); |
| 3670 | if (err) { |
| 3671 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3672 | mdname(mddev), err); |
| 3673 | mddev->pers->stop(mddev); |
| 3674 | } |
| 3675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | module_put(mddev->pers->owner); |
| 3678 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3679 | bitmap_destroy(mddev); |
| 3680 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3682 | if (mddev->pers->sync_request) { |
| 3683 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3684 | printk(KERN_WARNING |
| 3685 | "md: cannot register extra attributes for %s\n", |
| 3686 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 3687 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3688 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3689 | mddev->ro = 0; |
| 3690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | atomic_set(&mddev->writes_pending,0); |
| 3692 | mddev->safemode = 0; |
| 3693 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3694 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3695 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3697 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3698 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3699 | if (rdev->raid_disk >= 0) { |
| 3700 | char nm[20]; |
| 3701 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3702 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3703 | printk("md: cannot register %s for %s\n", |
| 3704 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | |
| 3707 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3708 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3709 | if (mddev->flags) |
| 3710 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 3712 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
| 3714 | /* If we call blk_queue_make_request here, it will |
| 3715 | * re-initialise max_sectors etc which may have been |
| 3716 | * refined inside -> run. So just set the bits we need to set. |
| 3717 | * Most initialisation happended when we called |
| 3718 | * blk_queue_make_request(..., md_fail_request) |
| 3719 | * earlier. |
| 3720 | */ |
| 3721 | mddev->queue->queuedata = mddev; |
| 3722 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3723 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3724 | /* If there is a partially-recovered drive we need to |
| 3725 | * start recovery here. If we leave it to md_check_recovery, |
| 3726 | * it will remove the drives and not do the right thing |
| 3727 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3728 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3729 | struct list_head *rtmp; |
| 3730 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3731 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3732 | if (rdev->raid_disk >= 0 && |
| 3733 | !test_bit(In_sync, &rdev->flags) && |
| 3734 | !test_bit(Faulty, &rdev->flags)) |
| 3735 | /* complete an interrupted recovery */ |
| 3736 | spares++; |
| 3737 | if (spares && mddev->pers->sync_request) { |
| 3738 | mddev->recovery = 0; |
| 3739 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3740 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3741 | mddev, |
| 3742 | "%s_resync"); |
| 3743 | if (!mddev->sync_thread) { |
| 3744 | printk(KERN_ERR "%s: could not start resync" |
| 3745 | " thread...\n", |
| 3746 | mdname(mddev)); |
| 3747 | /* leave the spares where they are, it shouldn't hurt */ |
| 3748 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3749 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3750 | } |
| 3751 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3752 | md_wakeup_thread(mddev->thread); |
| 3753 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3754 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3755 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3756 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3757 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 3758 | if (mddev->sysfs_action) |
| 3759 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3760 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3761 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | return 0; |
| 3763 | } |
| 3764 | |
| 3765 | static int restart_array(mddev_t *mddev) |
| 3766 | { |
| 3767 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3769 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3771 | return -ENXIO; |
| 3772 | if (!mddev->pers) |
| 3773 | return -EINVAL; |
| 3774 | if (!mddev->ro) |
| 3775 | return -EBUSY; |
| 3776 | mddev->safemode = 0; |
| 3777 | mddev->ro = 0; |
| 3778 | set_disk_ro(disk, 0); |
| 3779 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3780 | mdname(mddev)); |
| 3781 | /* Kick recovery or resync if necessary */ |
| 3782 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3783 | md_wakeup_thread(mddev->thread); |
| 3784 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3785 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3786 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | } |
| 3788 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3789 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3790 | * to the file ourselves */ |
| 3791 | static int deny_bitmap_write_access(struct file * file) |
| 3792 | { |
| 3793 | struct inode *inode = file->f_mapping->host; |
| 3794 | |
| 3795 | spin_lock(&inode->i_lock); |
| 3796 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3797 | spin_unlock(&inode->i_lock); |
| 3798 | return -ETXTBSY; |
| 3799 | } |
| 3800 | atomic_set(&inode->i_writecount, -1); |
| 3801 | spin_unlock(&inode->i_lock); |
| 3802 | |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
| 3806 | static void restore_bitmap_write_access(struct file *file) |
| 3807 | { |
| 3808 | struct inode *inode = file->f_mapping->host; |
| 3809 | |
| 3810 | spin_lock(&inode->i_lock); |
| 3811 | atomic_set(&inode->i_writecount, 1); |
| 3812 | spin_unlock(&inode->i_lock); |
| 3813 | } |
| 3814 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3815 | /* mode: |
| 3816 | * 0 - completely stop and dis-assemble array |
| 3817 | * 1 - switch to readonly |
| 3818 | * 2 - stop but do not disassemble array |
| 3819 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3820 | 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] | 3821 | { |
| 3822 | int err = 0; |
| 3823 | struct gendisk *disk = mddev->gendisk; |
| 3824 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3825 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3826 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3827 | return -EBUSY; |
| 3828 | } |
| 3829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | |
| 3832 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3833 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3835 | md_unregister_thread(mddev->sync_thread); |
| 3836 | mddev->sync_thread = NULL; |
| 3837 | } |
| 3838 | |
| 3839 | del_timer_sync(&mddev->safemode_timer); |
| 3840 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3841 | switch(mode) { |
| 3842 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3844 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | goto out; |
| 3846 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3847 | break; |
| 3848 | case 0: /* disassemble */ |
| 3849 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3850 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3851 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | if (mddev->ro) |
| 3853 | set_disk_ro(disk, 0); |
| 3854 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3855 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3856 | mddev->queue->merge_bvec_fn = NULL; |
| 3857 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3858 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 3859 | if (mddev->pers->sync_request) { |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3860 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 3861 | if (mddev->sysfs_action) |
| 3862 | sysfs_put(mddev->sysfs_action); |
| 3863 | mddev->sysfs_action = NULL; |
| 3864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | module_put(mddev->pers->owner); |
| 3866 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3867 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3868 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3869 | |
| 3870 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3871 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | if (mddev->ro) |
| 3874 | mddev->ro = 0; |
| 3875 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3876 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | /* mark array as shutdown cleanly */ |
| 3878 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3879 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3881 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3883 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | /* |
| 3887 | * Free resources if final stop |
| 3888 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3889 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3890 | mdk_rdev_t *rdev; |
| 3891 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3894 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3895 | bitmap_destroy(mddev); |
| 3896 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3897 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3898 | fput(mddev->bitmap_file); |
| 3899 | mddev->bitmap_file = NULL; |
| 3900 | } |
| 3901 | mddev->bitmap_offset = 0; |
| 3902 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3903 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3904 | if (rdev->raid_disk >= 0) { |
| 3905 | char nm[20]; |
| 3906 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3907 | sysfs_remove_link(&mddev->kobj, nm); |
| 3908 | } |
| 3909 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3910 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3911 | flush_scheduled_work(); |
| 3912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | export_array(mddev); |
| 3914 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 3915 | mddev->array_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3916 | mddev->size = 0; |
| 3917 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3918 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3919 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3920 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3921 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3922 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3923 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3924 | mddev->level = LEVEL_NONE; |
| 3925 | mddev->clevel[0] = 0; |
| 3926 | mddev->flags = 0; |
| 3927 | mddev->ro = 0; |
| 3928 | mddev->metadata_type[0] = 0; |
| 3929 | mddev->chunk_size = 0; |
| 3930 | mddev->ctime = mddev->utime = 0; |
| 3931 | mddev->layout = 0; |
| 3932 | mddev->max_disks = 0; |
| 3933 | mddev->events = 0; |
| 3934 | mddev->delta_disks = 0; |
| 3935 | mddev->new_level = LEVEL_NONE; |
| 3936 | mddev->new_layout = 0; |
| 3937 | mddev->new_chunk = 0; |
| 3938 | mddev->curr_resync = 0; |
| 3939 | mddev->resync_mismatches = 0; |
| 3940 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3941 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3942 | mddev->recovery = 0; |
| 3943 | mddev->in_sync = 0; |
| 3944 | mddev->changed = 0; |
| 3945 | mddev->degraded = 0; |
| 3946 | mddev->barriers_work = 0; |
| 3947 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 3948 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3949 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3950 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3952 | mdname(mddev)); |
| 3953 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3954 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3955 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | out: |
| 3957 | return err; |
| 3958 | } |
| 3959 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3960 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | static void autorun_array(mddev_t *mddev) |
| 3962 | { |
| 3963 | mdk_rdev_t *rdev; |
| 3964 | struct list_head *tmp; |
| 3965 | int err; |
| 3966 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3967 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | |
| 3970 | printk(KERN_INFO "md: running: "); |
| 3971 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3972 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | char b[BDEVNAME_SIZE]; |
| 3974 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3975 | } |
| 3976 | printk("\n"); |
| 3977 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3978 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | if (err) { |
| 3980 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3981 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | /* |
| 3986 | * lets try to run arrays based on all disks that have arrived |
| 3987 | * until now. (those are in pending_raid_disks) |
| 3988 | * |
| 3989 | * the method: pick the first pending disk, collect all disks with |
| 3990 | * the same UUID, remove all from the pending list and put them into |
| 3991 | * the 'same_array' list. Then order this list based on superblock |
| 3992 | * update time (freshest comes first), kick out 'old' disks and |
| 3993 | * compare superblocks. If everything's fine then run it. |
| 3994 | * |
| 3995 | * If "unit" is allocated, then bump its reference count |
| 3996 | */ |
| 3997 | static void autorun_devices(int part) |
| 3998 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | struct list_head *tmp; |
| 4000 | mdk_rdev_t *rdev0, *rdev; |
| 4001 | mddev_t *mddev; |
| 4002 | char b[BDEVNAME_SIZE]; |
| 4003 | |
| 4004 | printk(KERN_INFO "md: autorun ...\n"); |
| 4005 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4006 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4008 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | rdev0 = list_entry(pending_raid_disks.next, |
| 4010 | mdk_rdev_t, same_set); |
| 4011 | |
| 4012 | printk(KERN_INFO "md: considering %s ...\n", |
| 4013 | bdevname(rdev0->bdev,b)); |
| 4014 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4015 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4017 | printk(KERN_INFO "md: adding %s ...\n", |
| 4018 | bdevname(rdev->bdev,b)); |
| 4019 | list_move(&rdev->same_set, &candidates); |
| 4020 | } |
| 4021 | /* |
| 4022 | * now we have a set of devices, with all of them having |
| 4023 | * mostly sane superblocks. It's time to allocate the |
| 4024 | * mddev. |
| 4025 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4026 | if (part) { |
| 4027 | dev = MKDEV(mdp_major, |
| 4028 | rdev0->preferred_minor << MdpMinorShift); |
| 4029 | unit = MINOR(dev) >> MdpMinorShift; |
| 4030 | } else { |
| 4031 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4032 | unit = MINOR(dev); |
| 4033 | } |
| 4034 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4036 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4037 | break; |
| 4038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | |
| 4040 | md_probe(dev, NULL, NULL); |
| 4041 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4042 | if (!mddev || !mddev->gendisk) { |
| 4043 | if (mddev) |
| 4044 | mddev_put(mddev); |
| 4045 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | "md: cannot allocate memory for md drive.\n"); |
| 4047 | break; |
| 4048 | } |
| 4049 | if (mddev_lock(mddev)) |
| 4050 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4051 | mdname(mddev)); |
| 4052 | else if (mddev->raid_disks || mddev->major_version |
| 4053 | || !list_empty(&mddev->disks)) { |
| 4054 | printk(KERN_WARNING |
| 4055 | "md: %s already running, cannot run %s\n", |
| 4056 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4057 | mddev_unlock(mddev); |
| 4058 | } else { |
| 4059 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4060 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4061 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | list_del_init(&rdev->same_set); |
| 4063 | if (bind_rdev_to_array(rdev, mddev)) |
| 4064 | export_rdev(rdev); |
| 4065 | } |
| 4066 | autorun_array(mddev); |
| 4067 | mddev_unlock(mddev); |
| 4068 | } |
| 4069 | /* on success, candidates will be empty, on error |
| 4070 | * it won't... |
| 4071 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4072 | rdev_for_each_list(rdev, tmp, candidates) { |
| 4073 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | mddev_put(mddev); |
| 4077 | } |
| 4078 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4079 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4080 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | static int get_version(void __user * arg) |
| 4083 | { |
| 4084 | mdu_version_t ver; |
| 4085 | |
| 4086 | ver.major = MD_MAJOR_VERSION; |
| 4087 | ver.minor = MD_MINOR_VERSION; |
| 4088 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4089 | |
| 4090 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4091 | return -EFAULT; |
| 4092 | |
| 4093 | return 0; |
| 4094 | } |
| 4095 | |
| 4096 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4097 | { |
| 4098 | mdu_array_info_t info; |
| 4099 | int nr,working,active,failed,spare; |
| 4100 | mdk_rdev_t *rdev; |
| 4101 | struct list_head *tmp; |
| 4102 | |
| 4103 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4104 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4106 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | failed++; |
| 4108 | else { |
| 4109 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4110 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | active++; |
| 4112 | else |
| 4113 | spare++; |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | info.major_version = mddev->major_version; |
| 4118 | info.minor_version = mddev->minor_version; |
| 4119 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4120 | info.ctime = mddev->ctime; |
| 4121 | info.level = mddev->level; |
| 4122 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4123 | if (info.size != mddev->size) /* overflow */ |
| 4124 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | info.nr_disks = nr; |
| 4126 | info.raid_disks = mddev->raid_disks; |
| 4127 | info.md_minor = mddev->md_minor; |
| 4128 | info.not_persistent= !mddev->persistent; |
| 4129 | |
| 4130 | info.utime = mddev->utime; |
| 4131 | info.state = 0; |
| 4132 | if (mddev->in_sync) |
| 4133 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4134 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4135 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | info.active_disks = active; |
| 4137 | info.working_disks = working; |
| 4138 | info.failed_disks = failed; |
| 4139 | info.spare_disks = spare; |
| 4140 | |
| 4141 | info.layout = mddev->layout; |
| 4142 | info.chunk_size = mddev->chunk_size; |
| 4143 | |
| 4144 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4145 | return -EFAULT; |
| 4146 | |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4150 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4151 | { |
| 4152 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4153 | char *ptr, *buf = NULL; |
| 4154 | int err = -ENOMEM; |
| 4155 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4156 | if (md_allow_write(mddev)) |
| 4157 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4158 | else |
| 4159 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4160 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4161 | if (!file) |
| 4162 | goto out; |
| 4163 | |
| 4164 | /* bitmap disabled, zero the first byte and copy out */ |
| 4165 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4166 | file->pathname[0] = '\0'; |
| 4167 | goto copy_out; |
| 4168 | } |
| 4169 | |
| 4170 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4171 | if (!buf) |
| 4172 | goto out; |
| 4173 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4174 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4175 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4176 | goto out; |
| 4177 | |
| 4178 | strcpy(file->pathname, ptr); |
| 4179 | |
| 4180 | copy_out: |
| 4181 | err = 0; |
| 4182 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4183 | err = -EFAULT; |
| 4184 | out: |
| 4185 | kfree(buf); |
| 4186 | kfree(file); |
| 4187 | return err; |
| 4188 | } |
| 4189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4191 | { |
| 4192 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | mdk_rdev_t *rdev; |
| 4194 | |
| 4195 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4196 | return -EFAULT; |
| 4197 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4198 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | if (rdev) { |
| 4200 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4201 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4202 | info.raid_disk = rdev->raid_disk; |
| 4203 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4204 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4206 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4208 | info.state |= (1<<MD_DISK_SYNC); |
| 4209 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4210 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4211 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | } else { |
| 4213 | info.major = info.minor = 0; |
| 4214 | info.raid_disk = -1; |
| 4215 | info.state = (1<<MD_DISK_REMOVED); |
| 4216 | } |
| 4217 | |
| 4218 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4219 | return -EFAULT; |
| 4220 | |
| 4221 | return 0; |
| 4222 | } |
| 4223 | |
| 4224 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4225 | { |
| 4226 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4227 | mdk_rdev_t *rdev; |
| 4228 | dev_t dev = MKDEV(info->major,info->minor); |
| 4229 | |
| 4230 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4231 | return -EOVERFLOW; |
| 4232 | |
| 4233 | if (!mddev->raid_disks) { |
| 4234 | int err; |
| 4235 | /* expecting a device which has a superblock */ |
| 4236 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4237 | if (IS_ERR(rdev)) { |
| 4238 | printk(KERN_WARNING |
| 4239 | "md: md_import_device returned %ld\n", |
| 4240 | PTR_ERR(rdev)); |
| 4241 | return PTR_ERR(rdev); |
| 4242 | } |
| 4243 | if (!list_empty(&mddev->disks)) { |
| 4244 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4245 | mdk_rdev_t, same_set); |
| 4246 | int err = super_types[mddev->major_version] |
| 4247 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4248 | if (err < 0) { |
| 4249 | printk(KERN_WARNING |
| 4250 | "md: %s has different UUID to %s\n", |
| 4251 | bdevname(rdev->bdev,b), |
| 4252 | bdevname(rdev0->bdev,b2)); |
| 4253 | export_rdev(rdev); |
| 4254 | return -EINVAL; |
| 4255 | } |
| 4256 | } |
| 4257 | err = bind_rdev_to_array(rdev, mddev); |
| 4258 | if (err) |
| 4259 | export_rdev(rdev); |
| 4260 | return err; |
| 4261 | } |
| 4262 | |
| 4263 | /* |
| 4264 | * add_new_disk can be used once the array is assembled |
| 4265 | * to add "hot spares". They must already have a superblock |
| 4266 | * written |
| 4267 | */ |
| 4268 | if (mddev->pers) { |
| 4269 | int err; |
| 4270 | if (!mddev->pers->hot_add_disk) { |
| 4271 | printk(KERN_WARNING |
| 4272 | "%s: personality does not support diskops!\n", |
| 4273 | mdname(mddev)); |
| 4274 | return -EINVAL; |
| 4275 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4276 | if (mddev->persistent) |
| 4277 | rdev = md_import_device(dev, mddev->major_version, |
| 4278 | mddev->minor_version); |
| 4279 | else |
| 4280 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | if (IS_ERR(rdev)) { |
| 4282 | printk(KERN_WARNING |
| 4283 | "md: md_import_device returned %ld\n", |
| 4284 | PTR_ERR(rdev)); |
| 4285 | return PTR_ERR(rdev); |
| 4286 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4287 | /* set save_raid_disk if appropriate */ |
| 4288 | if (!mddev->persistent) { |
| 4289 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4290 | info->raid_disk < mddev->raid_disks) |
| 4291 | rdev->raid_disk = info->raid_disk; |
| 4292 | else |
| 4293 | rdev->raid_disk = -1; |
| 4294 | } else |
| 4295 | super_types[mddev->major_version]. |
| 4296 | validate_super(mddev, rdev); |
| 4297 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4298 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4299 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4300 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4301 | set_bit(WriteMostly, &rdev->flags); |
| 4302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | rdev->raid_disk = -1; |
| 4304 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4305 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4306 | /* If there is hot_add_disk but no hot_remove_disk |
| 4307 | * then added disks for geometry changes, |
| 4308 | * and should be added immediately. |
| 4309 | */ |
| 4310 | super_types[mddev->major_version]. |
| 4311 | validate_super(mddev, rdev); |
| 4312 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4313 | if (err) |
| 4314 | unbind_rdev_from_array(rdev); |
| 4315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | if (err) |
| 4317 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4318 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4319 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4320 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4321 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4322 | if (mddev->degraded) |
| 4323 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4324 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4325 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | return err; |
| 4327 | } |
| 4328 | |
| 4329 | /* otherwise, add_new_disk is only allowed |
| 4330 | * for major_version==0 superblocks |
| 4331 | */ |
| 4332 | if (mddev->major_version != 0) { |
| 4333 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4334 | mdname(mddev)); |
| 4335 | return -EINVAL; |
| 4336 | } |
| 4337 | |
| 4338 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4339 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4340 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | if (IS_ERR(rdev)) { |
| 4342 | printk(KERN_WARNING |
| 4343 | "md: error, md_import_device() returned %ld\n", |
| 4344 | PTR_ERR(rdev)); |
| 4345 | return PTR_ERR(rdev); |
| 4346 | } |
| 4347 | rdev->desc_nr = info->number; |
| 4348 | if (info->raid_disk < mddev->raid_disks) |
| 4349 | rdev->raid_disk = info->raid_disk; |
| 4350 | else |
| 4351 | rdev->raid_disk = -1; |
| 4352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4354 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4355 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4357 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4358 | set_bit(WriteMostly, &rdev->flags); |
| 4359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | if (!mddev->persistent) { |
| 4361 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4362 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4364 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4365 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4367 | err = bind_rdev_to_array(rdev, mddev); |
| 4368 | if (err) { |
| 4369 | export_rdev(rdev); |
| 4370 | return err; |
| 4371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | return 0; |
| 4375 | } |
| 4376 | |
| 4377 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4378 | { |
| 4379 | char b[BDEVNAME_SIZE]; |
| 4380 | mdk_rdev_t *rdev; |
| 4381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | rdev = find_rdev(mddev, dev); |
| 4383 | if (!rdev) |
| 4384 | return -ENXIO; |
| 4385 | |
| 4386 | if (rdev->raid_disk >= 0) |
| 4387 | goto busy; |
| 4388 | |
| 4389 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4390 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4391 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | |
| 4393 | return 0; |
| 4394 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4395 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4397 | return -EBUSY; |
| 4398 | } |
| 4399 | |
| 4400 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4401 | { |
| 4402 | char b[BDEVNAME_SIZE]; |
| 4403 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | mdk_rdev_t *rdev; |
| 4405 | |
| 4406 | if (!mddev->pers) |
| 4407 | return -ENODEV; |
| 4408 | |
| 4409 | if (mddev->major_version != 0) { |
| 4410 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4411 | " version-0 superblocks.\n", |
| 4412 | mdname(mddev)); |
| 4413 | return -EINVAL; |
| 4414 | } |
| 4415 | if (!mddev->pers->hot_add_disk) { |
| 4416 | printk(KERN_WARNING |
| 4417 | "%s: personality does not support diskops!\n", |
| 4418 | mdname(mddev)); |
| 4419 | return -EINVAL; |
| 4420 | } |
| 4421 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4422 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | if (IS_ERR(rdev)) { |
| 4424 | printk(KERN_WARNING |
| 4425 | "md: error, md_import_device() returned %ld\n", |
| 4426 | PTR_ERR(rdev)); |
| 4427 | return -EINVAL; |
| 4428 | } |
| 4429 | |
| 4430 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4431 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4433 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4435 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4437 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | printk(KERN_WARNING |
| 4439 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4440 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4441 | err = -EINVAL; |
| 4442 | goto abort_export; |
| 4443 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4444 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4446 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4447 | err = bind_rdev_to_array(rdev, mddev); |
| 4448 | if (err) |
| 4449 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
| 4451 | /* |
| 4452 | * The rest should better be atomic, we can have disk failures |
| 4453 | * noticed in interrupt contexts ... |
| 4454 | */ |
| 4455 | |
| 4456 | if (rdev->desc_nr == mddev->max_disks) { |
| 4457 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4458 | mdname(mddev)); |
| 4459 | err = -EBUSY; |
| 4460 | goto abort_unbind_export; |
| 4461 | } |
| 4462 | |
| 4463 | rdev->raid_disk = -1; |
| 4464 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4465 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | |
| 4467 | /* |
| 4468 | * Kick recovery, maybe this spare has to be added to the |
| 4469 | * array immediately. |
| 4470 | */ |
| 4471 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4472 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4473 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | return 0; |
| 4475 | |
| 4476 | abort_unbind_export: |
| 4477 | unbind_rdev_from_array(rdev); |
| 4478 | |
| 4479 | abort_export: |
| 4480 | export_rdev(rdev); |
| 4481 | return err; |
| 4482 | } |
| 4483 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4484 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4485 | { |
| 4486 | int err; |
| 4487 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4488 | if (mddev->pers) { |
| 4489 | if (!mddev->pers->quiesce) |
| 4490 | return -EBUSY; |
| 4491 | if (mddev->recovery || mddev->sync_thread) |
| 4492 | return -EBUSY; |
| 4493 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4494 | } |
| 4495 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4496 | |
| 4497 | if (fd >= 0) { |
| 4498 | if (mddev->bitmap) |
| 4499 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4500 | mddev->bitmap_file = fget(fd); |
| 4501 | |
| 4502 | if (mddev->bitmap_file == NULL) { |
| 4503 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4504 | mdname(mddev)); |
| 4505 | return -EBADF; |
| 4506 | } |
| 4507 | |
| 4508 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4509 | if (err) { |
| 4510 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4511 | mdname(mddev)); |
| 4512 | fput(mddev->bitmap_file); |
| 4513 | mddev->bitmap_file = NULL; |
| 4514 | return err; |
| 4515 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4516 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4517 | } else if (mddev->bitmap == NULL) |
| 4518 | return -ENOENT; /* cannot remove what isn't there */ |
| 4519 | err = 0; |
| 4520 | if (mddev->pers) { |
| 4521 | mddev->pers->quiesce(mddev, 1); |
| 4522 | if (fd >= 0) |
| 4523 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4524 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4525 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4526 | fd = -1; /* make sure to put the file */ |
| 4527 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4528 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4529 | } |
| 4530 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4531 | if (mddev->bitmap_file) { |
| 4532 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4533 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4534 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4535 | mddev->bitmap_file = NULL; |
| 4536 | } |
| 4537 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4538 | return err; |
| 4539 | } |
| 4540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | /* |
| 4542 | * set_array_info is used two different ways |
| 4543 | * The original usage is when creating a new array. |
| 4544 | * In this usage, raid_disks is > 0 and it together with |
| 4545 | * level, size, not_persistent,layout,chunksize determine the |
| 4546 | * shape of the array. |
| 4547 | * This will always create an array with a type-0.90.0 superblock. |
| 4548 | * The newer usage is when assembling an array. |
| 4549 | * In this case raid_disks will be 0, and the major_version field is |
| 4550 | * use to determine which style super-blocks are to be found on the devices. |
| 4551 | * The minor and patch _version numbers are also kept incase the |
| 4552 | * super_block handler wishes to interpret them. |
| 4553 | */ |
| 4554 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4555 | { |
| 4556 | |
| 4557 | if (info->raid_disks == 0) { |
| 4558 | /* just setting version number for superblock loading */ |
| 4559 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4560 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | super_types[info->major_version].name == NULL) { |
| 4562 | /* maybe try to auto-load a module? */ |
| 4563 | printk(KERN_INFO |
| 4564 | "md: superblock version %d not known\n", |
| 4565 | info->major_version); |
| 4566 | return -EINVAL; |
| 4567 | } |
| 4568 | mddev->major_version = info->major_version; |
| 4569 | mddev->minor_version = info->minor_version; |
| 4570 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4571 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | return 0; |
| 4573 | } |
| 4574 | mddev->major_version = MD_MAJOR_VERSION; |
| 4575 | mddev->minor_version = MD_MINOR_VERSION; |
| 4576 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4577 | mddev->ctime = get_seconds(); |
| 4578 | |
| 4579 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4580 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | mddev->size = info->size; |
| 4582 | mddev->raid_disks = info->raid_disks; |
| 4583 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4584 | * openned |
| 4585 | */ |
| 4586 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4587 | mddev->recovery_cp = MaxSector; |
| 4588 | else |
| 4589 | mddev->recovery_cp = 0; |
| 4590 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4591 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | |
| 4593 | mddev->layout = info->layout; |
| 4594 | mddev->chunk_size = info->chunk_size; |
| 4595 | |
| 4596 | mddev->max_disks = MD_SB_DISKS; |
| 4597 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4598 | if (mddev->persistent) |
| 4599 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4600 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4602 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4603 | mddev->bitmap_offset = 0; |
| 4604 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4605 | mddev->reshape_position = MaxSector; |
| 4606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | /* |
| 4608 | * Generate a 128 bit UUID |
| 4609 | */ |
| 4610 | get_random_bytes(mddev->uuid, 16); |
| 4611 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4612 | mddev->new_level = mddev->level; |
| 4613 | mddev->new_chunk = mddev->chunk_size; |
| 4614 | mddev->new_layout = mddev->layout; |
| 4615 | mddev->delta_disks = 0; |
| 4616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | return 0; |
| 4618 | } |
| 4619 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4620 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4621 | { |
| 4622 | mdk_rdev_t * rdev; |
| 4623 | int rv; |
| 4624 | struct list_head *tmp; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4625 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4626 | |
| 4627 | if (mddev->pers->resize == NULL) |
| 4628 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4629 | /* The "num_sectors" is the number of sectors of each device that |
| 4630 | * is used. This can only make sense for arrays with redundancy. |
| 4631 | * linear and raid0 always use whatever space is available. We can only |
| 4632 | * consider changing this number if no resync or reconstruction is |
| 4633 | * 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] | 4634 | * 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] | 4635 | * of each device. If num_sectors is zero, we find the largest size |
| 4636 | * that fits. |
| 4637 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4638 | */ |
| 4639 | if (mddev->sync_thread) |
| 4640 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 4641 | if (mddev->bitmap) |
| 4642 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 4643 | * grow, and re-add. |
| 4644 | */ |
| 4645 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4646 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4647 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4648 | avail = rdev->size * 2; |
| 4649 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4650 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 4651 | num_sectors = avail; |
| 4652 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4653 | return -ENOSPC; |
| 4654 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4655 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4656 | if (!rv) { |
| 4657 | struct block_device *bdev; |
| 4658 | |
| 4659 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4660 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4661 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4662 | i_size_write(bdev->bd_inode, |
| 4663 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4664 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4665 | bdput(bdev); |
| 4666 | } |
| 4667 | } |
| 4668 | return rv; |
| 4669 | } |
| 4670 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4671 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4672 | { |
| 4673 | int rv; |
| 4674 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4675 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4676 | return -EINVAL; |
| 4677 | if (raid_disks <= 0 || |
| 4678 | raid_disks >= mddev->max_disks) |
| 4679 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4680 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4681 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4682 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4683 | |
| 4684 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4685 | return rv; |
| 4686 | } |
| 4687 | |
| 4688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | /* |
| 4690 | * update_array_info is used to change the configuration of an |
| 4691 | * on-line array. |
| 4692 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4693 | * fields in the info are checked against the array. |
| 4694 | * Any differences that cannot be handled will cause an error. |
| 4695 | * Normally, only one change can be managed at a time. |
| 4696 | */ |
| 4697 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4698 | { |
| 4699 | int rv = 0; |
| 4700 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4701 | int state = 0; |
| 4702 | |
| 4703 | /* calculate expected state,ignoring low bits */ |
| 4704 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4705 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | |
| 4707 | if (mddev->major_version != info->major_version || |
| 4708 | mddev->minor_version != info->minor_version || |
| 4709 | /* mddev->patch_version != info->patch_version || */ |
| 4710 | mddev->ctime != info->ctime || |
| 4711 | mddev->level != info->level || |
| 4712 | /* mddev->layout != info->layout || */ |
| 4713 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4714 | mddev->chunk_size != info->chunk_size || |
| 4715 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4716 | ((state^info->state) & 0xfffffe00) |
| 4717 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | return -EINVAL; |
| 4719 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4720 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4722 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4723 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | if (cnt == 0) return 0; |
| 4725 | if (cnt > 1) return -EINVAL; |
| 4726 | |
| 4727 | if (mddev->layout != info->layout) { |
| 4728 | /* Change layout |
| 4729 | * we don't need to do anything at the md level, the |
| 4730 | * personality will take care of it all. |
| 4731 | */ |
| 4732 | if (mddev->pers->reconfig == NULL) |
| 4733 | return -EINVAL; |
| 4734 | else |
| 4735 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4736 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4737 | if (info->size >= 0 && mddev->size != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4738 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4740 | if (mddev->raid_disks != info->raid_disks) |
| 4741 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4743 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4744 | if (mddev->pers->quiesce == NULL) |
| 4745 | return -EINVAL; |
| 4746 | if (mddev->recovery || mddev->sync_thread) |
| 4747 | return -EBUSY; |
| 4748 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4749 | /* add the bitmap */ |
| 4750 | if (mddev->bitmap) |
| 4751 | return -EEXIST; |
| 4752 | if (mddev->default_bitmap_offset == 0) |
| 4753 | return -EINVAL; |
| 4754 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4755 | mddev->pers->quiesce(mddev, 1); |
| 4756 | rv = bitmap_create(mddev); |
| 4757 | if (rv) |
| 4758 | bitmap_destroy(mddev); |
| 4759 | mddev->pers->quiesce(mddev, 0); |
| 4760 | } else { |
| 4761 | /* remove the bitmap */ |
| 4762 | if (!mddev->bitmap) |
| 4763 | return -ENOENT; |
| 4764 | if (mddev->bitmap->file) |
| 4765 | return -EINVAL; |
| 4766 | mddev->pers->quiesce(mddev, 1); |
| 4767 | bitmap_destroy(mddev); |
| 4768 | mddev->pers->quiesce(mddev, 0); |
| 4769 | mddev->bitmap_offset = 0; |
| 4770 | } |
| 4771 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4772 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | return rv; |
| 4774 | } |
| 4775 | |
| 4776 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4777 | { |
| 4778 | mdk_rdev_t *rdev; |
| 4779 | |
| 4780 | if (mddev->pers == NULL) |
| 4781 | return -ENODEV; |
| 4782 | |
| 4783 | rdev = find_rdev(mddev, dev); |
| 4784 | if (!rdev) |
| 4785 | return -ENODEV; |
| 4786 | |
| 4787 | md_error(mddev, rdev); |
| 4788 | return 0; |
| 4789 | } |
| 4790 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 4791 | /* |
| 4792 | * We have a problem here : there is no easy way to give a CHS |
| 4793 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4794 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4795 | * dosfs just mad... ;-) |
| 4796 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4797 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4798 | { |
| 4799 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4800 | |
| 4801 | geo->heads = 2; |
| 4802 | geo->sectors = 4; |
| 4803 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4804 | return 0; |
| 4805 | } |
| 4806 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 4807 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | unsigned int cmd, unsigned long arg) |
| 4809 | { |
| 4810 | int err = 0; |
| 4811 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | mddev_t *mddev = NULL; |
| 4813 | |
| 4814 | if (!capable(CAP_SYS_ADMIN)) |
| 4815 | return -EACCES; |
| 4816 | |
| 4817 | /* |
| 4818 | * Commands dealing with the RAID driver but not any |
| 4819 | * particular array: |
| 4820 | */ |
| 4821 | switch (cmd) |
| 4822 | { |
| 4823 | case RAID_VERSION: |
| 4824 | err = get_version(argp); |
| 4825 | goto done; |
| 4826 | |
| 4827 | case PRINT_RAID_DEBUG: |
| 4828 | err = 0; |
| 4829 | md_print_devices(); |
| 4830 | goto done; |
| 4831 | |
| 4832 | #ifndef MODULE |
| 4833 | case RAID_AUTORUN: |
| 4834 | err = 0; |
| 4835 | autostart_arrays(arg); |
| 4836 | goto done; |
| 4837 | #endif |
| 4838 | default:; |
| 4839 | } |
| 4840 | |
| 4841 | /* |
| 4842 | * Commands creating/starting a new array: |
| 4843 | */ |
| 4844 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 4845 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | |
| 4847 | if (!mddev) { |
| 4848 | BUG(); |
| 4849 | goto abort; |
| 4850 | } |
| 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | err = mddev_lock(mddev); |
| 4853 | if (err) { |
| 4854 | printk(KERN_INFO |
| 4855 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4856 | err, cmd); |
| 4857 | goto abort; |
| 4858 | } |
| 4859 | |
| 4860 | switch (cmd) |
| 4861 | { |
| 4862 | case SET_ARRAY_INFO: |
| 4863 | { |
| 4864 | mdu_array_info_t info; |
| 4865 | if (!arg) |
| 4866 | memset(&info, 0, sizeof(info)); |
| 4867 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4868 | err = -EFAULT; |
| 4869 | goto abort_unlock; |
| 4870 | } |
| 4871 | if (mddev->pers) { |
| 4872 | err = update_array_info(mddev, &info); |
| 4873 | if (err) { |
| 4874 | printk(KERN_WARNING "md: couldn't update" |
| 4875 | " array info. %d\n", err); |
| 4876 | goto abort_unlock; |
| 4877 | } |
| 4878 | goto done_unlock; |
| 4879 | } |
| 4880 | if (!list_empty(&mddev->disks)) { |
| 4881 | printk(KERN_WARNING |
| 4882 | "md: array %s already has disks!\n", |
| 4883 | mdname(mddev)); |
| 4884 | err = -EBUSY; |
| 4885 | goto abort_unlock; |
| 4886 | } |
| 4887 | if (mddev->raid_disks) { |
| 4888 | printk(KERN_WARNING |
| 4889 | "md: array %s already initialised!\n", |
| 4890 | mdname(mddev)); |
| 4891 | err = -EBUSY; |
| 4892 | goto abort_unlock; |
| 4893 | } |
| 4894 | err = set_array_info(mddev, &info); |
| 4895 | if (err) { |
| 4896 | printk(KERN_WARNING "md: couldn't set" |
| 4897 | " array info. %d\n", err); |
| 4898 | goto abort_unlock; |
| 4899 | } |
| 4900 | } |
| 4901 | goto done_unlock; |
| 4902 | |
| 4903 | default:; |
| 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * Commands querying/configuring an existing array: |
| 4908 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4909 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4910 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4911 | if ((!mddev->raid_disks && !mddev->external) |
| 4912 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4913 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4914 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | err = -ENODEV; |
| 4916 | goto abort_unlock; |
| 4917 | } |
| 4918 | |
| 4919 | /* |
| 4920 | * Commands even a read-only array can execute: |
| 4921 | */ |
| 4922 | switch (cmd) |
| 4923 | { |
| 4924 | case GET_ARRAY_INFO: |
| 4925 | err = get_array_info(mddev, argp); |
| 4926 | goto done_unlock; |
| 4927 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4928 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4929 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4930 | goto done_unlock; |
| 4931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | case GET_DISK_INFO: |
| 4933 | err = get_disk_info(mddev, argp); |
| 4934 | goto done_unlock; |
| 4935 | |
| 4936 | case RESTART_ARRAY_RW: |
| 4937 | err = restart_array(mddev); |
| 4938 | goto done_unlock; |
| 4939 | |
| 4940 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4941 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | goto done_unlock; |
| 4943 | |
| 4944 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4945 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | goto done_unlock; |
| 4947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | /* |
| 4951 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4952 | * superblock, so we do not allow them on read-only arrays. |
| 4953 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4954 | * here and hit the 'default' below, so only disallow |
| 4955 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 4957 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4958 | if (mddev->ro == 2) { |
| 4959 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4960 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4961 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4962 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4963 | } else { |
| 4964 | err = -EROFS; |
| 4965 | goto abort_unlock; |
| 4966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | switch (cmd) |
| 4970 | { |
| 4971 | case ADD_NEW_DISK: |
| 4972 | { |
| 4973 | mdu_disk_info_t info; |
| 4974 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4975 | err = -EFAULT; |
| 4976 | else |
| 4977 | err = add_new_disk(mddev, &info); |
| 4978 | goto done_unlock; |
| 4979 | } |
| 4980 | |
| 4981 | case HOT_REMOVE_DISK: |
| 4982 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4983 | goto done_unlock; |
| 4984 | |
| 4985 | case HOT_ADD_DISK: |
| 4986 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4987 | goto done_unlock; |
| 4988 | |
| 4989 | case SET_DISK_FAULTY: |
| 4990 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4991 | goto done_unlock; |
| 4992 | |
| 4993 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4994 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | goto done_unlock; |
| 4996 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4997 | case SET_BITMAP_FILE: |
| 4998 | err = set_bitmap_file(mddev, (int)arg); |
| 4999 | goto done_unlock; |
| 5000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | err = -EINVAL; |
| 5003 | goto abort_unlock; |
| 5004 | } |
| 5005 | |
| 5006 | done_unlock: |
| 5007 | abort_unlock: |
| 5008 | mddev_unlock(mddev); |
| 5009 | |
| 5010 | return err; |
| 5011 | done: |
| 5012 | if (err) |
| 5013 | MD_BUG(); |
| 5014 | abort: |
| 5015 | return err; |
| 5016 | } |
| 5017 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5018 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | { |
| 5020 | /* |
| 5021 | * Succeed if we can lock the mddev, which confirms that |
| 5022 | * it isn't being stopped right now. |
| 5023 | */ |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5024 | mddev_t *mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | int err; |
| 5026 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5027 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | goto out; |
| 5029 | |
| 5030 | err = 0; |
| 5031 | mddev_get(mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5032 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | mddev_unlock(mddev); |
| 5034 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5035 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | out: |
| 5037 | return err; |
| 5038 | } |
| 5039 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5040 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5042 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5044 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5045 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | mddev_put(mddev); |
| 5047 | |
| 5048 | return 0; |
| 5049 | } |
| 5050 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5051 | static int md_media_changed(struct gendisk *disk) |
| 5052 | { |
| 5053 | mddev_t *mddev = disk->private_data; |
| 5054 | |
| 5055 | return mddev->changed; |
| 5056 | } |
| 5057 | |
| 5058 | static int md_revalidate(struct gendisk *disk) |
| 5059 | { |
| 5060 | mddev_t *mddev = disk->private_data; |
| 5061 | |
| 5062 | mddev->changed = 0; |
| 5063 | return 0; |
| 5064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | static struct block_device_operations md_fops = |
| 5066 | { |
| 5067 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5068 | .open = md_open, |
| 5069 | .release = md_release, |
| 5070 | .locked_ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5071 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5072 | .media_changed = md_media_changed, |
| 5073 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | }; |
| 5075 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5076 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | { |
| 5078 | mdk_thread_t *thread = arg; |
| 5079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | /* |
| 5081 | * md_thread is a 'system-thread', it's priority should be very |
| 5082 | * high. We avoid resource deadlocks individually in each |
| 5083 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5084 | * the very same RT priority as kswapd, thus we will never get |
| 5085 | * into a priority inversion deadlock. |
| 5086 | * |
| 5087 | * we definitely have to have equal or higher priority than |
| 5088 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5089 | * many dirty RAID5 blocks. |
| 5090 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5092 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5093 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5095 | /* We need to wait INTERRUPTIBLE so that |
| 5096 | * we don't add to the load-average. |
| 5097 | * That means we need to be sure no signals are |
| 5098 | * pending |
| 5099 | */ |
| 5100 | if (signal_pending(current)) |
| 5101 | flush_signals(current); |
| 5102 | |
| 5103 | wait_event_interruptible_timeout |
| 5104 | (thread->wqueue, |
| 5105 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5106 | || kthread_should_stop(), |
| 5107 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | |
| 5109 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5110 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5111 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | return 0; |
| 5115 | } |
| 5116 | |
| 5117 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5118 | { |
| 5119 | if (thread) { |
| 5120 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5121 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5122 | wake_up(&thread->wqueue); |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5127 | const char *name) |
| 5128 | { |
| 5129 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5131 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | if (!thread) |
| 5133 | return NULL; |
| 5134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | init_waitqueue_head(&thread->wqueue); |
| 5136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | thread->run = run; |
| 5138 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5139 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5140 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5141 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | kfree(thread); |
| 5143 | return NULL; |
| 5144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | return thread; |
| 5146 | } |
| 5147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | void md_unregister_thread(mdk_thread_t *thread) |
| 5149 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5150 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5151 | |
| 5152 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | kfree(thread); |
| 5154 | } |
| 5155 | |
| 5156 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5157 | { |
| 5158 | if (!mddev) { |
| 5159 | MD_BUG(); |
| 5160 | return; |
| 5161 | } |
| 5162 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5163 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5165 | |
| 5166 | if (mddev->external) |
| 5167 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5168 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5170 | mdname(mddev), |
| 5171 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5172 | __builtin_return_address(0),__builtin_return_address(1), |
| 5173 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5174 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5175 | if (!mddev->pers) |
| 5176 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | if (!mddev->pers->error_handler) |
| 5178 | return; |
| 5179 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5180 | if (mddev->degraded) |
| 5181 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5182 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5184 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5185 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5186 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | /* seq_file implementation /proc/mdstat */ |
| 5190 | |
| 5191 | static void status_unused(struct seq_file *seq) |
| 5192 | { |
| 5193 | int i = 0; |
| 5194 | mdk_rdev_t *rdev; |
| 5195 | struct list_head *tmp; |
| 5196 | |
| 5197 | seq_printf(seq, "unused devices: "); |
| 5198 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5199 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | char b[BDEVNAME_SIZE]; |
| 5201 | i++; |
| 5202 | seq_printf(seq, "%s ", |
| 5203 | bdevname(rdev->bdev,b)); |
| 5204 | } |
| 5205 | if (!i) |
| 5206 | seq_printf(seq, "<none>"); |
| 5207 | |
| 5208 | seq_printf(seq, "\n"); |
| 5209 | } |
| 5210 | |
| 5211 | |
| 5212 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5213 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5214 | sector_t max_blocks, resync, res; |
| 5215 | unsigned long dt, db, rt; |
| 5216 | int scale; |
| 5217 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
| 5219 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5220 | |
| 5221 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5222 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5223 | else |
| 5224 | max_blocks = mddev->size; |
| 5225 | |
| 5226 | /* |
| 5227 | * Should not happen. |
| 5228 | */ |
| 5229 | if (!max_blocks) { |
| 5230 | MD_BUG(); |
| 5231 | return; |
| 5232 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5233 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5234 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5235 | * u32, as those are the requirements for sector_div. |
| 5236 | * Thus 'scale' must be at least 10 |
| 5237 | */ |
| 5238 | scale = 10; |
| 5239 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5240 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5241 | scale++; |
| 5242 | } |
| 5243 | res = (resync>>scale)*1000; |
| 5244 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5245 | |
| 5246 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5248 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | seq_printf(seq, "["); |
| 5250 | for (i = 0; i < x; i++) |
| 5251 | seq_printf(seq, "="); |
| 5252 | seq_printf(seq, ">"); |
| 5253 | for (i = 0; i < y; i++) |
| 5254 | seq_printf(seq, "."); |
| 5255 | seq_printf(seq, "] "); |
| 5256 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5257 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5258 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5259 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5260 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5261 | "check" : |
| 5262 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5263 | "resync" : "recovery"))), |
| 5264 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5265 | (unsigned long long) resync, |
| 5266 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | |
| 5268 | /* |
| 5269 | * We do not want to overflow, so the order of operands and |
| 5270 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5271 | * safe against division by zero. We only estimate anyway. |
| 5272 | * |
| 5273 | * dt: time from mark until now |
| 5274 | * db: blocks written from mark until now |
| 5275 | * rt: remaining time |
| 5276 | */ |
| 5277 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5278 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5279 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5280 | - mddev->resync_mark_cnt; |
| 5281 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | |
| 5283 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5284 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5285 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5289 | { |
| 5290 | struct list_head *tmp; |
| 5291 | loff_t l = *pos; |
| 5292 | mddev_t *mddev; |
| 5293 | |
| 5294 | if (l >= 0x10000) |
| 5295 | return NULL; |
| 5296 | if (!l--) |
| 5297 | /* header */ |
| 5298 | return (void*)1; |
| 5299 | |
| 5300 | spin_lock(&all_mddevs_lock); |
| 5301 | list_for_each(tmp,&all_mddevs) |
| 5302 | if (!l--) { |
| 5303 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5304 | mddev_get(mddev); |
| 5305 | spin_unlock(&all_mddevs_lock); |
| 5306 | return mddev; |
| 5307 | } |
| 5308 | spin_unlock(&all_mddevs_lock); |
| 5309 | if (!l--) |
| 5310 | return (void*)2;/* tail */ |
| 5311 | return NULL; |
| 5312 | } |
| 5313 | |
| 5314 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5315 | { |
| 5316 | struct list_head *tmp; |
| 5317 | mddev_t *next_mddev, *mddev = v; |
| 5318 | |
| 5319 | ++*pos; |
| 5320 | if (v == (void*)2) |
| 5321 | return NULL; |
| 5322 | |
| 5323 | spin_lock(&all_mddevs_lock); |
| 5324 | if (v == (void*)1) |
| 5325 | tmp = all_mddevs.next; |
| 5326 | else |
| 5327 | tmp = mddev->all_mddevs.next; |
| 5328 | if (tmp != &all_mddevs) |
| 5329 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5330 | else { |
| 5331 | next_mddev = (void*)2; |
| 5332 | *pos = 0x10000; |
| 5333 | } |
| 5334 | spin_unlock(&all_mddevs_lock); |
| 5335 | |
| 5336 | if (v != (void*)1) |
| 5337 | mddev_put(mddev); |
| 5338 | return next_mddev; |
| 5339 | |
| 5340 | } |
| 5341 | |
| 5342 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5343 | { |
| 5344 | mddev_t *mddev = v; |
| 5345 | |
| 5346 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5347 | mddev_put(mddev); |
| 5348 | } |
| 5349 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5350 | struct mdstat_info { |
| 5351 | int event; |
| 5352 | }; |
| 5353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5355 | { |
| 5356 | mddev_t *mddev = v; |
| 5357 | sector_t size; |
| 5358 | struct list_head *tmp2; |
| 5359 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5360 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5361 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | |
| 5363 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5364 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | seq_printf(seq, "Personalities : "); |
| 5366 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5367 | list_for_each_entry(pers, &pers_list, list) |
| 5368 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | |
| 5370 | spin_unlock(&pers_lock); |
| 5371 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5372 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | return 0; |
| 5374 | } |
| 5375 | if (v == (void*)2) { |
| 5376 | status_unused(seq); |
| 5377 | return 0; |
| 5378 | } |
| 5379 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5380 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5384 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5385 | mddev->pers ? "" : "in"); |
| 5386 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5387 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5389 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5390 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | seq_printf(seq, " %s", mddev->pers->name); |
| 5392 | } |
| 5393 | |
| 5394 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5395 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | char b[BDEVNAME_SIZE]; |
| 5397 | seq_printf(seq, " %s[%d]", |
| 5398 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5399 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5400 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5401 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | seq_printf(seq, "(F)"); |
| 5403 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5404 | } else if (rdev->raid_disk < 0) |
| 5405 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | size += rdev->size; |
| 5407 | } |
| 5408 | |
| 5409 | if (!list_empty(&mddev->disks)) { |
| 5410 | if (mddev->pers) |
| 5411 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5412 | (unsigned long long) |
| 5413 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | else |
| 5415 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5416 | (unsigned long long)size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5418 | if (mddev->persistent) { |
| 5419 | if (mddev->major_version != 0 || |
| 5420 | mddev->minor_version != 90) { |
| 5421 | seq_printf(seq," super %d.%d", |
| 5422 | mddev->major_version, |
| 5423 | mddev->minor_version); |
| 5424 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5425 | } else if (mddev->external) |
| 5426 | seq_printf(seq, " super external:%s", |
| 5427 | mddev->metadata_type); |
| 5428 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5429 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | |
| 5431 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5432 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5434 | if (mddev->pers->sync_request) { |
| 5435 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5436 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5437 | seq_printf(seq, "\n "); |
| 5438 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5439 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5440 | else if (mddev->recovery_cp < MaxSector) |
| 5441 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5442 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5443 | } else |
| 5444 | seq_printf(seq, "\n "); |
| 5445 | |
| 5446 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5447 | unsigned long chunk_kb; |
| 5448 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5449 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5450 | chunk_kb = bitmap->chunksize >> 10; |
| 5451 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5452 | "%lu%s chunk", |
| 5453 | bitmap->pages - bitmap->missing_pages, |
| 5454 | bitmap->pages, |
| 5455 | (bitmap->pages - bitmap->missing_pages) |
| 5456 | << (PAGE_SHIFT - 10), |
| 5457 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5458 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5459 | if (bitmap->file) { |
| 5460 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5461 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5462 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5463 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5464 | seq_printf(seq, "\n"); |
| 5465 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | seq_printf(seq, "\n"); |
| 5469 | } |
| 5470 | mddev_unlock(mddev); |
| 5471 | |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
| 5475 | static struct seq_operations md_seq_ops = { |
| 5476 | .start = md_seq_start, |
| 5477 | .next = md_seq_next, |
| 5478 | .stop = md_seq_stop, |
| 5479 | .show = md_seq_show, |
| 5480 | }; |
| 5481 | |
| 5482 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5483 | { |
| 5484 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5485 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5486 | if (mi == NULL) |
| 5487 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | |
| 5489 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5490 | if (error) |
| 5491 | kfree(mi); |
| 5492 | else { |
| 5493 | struct seq_file *p = file->private_data; |
| 5494 | p->private = mi; |
| 5495 | mi->event = atomic_read(&md_event_count); |
| 5496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | return error; |
| 5498 | } |
| 5499 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5500 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5501 | { |
| 5502 | struct seq_file *m = filp->private_data; |
| 5503 | struct mdstat_info *mi = m->private; |
| 5504 | int mask; |
| 5505 | |
| 5506 | poll_wait(filp, &md_event_waiters, wait); |
| 5507 | |
| 5508 | /* always allow read */ |
| 5509 | mask = POLLIN | POLLRDNORM; |
| 5510 | |
| 5511 | if (mi->event != atomic_read(&md_event_count)) |
| 5512 | mask |= POLLERR | POLLPRI; |
| 5513 | return mask; |
| 5514 | } |
| 5515 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5516 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5517 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | .open = md_seq_open, |
| 5519 | .read = seq_read, |
| 5520 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5521 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5522 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | }; |
| 5524 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5525 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5528 | list_add_tail(&p->list, &pers_list); |
| 5529 | 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] | 5530 | spin_unlock(&pers_lock); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5534 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5536 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5538 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | spin_unlock(&pers_lock); |
| 5540 | return 0; |
| 5541 | } |
| 5542 | |
| 5543 | static int is_mddev_idle(mddev_t *mddev) |
| 5544 | { |
| 5545 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5547 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | |
| 5549 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5550 | rcu_read_lock(); |
| 5551 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 5553 | curr_events = part_stat_read(&disk->part0, sectors[0]) + |
| 5554 | part_stat_read(&disk->part0, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5556 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5557 | * as sync_io is counted when a request starts, and |
| 5558 | * disk_stats is counted when it completes. |
| 5559 | * So resync activity will cause curr_events to be smaller than |
| 5560 | * when there was no such activity. |
| 5561 | * non-sync IO will cause disk_stat to increase without |
| 5562 | * increasing sync_io so curr_events will (eventually) |
| 5563 | * be larger than it was before. Once it becomes |
| 5564 | * substantially larger, the test below will cause |
| 5565 | * the array to appear non-idle, and resync will slow |
| 5566 | * down. |
| 5567 | * If there is a lot of outstanding resync activity when |
| 5568 | * we set last_event to curr_events, then all that activity |
| 5569 | * completing might cause the array to appear non-idle |
| 5570 | * and resync will be slowed down even though there might |
| 5571 | * not have been non-resync activity. This will only |
| 5572 | * happen once though. 'last_events' will soon reflect |
| 5573 | * the state where there is little or no outstanding |
| 5574 | * resync requests, and further resync activity will |
| 5575 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5576 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5578 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | rdev->last_events = curr_events; |
| 5580 | idle = 0; |
| 5581 | } |
| 5582 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5583 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | return idle; |
| 5585 | } |
| 5586 | |
| 5587 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5588 | { |
| 5589 | /* another "blocks" (512byte) blocks have been synced */ |
| 5590 | atomic_sub(blocks, &mddev->recovery_active); |
| 5591 | wake_up(&mddev->recovery_wait); |
| 5592 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5593 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | md_wakeup_thread(mddev->thread); |
| 5595 | // stop recovery, signal do_sync .... |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5600 | /* md_write_start(mddev, bi) |
| 5601 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5602 | * in superblock) before writing, schedule a superblock update |
| 5603 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5604 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5605 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5607 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5608 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5609 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5610 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5611 | BUG_ON(mddev->ro == 1); |
| 5612 | if (mddev->ro == 2) { |
| 5613 | /* need to switch to read/write */ |
| 5614 | mddev->ro = 0; |
| 5615 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5616 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5617 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5618 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5619 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5620 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5621 | if (mddev->safemode == 1) |
| 5622 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5623 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5624 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5625 | if (mddev->in_sync) { |
| 5626 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5627 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5628 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5629 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5630 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5631 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5632 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5633 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5634 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5635 | wait_event(mddev->sb_wait, |
| 5636 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5637 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | } |
| 5639 | |
| 5640 | void md_write_end(mddev_t *mddev) |
| 5641 | { |
| 5642 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5643 | if (mddev->safemode == 2) |
| 5644 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5645 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5647 | } |
| 5648 | } |
| 5649 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5650 | /* md_allow_write(mddev) |
| 5651 | * Calling this ensures that the array is marked 'active' so that writes |
| 5652 | * may proceed without blocking. It is important to call this before |
| 5653 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5654 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5655 | * |
| 5656 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5657 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5658 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5659 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5660 | { |
| 5661 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5662 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5663 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5664 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5665 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5666 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5667 | |
| 5668 | spin_lock_irq(&mddev->write_lock); |
| 5669 | if (mddev->in_sync) { |
| 5670 | mddev->in_sync = 0; |
| 5671 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5672 | if (mddev->safemode_delay && |
| 5673 | mddev->safemode == 0) |
| 5674 | mddev->safemode = 1; |
| 5675 | spin_unlock_irq(&mddev->write_lock); |
| 5676 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5677 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5678 | } else |
| 5679 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5680 | |
| 5681 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5682 | return -EAGAIN; |
| 5683 | else |
| 5684 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5685 | } |
| 5686 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | #define SYNC_MARKS 10 |
| 5689 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5690 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | { |
| 5692 | mddev_t *mddev2; |
| 5693 | unsigned int currspeed = 0, |
| 5694 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5695 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | unsigned long mark[SYNC_MARKS]; |
| 5697 | sector_t mark_cnt[SYNC_MARKS]; |
| 5698 | int last_mark,m; |
| 5699 | struct list_head *tmp; |
| 5700 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5701 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5702 | struct list_head *rtmp; |
| 5703 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5704 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | |
| 5706 | /* just incase thread restarts... */ |
| 5707 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5708 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5709 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5710 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5712 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5713 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5714 | desc = "data-check"; |
| 5715 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5716 | desc = "requested-resync"; |
| 5717 | else |
| 5718 | desc = "resync"; |
| 5719 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5720 | desc = "reshape"; |
| 5721 | else |
| 5722 | desc = "recovery"; |
| 5723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | /* we overload curr_resync somewhat here. |
| 5725 | * 0 == not engaged in resync at all |
| 5726 | * 2 == checking that there is no conflict with another sync |
| 5727 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5728 | * commense |
| 5729 | * other == active in resync - this many blocks |
| 5730 | * |
| 5731 | * Before starting a resync we must have set curr_resync to |
| 5732 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5733 | * less than ours. When we find one that is the same or higher |
| 5734 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5735 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5736 | * This will mean we have to start checking from the beginning again. |
| 5737 | * |
| 5738 | */ |
| 5739 | |
| 5740 | do { |
| 5741 | mddev->curr_resync = 2; |
| 5742 | |
| 5743 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5744 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5745 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | goto skip; |
| 5747 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5748 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | if (mddev2 == mddev) |
| 5750 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5751 | if (!mddev->parallel_resync |
| 5752 | && mddev2->curr_resync |
| 5753 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | DEFINE_WAIT(wq); |
| 5755 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5756 | /* arbitrarily yield */ |
| 5757 | mddev->curr_resync = 1; |
| 5758 | wake_up(&resync_wait); |
| 5759 | } |
| 5760 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5761 | /* no need to wait here, we can wait the next |
| 5762 | * time 'round when curr_resync == 2 |
| 5763 | */ |
| 5764 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5765 | /* We need to wait 'interruptible' so as not to |
| 5766 | * contribute to the load average, and not to |
| 5767 | * be caught by 'softlockup' |
| 5768 | */ |
| 5769 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5770 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5771 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5772 | printk(KERN_INFO "md: delaying %s of %s" |
| 5773 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5775 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5777 | if (signal_pending(current)) |
| 5778 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | schedule(); |
| 5780 | finish_wait(&resync_wait, &wq); |
| 5781 | goto try_again; |
| 5782 | } |
| 5783 | finish_wait(&resync_wait, &wq); |
| 5784 | } |
| 5785 | } |
| 5786 | } while (mddev->curr_resync < 2); |
| 5787 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5788 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5789 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5791 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | */ |
| 5793 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5794 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5795 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5796 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5797 | j = mddev->resync_min; |
| 5798 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5799 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5800 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5801 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5802 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5803 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | /* recovery follows the physical size of devices */ |
| 5805 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5806 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5807 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5808 | if (rdev->raid_disk >= 0 && |
| 5809 | !test_bit(Faulty, &rdev->flags) && |
| 5810 | !test_bit(In_sync, &rdev->flags) && |
| 5811 | rdev->recovery_offset < j) |
| 5812 | j = rdev->recovery_offset; |
| 5813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5815 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5816 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5817 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5818 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5819 | "(but not more than %d KB/sec) for %s.\n", |
| 5820 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | |
| 5822 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5823 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5824 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5826 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5827 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | } |
| 5829 | last_mark = 0; |
| 5830 | mddev->resync_mark = mark[last_mark]; |
| 5831 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5832 | |
| 5833 | /* |
| 5834 | * Tune reconstruction: |
| 5835 | */ |
| 5836 | window = 32*(PAGE_SIZE/512); |
| 5837 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5838 | window/2,(unsigned long long) max_sectors/2); |
| 5839 | |
| 5840 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | last_check = 0; |
| 5842 | |
| 5843 | if (j>2) { |
| 5844 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5845 | "md: resuming %s of %s from checkpoint.\n", |
| 5846 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | mddev->curr_resync = j; |
| 5848 | } |
| 5849 | |
| 5850 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5851 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5853 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5854 | if (j >= mddev->resync_max) { |
| 5855 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5856 | wait_event(mddev->recovery_wait, |
| 5857 | mddev->resync_max > j |
| 5858 | || kthread_should_stop()); |
| 5859 | } |
| 5860 | if (kthread_should_stop()) |
| 5861 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5862 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5863 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5864 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5865 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | goto out; |
| 5867 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5868 | |
| 5869 | if (!skipped) { /* actual IO requested */ |
| 5870 | io_sectors += sectors; |
| 5871 | atomic_add(sectors, &mddev->recovery_active); |
| 5872 | } |
| 5873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | j += sectors; |
| 5875 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5876 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5877 | if (last_check == 0) |
| 5878 | /* this is the earliers that rebuilt will be |
| 5879 | * visible in /proc/mdstat |
| 5880 | */ |
| 5881 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5882 | |
| 5883 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | continue; |
| 5885 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5886 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5888 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | break; |
| 5890 | |
| 5891 | repeat: |
| 5892 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5893 | /* step marks */ |
| 5894 | int next = (last_mark+1) % SYNC_MARKS; |
| 5895 | |
| 5896 | mddev->resync_mark = mark[next]; |
| 5897 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5898 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5899 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | last_mark = next; |
| 5901 | } |
| 5902 | |
| 5903 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5904 | if (kthread_should_stop()) |
| 5905 | goto interrupted; |
| 5906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | |
| 5908 | /* |
| 5909 | * this loop exits only if either when we are slower than |
| 5910 | * the 'hard' speed limit, or the system was IO-idle for |
| 5911 | * a jiffy. |
| 5912 | * the system might be non-idle CPU-wise, but we only care |
| 5913 | * about not overloading the IO subsystem. (things like an |
| 5914 | * e2fsck being done on the RAID array should execute fast) |
| 5915 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5916 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | cond_resched(); |
| 5918 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5919 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5920 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5922 | if (currspeed > speed_min(mddev)) { |
| 5923 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5925 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | goto repeat; |
| 5927 | } |
| 5928 | } |
| 5929 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5930 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | /* |
| 5932 | * this also signals 'finished resyncing' to md_stop |
| 5933 | */ |
| 5934 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5935 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | |
| 5937 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5938 | |
| 5939 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5940 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5942 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5943 | mddev->curr_resync > 2) { |
| 5944 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5945 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5946 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5947 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5948 | "md: checkpointing %s of %s.\n", |
| 5949 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5950 | mddev->recovery_cp = mddev->curr_resync; |
| 5951 | } |
| 5952 | } else |
| 5953 | mddev->recovery_cp = MaxSector; |
| 5954 | } else { |
| 5955 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5956 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5957 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5958 | if (rdev->raid_disk >= 0 && |
| 5959 | !test_bit(Faulty, &rdev->flags) && |
| 5960 | !test_bit(In_sync, &rdev->flags) && |
| 5961 | rdev->recovery_offset < mddev->curr_resync) |
| 5962 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5965 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | skip: |
| 5968 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5969 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5970 | mddev->resync_max = MaxSector; |
| 5971 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | wake_up(&resync_wait); |
| 5973 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5974 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5975 | return; |
| 5976 | |
| 5977 | interrupted: |
| 5978 | /* |
| 5979 | * got a signal, exit. |
| 5980 | */ |
| 5981 | printk(KERN_INFO |
| 5982 | "md: md_do_sync() got signal ... exiting\n"); |
| 5983 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5984 | goto out; |
| 5985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5987 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | |
| 5989 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5990 | static int remove_and_add_spares(mddev_t *mddev) |
| 5991 | { |
| 5992 | mdk_rdev_t *rdev; |
| 5993 | struct list_head *rtmp; |
| 5994 | int spares = 0; |
| 5995 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5996 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5997 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5998 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5999 | (test_bit(Faulty, &rdev->flags) || |
| 6000 | ! test_bit(In_sync, &rdev->flags)) && |
| 6001 | atomic_read(&rdev->nr_pending)==0) { |
| 6002 | if (mddev->pers->hot_remove_disk( |
| 6003 | mddev, rdev->raid_disk)==0) { |
| 6004 | char nm[20]; |
| 6005 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6006 | sysfs_remove_link(&mddev->kobj, nm); |
| 6007 | rdev->raid_disk = -1; |
| 6008 | } |
| 6009 | } |
| 6010 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6011 | if (mddev->degraded && ! mddev->ro) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6012 | rdev_for_each(rdev, rtmp, mddev) { |
| 6013 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6014 | !test_bit(In_sync, &rdev->flags) && |
| 6015 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6016 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6017 | if (rdev->raid_disk < 0 |
| 6018 | && !test_bit(Faulty, &rdev->flags)) { |
| 6019 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6020 | if (mddev->pers-> |
| 6021 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6022 | char nm[20]; |
| 6023 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6024 | if (sysfs_create_link(&mddev->kobj, |
| 6025 | &rdev->kobj, nm)) |
| 6026 | printk(KERN_WARNING |
| 6027 | "md: cannot register " |
| 6028 | "%s for %s\n", |
| 6029 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6030 | spares++; |
| 6031 | md_new_event(mddev); |
| 6032 | } else |
| 6033 | break; |
| 6034 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6035 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6036 | } |
| 6037 | return spares; |
| 6038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | /* |
| 6040 | * This routine is regularly called by all per-raid-array threads to |
| 6041 | * deal with generic issues like resync and super-block update. |
| 6042 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6043 | * need this as they never do any recovery or update the superblock. |
| 6044 | * |
| 6045 | * It does not do any resync itself, but rather "forks" off other threads |
| 6046 | * to do that as needed. |
| 6047 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6048 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6049 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | * and wakeups up this thread which will reap the thread and finish up. |
| 6051 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6052 | * |
| 6053 | * The overall approach is: |
| 6054 | * 1/ if the superblock needs updating, update it. |
| 6055 | * 2/ If a recovery thread is running, don't do anything else. |
| 6056 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6057 | * 4/ If there are any faulty devices, remove them. |
| 6058 | * 5/ If array is degraded, try to add spares devices |
| 6059 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6060 | */ |
| 6061 | void md_check_recovery(mddev_t *mddev) |
| 6062 | { |
| 6063 | mdk_rdev_t *rdev; |
| 6064 | struct list_head *rtmp; |
| 6065 | |
| 6066 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6067 | if (mddev->bitmap) |
| 6068 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | |
| 6070 | if (mddev->ro) |
| 6071 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6072 | |
| 6073 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6074 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6075 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6076 | mdname(mddev)); |
| 6077 | mddev->safemode = 2; |
| 6078 | } |
| 6079 | flush_signals(current); |
| 6080 | } |
| 6081 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6082 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6083 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6085 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6087 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6088 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6089 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6090 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | )) |
| 6092 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6093 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6094 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6095 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6096 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6097 | if (mddev->ro) { |
| 6098 | /* Only thing we do on a ro array is remove |
| 6099 | * failed devices. |
| 6100 | */ |
| 6101 | remove_and_add_spares(mddev); |
| 6102 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6103 | goto unlock; |
| 6104 | } |
| 6105 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6106 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6107 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6108 | spin_lock_irq(&mddev->write_lock); |
| 6109 | if (mddev->safemode && |
| 6110 | !atomic_read(&mddev->writes_pending) && |
| 6111 | !mddev->in_sync && |
| 6112 | mddev->recovery_cp == MaxSector) { |
| 6113 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6114 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6115 | if (mddev->persistent) |
| 6116 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6117 | } |
| 6118 | if (mddev->safemode == 1) |
| 6119 | mddev->safemode = 0; |
| 6120 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6121 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6122 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6123 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6124 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6125 | if (mddev->flags) |
| 6126 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6127 | |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6128 | rdev_for_each(rdev, rtmp, mddev) |
| 6129 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6130 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6131 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6134 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6135 | /* resync/recovery still happening */ |
| 6136 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6137 | goto unlock; |
| 6138 | } |
| 6139 | if (mddev->sync_thread) { |
| 6140 | /* resync has finished, collect result */ |
| 6141 | md_unregister_thread(mddev->sync_thread); |
| 6142 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6143 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6144 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | /* success...*/ |
| 6146 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6147 | if (mddev->pers->spare_active(mddev)) |
| 6148 | sysfs_notify(&mddev->kobj, NULL, |
| 6149 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6151 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6152 | |
| 6153 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6154 | * information must be scrapped |
| 6155 | */ |
| 6156 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6157 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6158 | rdev->saved_raid_disk = -1; |
| 6159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | mddev->recovery = 0; |
| 6161 | /* flag recovery needed just to double check */ |
| 6162 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 6163 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6164 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | goto unlock; |
| 6166 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6167 | /* Set RUNNING before clearing NEEDED to avoid |
| 6168 | * any transients in the value of "sync_action". |
| 6169 | */ |
| 6170 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6171 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6172 | /* Clear some bits that don't mean anything, but |
| 6173 | * might be left set |
| 6174 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6175 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6176 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6177 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6178 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6179 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | /* no recovery is running. |
| 6181 | * remove any failed drives, then |
| 6182 | * add spares if possible. |
| 6183 | * Spare are also removed and re-added, to allow |
| 6184 | * the personality to fail the re-add. |
| 6185 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6187 | if (mddev->reshape_position != MaxSector) { |
| 6188 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6189 | /* Cannot proceed */ |
| 6190 | goto unlock; |
| 6191 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6192 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6193 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6194 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6195 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6196 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6197 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6198 | } else if (mddev->recovery_cp < MaxSector) { |
| 6199 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6200 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6201 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6202 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6206 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6207 | /* We are adding a device or devices to an array |
| 6208 | * which has the bitmap stored on all devices. |
| 6209 | * So make sure all bitmap pages get written |
| 6210 | */ |
| 6211 | bitmap_write_all(mddev->bitmap); |
| 6212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6214 | mddev, |
| 6215 | "%s_resync"); |
| 6216 | if (!mddev->sync_thread) { |
| 6217 | printk(KERN_ERR "%s: could not start resync" |
| 6218 | " thread...\n", |
| 6219 | mdname(mddev)); |
| 6220 | /* leave the spares where they are, it shouldn't hurt */ |
| 6221 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6222 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 6224 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6225 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | } |
| 6227 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6228 | if (!mddev->sync_thread) { |
| 6229 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6230 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6231 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame^] | 6232 | if (mddev->sysfs_action) |
| 6233 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | mddev_unlock(mddev); |
| 6236 | } |
| 6237 | } |
| 6238 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6239 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6240 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6241 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6242 | wait_event_timeout(rdev->blocked_wait, |
| 6243 | !test_bit(Blocked, &rdev->flags), |
| 6244 | msecs_to_jiffies(5000)); |
| 6245 | rdev_dec_pending(rdev, mddev); |
| 6246 | } |
| 6247 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6248 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6249 | static int md_notify_reboot(struct notifier_block *this, |
| 6250 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | { |
| 6252 | struct list_head *tmp; |
| 6253 | mddev_t *mddev; |
| 6254 | |
| 6255 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6256 | |
| 6257 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6258 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6259 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6260 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6261 | /* Force a switch to readonly even array |
| 6262 | * appears to still be in use. Hence |
| 6263 | * the '100'. |
| 6264 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6265 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6266 | mddev_unlock(mddev); |
| 6267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | /* |
| 6269 | * certain more exotic SCSI devices are known to be |
| 6270 | * volatile wrt too early system reboots. While the |
| 6271 | * right place to handle this issue is the given |
| 6272 | * driver, we do want to have a safe RAID driver ... |
| 6273 | */ |
| 6274 | mdelay(1000*1); |
| 6275 | } |
| 6276 | return NOTIFY_DONE; |
| 6277 | } |
| 6278 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6279 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | .notifier_call = md_notify_reboot, |
| 6281 | .next = NULL, |
| 6282 | .priority = INT_MAX, /* before any real devices */ |
| 6283 | }; |
| 6284 | |
| 6285 | static void md_geninit(void) |
| 6286 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6288 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6289 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | } |
| 6291 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6292 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | if (register_blkdev(MAJOR_NR, "md")) |
| 6295 | return -1; |
| 6296 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6297 | unregister_blkdev(MAJOR_NR, "md"); |
| 6298 | return -1; |
| 6299 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6300 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6301 | md_probe, NULL, NULL); |
| 6302 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6303 | md_probe, NULL, NULL); |
| 6304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6306 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | |
| 6308 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | |
| 6313 | #ifndef MODULE |
| 6314 | |
| 6315 | /* |
| 6316 | * Searches all registered partitions for autorun RAID arrays |
| 6317 | * at boot time. |
| 6318 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6319 | |
| 6320 | static LIST_HEAD(all_detected_devices); |
| 6321 | struct detected_devices_node { |
| 6322 | struct list_head list; |
| 6323 | dev_t dev; |
| 6324 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | |
| 6326 | void md_autodetect_dev(dev_t dev) |
| 6327 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6328 | struct detected_devices_node *node_detected_dev; |
| 6329 | |
| 6330 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6331 | if (node_detected_dev) { |
| 6332 | node_detected_dev->dev = dev; |
| 6333 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6334 | } else { |
| 6335 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6336 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | } |
| 6339 | |
| 6340 | |
| 6341 | static void autostart_arrays(int part) |
| 6342 | { |
| 6343 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6344 | struct detected_devices_node *node_detected_dev; |
| 6345 | dev_t dev; |
| 6346 | int i_scanned, i_passed; |
| 6347 | |
| 6348 | i_scanned = 0; |
| 6349 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | |
| 6351 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6352 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6353 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6354 | i_scanned++; |
| 6355 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6356 | struct detected_devices_node, list); |
| 6357 | list_del(&node_detected_dev->list); |
| 6358 | dev = node_detected_dev->dev; |
| 6359 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6360 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | if (IS_ERR(rdev)) |
| 6362 | continue; |
| 6363 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6364 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | MD_BUG(); |
| 6366 | continue; |
| 6367 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6368 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6370 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6372 | |
| 6373 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6374 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
| 6376 | autorun_devices(part); |
| 6377 | } |
| 6378 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6379 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | |
| 6381 | static __exit void md_exit(void) |
| 6382 | { |
| 6383 | mddev_t *mddev; |
| 6384 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6385 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6386 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6387 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | |
| 6389 | unregister_blkdev(MAJOR_NR,"md"); |
| 6390 | unregister_blkdev(mdp_major, "mdp"); |
| 6391 | unregister_reboot_notifier(&md_notifier); |
| 6392 | unregister_sysctl_table(raid_table_header); |
| 6393 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6394 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | struct gendisk *disk = mddev->gendisk; |
| 6396 | if (!disk) |
| 6397 | continue; |
| 6398 | export_array(mddev); |
| 6399 | del_gendisk(disk); |
| 6400 | put_disk(disk); |
| 6401 | mddev->gendisk = NULL; |
| 6402 | mddev_put(mddev); |
| 6403 | } |
| 6404 | } |
| 6405 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6406 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | module_exit(md_exit) |
| 6408 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6409 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6410 | { |
| 6411 | return sprintf(buffer, "%d", start_readonly); |
| 6412 | } |
| 6413 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6414 | { |
| 6415 | char *e; |
| 6416 | int num = simple_strtoul(val, &e, 10); |
| 6417 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6418 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6419 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6420 | } |
| 6421 | return -EINVAL; |
| 6422 | } |
| 6423 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6424 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6425 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6426 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | EXPORT_SYMBOL(register_md_personality); |
| 6429 | EXPORT_SYMBOL(unregister_md_personality); |
| 6430 | EXPORT_SYMBOL(md_error); |
| 6431 | EXPORT_SYMBOL(md_done_sync); |
| 6432 | EXPORT_SYMBOL(md_write_start); |
| 6433 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | EXPORT_SYMBOL(md_register_thread); |
| 6435 | EXPORT_SYMBOL(md_unregister_thread); |
| 6436 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | EXPORT_SYMBOL(md_check_recovery); |
| 6438 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6439 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6440 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |