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 | |
| 35 | #include <linux/module.h> |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 37 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/linkage.h> |
| 39 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 43 | #include <linux/poll.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 44 | #include <linux/mutex.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 45 | #include <linux/ctype.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 46 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <linux/init.h> |
| 49 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 50 | #include <linux/file.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_KMOD |
| 53 | #include <linux/kmod.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <asm/unaligned.h> |
| 57 | |
| 58 | #define MAJOR_NR MD_MAJOR |
| 59 | #define MD_DRIVER |
| 60 | |
| 61 | /* 63 partitions with the alternate major number (mdp) */ |
| 62 | #define MdpMinorShift 6 |
| 63 | |
| 64 | #define DEBUG 0 |
| 65 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 66 | |
| 67 | |
| 68 | #ifndef MODULE |
| 69 | static void autostart_arrays (int part); |
| 70 | #endif |
| 71 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 72 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(pers_lock); |
| 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | static void md_print_devices(void); |
| 76 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 77 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 78 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 79 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * 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] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
| 112 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 113 | .procname = "speed_limit_min", |
| 114 | .data = &sysctl_speed_limit_min, |
| 115 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 116 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .proc_handler = &proc_dointvec, |
| 118 | }, |
| 119 | { |
| 120 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .proc_handler = &proc_dointvec, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
| 132 | .ctl_name = DEV_RAID, |
| 133 | .procname = "raid", |
| 134 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 135 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .child = raid_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static ctl_table raid_root_table[] = { |
| 142 | { |
| 143 | .ctl_name = CTL_DEV, |
| 144 | .procname = "dev", |
| 145 | .maxlen = 0, |
| 146 | .mode = 0555, |
| 147 | .child = raid_dir_table, |
| 148 | }, |
| 149 | { .ctl_name = 0 } |
| 150 | }; |
| 151 | |
| 152 | static struct block_device_operations md_fops; |
| 153 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 154 | static int start_readonly; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | * We have a system wide 'event count' that is incremented |
| 158 | * on any 'interesting' event, and readers of /proc/mdstat |
| 159 | * can use 'poll' or 'select' to find out when the event |
| 160 | * count increases. |
| 161 | * |
| 162 | * Events are: |
| 163 | * start array, stop array, error, add device, remove device, |
| 164 | * start build, activate spare |
| 165 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 166 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 168 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
| 172 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 174 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 175 | /* Alternate version that can be called from interrupts |
| 176 | * when calling sysfs_notify isn't needed. |
| 177 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 178 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 179 | { |
| 180 | atomic_inc(&md_event_count); |
| 181 | wake_up(&md_event_waiters); |
| 182 | } |
| 183 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 184 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * Enables to iterate over all existing md arrays |
| 186 | * all_mddevs_lock protects this list. |
| 187 | */ |
| 188 | static LIST_HEAD(all_mddevs); |
| 189 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | * iterates through all used mddevs in the system. |
| 194 | * We take care to grab the all_mddevs_lock whenever navigating |
| 195 | * the list, and to always hold a refcount when unlocked. |
| 196 | * Any code which breaks out of this loop while own |
| 197 | * a reference to the current mddev and must mddev_put it. |
| 198 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 199 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | \ |
| 201 | for (({ spin_lock(&all_mddevs_lock); \ |
| 202 | tmp = all_mddevs.next; \ |
| 203 | mddev = NULL;}); \ |
| 204 | ({ if (tmp != &all_mddevs) \ |
| 205 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 206 | spin_unlock(&all_mddevs_lock); \ |
| 207 | if (mddev) mddev_put(mddev); \ |
| 208 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 209 | tmp != &all_mddevs;}); \ |
| 210 | ({ spin_lock(&all_mddevs_lock); \ |
| 211 | tmp = tmp->next;}) \ |
| 212 | ) |
| 213 | |
| 214 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 215 | static int md_fail_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 217 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 222 | { |
| 223 | atomic_inc(&mddev->active); |
| 224 | return mddev; |
| 225 | } |
| 226 | |
| 227 | static void mddev_put(mddev_t *mddev) |
| 228 | { |
| 229 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 230 | return; |
| 231 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 232 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 233 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 234 | blk_cleanup_queue(mddev->queue); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 235 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 236 | } else |
| 237 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static mddev_t * mddev_find(dev_t unit) |
| 241 | { |
| 242 | mddev_t *mddev, *new = NULL; |
| 243 | |
| 244 | retry: |
| 245 | spin_lock(&all_mddevs_lock); |
| 246 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 247 | if (mddev->unit == unit) { |
| 248 | mddev_get(mddev); |
| 249 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 250 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return mddev; |
| 252 | } |
| 253 | |
| 254 | if (new) { |
| 255 | list_add(&new->all_mddevs, &all_mddevs); |
| 256 | spin_unlock(&all_mddevs_lock); |
| 257 | return new; |
| 258 | } |
| 259 | spin_unlock(&all_mddevs_lock); |
| 260 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 261 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (!new) |
| 263 | return NULL; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | new->unit = unit; |
| 266 | if (MAJOR(unit) == MD_MAJOR) |
| 267 | new->md_minor = MINOR(unit); |
| 268 | else |
| 269 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 270 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 271 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | INIT_LIST_HEAD(&new->disks); |
| 273 | INIT_LIST_HEAD(&new->all_mddevs); |
| 274 | init_timer(&new->safemode_timer); |
| 275 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 276 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 277 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 278 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 279 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 280 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 281 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 282 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 285 | if (!new->queue) { |
| 286 | kfree(new); |
| 287 | return NULL; |
| 288 | } |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 289 | /* Can be unlocked because the queue is new: no concurrency */ |
| 290 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | blk_queue_make_request(new->queue, md_fail_request); |
| 293 | |
| 294 | goto retry; |
| 295 | } |
| 296 | |
| 297 | static inline int mddev_lock(mddev_t * mddev) |
| 298 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 299 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | static inline int mddev_trylock(mddev_t * mddev) |
| 303 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 304 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static inline void mddev_unlock(mddev_t * mddev) |
| 308 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 309 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 311 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 314 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | mdk_rdev_t * rdev; |
| 317 | struct list_head *tmp; |
| 318 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 319 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (rdev->desc_nr == nr) |
| 321 | return rdev; |
| 322 | } |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 327 | { |
| 328 | struct list_head *tmp; |
| 329 | mdk_rdev_t *rdev; |
| 330 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 331 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (rdev->bdev->bd_dev == dev) |
| 333 | return rdev; |
| 334 | } |
| 335 | return NULL; |
| 336 | } |
| 337 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 338 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 339 | { |
| 340 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 341 | list_for_each_entry(pers, &pers_list, list) { |
| 342 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 343 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 344 | if (strcmp(pers->name, clevel)==0) |
| 345 | return pers; |
| 346 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 347 | return NULL; |
| 348 | } |
| 349 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 350 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
| 352 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 353 | return MD_NEW_SIZE_BLOCKS(size); |
| 354 | } |
| 355 | |
| 356 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 357 | { |
| 358 | sector_t size; |
| 359 | |
| 360 | size = rdev->sb_offset; |
| 361 | |
| 362 | if (chunk_size) |
| 363 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 364 | return size; |
| 365 | } |
| 366 | |
| 367 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 368 | { |
| 369 | if (rdev->sb_page) |
| 370 | MD_BUG(); |
| 371 | |
| 372 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 373 | if (!rdev->sb_page) { |
| 374 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 375 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 382 | { |
| 383 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 384 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | rdev->sb_loaded = 0; |
| 386 | rdev->sb_page = NULL; |
| 387 | rdev->sb_offset = 0; |
| 388 | rdev->size = 0; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 393 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 394 | { |
| 395 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 396 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 397 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 398 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 399 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 400 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 401 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 402 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 403 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 404 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 405 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 406 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 407 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 408 | } |
| 409 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 410 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 411 | { |
| 412 | struct bio *bio2 = bio->bi_private; |
| 413 | mdk_rdev_t *rdev = bio2->bi_private; |
| 414 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 415 | |
| 416 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 417 | error == -EOPNOTSUPP) { |
| 418 | unsigned long flags; |
| 419 | /* barriers don't appear to be supported :-( */ |
| 420 | set_bit(BarriersNotsupp, &rdev->flags); |
| 421 | mddev->barriers_work = 0; |
| 422 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 423 | bio2->bi_next = mddev->biolist; |
| 424 | mddev->biolist = bio2; |
| 425 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 426 | wake_up(&mddev->sb_wait); |
| 427 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 428 | } else { |
| 429 | bio_put(bio2); |
| 430 | bio->bi_private = rdev; |
| 431 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 432 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 433 | } |
| 434 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 435 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 436 | sector_t sector, int size, struct page *page) |
| 437 | { |
| 438 | /* write first size bytes of page to sector of rdev |
| 439 | * Increment mddev->pending_writes before returning |
| 440 | * and decrement it on completion, waking up sb_wait |
| 441 | * if zero is reached. |
| 442 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 443 | * |
| 444 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 445 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 446 | */ |
| 447 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 448 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 449 | |
| 450 | bio->bi_bdev = rdev->bdev; |
| 451 | bio->bi_sector = sector; |
| 452 | bio_add_page(bio, page, size, 0); |
| 453 | bio->bi_private = rdev; |
| 454 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 455 | bio->bi_rw = rw; |
| 456 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 457 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 458 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 459 | struct bio *rbio; |
| 460 | rw |= (1<<BIO_RW_BARRIER); |
| 461 | rbio = bio_clone(bio, GFP_NOIO); |
| 462 | rbio->bi_private = bio; |
| 463 | rbio->bi_end_io = super_written_barrier; |
| 464 | submit_bio(rw, rbio); |
| 465 | } else |
| 466 | submit_bio(rw, bio); |
| 467 | } |
| 468 | |
| 469 | void md_super_wait(mddev_t *mddev) |
| 470 | { |
| 471 | /* wait for all superblock writes that were scheduled to complete. |
| 472 | * if any had to be retried (due to BARRIER problems), retry them |
| 473 | */ |
| 474 | DEFINE_WAIT(wq); |
| 475 | for(;;) { |
| 476 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 477 | if (atomic_read(&mddev->pending_writes)==0) |
| 478 | break; |
| 479 | while (mddev->biolist) { |
| 480 | struct bio *bio; |
| 481 | spin_lock_irq(&mddev->write_lock); |
| 482 | bio = mddev->biolist; |
| 483 | mddev->biolist = bio->bi_next ; |
| 484 | bio->bi_next = NULL; |
| 485 | spin_unlock_irq(&mddev->write_lock); |
| 486 | submit_bio(bio->bi_rw, bio); |
| 487 | } |
| 488 | schedule(); |
| 489 | } |
| 490 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 491 | } |
| 492 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 493 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 498 | 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] | 499 | struct page *page, int rw) |
| 500 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 501 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | struct completion event; |
| 503 | int ret; |
| 504 | |
| 505 | rw |= (1 << BIO_RW_SYNC); |
| 506 | |
| 507 | bio->bi_bdev = bdev; |
| 508 | bio->bi_sector = sector; |
| 509 | bio_add_page(bio, page, size, 0); |
| 510 | init_completion(&event); |
| 511 | bio->bi_private = &event; |
| 512 | bio->bi_end_io = bi_complete; |
| 513 | submit_bio(rw, bio); |
| 514 | wait_for_completion(&event); |
| 515 | |
| 516 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 517 | bio_put(bio); |
| 518 | return ret; |
| 519 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 522 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | char b[BDEVNAME_SIZE]; |
| 525 | if (!rdev->sb_page) { |
| 526 | MD_BUG(); |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | if (rdev->sb_loaded) |
| 530 | return 0; |
| 531 | |
| 532 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 533 | if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto fail; |
| 535 | rdev->sb_loaded = 1; |
| 536 | return 0; |
| 537 | |
| 538 | fail: |
| 539 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 540 | bdevname(rdev->bdev,b)); |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
| 544 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 545 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 546 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 547 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 548 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 549 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 553 | { |
| 554 | int ret; |
| 555 | mdp_super_t *tmp1, *tmp2; |
| 556 | |
| 557 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 558 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 559 | |
| 560 | if (!tmp1 || !tmp2) { |
| 561 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 562 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | goto abort; |
| 564 | } |
| 565 | |
| 566 | *tmp1 = *sb1; |
| 567 | *tmp2 = *sb2; |
| 568 | |
| 569 | /* |
| 570 | * nr_disks is not constant |
| 571 | */ |
| 572 | tmp1->nr_disks = 0; |
| 573 | tmp2->nr_disks = 0; |
| 574 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 575 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 577 | kfree(tmp1); |
| 578 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return ret; |
| 580 | } |
| 581 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 582 | |
| 583 | static u32 md_csum_fold(u32 csum) |
| 584 | { |
| 585 | csum = (csum & 0xffff) + (csum >> 16); |
| 586 | return (csum & 0xffff) + (csum >> 16); |
| 587 | } |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 590 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 591 | u64 newcsum = 0; |
| 592 | u32 *sb32 = (u32*)sb; |
| 593 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | unsigned int disk_csum, csum; |
| 595 | |
| 596 | disk_csum = sb->sb_csum; |
| 597 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 598 | |
| 599 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 600 | newcsum += sb32[i]; |
| 601 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 602 | |
| 603 | |
| 604 | #ifdef CONFIG_ALPHA |
| 605 | /* This used to use csum_partial, which was wrong for several |
| 606 | * reasons including that different results are returned on |
| 607 | * different architectures. It isn't critical that we get exactly |
| 608 | * the same return value as before (we always csum_fold before |
| 609 | * testing, and that removes any differences). However as we |
| 610 | * know that csum_partial always returned a 16bit value on |
| 611 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 612 | */ |
| 613 | sb->sb_csum = md_csum_fold(disk_csum); |
| 614 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 616 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return csum; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /* |
| 622 | * Handle superblock details. |
| 623 | * We want to be able to handle multiple superblock formats |
| 624 | * so we have a common interface to them all, and an array of |
| 625 | * different handlers. |
| 626 | * We rely on user-space to write the initial superblock, and support |
| 627 | * reading and updating of superblocks. |
| 628 | * Interface methods are: |
| 629 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 630 | * loads and validates a superblock on dev. |
| 631 | * if refdev != NULL, compare superblocks on both devices |
| 632 | * Return: |
| 633 | * 0 - dev has a superblock that is compatible with refdev |
| 634 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 635 | * so dev should be used as the refdev in future |
| 636 | * -EINVAL superblock incompatible or invalid |
| 637 | * -othererror e.g. -EIO |
| 638 | * |
| 639 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 640 | * Verify that dev is acceptable into mddev. |
| 641 | * The first time, mddev->raid_disks will be 0, and data from |
| 642 | * dev should be merged in. Subsequent calls check that dev |
| 643 | * is new enough. Return 0 or -EINVAL |
| 644 | * |
| 645 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 646 | * Update the superblock for rdev with data in mddev |
| 647 | * This does not write to disc. |
| 648 | * |
| 649 | */ |
| 650 | |
| 651 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 652 | char *name; |
| 653 | struct module *owner; |
| 654 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 655 | int minor_version); |
| 656 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 657 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 658 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
| 659 | unsigned long long size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | /* |
| 663 | * load_super for 0.90.0 |
| 664 | */ |
| 665 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 666 | { |
| 667 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 668 | mdp_super_t *sb; |
| 669 | int ret; |
| 670 | sector_t sb_offset; |
| 671 | |
| 672 | /* |
| 673 | * Calculate the position of the superblock, |
| 674 | * it's at the end of the disk. |
| 675 | * |
| 676 | * It also happens to be a multiple of 4Kb. |
| 677 | */ |
| 678 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 679 | rdev->sb_offset = sb_offset; |
| 680 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 681 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (ret) return ret; |
| 683 | |
| 684 | ret = -EINVAL; |
| 685 | |
| 686 | bdevname(rdev->bdev, b); |
| 687 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 688 | |
| 689 | if (sb->md_magic != MD_SB_MAGIC) { |
| 690 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 691 | b); |
| 692 | goto abort; |
| 693 | } |
| 694 | |
| 695 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 696 | sb->minor_version < 90 || |
| 697 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 699 | sb->major_version, sb->minor_version, |
| 700 | b); |
| 701 | goto abort; |
| 702 | } |
| 703 | |
| 704 | if (sb->raid_disks <= 0) |
| 705 | goto abort; |
| 706 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 707 | 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] | 708 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 709 | b); |
| 710 | goto abort; |
| 711 | } |
| 712 | |
| 713 | rdev->preferred_minor = sb->md_minor; |
| 714 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 715 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 717 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 718 | if (sb->level != 1 && sb->level != 4 |
| 719 | && sb->level != 5 && sb->level != 6 |
| 720 | && sb->level != 10) { |
| 721 | /* FIXME use a better test */ |
| 722 | printk(KERN_WARNING |
| 723 | "md: bitmaps not supported for this level.\n"); |
| 724 | goto abort; |
| 725 | } |
| 726 | } |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | if (sb->level == LEVEL_MULTIPATH) |
| 729 | rdev->desc_nr = -1; |
| 730 | else |
| 731 | rdev->desc_nr = sb->this_disk.number; |
| 732 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 733 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 735 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | __u64 ev1, ev2; |
| 737 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 738 | if (!uuid_equal(refsb, sb)) { |
| 739 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 740 | b, bdevname(refdev->bdev,b2)); |
| 741 | goto abort; |
| 742 | } |
| 743 | if (!sb_equal(refsb, sb)) { |
| 744 | printk(KERN_WARNING "md: %s has same UUID" |
| 745 | " but different superblock to %s\n", |
| 746 | b, bdevname(refdev->bdev, b2)); |
| 747 | goto abort; |
| 748 | } |
| 749 | ev1 = md_event(sb); |
| 750 | ev2 = md_event(refsb); |
| 751 | if (ev1 > ev2) |
| 752 | ret = 1; |
| 753 | else |
| 754 | ret = 0; |
| 755 | } |
| 756 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 757 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 758 | if (rdev->size < sb->size && sb->level > 1) |
| 759 | /* "this cannot possibly happen" ... */ |
| 760 | ret = -EINVAL; |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | abort: |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * validate_super for 0.90.0 |
| 768 | */ |
| 769 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 770 | { |
| 771 | mdp_disk_t *desc; |
| 772 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 773 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 775 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 776 | clear_bit(Faulty, &rdev->flags); |
| 777 | clear_bit(In_sync, &rdev->flags); |
| 778 | clear_bit(WriteMostly, &rdev->flags); |
| 779 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (mddev->raid_disks == 0) { |
| 782 | mddev->major_version = 0; |
| 783 | mddev->minor_version = sb->minor_version; |
| 784 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 785 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | mddev->chunk_size = sb->chunk_size; |
| 787 | mddev->ctime = sb->ctime; |
| 788 | mddev->utime = sb->utime; |
| 789 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 790 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | mddev->layout = sb->layout; |
| 792 | mddev->raid_disks = sb->raid_disks; |
| 793 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 794 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 795 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 796 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 798 | if (mddev->minor_version >= 91) { |
| 799 | mddev->reshape_position = sb->reshape_position; |
| 800 | mddev->delta_disks = sb->delta_disks; |
| 801 | mddev->new_level = sb->new_level; |
| 802 | mddev->new_layout = sb->new_layout; |
| 803 | mddev->new_chunk = sb->new_chunk; |
| 804 | } else { |
| 805 | mddev->reshape_position = MaxSector; |
| 806 | mddev->delta_disks = 0; |
| 807 | mddev->new_level = mddev->level; |
| 808 | mddev->new_layout = mddev->layout; |
| 809 | mddev->new_chunk = mddev->chunk_size; |
| 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 813 | mddev->recovery_cp = MaxSector; |
| 814 | else { |
| 815 | if (sb->events_hi == sb->cp_events_hi && |
| 816 | sb->events_lo == sb->cp_events_lo) { |
| 817 | mddev->recovery_cp = sb->recovery_cp; |
| 818 | } else |
| 819 | mddev->recovery_cp = 0; |
| 820 | } |
| 821 | |
| 822 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 823 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 824 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 825 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 826 | |
| 827 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 828 | |
| 829 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 830 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 831 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 832 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 833 | } else if (mddev->pers == NULL) { |
| 834 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | ++ev1; |
| 836 | if (ev1 < mddev->events) |
| 837 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 838 | } else if (mddev->bitmap) { |
| 839 | /* if adding to array with a bitmap, then we can accept an |
| 840 | * older device ... but not too old. |
| 841 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 842 | if (ev1 < mddev->bitmap->events_cleared) |
| 843 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 844 | } else { |
| 845 | if (ev1 < mddev->events) |
| 846 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 847 | return 0; |
| 848 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | desc = sb->disks + rdev->desc_nr; |
| 852 | |
| 853 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 854 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 855 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 856 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 857 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | rdev->raid_disk = desc->raid_disk; |
| 859 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 860 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 861 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 862 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 863 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * sync_super for 0.90.0 |
| 869 | */ |
| 870 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 871 | { |
| 872 | mdp_super_t *sb; |
| 873 | struct list_head *tmp; |
| 874 | mdk_rdev_t *rdev2; |
| 875 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | /* make rdev->sb match mddev data.. |
| 879 | * |
| 880 | * 1/ zero out disks |
| 881 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 882 | * 3/ any empty disks < next_spare become removed |
| 883 | * |
| 884 | * disks[0] gets initialised to REMOVED because |
| 885 | * we cannot be sure from other fields if it has |
| 886 | * been initialised or not. |
| 887 | */ |
| 888 | int i; |
| 889 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 890 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 891 | rdev->sb_size = MD_SB_BYTES; |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 894 | |
| 895 | memset(sb, 0, sizeof(*sb)); |
| 896 | |
| 897 | sb->md_magic = MD_SB_MAGIC; |
| 898 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | sb->patch_version = mddev->patch_version; |
| 900 | sb->gvalid_words = 0; /* ignored */ |
| 901 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 902 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 903 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 904 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 905 | |
| 906 | sb->ctime = mddev->ctime; |
| 907 | sb->level = mddev->level; |
| 908 | sb->size = mddev->size; |
| 909 | sb->raid_disks = mddev->raid_disks; |
| 910 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 911 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | sb->utime = mddev->utime; |
| 913 | sb->state = 0; |
| 914 | sb->events_hi = (mddev->events>>32); |
| 915 | sb->events_lo = (u32)mddev->events; |
| 916 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 917 | if (mddev->reshape_position == MaxSector) |
| 918 | sb->minor_version = 90; |
| 919 | else { |
| 920 | sb->minor_version = 91; |
| 921 | sb->reshape_position = mddev->reshape_position; |
| 922 | sb->new_level = mddev->new_level; |
| 923 | sb->delta_disks = mddev->delta_disks; |
| 924 | sb->new_layout = mddev->new_layout; |
| 925 | sb->new_chunk = mddev->new_chunk; |
| 926 | } |
| 927 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (mddev->in_sync) |
| 929 | { |
| 930 | sb->recovery_cp = mddev->recovery_cp; |
| 931 | sb->cp_events_hi = (mddev->events>>32); |
| 932 | sb->cp_events_lo = (u32)mddev->events; |
| 933 | if (mddev->recovery_cp == MaxSector) |
| 934 | sb->state = (1<< MD_SB_CLEAN); |
| 935 | } else |
| 936 | sb->recovery_cp = 0; |
| 937 | |
| 938 | sb->layout = mddev->layout; |
| 939 | sb->chunk_size = mddev->chunk_size; |
| 940 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 941 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 942 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 945 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 947 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 948 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 949 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 950 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 952 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 953 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | d = &sb->disks[rdev2->desc_nr]; |
| 955 | nr_disks++; |
| 956 | d->number = rdev2->desc_nr; |
| 957 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 958 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 959 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 960 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | d->raid_disk = rdev2->raid_disk; |
| 962 | else |
| 963 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 964 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 966 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | d->state = (1<<MD_DISK_ACTIVE); |
| 968 | d->state |= (1<<MD_DISK_SYNC); |
| 969 | active++; |
| 970 | working++; |
| 971 | } else { |
| 972 | d->state = 0; |
| 973 | spare++; |
| 974 | working++; |
| 975 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 976 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 977 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 980 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 981 | mdp_disk_t *d = &sb->disks[i]; |
| 982 | if (d->state == 0 && d->number == 0) { |
| 983 | d->number = i; |
| 984 | d->raid_disk = i; |
| 985 | d->state = (1<<MD_DISK_REMOVED); |
| 986 | d->state |= (1<<MD_DISK_FAULTY); |
| 987 | failed++; |
| 988 | } |
| 989 | } |
| 990 | sb->nr_disks = nr_disks; |
| 991 | sb->active_disks = active; |
| 992 | sb->working_disks = working; |
| 993 | sb->failed_disks = failed; |
| 994 | sb->spare_disks = spare; |
| 995 | |
| 996 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 997 | sb->sb_csum = calc_sb_csum(sb); |
| 998 | } |
| 999 | |
| 1000 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1001 | * rdev_size_change for 0.90.0 |
| 1002 | */ |
| 1003 | static unsigned long long |
| 1004 | super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size) |
| 1005 | { |
| 1006 | if (size && size < rdev->mddev->size) |
| 1007 | return 0; /* component must fit device */ |
| 1008 | size *= 2; /* convert to sectors */ |
| 1009 | if (rdev->mddev->bitmap_offset) |
| 1010 | return 0; /* can't move bitmap */ |
| 1011 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 1012 | if (!size || size > rdev->sb_offset*2) |
| 1013 | size = rdev->sb_offset*2; |
| 1014 | md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size, |
| 1015 | rdev->sb_page); |
| 1016 | md_super_wait(rdev->mddev); |
| 1017 | return size/2; /* kB for sysfs */ |
| 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | * version 1 superblock |
| 1023 | */ |
| 1024 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1025 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1027 | __le32 disk_csum; |
| 1028 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | unsigned long long newcsum; |
| 1030 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1031 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | int i; |
| 1033 | |
| 1034 | disk_csum = sb->sb_csum; |
| 1035 | sb->sb_csum = 0; |
| 1036 | newcsum = 0; |
| 1037 | for (i=0; size>=4; size -= 4 ) |
| 1038 | newcsum += le32_to_cpu(*isuper++); |
| 1039 | |
| 1040 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1041 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1044 | sb->sb_csum = disk_csum; |
| 1045 | return cpu_to_le32(csum); |
| 1046 | } |
| 1047 | |
| 1048 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1049 | { |
| 1050 | struct mdp_superblock_1 *sb; |
| 1051 | int ret; |
| 1052 | sector_t sb_offset; |
| 1053 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1054 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * Calculate the position of the superblock. |
| 1058 | * It is always aligned to a 4K boundary and |
| 1059 | * depeding on minor_version, it can be: |
| 1060 | * 0: At least 8K, but less than 12K, from end of device |
| 1061 | * 1: At start of device |
| 1062 | * 2: 4K from start of device. |
| 1063 | */ |
| 1064 | switch(minor_version) { |
| 1065 | case 0: |
| 1066 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 1067 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1068 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | /* convert from sectors to K */ |
| 1070 | sb_offset /= 2; |
| 1071 | break; |
| 1072 | case 1: |
| 1073 | sb_offset = 0; |
| 1074 | break; |
| 1075 | case 2: |
| 1076 | sb_offset = 4; |
| 1077 | break; |
| 1078 | default: |
| 1079 | return -EINVAL; |
| 1080 | } |
| 1081 | rdev->sb_offset = sb_offset; |
| 1082 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1083 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1084 | * and it is safe to read 4k, so we do that |
| 1085 | */ |
| 1086 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (ret) return ret; |
| 1088 | |
| 1089 | |
| 1090 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1091 | |
| 1092 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1093 | sb->major_version != cpu_to_le32(1) || |
| 1094 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 1095 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1096 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | return -EINVAL; |
| 1098 | |
| 1099 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1100 | printk("md: invalid superblock checksum on %s\n", |
| 1101 | bdevname(rdev->bdev,b)); |
| 1102 | return -EINVAL; |
| 1103 | } |
| 1104 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1105 | printk("md: data_size too small on %s\n", |
| 1106 | bdevname(rdev->bdev,b)); |
| 1107 | return -EINVAL; |
| 1108 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1109 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1110 | if (sb->level != cpu_to_le32(1) && |
| 1111 | sb->level != cpu_to_le32(4) && |
| 1112 | sb->level != cpu_to_le32(5) && |
| 1113 | sb->level != cpu_to_le32(6) && |
| 1114 | sb->level != cpu_to_le32(10)) { |
| 1115 | printk(KERN_WARNING |
| 1116 | "md: bitmaps not supported for this level.\n"); |
| 1117 | return -EINVAL; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | rdev->preferred_minor = 0xffff; |
| 1122 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1123 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1125 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1126 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1127 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1128 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1129 | |
| 1130 | if (minor_version |
| 1131 | && rdev->data_offset < sb_offset + (rdev->sb_size/512)) |
| 1132 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1133 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1134 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1135 | rdev->desc_nr = -1; |
| 1136 | else |
| 1137 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1138 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1139 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1140 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1141 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | __u64 ev1, ev2; |
| 1143 | struct mdp_superblock_1 *refsb = |
| 1144 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1145 | |
| 1146 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1147 | sb->level != refsb->level || |
| 1148 | sb->layout != refsb->layout || |
| 1149 | sb->chunksize != refsb->chunksize) { |
| 1150 | printk(KERN_WARNING "md: %s has strangely different" |
| 1151 | " superblock to %s\n", |
| 1152 | bdevname(rdev->bdev,b), |
| 1153 | bdevname(refdev->bdev,b2)); |
| 1154 | return -EINVAL; |
| 1155 | } |
| 1156 | ev1 = le64_to_cpu(sb->events); |
| 1157 | ev2 = le64_to_cpu(refsb->events); |
| 1158 | |
| 1159 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1160 | ret = 1; |
| 1161 | else |
| 1162 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1164 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1166 | else |
| 1167 | rdev->size = rdev->sb_offset; |
| 1168 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1169 | return -EINVAL; |
| 1170 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1171 | if (le32_to_cpu(sb->chunksize)) |
| 1172 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1173 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1174 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1175 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1176 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1180 | { |
| 1181 | 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] | 1182 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1184 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1185 | clear_bit(Faulty, &rdev->flags); |
| 1186 | clear_bit(In_sync, &rdev->flags); |
| 1187 | clear_bit(WriteMostly, &rdev->flags); |
| 1188 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | if (mddev->raid_disks == 0) { |
| 1191 | mddev->major_version = 1; |
| 1192 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1193 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1195 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1196 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1197 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1198 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | mddev->layout = le32_to_cpu(sb->layout); |
| 1200 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1201 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1202 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1203 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1204 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1207 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1208 | |
| 1209 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1210 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1211 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1212 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1213 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1214 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1215 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1216 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1217 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1218 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1219 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1220 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1221 | } else { |
| 1222 | mddev->reshape_position = MaxSector; |
| 1223 | mddev->delta_disks = 0; |
| 1224 | mddev->new_level = mddev->level; |
| 1225 | mddev->new_layout = mddev->layout; |
| 1226 | mddev->new_chunk = mddev->chunk_size; |
| 1227 | } |
| 1228 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1229 | } else if (mddev->pers == NULL) { |
| 1230 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | ++ev1; |
| 1232 | if (ev1 < mddev->events) |
| 1233 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1234 | } else if (mddev->bitmap) { |
| 1235 | /* If adding to array with a bitmap, then we can accept an |
| 1236 | * older device, but not too old. |
| 1237 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1238 | if (ev1 < mddev->bitmap->events_cleared) |
| 1239 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1240 | } else { |
| 1241 | if (ev1 < mddev->events) |
| 1242 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1243 | return 0; |
| 1244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1246 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1248 | switch(role) { |
| 1249 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | break; |
| 1251 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1252 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | break; |
| 1254 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1255 | if ((le32_to_cpu(sb->feature_map) & |
| 1256 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1257 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1258 | else |
| 1259 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | rdev->raid_disk = role; |
| 1261 | break; |
| 1262 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1263 | if (sb->devflags & WriteMostly1) |
| 1264 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1265 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1266 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1272 | { |
| 1273 | struct mdp_superblock_1 *sb; |
| 1274 | struct list_head *tmp; |
| 1275 | mdk_rdev_t *rdev2; |
| 1276 | int max_dev, i; |
| 1277 | /* make rdev->sb match mddev and rdev data. */ |
| 1278 | |
| 1279 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1280 | |
| 1281 | sb->feature_map = 0; |
| 1282 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1283 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1285 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1286 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1287 | |
| 1288 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1289 | sb->events = cpu_to_le64(mddev->events); |
| 1290 | if (mddev->in_sync) |
| 1291 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1292 | else |
| 1293 | sb->resync_offset = cpu_to_le64(0); |
| 1294 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1295 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1296 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1297 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1298 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1299 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1300 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1301 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1302 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1303 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1304 | |
| 1305 | if (rdev->raid_disk >= 0 && |
| 1306 | !test_bit(In_sync, &rdev->flags) && |
| 1307 | rdev->recovery_offset > 0) { |
| 1308 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1309 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1310 | } |
| 1311 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1312 | if (mddev->reshape_position != MaxSector) { |
| 1313 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1314 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1315 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1316 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1317 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1318 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1319 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | max_dev = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1322 | rdev_for_each(rdev2, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | if (rdev2->desc_nr+1 > max_dev) |
| 1324 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1327 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | for (i=0; i<max_dev;i++) |
| 1329 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1330 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1331 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1333 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1335 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1337 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1338 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | else |
| 1340 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1341 | } |
| 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1344 | } |
| 1345 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1346 | static unsigned long long |
| 1347 | super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size) |
| 1348 | { |
| 1349 | struct mdp_superblock_1 *sb; |
| 1350 | unsigned long long max_size; |
| 1351 | if (size && size < rdev->mddev->size) |
| 1352 | return 0; /* component must fit device */ |
| 1353 | size *= 2; /* convert to sectors */ |
| 1354 | if (rdev->sb_offset < rdev->data_offset/2) { |
| 1355 | /* minor versions 1 and 2; superblock before data */ |
| 1356 | max_size = (rdev->bdev->bd_inode->i_size >> 9); |
| 1357 | max_size -= rdev->data_offset; |
| 1358 | if (!size || size > max_size) |
| 1359 | size = max_size; |
| 1360 | } else if (rdev->mddev->bitmap_offset) { |
| 1361 | /* minor version 0 with bitmap we can't move */ |
| 1362 | return 0; |
| 1363 | } else { |
| 1364 | /* minor version 0; superblock after data */ |
| 1365 | sector_t sb_offset; |
| 1366 | sb_offset = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1367 | sb_offset &= ~(sector_t)(4*2 - 1); |
| 1368 | max_size = rdev->size*2 + sb_offset - rdev->sb_offset*2; |
| 1369 | if (!size || size > max_size) |
| 1370 | size = max_size; |
| 1371 | rdev->sb_offset = sb_offset/2; |
| 1372 | } |
| 1373 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
| 1374 | sb->data_size = cpu_to_le64(size); |
| 1375 | sb->super_offset = rdev->sb_offset*2; |
| 1376 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1377 | md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size, |
| 1378 | rdev->sb_page); |
| 1379 | md_super_wait(rdev->mddev); |
| 1380 | return size/2; /* kB for sysfs */ |
| 1381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1383 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | [0] = { |
| 1385 | .name = "0.90.0", |
| 1386 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1387 | .load_super = super_90_load, |
| 1388 | .validate_super = super_90_validate, |
| 1389 | .sync_super = super_90_sync, |
| 1390 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | }, |
| 1392 | [1] = { |
| 1393 | .name = "md-1", |
| 1394 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1395 | .load_super = super_1_load, |
| 1396 | .validate_super = super_1_validate, |
| 1397 | .sync_super = super_1_sync, |
| 1398 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | }, |
| 1400 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1403 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1404 | struct list_head *tmp, *tmp2; |
| 1405 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1407 | rdev_for_each(rdev, tmp, mddev1) |
| 1408 | rdev_for_each(rdev2, tmp2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1409 | if (rdev->bdev->bd_contains == |
| 1410 | rdev2->bdev->bd_contains) |
| 1411 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static LIST_HEAD(pending_raid_disks); |
| 1417 | |
| 1418 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1419 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1420 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1421 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1422 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1423 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | if (rdev->mddev) { |
| 1426 | MD_BUG(); |
| 1427 | return -EINVAL; |
| 1428 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* prevent duplicates */ |
| 1431 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1432 | return -EEXIST; |
| 1433 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1434 | /* make sure rdev->size exceeds mddev->size */ |
| 1435 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1436 | if (mddev->pers) { |
| 1437 | /* Cannot change size, so fail |
| 1438 | * If mddev->level <= 0, then we don't care |
| 1439 | * about aligning sizes (e.g. linear) |
| 1440 | */ |
| 1441 | if (mddev->level > 0) |
| 1442 | return -ENOSPC; |
| 1443 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1444 | mddev->size = rdev->size; |
| 1445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | /* Verify rdev->desc_nr is unique. |
| 1448 | * If it is -1, assign a free number, else |
| 1449 | * check number is not in use |
| 1450 | */ |
| 1451 | if (rdev->desc_nr < 0) { |
| 1452 | int choice = 0; |
| 1453 | if (mddev->pers) choice = mddev->raid_disks; |
| 1454 | while (find_rdev_nr(mddev, choice)) |
| 1455 | choice++; |
| 1456 | rdev->desc_nr = choice; |
| 1457 | } else { |
| 1458 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1459 | return -EBUSY; |
| 1460 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1461 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1462 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1463 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1466 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1467 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1468 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1469 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1470 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1471 | if (rdev->bdev->bd_part) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1472 | ko = &rdev->bdev->bd_part->dev.kobj; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1473 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1474 | ko = &rdev->bdev->bd_disk->dev.kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1475 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1476 | kobject_del(&rdev->kobj); |
| 1477 | goto fail; |
| 1478 | } |
| 1479 | list_add(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1480 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1482 | |
| 1483 | fail: |
| 1484 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1485 | b, mdname(mddev)); |
| 1486 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1489 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1490 | { |
| 1491 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1492 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1493 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1497 | { |
| 1498 | char b[BDEVNAME_SIZE]; |
| 1499 | if (!rdev->mddev) { |
| 1500 | MD_BUG(); |
| 1501 | return; |
| 1502 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1503 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | list_del_init(&rdev->same_set); |
| 1505 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1506 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1507 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1508 | |
| 1509 | /* We need to delay this, otherwise we can deadlock when |
| 1510 | * writing to 'remove' to "dev/state" |
| 1511 | */ |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1512 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1513 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1514 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * prevent the device from being mounted, repartitioned or |
| 1519 | * otherwise reused by a RAID array (or any other kernel |
| 1520 | * subsystem), by bd_claiming the device. |
| 1521 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1522 | 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] | 1523 | { |
| 1524 | int err = 0; |
| 1525 | struct block_device *bdev; |
| 1526 | char b[BDEVNAME_SIZE]; |
| 1527 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1528 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | if (IS_ERR(bdev)) { |
| 1530 | printk(KERN_ERR "md: could not open %s.\n", |
| 1531 | __bdevname(dev, b)); |
| 1532 | return PTR_ERR(bdev); |
| 1533 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1534 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (err) { |
| 1536 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1537 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1538 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | return err; |
| 1540 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1541 | if (!shared) |
| 1542 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | rdev->bdev = bdev; |
| 1544 | return err; |
| 1545 | } |
| 1546 | |
| 1547 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1548 | { |
| 1549 | struct block_device *bdev = rdev->bdev; |
| 1550 | rdev->bdev = NULL; |
| 1551 | if (!bdev) |
| 1552 | MD_BUG(); |
| 1553 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1554 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | void md_autodetect_dev(dev_t dev); |
| 1558 | |
| 1559 | static void export_rdev(mdk_rdev_t * rdev) |
| 1560 | { |
| 1561 | char b[BDEVNAME_SIZE]; |
| 1562 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1563 | bdevname(rdev->bdev,b)); |
| 1564 | if (rdev->mddev) |
| 1565 | MD_BUG(); |
| 1566 | free_disk_sb(rdev); |
| 1567 | list_del_init(&rdev->same_set); |
| 1568 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1569 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1570 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | #endif |
| 1572 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1573 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1577 | { |
| 1578 | unbind_rdev_from_array(rdev); |
| 1579 | export_rdev(rdev); |
| 1580 | } |
| 1581 | |
| 1582 | static void export_array(mddev_t *mddev) |
| 1583 | { |
| 1584 | struct list_head *tmp; |
| 1585 | mdk_rdev_t *rdev; |
| 1586 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1587 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | if (!rdev->mddev) { |
| 1589 | MD_BUG(); |
| 1590 | continue; |
| 1591 | } |
| 1592 | kick_rdev_from_array(rdev); |
| 1593 | } |
| 1594 | if (!list_empty(&mddev->disks)) |
| 1595 | MD_BUG(); |
| 1596 | mddev->raid_disks = 0; |
| 1597 | mddev->major_version = 0; |
| 1598 | } |
| 1599 | |
| 1600 | static void print_desc(mdp_disk_t *desc) |
| 1601 | { |
| 1602 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1603 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1604 | } |
| 1605 | |
| 1606 | static void print_sb(mdp_super_t *sb) |
| 1607 | { |
| 1608 | int i; |
| 1609 | |
| 1610 | printk(KERN_INFO |
| 1611 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1612 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1613 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1614 | sb->ctime); |
| 1615 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1616 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1617 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1618 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1619 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1620 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1621 | sb->failed_disks, sb->spare_disks, |
| 1622 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1623 | |
| 1624 | printk(KERN_INFO); |
| 1625 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1626 | mdp_disk_t *desc; |
| 1627 | |
| 1628 | desc = sb->disks + i; |
| 1629 | if (desc->number || desc->major || desc->minor || |
| 1630 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1631 | printk(" D %2d: ", i); |
| 1632 | print_desc(desc); |
| 1633 | } |
| 1634 | } |
| 1635 | printk(KERN_INFO "md: THIS: "); |
| 1636 | print_desc(&sb->this_disk); |
| 1637 | |
| 1638 | } |
| 1639 | |
| 1640 | static void print_rdev(mdk_rdev_t *rdev) |
| 1641 | { |
| 1642 | char b[BDEVNAME_SIZE]; |
| 1643 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1644 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1645 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1646 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | if (rdev->sb_loaded) { |
| 1648 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1649 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1650 | } else |
| 1651 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1652 | } |
| 1653 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1654 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | { |
| 1656 | struct list_head *tmp, *tmp2; |
| 1657 | mdk_rdev_t *rdev; |
| 1658 | mddev_t *mddev; |
| 1659 | char b[BDEVNAME_SIZE]; |
| 1660 | |
| 1661 | printk("\n"); |
| 1662 | printk("md: **********************************\n"); |
| 1663 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1664 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1665 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1667 | if (mddev->bitmap) |
| 1668 | bitmap_print_sb(mddev->bitmap); |
| 1669 | else |
| 1670 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1671 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1673 | printk("\n"); |
| 1674 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1675 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | print_rdev(rdev); |
| 1677 | } |
| 1678 | printk("md: **********************************\n"); |
| 1679 | printk("\n"); |
| 1680 | } |
| 1681 | |
| 1682 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1683 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1685 | /* Update each superblock (in-memory image), but |
| 1686 | * if we are allowed to, skip spares which already |
| 1687 | * have the right event counter, or have one earlier |
| 1688 | * (which would mean they aren't being marked as dirty |
| 1689 | * with the rest of the array) |
| 1690 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | mdk_rdev_t *rdev; |
| 1692 | struct list_head *tmp; |
| 1693 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1694 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1695 | if (rdev->sb_events == mddev->events || |
| 1696 | (nospares && |
| 1697 | rdev->raid_disk < 0 && |
| 1698 | (rdev->sb_events&1)==0 && |
| 1699 | rdev->sb_events+1 == mddev->events)) { |
| 1700 | /* Don't update this superblock */ |
| 1701 | rdev->sb_loaded = 2; |
| 1702 | } else { |
| 1703 | super_types[mddev->major_version]. |
| 1704 | sync_super(mddev, rdev); |
| 1705 | rdev->sb_loaded = 1; |
| 1706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1710 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | struct list_head *tmp; |
| 1713 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1714 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1715 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1717 | if (mddev->external) |
| 1718 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1720 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1721 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1722 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1723 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1724 | force_change = 1; |
| 1725 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1726 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1727 | * though if events isn't the right even/odd, we will have to do |
| 1728 | * spares after all |
| 1729 | */ |
| 1730 | nospares = 1; |
| 1731 | if (force_change) |
| 1732 | nospares = 0; |
| 1733 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1734 | /* If the array is degraded, then skipping spares is both |
| 1735 | * dangerous and fairly pointless. |
| 1736 | * Dangerous because a device that was removed from the array |
| 1737 | * might have a event_count that still looks up-to-date, |
| 1738 | * so it can be re-added without a resync. |
| 1739 | * Pointless because if there are any spares to skip, |
| 1740 | * then a recovery will happen and soon that array won't |
| 1741 | * be degraded any more and the spare can go back to sleep then. |
| 1742 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1743 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1744 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1745 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1747 | |
| 1748 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1749 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1750 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1751 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1752 | && (mddev->events & 1) |
| 1753 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1754 | mddev->events--; |
| 1755 | else { |
| 1756 | /* otherwise we have to go forward and ... */ |
| 1757 | mddev->events ++; |
| 1758 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1759 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1760 | if ((mddev->events&1)==0) { |
| 1761 | mddev->events++; |
| 1762 | nospares = 0; |
| 1763 | } |
| 1764 | } else { |
| 1765 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1766 | if ((mddev->events&1)) { |
| 1767 | mddev->events++; |
| 1768 | nospares = 0; |
| 1769 | } |
| 1770 | } |
| 1771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
| 1773 | if (!mddev->events) { |
| 1774 | /* |
| 1775 | * oops, this 64-bit counter should never wrap. |
| 1776 | * Either we are in around ~1 trillion A.C., assuming |
| 1777 | * 1 reboot per second, or we have a bug: |
| 1778 | */ |
| 1779 | MD_BUG(); |
| 1780 | mddev->events --; |
| 1781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
| 1783 | /* |
| 1784 | * do not write anything to disk if using |
| 1785 | * nonpersistent superblocks |
| 1786 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1787 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1788 | if (!mddev->external) |
| 1789 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1790 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1791 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1792 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1794 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1795 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1796 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
| 1798 | dprintk(KERN_INFO |
| 1799 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1800 | mdname(mddev),mddev->in_sync); |
| 1801 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1802 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1803 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | char b[BDEVNAME_SIZE]; |
| 1805 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1806 | if (rdev->sb_loaded != 1) |
| 1807 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1808 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | dprintk("(skipping faulty "); |
| 1810 | |
| 1811 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1812 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1813 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1814 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1815 | rdev->sb_page); |
| 1816 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1817 | bdevname(rdev->bdev,b), |
| 1818 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1819 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | } else |
| 1822 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1823 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | /* only need to write one superblock... */ |
| 1825 | break; |
| 1826 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1827 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1828 | /* 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] | 1829 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1830 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1831 | if (mddev->in_sync != sync_req || |
| 1832 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1833 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1834 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1835 | goto repeat; |
| 1836 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1837 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1838 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1839 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 1843 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1844 | * We want to accept with case. For this we use cmd_match. |
| 1845 | */ |
| 1846 | static int cmd_match(const char *cmd, const char *str) |
| 1847 | { |
| 1848 | /* See if cmd, written into a sysfs file, matches |
| 1849 | * str. They must either be the same, or cmd can |
| 1850 | * have a trailing newline |
| 1851 | */ |
| 1852 | while (*cmd && *str && *cmd == *str) { |
| 1853 | cmd++; |
| 1854 | str++; |
| 1855 | } |
| 1856 | if (*cmd == '\n') |
| 1857 | cmd++; |
| 1858 | if (*str || *cmd) |
| 1859 | return 0; |
| 1860 | return 1; |
| 1861 | } |
| 1862 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1863 | struct rdev_sysfs_entry { |
| 1864 | struct attribute attr; |
| 1865 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1866 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1867 | }; |
| 1868 | |
| 1869 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1870 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1871 | { |
| 1872 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1873 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1874 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1875 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1876 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1877 | sep = ","; |
| 1878 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1879 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1880 | len += sprintf(page+len, "%sin_sync",sep); |
| 1881 | sep = ","; |
| 1882 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1883 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1884 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1885 | sep = ","; |
| 1886 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1887 | if (test_bit(Blocked, &rdev->flags)) { |
| 1888 | len += sprintf(page+len, "%sblocked", sep); |
| 1889 | sep = ","; |
| 1890 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1891 | if (!test_bit(Faulty, &rdev->flags) && |
| 1892 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1893 | len += sprintf(page+len, "%sspare", sep); |
| 1894 | sep = ","; |
| 1895 | } |
| 1896 | return len+sprintf(page+len, "\n"); |
| 1897 | } |
| 1898 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1899 | static ssize_t |
| 1900 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1901 | { |
| 1902 | /* can write |
| 1903 | * faulty - simulates and error |
| 1904 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1905 | * writemostly - sets write_mostly |
| 1906 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1907 | * blocked - sets the Blocked flag |
| 1908 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1909 | */ |
| 1910 | int err = -EINVAL; |
| 1911 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1912 | md_error(rdev->mddev, rdev); |
| 1913 | err = 0; |
| 1914 | } else if (cmd_match(buf, "remove")) { |
| 1915 | if (rdev->raid_disk >= 0) |
| 1916 | err = -EBUSY; |
| 1917 | else { |
| 1918 | mddev_t *mddev = rdev->mddev; |
| 1919 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1920 | if (mddev->pers) |
| 1921 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1922 | md_new_event(mddev); |
| 1923 | err = 0; |
| 1924 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1925 | } else if (cmd_match(buf, "writemostly")) { |
| 1926 | set_bit(WriteMostly, &rdev->flags); |
| 1927 | err = 0; |
| 1928 | } else if (cmd_match(buf, "-writemostly")) { |
| 1929 | clear_bit(WriteMostly, &rdev->flags); |
| 1930 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1931 | } else if (cmd_match(buf, "blocked")) { |
| 1932 | set_bit(Blocked, &rdev->flags); |
| 1933 | err = 0; |
| 1934 | } else if (cmd_match(buf, "-blocked")) { |
| 1935 | clear_bit(Blocked, &rdev->flags); |
| 1936 | wake_up(&rdev->blocked_wait); |
| 1937 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1938 | md_wakeup_thread(rdev->mddev->thread); |
| 1939 | |
| 1940 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1941 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 1942 | if (!err) |
| 1943 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1944 | return err ? err : len; |
| 1945 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1946 | static struct rdev_sysfs_entry rdev_state = |
| 1947 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1948 | |
| 1949 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1950 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1951 | { |
| 1952 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1953 | } |
| 1954 | |
| 1955 | static ssize_t |
| 1956 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1957 | { |
| 1958 | char *e; |
| 1959 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1960 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1961 | atomic_set(&rdev->corrected_errors, n); |
| 1962 | return len; |
| 1963 | } |
| 1964 | return -EINVAL; |
| 1965 | } |
| 1966 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1967 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1968 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1969 | static ssize_t |
| 1970 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1971 | { |
| 1972 | if (rdev->raid_disk < 0) |
| 1973 | return sprintf(page, "none\n"); |
| 1974 | else |
| 1975 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1976 | } |
| 1977 | |
| 1978 | static ssize_t |
| 1979 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1980 | { |
| 1981 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1982 | int err; |
| 1983 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1984 | int slot = simple_strtoul(buf, &e, 10); |
| 1985 | if (strncmp(buf, "none", 4)==0) |
| 1986 | slot = -1; |
| 1987 | else if (e==buf || (*e && *e!= '\n')) |
| 1988 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1989 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1990 | /* Setting 'slot' on an active array requires also |
| 1991 | * updating the 'rd%d' link, and communicating |
| 1992 | * with the personality with ->hot_*_disk. |
| 1993 | * For now we only support removing |
| 1994 | * failed/spare devices. This normally happens automatically, |
| 1995 | * but not when the metadata is externally managed. |
| 1996 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1997 | if (rdev->raid_disk == -1) |
| 1998 | return -EEXIST; |
| 1999 | /* personality does all needed checks */ |
| 2000 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2001 | return -EINVAL; |
| 2002 | err = rdev->mddev->pers-> |
| 2003 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2004 | if (err) |
| 2005 | return err; |
| 2006 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2007 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2008 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2009 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2010 | } else if (rdev->mddev->pers) { |
| 2011 | mdk_rdev_t *rdev2; |
| 2012 | struct list_head *tmp; |
| 2013 | /* Activating a spare .. or possibly reactivating |
| 2014 | * if we every get bitmaps working here. |
| 2015 | */ |
| 2016 | |
| 2017 | if (rdev->raid_disk != -1) |
| 2018 | return -EBUSY; |
| 2019 | |
| 2020 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2021 | return -EINVAL; |
| 2022 | |
| 2023 | rdev_for_each(rdev2, tmp, rdev->mddev) |
| 2024 | if (rdev2->raid_disk == slot) |
| 2025 | return -EEXIST; |
| 2026 | |
| 2027 | rdev->raid_disk = slot; |
| 2028 | if (test_bit(In_sync, &rdev->flags)) |
| 2029 | rdev->saved_raid_disk = slot; |
| 2030 | else |
| 2031 | rdev->saved_raid_disk = -1; |
| 2032 | err = rdev->mddev->pers-> |
| 2033 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2034 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2035 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2036 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2037 | } else |
| 2038 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2039 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2040 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2041 | printk(KERN_WARNING |
| 2042 | "md: cannot register " |
| 2043 | "%s for %s\n", |
| 2044 | nm, mdname(rdev->mddev)); |
| 2045 | |
| 2046 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2047 | } else { |
| 2048 | if (slot >= rdev->mddev->raid_disks) |
| 2049 | return -ENOSPC; |
| 2050 | rdev->raid_disk = slot; |
| 2051 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2052 | clear_bit(Faulty, &rdev->flags); |
| 2053 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2054 | set_bit(In_sync, &rdev->flags); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2055 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2056 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2057 | return len; |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2062 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2063 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2064 | static ssize_t |
| 2065 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2066 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2067 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | static ssize_t |
| 2071 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2072 | { |
| 2073 | char *e; |
| 2074 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2075 | if (e==buf || (*e && *e != '\n')) |
| 2076 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2077 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2078 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2079 | if (rdev->size && rdev->mddev->external) |
| 2080 | /* Must set offset before size, so overlap checks |
| 2081 | * can be sane */ |
| 2082 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2083 | rdev->data_offset = offset; |
| 2084 | return len; |
| 2085 | } |
| 2086 | |
| 2087 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2088 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2089 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2090 | static ssize_t |
| 2091 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2092 | { |
| 2093 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2094 | } |
| 2095 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2096 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2097 | { |
| 2098 | /* check if two start/length pairs overlap */ |
| 2099 | if (s1+l1 <= s2) |
| 2100 | return 0; |
| 2101 | if (s2+l2 <= s1) |
| 2102 | return 0; |
| 2103 | return 1; |
| 2104 | } |
| 2105 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2106 | static ssize_t |
| 2107 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2108 | { |
| 2109 | char *e; |
| 2110 | unsigned long long size = simple_strtoull(buf, &e, 10); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2111 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2112 | mddev_t *my_mddev = rdev->mddev; |
| 2113 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2114 | if (e==buf || (*e && *e != '\n')) |
| 2115 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2116 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
| 2117 | if (rdev->mddev->persistent) { |
| 2118 | size = super_types[rdev->mddev->major_version]. |
| 2119 | rdev_size_change(rdev, size); |
| 2120 | if (!size) |
| 2121 | return -EBUSY; |
| 2122 | } else if (!size) { |
| 2123 | size = (rdev->bdev->bd_inode->i_size >> 10); |
| 2124 | size -= rdev->data_offset/2; |
| 2125 | } |
| 2126 | if (size < rdev->mddev->size) |
| 2127 | return -EINVAL; /* component must fit device */ |
| 2128 | } |
| 2129 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2130 | rdev->size = size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2131 | if (size > oldsize && rdev->mddev->external) { |
| 2132 | /* need to check that all other rdevs with the same ->bdev |
| 2133 | * do not overlap. We need to unlock the mddev to avoid |
| 2134 | * a deadlock. We have already changed rdev->size, and if |
| 2135 | * we have to change it back, we will have the lock again. |
| 2136 | */ |
| 2137 | mddev_t *mddev; |
| 2138 | int overlap = 0; |
| 2139 | struct list_head *tmp, *tmp2; |
| 2140 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2141 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2142 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2143 | mdk_rdev_t *rdev2; |
| 2144 | |
| 2145 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2146 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2147 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2148 | (rdev->bdev == rdev2->bdev && |
| 2149 | rdev != rdev2 && |
| 2150 | overlaps(rdev->data_offset, rdev->size, |
| 2151 | rdev2->data_offset, rdev2->size))) { |
| 2152 | overlap = 1; |
| 2153 | break; |
| 2154 | } |
| 2155 | mddev_unlock(mddev); |
| 2156 | if (overlap) { |
| 2157 | mddev_put(mddev); |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2161 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2162 | if (overlap) { |
| 2163 | /* Someone else could have slipped in a size |
| 2164 | * change here, but doing so is just silly. |
| 2165 | * We put oldsize back because we *know* it is |
| 2166 | * safe, and trust userspace not to race with |
| 2167 | * itself |
| 2168 | */ |
| 2169 | rdev->size = oldsize; |
| 2170 | return -EBUSY; |
| 2171 | } |
| 2172 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2173 | if (size < my_mddev->size || my_mddev->size == 0) |
| 2174 | my_mddev->size = size; |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2175 | return len; |
| 2176 | } |
| 2177 | |
| 2178 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2179 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2180 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2181 | static struct attribute *rdev_default_attrs[] = { |
| 2182 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2183 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2184 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2185 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2186 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2187 | NULL, |
| 2188 | }; |
| 2189 | static ssize_t |
| 2190 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2191 | { |
| 2192 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2193 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2194 | mddev_t *mddev = rdev->mddev; |
| 2195 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2196 | |
| 2197 | if (!entry->show) |
| 2198 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2199 | |
| 2200 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2201 | if (!rv) { |
| 2202 | if (rdev->mddev == NULL) |
| 2203 | rv = -EBUSY; |
| 2204 | else |
| 2205 | rv = entry->show(rdev, page); |
| 2206 | mddev_unlock(mddev); |
| 2207 | } |
| 2208 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | static ssize_t |
| 2212 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2213 | const char *page, size_t length) |
| 2214 | { |
| 2215 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2216 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2217 | ssize_t rv; |
| 2218 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2219 | |
| 2220 | if (!entry->store) |
| 2221 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2222 | if (!capable(CAP_SYS_ADMIN)) |
| 2223 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2224 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2225 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2226 | if (rdev->mddev == NULL) |
| 2227 | rv = -EBUSY; |
| 2228 | else |
| 2229 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2230 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2231 | } |
| 2232 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | static void rdev_free(struct kobject *ko) |
| 2236 | { |
| 2237 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2238 | kfree(rdev); |
| 2239 | } |
| 2240 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2241 | .show = rdev_attr_show, |
| 2242 | .store = rdev_attr_store, |
| 2243 | }; |
| 2244 | static struct kobj_type rdev_ktype = { |
| 2245 | .release = rdev_free, |
| 2246 | .sysfs_ops = &rdev_sysfs_ops, |
| 2247 | .default_attrs = rdev_default_attrs, |
| 2248 | }; |
| 2249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | /* |
| 2251 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2252 | * |
| 2253 | * mark the device faulty if: |
| 2254 | * |
| 2255 | * - the device is nonexistent (zero size) |
| 2256 | * - the device has no valid superblock |
| 2257 | * |
| 2258 | * a faulty rdev _never_ has rdev->sb set. |
| 2259 | */ |
| 2260 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2261 | { |
| 2262 | char b[BDEVNAME_SIZE]; |
| 2263 | int err; |
| 2264 | mdk_rdev_t *rdev; |
| 2265 | sector_t size; |
| 2266 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2267 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | if (!rdev) { |
| 2269 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2270 | return ERR_PTR(-ENOMEM); |
| 2271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
| 2273 | if ((err = alloc_disk_sb(rdev))) |
| 2274 | goto abort_free; |
| 2275 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2276 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | if (err) |
| 2278 | goto abort_free; |
| 2279 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2280 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2283 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2284 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2285 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2287 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2289 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2290 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
| 2292 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2293 | if (!size) { |
| 2294 | printk(KERN_WARNING |
| 2295 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2296 | bdevname(rdev->bdev,b)); |
| 2297 | err = -EINVAL; |
| 2298 | goto abort_free; |
| 2299 | } |
| 2300 | |
| 2301 | if (super_format >= 0) { |
| 2302 | err = super_types[super_format]. |
| 2303 | load_super(rdev, NULL, super_minor); |
| 2304 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2305 | printk(KERN_WARNING |
| 2306 | "md: %s does not have a valid v%d.%d " |
| 2307 | "superblock, not importing!\n", |
| 2308 | bdevname(rdev->bdev,b), |
| 2309 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | goto abort_free; |
| 2311 | } |
| 2312 | if (err < 0) { |
| 2313 | printk(KERN_WARNING |
| 2314 | "md: could not read %s's sb, not importing!\n", |
| 2315 | bdevname(rdev->bdev,b)); |
| 2316 | goto abort_free; |
| 2317 | } |
| 2318 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2321 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | |
| 2323 | return rdev; |
| 2324 | |
| 2325 | abort_free: |
| 2326 | if (rdev->sb_page) { |
| 2327 | if (rdev->bdev) |
| 2328 | unlock_rdev(rdev); |
| 2329 | free_disk_sb(rdev); |
| 2330 | } |
| 2331 | kfree(rdev); |
| 2332 | return ERR_PTR(err); |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Check a full RAID array for plausibility |
| 2337 | */ |
| 2338 | |
| 2339 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2340 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | { |
| 2342 | int i; |
| 2343 | struct list_head *tmp; |
| 2344 | mdk_rdev_t *rdev, *freshest; |
| 2345 | char b[BDEVNAME_SIZE]; |
| 2346 | |
| 2347 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2348 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | switch (super_types[mddev->major_version]. |
| 2350 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2351 | case 1: |
| 2352 | freshest = rdev; |
| 2353 | break; |
| 2354 | case 0: |
| 2355 | break; |
| 2356 | default: |
| 2357 | printk( KERN_ERR \ |
| 2358 | "md: fatal superblock inconsistency in %s" |
| 2359 | " -- removing from array\n", |
| 2360 | bdevname(rdev->bdev,b)); |
| 2361 | kick_rdev_from_array(rdev); |
| 2362 | } |
| 2363 | |
| 2364 | |
| 2365 | super_types[mddev->major_version]. |
| 2366 | validate_super(mddev, freshest); |
| 2367 | |
| 2368 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2369 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | if (rdev != freshest) |
| 2371 | if (super_types[mddev->major_version]. |
| 2372 | validate_super(mddev, rdev)) { |
| 2373 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2374 | " from array!\n", |
| 2375 | bdevname(rdev->bdev,b)); |
| 2376 | kick_rdev_from_array(rdev); |
| 2377 | continue; |
| 2378 | } |
| 2379 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2380 | rdev->desc_nr = i++; |
| 2381 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2382 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2383 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2384 | rdev->raid_disk = -1; |
| 2385 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | if (mddev->recovery_cp != MaxSector && |
| 2392 | mddev->level >= 1) |
| 2393 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2394 | " -- starting background reconstruction\n", |
| 2395 | mdname(mddev)); |
| 2396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2399 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2400 | safe_delay_show(mddev_t *mddev, char *page) |
| 2401 | { |
| 2402 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2403 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2404 | } |
| 2405 | static ssize_t |
| 2406 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2407 | { |
| 2408 | int scale=1; |
| 2409 | int dot=0; |
| 2410 | int i; |
| 2411 | unsigned long msec; |
| 2412 | char buf[30]; |
| 2413 | char *e; |
| 2414 | /* remove a period, and count digits after it */ |
| 2415 | if (len >= sizeof(buf)) |
| 2416 | return -EINVAL; |
| 2417 | strlcpy(buf, cbuf, len); |
| 2418 | buf[len] = 0; |
| 2419 | for (i=0; i<len; i++) { |
| 2420 | if (dot) { |
| 2421 | if (isdigit(buf[i])) { |
| 2422 | buf[i-1] = buf[i]; |
| 2423 | scale *= 10; |
| 2424 | } |
| 2425 | buf[i] = 0; |
| 2426 | } else if (buf[i] == '.') { |
| 2427 | dot=1; |
| 2428 | buf[i] = 0; |
| 2429 | } |
| 2430 | } |
| 2431 | msec = simple_strtoul(buf, &e, 10); |
| 2432 | if (e == buf || (*e && *e != '\n')) |
| 2433 | return -EINVAL; |
| 2434 | msec = (msec * 1000) / scale; |
| 2435 | if (msec == 0) |
| 2436 | mddev->safemode_delay = 0; |
| 2437 | else { |
| 2438 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2439 | if (mddev->safemode_delay == 0) |
| 2440 | mddev->safemode_delay = 1; |
| 2441 | } |
| 2442 | return len; |
| 2443 | } |
| 2444 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2445 | __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] | 2446 | |
| 2447 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2448 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2449 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2450 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2451 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2452 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2453 | else if (mddev->clevel[0]) |
| 2454 | return sprintf(page, "%s\n", mddev->clevel); |
| 2455 | else if (mddev->level != LEVEL_NONE) |
| 2456 | return sprintf(page, "%d\n", mddev->level); |
| 2457 | else |
| 2458 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2461 | static ssize_t |
| 2462 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2463 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2464 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2465 | if (mddev->pers) |
| 2466 | return -EBUSY; |
| 2467 | if (len == 0) |
| 2468 | return 0; |
| 2469 | if (len >= sizeof(mddev->clevel)) |
| 2470 | return -ENOSPC; |
| 2471 | strncpy(mddev->clevel, buf, len); |
| 2472 | if (mddev->clevel[len-1] == '\n') |
| 2473 | len--; |
| 2474 | mddev->clevel[len] = 0; |
| 2475 | mddev->level = LEVEL_NONE; |
| 2476 | return rv; |
| 2477 | } |
| 2478 | |
| 2479 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2480 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2481 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2482 | |
| 2483 | static ssize_t |
| 2484 | layout_show(mddev_t *mddev, char *page) |
| 2485 | { |
| 2486 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2487 | if (mddev->reshape_position != MaxSector && |
| 2488 | mddev->layout != mddev->new_layout) |
| 2489 | return sprintf(page, "%d (%d)\n", |
| 2490 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2491 | return sprintf(page, "%d\n", mddev->layout); |
| 2492 | } |
| 2493 | |
| 2494 | static ssize_t |
| 2495 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2496 | { |
| 2497 | char *e; |
| 2498 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2499 | |
| 2500 | if (!*buf || (*e && *e != '\n')) |
| 2501 | return -EINVAL; |
| 2502 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2503 | if (mddev->pers) |
| 2504 | return -EBUSY; |
| 2505 | if (mddev->reshape_position != MaxSector) |
| 2506 | mddev->new_layout = n; |
| 2507 | else |
| 2508 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2509 | return len; |
| 2510 | } |
| 2511 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2512 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2513 | |
| 2514 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2515 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2516 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2517 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2518 | if (mddev->raid_disks == 0) |
| 2519 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2520 | if (mddev->reshape_position != MaxSector && |
| 2521 | mddev->delta_disks != 0) |
| 2522 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2523 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2524 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2525 | } |
| 2526 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2527 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2528 | |
| 2529 | static ssize_t |
| 2530 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2531 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2532 | char *e; |
| 2533 | int rv = 0; |
| 2534 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2535 | |
| 2536 | if (!*buf || (*e && *e != '\n')) |
| 2537 | return -EINVAL; |
| 2538 | |
| 2539 | if (mddev->pers) |
| 2540 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2541 | else if (mddev->reshape_position != MaxSector) { |
| 2542 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2543 | mddev->delta_disks = n - olddisks; |
| 2544 | mddev->raid_disks = n; |
| 2545 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2546 | mddev->raid_disks = n; |
| 2547 | return rv ? rv : len; |
| 2548 | } |
| 2549 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2550 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2551 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2552 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2553 | chunk_size_show(mddev_t *mddev, char *page) |
| 2554 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2555 | if (mddev->reshape_position != MaxSector && |
| 2556 | mddev->chunk_size != mddev->new_chunk) |
| 2557 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2558 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2559 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2560 | } |
| 2561 | |
| 2562 | static ssize_t |
| 2563 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2564 | { |
| 2565 | /* can only set chunk_size if array is not yet active */ |
| 2566 | char *e; |
| 2567 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2568 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2569 | if (!*buf || (*e && *e != '\n')) |
| 2570 | return -EINVAL; |
| 2571 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2572 | if (mddev->pers) |
| 2573 | return -EBUSY; |
| 2574 | else if (mddev->reshape_position != MaxSector) |
| 2575 | mddev->new_chunk = n; |
| 2576 | else |
| 2577 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2578 | return len; |
| 2579 | } |
| 2580 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2581 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2582 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2583 | static ssize_t |
| 2584 | resync_start_show(mddev_t *mddev, char *page) |
| 2585 | { |
| 2586 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2587 | } |
| 2588 | |
| 2589 | static ssize_t |
| 2590 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2591 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2592 | char *e; |
| 2593 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2594 | |
| 2595 | if (mddev->pers) |
| 2596 | return -EBUSY; |
| 2597 | if (!*buf || (*e && *e != '\n')) |
| 2598 | return -EINVAL; |
| 2599 | |
| 2600 | mddev->recovery_cp = n; |
| 2601 | return len; |
| 2602 | } |
| 2603 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2604 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2605 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2606 | /* |
| 2607 | * The array state can be: |
| 2608 | * |
| 2609 | * clear |
| 2610 | * No devices, no size, no level |
| 2611 | * Equivalent to STOP_ARRAY ioctl |
| 2612 | * inactive |
| 2613 | * May have some settings, but array is not active |
| 2614 | * all IO results in error |
| 2615 | * When written, doesn't tear down array, but just stops it |
| 2616 | * suspended (not supported yet) |
| 2617 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2618 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2619 | * readonly |
| 2620 | * no resync can happen. no superblocks get written. |
| 2621 | * write requests fail |
| 2622 | * read-auto |
| 2623 | * like readonly, but behaves like 'clean' on a write request. |
| 2624 | * |
| 2625 | * clean - no pending writes, but otherwise active. |
| 2626 | * When written to inactive array, starts without resync |
| 2627 | * If a write request arrives then |
| 2628 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2629 | * if not known, block and switch to write-pending |
| 2630 | * If written to an active array that has pending writes, then fails. |
| 2631 | * active |
| 2632 | * fully active: IO and resync can be happening. |
| 2633 | * When written to inactive array, starts with resync |
| 2634 | * |
| 2635 | * write-pending |
| 2636 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2637 | * |
| 2638 | * active-idle |
| 2639 | * like active, but no writes have been seen for a while (100msec). |
| 2640 | * |
| 2641 | */ |
| 2642 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2643 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2644 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2645 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2646 | "write-pending", "active-idle", NULL }; |
| 2647 | |
| 2648 | static int match_word(const char *word, char **list) |
| 2649 | { |
| 2650 | int n; |
| 2651 | for (n=0; list[n]; n++) |
| 2652 | if (cmd_match(word, list[n])) |
| 2653 | break; |
| 2654 | return n; |
| 2655 | } |
| 2656 | |
| 2657 | static ssize_t |
| 2658 | array_state_show(mddev_t *mddev, char *page) |
| 2659 | { |
| 2660 | enum array_state st = inactive; |
| 2661 | |
| 2662 | if (mddev->pers) |
| 2663 | switch(mddev->ro) { |
| 2664 | case 1: |
| 2665 | st = readonly; |
| 2666 | break; |
| 2667 | case 2: |
| 2668 | st = read_auto; |
| 2669 | break; |
| 2670 | case 0: |
| 2671 | if (mddev->in_sync) |
| 2672 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2673 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2674 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2675 | else if (mddev->safemode) |
| 2676 | st = active_idle; |
| 2677 | else |
| 2678 | st = active; |
| 2679 | } |
| 2680 | else { |
| 2681 | if (list_empty(&mddev->disks) && |
| 2682 | mddev->raid_disks == 0 && |
| 2683 | mddev->size == 0) |
| 2684 | st = clear; |
| 2685 | else |
| 2686 | st = inactive; |
| 2687 | } |
| 2688 | return sprintf(page, "%s\n", array_states[st]); |
| 2689 | } |
| 2690 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 2691 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2692 | static int do_md_run(mddev_t * mddev); |
| 2693 | static int restart_array(mddev_t *mddev); |
| 2694 | |
| 2695 | static ssize_t |
| 2696 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2697 | { |
| 2698 | int err = -EINVAL; |
| 2699 | enum array_state st = match_word(buf, array_states); |
| 2700 | switch(st) { |
| 2701 | case bad_word: |
| 2702 | break; |
| 2703 | case clear: |
| 2704 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2705 | if (atomic_read(&mddev->active) > 1) |
| 2706 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 2707 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2708 | break; |
| 2709 | case inactive: |
| 2710 | /* stopping an active array */ |
| 2711 | if (mddev->pers) { |
| 2712 | if (atomic_read(&mddev->active) > 1) |
| 2713 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 2714 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2715 | } else |
| 2716 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2717 | break; |
| 2718 | case suspended: |
| 2719 | break; /* not supported yet */ |
| 2720 | case readonly: |
| 2721 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 2722 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2723 | else { |
| 2724 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2725 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2726 | err = do_md_run(mddev); |
| 2727 | } |
| 2728 | break; |
| 2729 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2730 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2731 | if (mddev->ro != 1) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 2732 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2733 | else |
| 2734 | err = restart_array(mddev); |
| 2735 | if (err == 0) { |
| 2736 | mddev->ro = 2; |
| 2737 | set_disk_ro(mddev->gendisk, 0); |
| 2738 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2739 | } else { |
| 2740 | mddev->ro = 2; |
| 2741 | err = do_md_run(mddev); |
| 2742 | } |
| 2743 | break; |
| 2744 | case clean: |
| 2745 | if (mddev->pers) { |
| 2746 | restart_array(mddev); |
| 2747 | spin_lock_irq(&mddev->write_lock); |
| 2748 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2749 | if (mddev->in_sync == 0) { |
| 2750 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2751 | if (mddev->safemode == 1) |
| 2752 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2753 | if (mddev->persistent) |
| 2754 | set_bit(MD_CHANGE_CLEAN, |
| 2755 | &mddev->flags); |
| 2756 | } |
| 2757 | err = 0; |
| 2758 | } else |
| 2759 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2760 | spin_unlock_irq(&mddev->write_lock); |
| 2761 | } else { |
| 2762 | mddev->ro = 0; |
| 2763 | mddev->recovery_cp = MaxSector; |
| 2764 | err = do_md_run(mddev); |
| 2765 | } |
| 2766 | break; |
| 2767 | case active: |
| 2768 | if (mddev->pers) { |
| 2769 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2770 | if (mddev->external) |
| 2771 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2772 | wake_up(&mddev->sb_wait); |
| 2773 | err = 0; |
| 2774 | } else { |
| 2775 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2776 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2777 | err = do_md_run(mddev); |
| 2778 | } |
| 2779 | break; |
| 2780 | case write_pending: |
| 2781 | case active_idle: |
| 2782 | /* these cannot be set */ |
| 2783 | break; |
| 2784 | } |
| 2785 | if (err) |
| 2786 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2787 | else { |
| 2788 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2789 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2790 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2791 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2792 | static struct md_sysfs_entry md_array_state = |
| 2793 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2794 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2795 | static ssize_t |
| 2796 | null_show(mddev_t *mddev, char *page) |
| 2797 | { |
| 2798 | return -EINVAL; |
| 2799 | } |
| 2800 | |
| 2801 | static ssize_t |
| 2802 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2803 | { |
| 2804 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2805 | /* The new device is added to the array. |
| 2806 | * If the array has a persistent superblock, we read the |
| 2807 | * superblock to initialise info and check validity. |
| 2808 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2809 | * which mainly checks size. |
| 2810 | */ |
| 2811 | char *e; |
| 2812 | int major = simple_strtoul(buf, &e, 10); |
| 2813 | int minor; |
| 2814 | dev_t dev; |
| 2815 | mdk_rdev_t *rdev; |
| 2816 | int err; |
| 2817 | |
| 2818 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2819 | return -EINVAL; |
| 2820 | minor = simple_strtoul(e+1, &e, 10); |
| 2821 | if (*e && *e != '\n') |
| 2822 | return -EINVAL; |
| 2823 | dev = MKDEV(major, minor); |
| 2824 | if (major != MAJOR(dev) || |
| 2825 | minor != MINOR(dev)) |
| 2826 | return -EOVERFLOW; |
| 2827 | |
| 2828 | |
| 2829 | if (mddev->persistent) { |
| 2830 | rdev = md_import_device(dev, mddev->major_version, |
| 2831 | mddev->minor_version); |
| 2832 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2833 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2834 | mdk_rdev_t, same_set); |
| 2835 | err = super_types[mddev->major_version] |
| 2836 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2837 | if (err < 0) |
| 2838 | goto out; |
| 2839 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2840 | } else if (mddev->external) |
| 2841 | rdev = md_import_device(dev, -2, -1); |
| 2842 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2843 | rdev = md_import_device(dev, -1, -1); |
| 2844 | |
| 2845 | if (IS_ERR(rdev)) |
| 2846 | return PTR_ERR(rdev); |
| 2847 | err = bind_rdev_to_array(rdev, mddev); |
| 2848 | out: |
| 2849 | if (err) |
| 2850 | export_rdev(rdev); |
| 2851 | return err ? err : len; |
| 2852 | } |
| 2853 | |
| 2854 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2855 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2856 | |
| 2857 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2858 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2859 | { |
| 2860 | char *end; |
| 2861 | unsigned long chunk, end_chunk; |
| 2862 | |
| 2863 | if (!mddev->bitmap) |
| 2864 | goto out; |
| 2865 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2866 | while (*buf) { |
| 2867 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2868 | if (buf == end) break; |
| 2869 | if (*end == '-') { /* range */ |
| 2870 | buf = end + 1; |
| 2871 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2872 | if (buf == end) break; |
| 2873 | } |
| 2874 | if (*end && !isspace(*end)) break; |
| 2875 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2876 | buf = end; |
| 2877 | while (isspace(*buf)) buf++; |
| 2878 | } |
| 2879 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2880 | out: |
| 2881 | return len; |
| 2882 | } |
| 2883 | |
| 2884 | static struct md_sysfs_entry md_bitmap = |
| 2885 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2886 | |
| 2887 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2888 | size_show(mddev_t *mddev, char *page) |
| 2889 | { |
| 2890 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2891 | } |
| 2892 | |
| 2893 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2894 | |
| 2895 | static ssize_t |
| 2896 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2897 | { |
| 2898 | /* If array is inactive, we can reduce the component size, but |
| 2899 | * not increase it (except from 0). |
| 2900 | * If array is active, we can try an on-line resize |
| 2901 | */ |
| 2902 | char *e; |
| 2903 | int err = 0; |
| 2904 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2905 | if (!*buf || *buf == '\n' || |
| 2906 | (*e && *e != '\n')) |
| 2907 | return -EINVAL; |
| 2908 | |
| 2909 | if (mddev->pers) { |
| 2910 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2911 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2912 | } else { |
| 2913 | if (mddev->size == 0 || |
| 2914 | mddev->size > size) |
| 2915 | mddev->size = size; |
| 2916 | else |
| 2917 | err = -ENOSPC; |
| 2918 | } |
| 2919 | return err ? err : len; |
| 2920 | } |
| 2921 | |
| 2922 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2923 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2924 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2925 | |
| 2926 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2927 | * This is one of |
| 2928 | * 'none' for arrays with no metadata (good luck...) |
| 2929 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2930 | * or N.M for internally known formats |
| 2931 | */ |
| 2932 | static ssize_t |
| 2933 | metadata_show(mddev_t *mddev, char *page) |
| 2934 | { |
| 2935 | if (mddev->persistent) |
| 2936 | return sprintf(page, "%d.%d\n", |
| 2937 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2938 | else if (mddev->external) |
| 2939 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2940 | else |
| 2941 | return sprintf(page, "none\n"); |
| 2942 | } |
| 2943 | |
| 2944 | static ssize_t |
| 2945 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2946 | { |
| 2947 | int major, minor; |
| 2948 | char *e; |
| 2949 | if (!list_empty(&mddev->disks)) |
| 2950 | return -EBUSY; |
| 2951 | |
| 2952 | if (cmd_match(buf, "none")) { |
| 2953 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2954 | mddev->external = 0; |
| 2955 | mddev->major_version = 0; |
| 2956 | mddev->minor_version = 90; |
| 2957 | return len; |
| 2958 | } |
| 2959 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2960 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2961 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2962 | namelen = sizeof(mddev->metadata_type)-1; |
| 2963 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2964 | mddev->metadata_type[namelen] = 0; |
| 2965 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2966 | mddev->metadata_type[--namelen] = 0; |
| 2967 | mddev->persistent = 0; |
| 2968 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2969 | mddev->major_version = 0; |
| 2970 | mddev->minor_version = 90; |
| 2971 | return len; |
| 2972 | } |
| 2973 | major = simple_strtoul(buf, &e, 10); |
| 2974 | if (e==buf || *e != '.') |
| 2975 | return -EINVAL; |
| 2976 | buf = e+1; |
| 2977 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2978 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2979 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2980 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2981 | return -ENOENT; |
| 2982 | mddev->major_version = major; |
| 2983 | mddev->minor_version = minor; |
| 2984 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2985 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2986 | return len; |
| 2987 | } |
| 2988 | |
| 2989 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2990 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2991 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2992 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2993 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2994 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2995 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2996 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2997 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2998 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2999 | type = "reshape"; |
| 3000 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3001 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3002 | type = "resync"; |
| 3003 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3004 | type = "check"; |
| 3005 | else |
| 3006 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3007 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3008 | type = "recover"; |
| 3009 | } |
| 3010 | return sprintf(page, "%s\n", type); |
| 3011 | } |
| 3012 | |
| 3013 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3014 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3015 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3016 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3017 | return -EINVAL; |
| 3018 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3019 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3020 | if (mddev->sync_thread) { |
| 3021 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3022 | md_unregister_thread(mddev->sync_thread); |
| 3023 | mddev->sync_thread = NULL; |
| 3024 | mddev->recovery = 0; |
| 3025 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3026 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3027 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3028 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3029 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3030 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3031 | else if (cmd_match(page, "recover")) { |
| 3032 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3033 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3034 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3035 | int err; |
| 3036 | if (mddev->pers->start_reshape == NULL) |
| 3037 | return -EINVAL; |
| 3038 | err = mddev->pers->start_reshape(mddev); |
| 3039 | if (err) |
| 3040 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3041 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3042 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3043 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3044 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3045 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3046 | return -EINVAL; |
| 3047 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3048 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3049 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3050 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3051 | md_wakeup_thread(mddev->thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3052 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3053 | return len; |
| 3054 | } |
| 3055 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3056 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3057 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3058 | { |
| 3059 | return sprintf(page, "%llu\n", |
| 3060 | (unsigned long long) mddev->resync_mismatches); |
| 3061 | } |
| 3062 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3063 | static struct md_sysfs_entry md_scan_mode = |
| 3064 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3065 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3066 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3067 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3068 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3069 | static ssize_t |
| 3070 | sync_min_show(mddev_t *mddev, char *page) |
| 3071 | { |
| 3072 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3073 | mddev->sync_speed_min ? "local": "system"); |
| 3074 | } |
| 3075 | |
| 3076 | static ssize_t |
| 3077 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3078 | { |
| 3079 | int min; |
| 3080 | char *e; |
| 3081 | if (strncmp(buf, "system", 6)==0) { |
| 3082 | mddev->sync_speed_min = 0; |
| 3083 | return len; |
| 3084 | } |
| 3085 | min = simple_strtoul(buf, &e, 10); |
| 3086 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3087 | return -EINVAL; |
| 3088 | mddev->sync_speed_min = min; |
| 3089 | return len; |
| 3090 | } |
| 3091 | |
| 3092 | static struct md_sysfs_entry md_sync_min = |
| 3093 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3094 | |
| 3095 | static ssize_t |
| 3096 | sync_max_show(mddev_t *mddev, char *page) |
| 3097 | { |
| 3098 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3099 | mddev->sync_speed_max ? "local": "system"); |
| 3100 | } |
| 3101 | |
| 3102 | static ssize_t |
| 3103 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3104 | { |
| 3105 | int max; |
| 3106 | char *e; |
| 3107 | if (strncmp(buf, "system", 6)==0) { |
| 3108 | mddev->sync_speed_max = 0; |
| 3109 | return len; |
| 3110 | } |
| 3111 | max = simple_strtoul(buf, &e, 10); |
| 3112 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3113 | return -EINVAL; |
| 3114 | mddev->sync_speed_max = max; |
| 3115 | return len; |
| 3116 | } |
| 3117 | |
| 3118 | static struct md_sysfs_entry md_sync_max = |
| 3119 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3120 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3121 | static ssize_t |
| 3122 | degraded_show(mddev_t *mddev, char *page) |
| 3123 | { |
| 3124 | return sprintf(page, "%d\n", mddev->degraded); |
| 3125 | } |
| 3126 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3127 | |
| 3128 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3129 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3130 | { |
| 3131 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3132 | } |
| 3133 | |
| 3134 | static ssize_t |
| 3135 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3136 | { |
| 3137 | long n; |
| 3138 | |
| 3139 | if (strict_strtol(buf, 10, &n)) |
| 3140 | return -EINVAL; |
| 3141 | |
| 3142 | if (n != 0 && n != 1) |
| 3143 | return -EINVAL; |
| 3144 | |
| 3145 | mddev->parallel_resync = n; |
| 3146 | |
| 3147 | if (mddev->sync_thread) |
| 3148 | wake_up(&resync_wait); |
| 3149 | |
| 3150 | return len; |
| 3151 | } |
| 3152 | |
| 3153 | /* force parallel resync, even with shared block devices */ |
| 3154 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3155 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3156 | sync_force_parallel_show, sync_force_parallel_store); |
| 3157 | |
| 3158 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3159 | sync_speed_show(mddev_t *mddev, char *page) |
| 3160 | { |
| 3161 | unsigned long resync, dt, db; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3162 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3163 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3164 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3165 | db = resync - mddev->resync_mark_cnt; |
| 3166 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3169 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3170 | |
| 3171 | static ssize_t |
| 3172 | sync_completed_show(mddev_t *mddev, char *page) |
| 3173 | { |
| 3174 | unsigned long max_blocks, resync; |
| 3175 | |
| 3176 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3177 | max_blocks = mddev->resync_max_sectors; |
| 3178 | else |
| 3179 | max_blocks = mddev->size << 1; |
| 3180 | |
| 3181 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3182 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3183 | } |
| 3184 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3185 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3186 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3187 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3188 | min_sync_show(mddev_t *mddev, char *page) |
| 3189 | { |
| 3190 | return sprintf(page, "%llu\n", |
| 3191 | (unsigned long long)mddev->resync_min); |
| 3192 | } |
| 3193 | static ssize_t |
| 3194 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3195 | { |
| 3196 | unsigned long long min; |
| 3197 | if (strict_strtoull(buf, 10, &min)) |
| 3198 | return -EINVAL; |
| 3199 | if (min > mddev->resync_max) |
| 3200 | return -EINVAL; |
| 3201 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3202 | return -EBUSY; |
| 3203 | |
| 3204 | /* Must be a multiple of chunk_size */ |
| 3205 | if (mddev->chunk_size) { |
| 3206 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3207 | return -EINVAL; |
| 3208 | } |
| 3209 | mddev->resync_min = min; |
| 3210 | |
| 3211 | return len; |
| 3212 | } |
| 3213 | |
| 3214 | static struct md_sysfs_entry md_min_sync = |
| 3215 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3216 | |
| 3217 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3218 | max_sync_show(mddev_t *mddev, char *page) |
| 3219 | { |
| 3220 | if (mddev->resync_max == MaxSector) |
| 3221 | return sprintf(page, "max\n"); |
| 3222 | else |
| 3223 | return sprintf(page, "%llu\n", |
| 3224 | (unsigned long long)mddev->resync_max); |
| 3225 | } |
| 3226 | static ssize_t |
| 3227 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3228 | { |
| 3229 | if (strncmp(buf, "max", 3) == 0) |
| 3230 | mddev->resync_max = MaxSector; |
| 3231 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3232 | unsigned long long max; |
| 3233 | if (strict_strtoull(buf, 10, &max)) |
| 3234 | return -EINVAL; |
| 3235 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3236 | return -EINVAL; |
| 3237 | if (max < mddev->resync_max && |
| 3238 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3239 | return -EBUSY; |
| 3240 | |
| 3241 | /* Must be a multiple of chunk_size */ |
| 3242 | if (mddev->chunk_size) { |
| 3243 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3244 | return -EINVAL; |
| 3245 | } |
| 3246 | mddev->resync_max = max; |
| 3247 | } |
| 3248 | wake_up(&mddev->recovery_wait); |
| 3249 | return len; |
| 3250 | } |
| 3251 | |
| 3252 | static struct md_sysfs_entry md_max_sync = |
| 3253 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3254 | |
| 3255 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3256 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3257 | { |
| 3258 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3259 | } |
| 3260 | |
| 3261 | static ssize_t |
| 3262 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3263 | { |
| 3264 | char *e; |
| 3265 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3266 | |
| 3267 | if (mddev->pers->quiesce == NULL) |
| 3268 | return -EINVAL; |
| 3269 | if (buf == e || (*e && *e != '\n')) |
| 3270 | return -EINVAL; |
| 3271 | if (new >= mddev->suspend_hi || |
| 3272 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3273 | mddev->suspend_lo = new; |
| 3274 | mddev->pers->quiesce(mddev, 2); |
| 3275 | return len; |
| 3276 | } else |
| 3277 | return -EINVAL; |
| 3278 | } |
| 3279 | static struct md_sysfs_entry md_suspend_lo = |
| 3280 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3281 | |
| 3282 | |
| 3283 | static ssize_t |
| 3284 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3285 | { |
| 3286 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3287 | } |
| 3288 | |
| 3289 | static ssize_t |
| 3290 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3291 | { |
| 3292 | char *e; |
| 3293 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3294 | |
| 3295 | if (mddev->pers->quiesce == NULL) |
| 3296 | return -EINVAL; |
| 3297 | if (buf == e || (*e && *e != '\n')) |
| 3298 | return -EINVAL; |
| 3299 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3300 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3301 | mddev->suspend_hi = new; |
| 3302 | mddev->pers->quiesce(mddev, 1); |
| 3303 | mddev->pers->quiesce(mddev, 0); |
| 3304 | return len; |
| 3305 | } else |
| 3306 | return -EINVAL; |
| 3307 | } |
| 3308 | static struct md_sysfs_entry md_suspend_hi = |
| 3309 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3310 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3311 | static ssize_t |
| 3312 | reshape_position_show(mddev_t *mddev, char *page) |
| 3313 | { |
| 3314 | if (mddev->reshape_position != MaxSector) |
| 3315 | return sprintf(page, "%llu\n", |
| 3316 | (unsigned long long)mddev->reshape_position); |
| 3317 | strcpy(page, "none\n"); |
| 3318 | return 5; |
| 3319 | } |
| 3320 | |
| 3321 | static ssize_t |
| 3322 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3323 | { |
| 3324 | char *e; |
| 3325 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3326 | if (mddev->pers) |
| 3327 | return -EBUSY; |
| 3328 | if (buf == e || (*e && *e != '\n')) |
| 3329 | return -EINVAL; |
| 3330 | mddev->reshape_position = new; |
| 3331 | mddev->delta_disks = 0; |
| 3332 | mddev->new_level = mddev->level; |
| 3333 | mddev->new_layout = mddev->layout; |
| 3334 | mddev->new_chunk = mddev->chunk_size; |
| 3335 | return len; |
| 3336 | } |
| 3337 | |
| 3338 | static struct md_sysfs_entry md_reshape_position = |
| 3339 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3340 | reshape_position_store); |
| 3341 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3342 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3343 | static struct attribute *md_default_attrs[] = { |
| 3344 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3345 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3346 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3347 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3348 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3349 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3350 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3351 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3352 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3353 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3354 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3355 | NULL, |
| 3356 | }; |
| 3357 | |
| 3358 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3359 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3360 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3361 | &md_sync_min.attr, |
| 3362 | &md_sync_max.attr, |
| 3363 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3364 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3365 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3366 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3367 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3368 | &md_suspend_lo.attr, |
| 3369 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3370 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3371 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3372 | NULL, |
| 3373 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3374 | static struct attribute_group md_redundancy_group = { |
| 3375 | .name = NULL, |
| 3376 | .attrs = md_redundancy_attrs, |
| 3377 | }; |
| 3378 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3379 | |
| 3380 | static ssize_t |
| 3381 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3382 | { |
| 3383 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3384 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3385 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3386 | |
| 3387 | if (!entry->show) |
| 3388 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3389 | rv = mddev_lock(mddev); |
| 3390 | if (!rv) { |
| 3391 | rv = entry->show(mddev, page); |
| 3392 | mddev_unlock(mddev); |
| 3393 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3394 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static ssize_t |
| 3398 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3399 | const char *page, size_t length) |
| 3400 | { |
| 3401 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3402 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3403 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3404 | |
| 3405 | if (!entry->store) |
| 3406 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3407 | if (!capable(CAP_SYS_ADMIN)) |
| 3408 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3409 | rv = mddev_lock(mddev); |
| 3410 | if (!rv) { |
| 3411 | rv = entry->store(mddev, page, length); |
| 3412 | mddev_unlock(mddev); |
| 3413 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3414 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | static void md_free(struct kobject *ko) |
| 3418 | { |
| 3419 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3420 | kfree(mddev); |
| 3421 | } |
| 3422 | |
| 3423 | static struct sysfs_ops md_sysfs_ops = { |
| 3424 | .show = md_attr_show, |
| 3425 | .store = md_attr_store, |
| 3426 | }; |
| 3427 | static struct kobj_type md_ktype = { |
| 3428 | .release = md_free, |
| 3429 | .sysfs_ops = &md_sysfs_ops, |
| 3430 | .default_attrs = md_default_attrs, |
| 3431 | }; |
| 3432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | int mdp_major = 0; |
| 3434 | |
| 3435 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3436 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3437 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | mddev_t *mddev = mddev_find(dev); |
| 3439 | struct gendisk *disk; |
| 3440 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3441 | int shift = partitioned ? MdpMinorShift : 0; |
| 3442 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3443 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | |
| 3445 | if (!mddev) |
| 3446 | return NULL; |
| 3447 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3448 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3450 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | mddev_put(mddev); |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | disk = alloc_disk(1 << shift); |
| 3455 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3456 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | mddev_put(mddev); |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | disk->major = MAJOR(dev); |
| 3461 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3462 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3464 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | disk->fops = &md_fops; |
| 3467 | disk->private_data = mddev; |
| 3468 | disk->queue = mddev->queue; |
| 3469 | add_disk(disk); |
| 3470 | mddev->gendisk = disk; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3471 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj, |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3472 | "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3473 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3474 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3475 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3476 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3477 | else |
| 3478 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | return NULL; |
| 3480 | } |
| 3481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | static void md_safemode_timeout(unsigned long data) |
| 3483 | { |
| 3484 | mddev_t *mddev = (mddev_t *) data; |
| 3485 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3486 | if (!atomic_read(&mddev->writes_pending)) { |
| 3487 | mddev->safemode = 1; |
| 3488 | if (mddev->external) |
| 3489 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | md_wakeup_thread(mddev->thread); |
| 3492 | } |
| 3493 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3494 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | |
| 3496 | static int do_md_run(mddev_t * mddev) |
| 3497 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3498 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | int chunk_size; |
| 3500 | struct list_head *tmp; |
| 3501 | mdk_rdev_t *rdev; |
| 3502 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3503 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | char b[BDEVNAME_SIZE]; |
| 3505 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3506 | if (list_empty(&mddev->disks)) |
| 3507 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | |
| 3510 | if (mddev->pers) |
| 3511 | return -EBUSY; |
| 3512 | |
| 3513 | /* |
| 3514 | * Analyze all RAID superblock(s) |
| 3515 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3516 | if (!mddev->raid_disks) { |
| 3517 | if (!mddev->persistent) |
| 3518 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3519 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | |
| 3522 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3524 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3526 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3527 | chunk_size, MAX_CHUNK_SIZE); |
| 3528 | return -EINVAL; |
| 3529 | } |
| 3530 | /* |
| 3531 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3532 | */ |
| 3533 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3534 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | return -EINVAL; |
| 3536 | } |
| 3537 | if (chunk_size < PAGE_SIZE) { |
| 3538 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3539 | chunk_size, PAGE_SIZE); |
| 3540 | return -EINVAL; |
| 3541 | } |
| 3542 | |
| 3543 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3544 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3545 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | continue; |
| 3547 | if (rdev->size < chunk_size / 1024) { |
| 3548 | printk(KERN_WARNING |
| 3549 | "md: Dev %s smaller than chunk_size:" |
| 3550 | " %lluk < %dk\n", |
| 3551 | bdevname(rdev->bdev,b), |
| 3552 | (unsigned long long)rdev->size, |
| 3553 | chunk_size / 1024); |
| 3554 | return -EINVAL; |
| 3555 | } |
| 3556 | } |
| 3557 | } |
| 3558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3560 | if (mddev->level != LEVEL_NONE) |
| 3561 | request_module("md-level-%d", mddev->level); |
| 3562 | else if (mddev->clevel[0]) |
| 3563 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | #endif |
| 3565 | |
| 3566 | /* |
| 3567 | * Drop all container device buffers, from now on |
| 3568 | * the only valid external interface is through the md |
| 3569 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3571 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3572 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | continue; |
| 3574 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3575 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3576 | |
| 3577 | /* perform some consistency tests on the device. |
| 3578 | * We don't want the data to overlap the metadata, |
| 3579 | * Internal Bitmap issues has handled elsewhere. |
| 3580 | */ |
| 3581 | if (rdev->data_offset < rdev->sb_offset) { |
| 3582 | if (mddev->size && |
| 3583 | rdev->data_offset + mddev->size*2 |
| 3584 | > rdev->sb_offset*2) { |
| 3585 | printk("md: %s: data overlaps metadata\n", |
| 3586 | mdname(mddev)); |
| 3587 | return -EINVAL; |
| 3588 | } |
| 3589 | } else { |
| 3590 | if (rdev->sb_offset*2 + rdev->sb_size/512 |
| 3591 | > rdev->data_offset) { |
| 3592 | printk("md: %s: metadata overlaps data\n", |
| 3593 | mdname(mddev)); |
| 3594 | return -EINVAL; |
| 3595 | } |
| 3596 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 3597 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | md_probe(mddev->unit, NULL, NULL); |
| 3601 | disk = mddev->gendisk; |
| 3602 | if (!disk) |
| 3603 | return -ENOMEM; |
| 3604 | |
| 3605 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3606 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3607 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3609 | if (mddev->level != LEVEL_NONE) |
| 3610 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3611 | mddev->level); |
| 3612 | else |
| 3613 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3614 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | return -EINVAL; |
| 3616 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3617 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3619 | mddev->level = pers->level; |
| 3620 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3622 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3623 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3624 | /* This personality cannot handle reshaping... */ |
| 3625 | mddev->pers = NULL; |
| 3626 | module_put(pers->owner); |
| 3627 | return -EINVAL; |
| 3628 | } |
| 3629 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3630 | if (pers->sync_request) { |
| 3631 | /* Warn if this is a potentially silly |
| 3632 | * configuration. |
| 3633 | */ |
| 3634 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3635 | mdk_rdev_t *rdev2; |
| 3636 | struct list_head *tmp2; |
| 3637 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3638 | rdev_for_each(rdev, tmp, mddev) { |
| 3639 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3640 | if (rdev < rdev2 && |
| 3641 | rdev->bdev->bd_contains == |
| 3642 | rdev2->bdev->bd_contains) { |
| 3643 | printk(KERN_WARNING |
| 3644 | "%s: WARNING: %s appears to be" |
| 3645 | " on the same physical disk as" |
| 3646 | " %s.\n", |
| 3647 | mdname(mddev), |
| 3648 | bdevname(rdev->bdev,b), |
| 3649 | bdevname(rdev2->bdev,b2)); |
| 3650 | warned = 1; |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | if (warned) |
| 3655 | printk(KERN_WARNING |
| 3656 | "True protection against single-disk" |
| 3657 | " failure might be compromised.\n"); |
| 3658 | } |
| 3659 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3660 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3662 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3663 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3665 | if (start_readonly) |
| 3666 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3667 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3668 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 3669 | if (err) |
| 3670 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3671 | else if (mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3672 | err = bitmap_create(mddev); |
| 3673 | if (err) { |
| 3674 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3675 | mdname(mddev), err); |
| 3676 | mddev->pers->stop(mddev); |
| 3677 | } |
| 3678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | module_put(mddev->pers->owner); |
| 3681 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3682 | bitmap_destroy(mddev); |
| 3683 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3685 | if (mddev->pers->sync_request) { |
| 3686 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3687 | printk(KERN_WARNING |
| 3688 | "md: cannot register extra attributes for %s\n", |
| 3689 | mdname(mddev)); |
| 3690 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3691 | mddev->ro = 0; |
| 3692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | atomic_set(&mddev->writes_pending,0); |
| 3694 | mddev->safemode = 0; |
| 3695 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3696 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3697 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3699 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3700 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3701 | if (rdev->raid_disk >= 0) { |
| 3702 | char nm[20]; |
| 3703 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3704 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3705 | printk("md: cannot register %s for %s\n", |
| 3706 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
| 3709 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3710 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3711 | if (mddev->flags) |
| 3712 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
| 3714 | set_capacity(disk, mddev->array_size<<1); |
| 3715 | |
| 3716 | /* If we call blk_queue_make_request here, it will |
| 3717 | * re-initialise max_sectors etc which may have been |
| 3718 | * refined inside -> run. So just set the bits we need to set. |
| 3719 | * Most initialisation happended when we called |
| 3720 | * blk_queue_make_request(..., md_fail_request) |
| 3721 | * earlier. |
| 3722 | */ |
| 3723 | mddev->queue->queuedata = mddev; |
| 3724 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3725 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3726 | /* If there is a partially-recovered drive we need to |
| 3727 | * start recovery here. If we leave it to md_check_recovery, |
| 3728 | * it will remove the drives and not do the right thing |
| 3729 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3730 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3731 | struct list_head *rtmp; |
| 3732 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3733 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3734 | if (rdev->raid_disk >= 0 && |
| 3735 | !test_bit(In_sync, &rdev->flags) && |
| 3736 | !test_bit(Faulty, &rdev->flags)) |
| 3737 | /* complete an interrupted recovery */ |
| 3738 | spares++; |
| 3739 | if (spares && mddev->pers->sync_request) { |
| 3740 | mddev->recovery = 0; |
| 3741 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3742 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3743 | mddev, |
| 3744 | "%s_resync"); |
| 3745 | if (!mddev->sync_thread) { |
| 3746 | printk(KERN_ERR "%s: could not start resync" |
| 3747 | " thread...\n", |
| 3748 | mdname(mddev)); |
| 3749 | /* leave the spares where they are, it shouldn't hurt */ |
| 3750 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3751 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3752 | } |
| 3753 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3754 | md_wakeup_thread(mddev->thread); |
| 3755 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3756 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3757 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3758 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3759 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3760 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3761 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3762 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | return 0; |
| 3764 | } |
| 3765 | |
| 3766 | static int restart_array(mddev_t *mddev) |
| 3767 | { |
| 3768 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3770 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3772 | return -ENXIO; |
| 3773 | if (!mddev->pers) |
| 3774 | return -EINVAL; |
| 3775 | if (!mddev->ro) |
| 3776 | return -EBUSY; |
| 3777 | mddev->safemode = 0; |
| 3778 | mddev->ro = 0; |
| 3779 | set_disk_ro(disk, 0); |
| 3780 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3781 | mdname(mddev)); |
| 3782 | /* Kick recovery or resync if necessary */ |
| 3783 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3784 | md_wakeup_thread(mddev->thread); |
| 3785 | md_wakeup_thread(mddev->sync_thread); |
| 3786 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3787 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | } |
| 3789 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3790 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3791 | * to the file ourselves */ |
| 3792 | static int deny_bitmap_write_access(struct file * file) |
| 3793 | { |
| 3794 | struct inode *inode = file->f_mapping->host; |
| 3795 | |
| 3796 | spin_lock(&inode->i_lock); |
| 3797 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3798 | spin_unlock(&inode->i_lock); |
| 3799 | return -ETXTBSY; |
| 3800 | } |
| 3801 | atomic_set(&inode->i_writecount, -1); |
| 3802 | spin_unlock(&inode->i_lock); |
| 3803 | |
| 3804 | return 0; |
| 3805 | } |
| 3806 | |
| 3807 | static void restore_bitmap_write_access(struct file *file) |
| 3808 | { |
| 3809 | struct inode *inode = file->f_mapping->host; |
| 3810 | |
| 3811 | spin_lock(&inode->i_lock); |
| 3812 | atomic_set(&inode->i_writecount, 1); |
| 3813 | spin_unlock(&inode->i_lock); |
| 3814 | } |
| 3815 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3816 | /* mode: |
| 3817 | * 0 - completely stop and dis-assemble array |
| 3818 | * 1 - switch to readonly |
| 3819 | * 2 - stop but do not disassemble array |
| 3820 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 3821 | 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] | 3822 | { |
| 3823 | int err = 0; |
| 3824 | struct gendisk *disk = mddev->gendisk; |
| 3825 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 3826 | if (atomic_read(&mddev->active) > 1 + is_open) { |
| 3827 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3828 | return -EBUSY; |
| 3829 | } |
| 3830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | |
| 3833 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3834 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3836 | md_unregister_thread(mddev->sync_thread); |
| 3837 | mddev->sync_thread = NULL; |
| 3838 | } |
| 3839 | |
| 3840 | del_timer_sync(&mddev->safemode_timer); |
| 3841 | |
| 3842 | invalidate_partition(disk, 0); |
| 3843 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3844 | switch(mode) { |
| 3845 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3847 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | goto out; |
| 3849 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3850 | break; |
| 3851 | case 0: /* disassemble */ |
| 3852 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3853 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3854 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | if (mddev->ro) |
| 3856 | set_disk_ro(disk, 0); |
| 3857 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3858 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3859 | mddev->queue->merge_bvec_fn = NULL; |
| 3860 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3861 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3862 | if (mddev->pers->sync_request) |
| 3863 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 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' */ |
| 3868 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3869 | |
| 3870 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 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 | |
| 3915 | mddev->array_size = 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 | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3948 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3949 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3951 | mdname(mddev)); |
| 3952 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3953 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3954 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | out: |
| 3956 | return err; |
| 3957 | } |
| 3958 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3959 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | static void autorun_array(mddev_t *mddev) |
| 3961 | { |
| 3962 | mdk_rdev_t *rdev; |
| 3963 | struct list_head *tmp; |
| 3964 | int err; |
| 3965 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3966 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | |
| 3969 | printk(KERN_INFO "md: running: "); |
| 3970 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3971 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | char b[BDEVNAME_SIZE]; |
| 3973 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3974 | } |
| 3975 | printk("\n"); |
| 3976 | |
| 3977 | err = do_md_run (mddev); |
| 3978 | if (err) { |
| 3979 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 3980 | do_md_stop (mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * lets try to run arrays based on all disks that have arrived |
| 3986 | * until now. (those are in pending_raid_disks) |
| 3987 | * |
| 3988 | * the method: pick the first pending disk, collect all disks with |
| 3989 | * the same UUID, remove all from the pending list and put them into |
| 3990 | * the 'same_array' list. Then order this list based on superblock |
| 3991 | * update time (freshest comes first), kick out 'old' disks and |
| 3992 | * compare superblocks. If everything's fine then run it. |
| 3993 | * |
| 3994 | * If "unit" is allocated, then bump its reference count |
| 3995 | */ |
| 3996 | static void autorun_devices(int part) |
| 3997 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | struct list_head *tmp; |
| 3999 | mdk_rdev_t *rdev0, *rdev; |
| 4000 | mddev_t *mddev; |
| 4001 | char b[BDEVNAME_SIZE]; |
| 4002 | |
| 4003 | printk(KERN_INFO "md: autorun ...\n"); |
| 4004 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4005 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4007 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | rdev0 = list_entry(pending_raid_disks.next, |
| 4009 | mdk_rdev_t, same_set); |
| 4010 | |
| 4011 | printk(KERN_INFO "md: considering %s ...\n", |
| 4012 | bdevname(rdev0->bdev,b)); |
| 4013 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4014 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4016 | printk(KERN_INFO "md: adding %s ...\n", |
| 4017 | bdevname(rdev->bdev,b)); |
| 4018 | list_move(&rdev->same_set, &candidates); |
| 4019 | } |
| 4020 | /* |
| 4021 | * now we have a set of devices, with all of them having |
| 4022 | * mostly sane superblocks. It's time to allocate the |
| 4023 | * mddev. |
| 4024 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4025 | if (part) { |
| 4026 | dev = MKDEV(mdp_major, |
| 4027 | rdev0->preferred_minor << MdpMinorShift); |
| 4028 | unit = MINOR(dev) >> MdpMinorShift; |
| 4029 | } else { |
| 4030 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4031 | unit = MINOR(dev); |
| 4032 | } |
| 4033 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4035 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4036 | break; |
| 4037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | |
| 4039 | md_probe(dev, NULL, NULL); |
| 4040 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4041 | if (!mddev || !mddev->gendisk) { |
| 4042 | if (mddev) |
| 4043 | mddev_put(mddev); |
| 4044 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | "md: cannot allocate memory for md drive.\n"); |
| 4046 | break; |
| 4047 | } |
| 4048 | if (mddev_lock(mddev)) |
| 4049 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4050 | mdname(mddev)); |
| 4051 | else if (mddev->raid_disks || mddev->major_version |
| 4052 | || !list_empty(&mddev->disks)) { |
| 4053 | printk(KERN_WARNING |
| 4054 | "md: %s already running, cannot run %s\n", |
| 4055 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4056 | mddev_unlock(mddev); |
| 4057 | } else { |
| 4058 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4059 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4060 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | list_del_init(&rdev->same_set); |
| 4062 | if (bind_rdev_to_array(rdev, mddev)) |
| 4063 | export_rdev(rdev); |
| 4064 | } |
| 4065 | autorun_array(mddev); |
| 4066 | mddev_unlock(mddev); |
| 4067 | } |
| 4068 | /* on success, candidates will be empty, on error |
| 4069 | * it won't... |
| 4070 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4071 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | export_rdev(rdev); |
| 4073 | mddev_put(mddev); |
| 4074 | } |
| 4075 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4076 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4077 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | static int get_version(void __user * arg) |
| 4080 | { |
| 4081 | mdu_version_t ver; |
| 4082 | |
| 4083 | ver.major = MD_MAJOR_VERSION; |
| 4084 | ver.minor = MD_MINOR_VERSION; |
| 4085 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4086 | |
| 4087 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4088 | return -EFAULT; |
| 4089 | |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4094 | { |
| 4095 | mdu_array_info_t info; |
| 4096 | int nr,working,active,failed,spare; |
| 4097 | mdk_rdev_t *rdev; |
| 4098 | struct list_head *tmp; |
| 4099 | |
| 4100 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4101 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4103 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | failed++; |
| 4105 | else { |
| 4106 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4107 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | active++; |
| 4109 | else |
| 4110 | spare++; |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | info.major_version = mddev->major_version; |
| 4115 | info.minor_version = mddev->minor_version; |
| 4116 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4117 | info.ctime = mddev->ctime; |
| 4118 | info.level = mddev->level; |
| 4119 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4120 | if (info.size != mddev->size) /* overflow */ |
| 4121 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | info.nr_disks = nr; |
| 4123 | info.raid_disks = mddev->raid_disks; |
| 4124 | info.md_minor = mddev->md_minor; |
| 4125 | info.not_persistent= !mddev->persistent; |
| 4126 | |
| 4127 | info.utime = mddev->utime; |
| 4128 | info.state = 0; |
| 4129 | if (mddev->in_sync) |
| 4130 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4131 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4132 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | info.active_disks = active; |
| 4134 | info.working_disks = working; |
| 4135 | info.failed_disks = failed; |
| 4136 | info.spare_disks = spare; |
| 4137 | |
| 4138 | info.layout = mddev->layout; |
| 4139 | info.chunk_size = mddev->chunk_size; |
| 4140 | |
| 4141 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4142 | return -EFAULT; |
| 4143 | |
| 4144 | return 0; |
| 4145 | } |
| 4146 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4147 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4148 | { |
| 4149 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4150 | char *ptr, *buf = NULL; |
| 4151 | int err = -ENOMEM; |
| 4152 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4153 | if (md_allow_write(mddev)) |
| 4154 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4155 | else |
| 4156 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4157 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4158 | if (!file) |
| 4159 | goto out; |
| 4160 | |
| 4161 | /* bitmap disabled, zero the first byte and copy out */ |
| 4162 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4163 | file->pathname[0] = '\0'; |
| 4164 | goto copy_out; |
| 4165 | } |
| 4166 | |
| 4167 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4168 | if (!buf) |
| 4169 | goto out; |
| 4170 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4171 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4172 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4173 | goto out; |
| 4174 | |
| 4175 | strcpy(file->pathname, ptr); |
| 4176 | |
| 4177 | copy_out: |
| 4178 | err = 0; |
| 4179 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4180 | err = -EFAULT; |
| 4181 | out: |
| 4182 | kfree(buf); |
| 4183 | kfree(file); |
| 4184 | return err; |
| 4185 | } |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4188 | { |
| 4189 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | mdk_rdev_t *rdev; |
| 4191 | |
| 4192 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4193 | return -EFAULT; |
| 4194 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4195 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | if (rdev) { |
| 4197 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4198 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4199 | info.raid_disk = rdev->raid_disk; |
| 4200 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4201 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4203 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4205 | info.state |= (1<<MD_DISK_SYNC); |
| 4206 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4207 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4208 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | } else { |
| 4210 | info.major = info.minor = 0; |
| 4211 | info.raid_disk = -1; |
| 4212 | info.state = (1<<MD_DISK_REMOVED); |
| 4213 | } |
| 4214 | |
| 4215 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4216 | return -EFAULT; |
| 4217 | |
| 4218 | return 0; |
| 4219 | } |
| 4220 | |
| 4221 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4222 | { |
| 4223 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4224 | mdk_rdev_t *rdev; |
| 4225 | dev_t dev = MKDEV(info->major,info->minor); |
| 4226 | |
| 4227 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4228 | return -EOVERFLOW; |
| 4229 | |
| 4230 | if (!mddev->raid_disks) { |
| 4231 | int err; |
| 4232 | /* expecting a device which has a superblock */ |
| 4233 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4234 | if (IS_ERR(rdev)) { |
| 4235 | printk(KERN_WARNING |
| 4236 | "md: md_import_device returned %ld\n", |
| 4237 | PTR_ERR(rdev)); |
| 4238 | return PTR_ERR(rdev); |
| 4239 | } |
| 4240 | if (!list_empty(&mddev->disks)) { |
| 4241 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4242 | mdk_rdev_t, same_set); |
| 4243 | int err = super_types[mddev->major_version] |
| 4244 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4245 | if (err < 0) { |
| 4246 | printk(KERN_WARNING |
| 4247 | "md: %s has different UUID to %s\n", |
| 4248 | bdevname(rdev->bdev,b), |
| 4249 | bdevname(rdev0->bdev,b2)); |
| 4250 | export_rdev(rdev); |
| 4251 | return -EINVAL; |
| 4252 | } |
| 4253 | } |
| 4254 | err = bind_rdev_to_array(rdev, mddev); |
| 4255 | if (err) |
| 4256 | export_rdev(rdev); |
| 4257 | return err; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * add_new_disk can be used once the array is assembled |
| 4262 | * to add "hot spares". They must already have a superblock |
| 4263 | * written |
| 4264 | */ |
| 4265 | if (mddev->pers) { |
| 4266 | int err; |
| 4267 | if (!mddev->pers->hot_add_disk) { |
| 4268 | printk(KERN_WARNING |
| 4269 | "%s: personality does not support diskops!\n", |
| 4270 | mdname(mddev)); |
| 4271 | return -EINVAL; |
| 4272 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4273 | if (mddev->persistent) |
| 4274 | rdev = md_import_device(dev, mddev->major_version, |
| 4275 | mddev->minor_version); |
| 4276 | else |
| 4277 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | if (IS_ERR(rdev)) { |
| 4279 | printk(KERN_WARNING |
| 4280 | "md: md_import_device returned %ld\n", |
| 4281 | PTR_ERR(rdev)); |
| 4282 | return PTR_ERR(rdev); |
| 4283 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4284 | /* set save_raid_disk if appropriate */ |
| 4285 | if (!mddev->persistent) { |
| 4286 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4287 | info->raid_disk < mddev->raid_disks) |
| 4288 | rdev->raid_disk = info->raid_disk; |
| 4289 | else |
| 4290 | rdev->raid_disk = -1; |
| 4291 | } else |
| 4292 | super_types[mddev->major_version]. |
| 4293 | validate_super(mddev, rdev); |
| 4294 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4295 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4296 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4297 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4298 | set_bit(WriteMostly, &rdev->flags); |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | rdev->raid_disk = -1; |
| 4301 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4302 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4303 | /* If there is hot_add_disk but no hot_remove_disk |
| 4304 | * then added disks for geometry changes, |
| 4305 | * and should be added immediately. |
| 4306 | */ |
| 4307 | super_types[mddev->major_version]. |
| 4308 | validate_super(mddev, rdev); |
| 4309 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4310 | if (err) |
| 4311 | unbind_rdev_from_array(rdev); |
| 4312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | if (err) |
| 4314 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4315 | else |
| 4316 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4317 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4318 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4319 | if (mddev->degraded) |
| 4320 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4321 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4322 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | return err; |
| 4324 | } |
| 4325 | |
| 4326 | /* otherwise, add_new_disk is only allowed |
| 4327 | * for major_version==0 superblocks |
| 4328 | */ |
| 4329 | if (mddev->major_version != 0) { |
| 4330 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4331 | mdname(mddev)); |
| 4332 | return -EINVAL; |
| 4333 | } |
| 4334 | |
| 4335 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4336 | int err; |
| 4337 | rdev = md_import_device (dev, -1, 0); |
| 4338 | if (IS_ERR(rdev)) { |
| 4339 | printk(KERN_WARNING |
| 4340 | "md: error, md_import_device() returned %ld\n", |
| 4341 | PTR_ERR(rdev)); |
| 4342 | return PTR_ERR(rdev); |
| 4343 | } |
| 4344 | rdev->desc_nr = info->number; |
| 4345 | if (info->raid_disk < mddev->raid_disks) |
| 4346 | rdev->raid_disk = info->raid_disk; |
| 4347 | else |
| 4348 | rdev->raid_disk = -1; |
| 4349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4351 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4352 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4354 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4355 | set_bit(WriteMostly, &rdev->flags); |
| 4356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | if (!mddev->persistent) { |
| 4358 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4359 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4360 | } else |
| 4361 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4362 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4363 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4364 | err = bind_rdev_to_array(rdev, mddev); |
| 4365 | if (err) { |
| 4366 | export_rdev(rdev); |
| 4367 | return err; |
| 4368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | return 0; |
| 4372 | } |
| 4373 | |
| 4374 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4375 | { |
| 4376 | char b[BDEVNAME_SIZE]; |
| 4377 | mdk_rdev_t *rdev; |
| 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | rdev = find_rdev(mddev, dev); |
| 4380 | if (!rdev) |
| 4381 | return -ENXIO; |
| 4382 | |
| 4383 | if (rdev->raid_disk >= 0) |
| 4384 | goto busy; |
| 4385 | |
| 4386 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4387 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4388 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | |
| 4390 | return 0; |
| 4391 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4392 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4394 | return -EBUSY; |
| 4395 | } |
| 4396 | |
| 4397 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4398 | { |
| 4399 | char b[BDEVNAME_SIZE]; |
| 4400 | int err; |
| 4401 | unsigned int size; |
| 4402 | mdk_rdev_t *rdev; |
| 4403 | |
| 4404 | if (!mddev->pers) |
| 4405 | return -ENODEV; |
| 4406 | |
| 4407 | if (mddev->major_version != 0) { |
| 4408 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4409 | " version-0 superblocks.\n", |
| 4410 | mdname(mddev)); |
| 4411 | return -EINVAL; |
| 4412 | } |
| 4413 | if (!mddev->pers->hot_add_disk) { |
| 4414 | printk(KERN_WARNING |
| 4415 | "%s: personality does not support diskops!\n", |
| 4416 | mdname(mddev)); |
| 4417 | return -EINVAL; |
| 4418 | } |
| 4419 | |
| 4420 | rdev = md_import_device (dev, -1, 0); |
| 4421 | if (IS_ERR(rdev)) { |
| 4422 | printk(KERN_WARNING |
| 4423 | "md: error, md_import_device() returned %ld\n", |
| 4424 | PTR_ERR(rdev)); |
| 4425 | return -EINVAL; |
| 4426 | } |
| 4427 | |
| 4428 | if (mddev->persistent) |
| 4429 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4430 | else |
| 4431 | rdev->sb_offset = |
| 4432 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4433 | |
| 4434 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4435 | rdev->size = size; |
| 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 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4620 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4621 | { |
| 4622 | mdk_rdev_t * rdev; |
| 4623 | int rv; |
| 4624 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4625 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4626 | |
| 4627 | if (mddev->pers->resize == NULL) |
| 4628 | return -EINVAL; |
| 4629 | /* The "size" is the amount of each device that is used. |
| 4630 | * This can only make sense for arrays with redundancy. |
| 4631 | * linear and raid0 always use whatever space is available |
| 4632 | * We can only consider changing the size if no resync |
| 4633 | * or reconstruction is happening, and if the new size |
| 4634 | * is acceptable. It must fit before the sb_offset or, |
| 4635 | * if that is <data_offset, it must fit before the |
| 4636 | * size of each device. |
| 4637 | * If size is zero, we find the largest size that fits. |
| 4638 | */ |
| 4639 | if (mddev->sync_thread) |
| 4640 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4641 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4642 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4643 | avail = rdev->size * 2; |
| 4644 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4645 | if (fit && (size == 0 || size > avail/2)) |
| 4646 | size = avail/2; |
| 4647 | if (avail < ((sector_t)size << 1)) |
| 4648 | return -ENOSPC; |
| 4649 | } |
| 4650 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4651 | if (!rv) { |
| 4652 | struct block_device *bdev; |
| 4653 | |
| 4654 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4655 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4656 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4657 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4658 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4659 | bdput(bdev); |
| 4660 | } |
| 4661 | } |
| 4662 | return rv; |
| 4663 | } |
| 4664 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4665 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4666 | { |
| 4667 | int rv; |
| 4668 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4669 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4670 | return -EINVAL; |
| 4671 | if (raid_disks <= 0 || |
| 4672 | raid_disks >= mddev->max_disks) |
| 4673 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4674 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4675 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4676 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4677 | |
| 4678 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4679 | return rv; |
| 4680 | } |
| 4681 | |
| 4682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | /* |
| 4684 | * update_array_info is used to change the configuration of an |
| 4685 | * on-line array. |
| 4686 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4687 | * fields in the info are checked against the array. |
| 4688 | * Any differences that cannot be handled will cause an error. |
| 4689 | * Normally, only one change can be managed at a time. |
| 4690 | */ |
| 4691 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4692 | { |
| 4693 | int rv = 0; |
| 4694 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4695 | int state = 0; |
| 4696 | |
| 4697 | /* calculate expected state,ignoring low bits */ |
| 4698 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4699 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | |
| 4701 | if (mddev->major_version != info->major_version || |
| 4702 | mddev->minor_version != info->minor_version || |
| 4703 | /* mddev->patch_version != info->patch_version || */ |
| 4704 | mddev->ctime != info->ctime || |
| 4705 | mddev->level != info->level || |
| 4706 | /* mddev->layout != info->layout || */ |
| 4707 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4708 | mddev->chunk_size != info->chunk_size || |
| 4709 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4710 | ((state^info->state) & 0xfffffe00) |
| 4711 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | return -EINVAL; |
| 4713 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4714 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4716 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4717 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | if (cnt == 0) return 0; |
| 4719 | if (cnt > 1) return -EINVAL; |
| 4720 | |
| 4721 | if (mddev->layout != info->layout) { |
| 4722 | /* Change layout |
| 4723 | * we don't need to do anything at the md level, the |
| 4724 | * personality will take care of it all. |
| 4725 | */ |
| 4726 | if (mddev->pers->reconfig == NULL) |
| 4727 | return -EINVAL; |
| 4728 | else |
| 4729 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4730 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4731 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4732 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4734 | if (mddev->raid_disks != info->raid_disks) |
| 4735 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4737 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4738 | if (mddev->pers->quiesce == NULL) |
| 4739 | return -EINVAL; |
| 4740 | if (mddev->recovery || mddev->sync_thread) |
| 4741 | return -EBUSY; |
| 4742 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4743 | /* add the bitmap */ |
| 4744 | if (mddev->bitmap) |
| 4745 | return -EEXIST; |
| 4746 | if (mddev->default_bitmap_offset == 0) |
| 4747 | return -EINVAL; |
| 4748 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4749 | mddev->pers->quiesce(mddev, 1); |
| 4750 | rv = bitmap_create(mddev); |
| 4751 | if (rv) |
| 4752 | bitmap_destroy(mddev); |
| 4753 | mddev->pers->quiesce(mddev, 0); |
| 4754 | } else { |
| 4755 | /* remove the bitmap */ |
| 4756 | if (!mddev->bitmap) |
| 4757 | return -ENOENT; |
| 4758 | if (mddev->bitmap->file) |
| 4759 | return -EINVAL; |
| 4760 | mddev->pers->quiesce(mddev, 1); |
| 4761 | bitmap_destroy(mddev); |
| 4762 | mddev->pers->quiesce(mddev, 0); |
| 4763 | mddev->bitmap_offset = 0; |
| 4764 | } |
| 4765 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4766 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | return rv; |
| 4768 | } |
| 4769 | |
| 4770 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4771 | { |
| 4772 | mdk_rdev_t *rdev; |
| 4773 | |
| 4774 | if (mddev->pers == NULL) |
| 4775 | return -ENODEV; |
| 4776 | |
| 4777 | rdev = find_rdev(mddev, dev); |
| 4778 | if (!rdev) |
| 4779 | return -ENODEV; |
| 4780 | |
| 4781 | md_error(mddev, rdev); |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 4785 | /* |
| 4786 | * We have a problem here : there is no easy way to give a CHS |
| 4787 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4788 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4789 | * dosfs just mad... ;-) |
| 4790 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4791 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4792 | { |
| 4793 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4794 | |
| 4795 | geo->heads = 2; |
| 4796 | geo->sectors = 4; |
| 4797 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4802 | unsigned int cmd, unsigned long arg) |
| 4803 | { |
| 4804 | int err = 0; |
| 4805 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | mddev_t *mddev = NULL; |
| 4807 | |
| 4808 | if (!capable(CAP_SYS_ADMIN)) |
| 4809 | return -EACCES; |
| 4810 | |
| 4811 | /* |
| 4812 | * Commands dealing with the RAID driver but not any |
| 4813 | * particular array: |
| 4814 | */ |
| 4815 | switch (cmd) |
| 4816 | { |
| 4817 | case RAID_VERSION: |
| 4818 | err = get_version(argp); |
| 4819 | goto done; |
| 4820 | |
| 4821 | case PRINT_RAID_DEBUG: |
| 4822 | err = 0; |
| 4823 | md_print_devices(); |
| 4824 | goto done; |
| 4825 | |
| 4826 | #ifndef MODULE |
| 4827 | case RAID_AUTORUN: |
| 4828 | err = 0; |
| 4829 | autostart_arrays(arg); |
| 4830 | goto done; |
| 4831 | #endif |
| 4832 | default:; |
| 4833 | } |
| 4834 | |
| 4835 | /* |
| 4836 | * Commands creating/starting a new array: |
| 4837 | */ |
| 4838 | |
| 4839 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4840 | |
| 4841 | if (!mddev) { |
| 4842 | BUG(); |
| 4843 | goto abort; |
| 4844 | } |
| 4845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | err = mddev_lock(mddev); |
| 4847 | if (err) { |
| 4848 | printk(KERN_INFO |
| 4849 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4850 | err, cmd); |
| 4851 | goto abort; |
| 4852 | } |
| 4853 | |
| 4854 | switch (cmd) |
| 4855 | { |
| 4856 | case SET_ARRAY_INFO: |
| 4857 | { |
| 4858 | mdu_array_info_t info; |
| 4859 | if (!arg) |
| 4860 | memset(&info, 0, sizeof(info)); |
| 4861 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4862 | err = -EFAULT; |
| 4863 | goto abort_unlock; |
| 4864 | } |
| 4865 | if (mddev->pers) { |
| 4866 | err = update_array_info(mddev, &info); |
| 4867 | if (err) { |
| 4868 | printk(KERN_WARNING "md: couldn't update" |
| 4869 | " array info. %d\n", err); |
| 4870 | goto abort_unlock; |
| 4871 | } |
| 4872 | goto done_unlock; |
| 4873 | } |
| 4874 | if (!list_empty(&mddev->disks)) { |
| 4875 | printk(KERN_WARNING |
| 4876 | "md: array %s already has disks!\n", |
| 4877 | mdname(mddev)); |
| 4878 | err = -EBUSY; |
| 4879 | goto abort_unlock; |
| 4880 | } |
| 4881 | if (mddev->raid_disks) { |
| 4882 | printk(KERN_WARNING |
| 4883 | "md: array %s already initialised!\n", |
| 4884 | mdname(mddev)); |
| 4885 | err = -EBUSY; |
| 4886 | goto abort_unlock; |
| 4887 | } |
| 4888 | err = set_array_info(mddev, &info); |
| 4889 | if (err) { |
| 4890 | printk(KERN_WARNING "md: couldn't set" |
| 4891 | " array info. %d\n", err); |
| 4892 | goto abort_unlock; |
| 4893 | } |
| 4894 | } |
| 4895 | goto done_unlock; |
| 4896 | |
| 4897 | default:; |
| 4898 | } |
| 4899 | |
| 4900 | /* |
| 4901 | * Commands querying/configuring an existing array: |
| 4902 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4903 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4904 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4905 | if ((!mddev->raid_disks && !mddev->external) |
| 4906 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4907 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4908 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | err = -ENODEV; |
| 4910 | goto abort_unlock; |
| 4911 | } |
| 4912 | |
| 4913 | /* |
| 4914 | * Commands even a read-only array can execute: |
| 4915 | */ |
| 4916 | switch (cmd) |
| 4917 | { |
| 4918 | case GET_ARRAY_INFO: |
| 4919 | err = get_array_info(mddev, argp); |
| 4920 | goto done_unlock; |
| 4921 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4922 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4923 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4924 | goto done_unlock; |
| 4925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | case GET_DISK_INFO: |
| 4927 | err = get_disk_info(mddev, argp); |
| 4928 | goto done_unlock; |
| 4929 | |
| 4930 | case RESTART_ARRAY_RW: |
| 4931 | err = restart_array(mddev); |
| 4932 | goto done_unlock; |
| 4933 | |
| 4934 | case STOP_ARRAY: |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 4935 | err = do_md_stop (mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | goto done_unlock; |
| 4937 | |
| 4938 | case STOP_ARRAY_RO: |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 4939 | err = do_md_stop (mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | goto done_unlock; |
| 4941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /* |
| 4945 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4946 | * superblock, so we do not allow them on read-only arrays. |
| 4947 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4948 | * here and hit the 'default' below, so only disallow |
| 4949 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 4951 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4952 | if (mddev->ro == 2) { |
| 4953 | mddev->ro = 0; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4954 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 4955 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4956 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4957 | } else { |
| 4958 | err = -EROFS; |
| 4959 | goto abort_unlock; |
| 4960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | switch (cmd) |
| 4964 | { |
| 4965 | case ADD_NEW_DISK: |
| 4966 | { |
| 4967 | mdu_disk_info_t info; |
| 4968 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4969 | err = -EFAULT; |
| 4970 | else |
| 4971 | err = add_new_disk(mddev, &info); |
| 4972 | goto done_unlock; |
| 4973 | } |
| 4974 | |
| 4975 | case HOT_REMOVE_DISK: |
| 4976 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4977 | goto done_unlock; |
| 4978 | |
| 4979 | case HOT_ADD_DISK: |
| 4980 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4981 | goto done_unlock; |
| 4982 | |
| 4983 | case SET_DISK_FAULTY: |
| 4984 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4985 | goto done_unlock; |
| 4986 | |
| 4987 | case RUN_ARRAY: |
| 4988 | err = do_md_run (mddev); |
| 4989 | goto done_unlock; |
| 4990 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4991 | case SET_BITMAP_FILE: |
| 4992 | err = set_bitmap_file(mddev, (int)arg); |
| 4993 | goto done_unlock; |
| 4994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | err = -EINVAL; |
| 4997 | goto abort_unlock; |
| 4998 | } |
| 4999 | |
| 5000 | done_unlock: |
| 5001 | abort_unlock: |
| 5002 | mddev_unlock(mddev); |
| 5003 | |
| 5004 | return err; |
| 5005 | done: |
| 5006 | if (err) |
| 5007 | MD_BUG(); |
| 5008 | abort: |
| 5009 | return err; |
| 5010 | } |
| 5011 | |
| 5012 | static int md_open(struct inode *inode, struct file *file) |
| 5013 | { |
| 5014 | /* |
| 5015 | * Succeed if we can lock the mddev, which confirms that |
| 5016 | * it isn't being stopped right now. |
| 5017 | */ |
| 5018 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5019 | int err; |
| 5020 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5021 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | goto out; |
| 5023 | |
| 5024 | err = 0; |
| 5025 | mddev_get(mddev); |
| 5026 | mddev_unlock(mddev); |
| 5027 | |
| 5028 | check_disk_change(inode->i_bdev); |
| 5029 | out: |
| 5030 | return err; |
| 5031 | } |
| 5032 | |
| 5033 | static int md_release(struct inode *inode, struct file * file) |
| 5034 | { |
| 5035 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5036 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5037 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | mddev_put(mddev); |
| 5039 | |
| 5040 | return 0; |
| 5041 | } |
| 5042 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5043 | static int md_media_changed(struct gendisk *disk) |
| 5044 | { |
| 5045 | mddev_t *mddev = disk->private_data; |
| 5046 | |
| 5047 | return mddev->changed; |
| 5048 | } |
| 5049 | |
| 5050 | static int md_revalidate(struct gendisk *disk) |
| 5051 | { |
| 5052 | mddev_t *mddev = disk->private_data; |
| 5053 | |
| 5054 | mddev->changed = 0; |
| 5055 | return 0; |
| 5056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | static struct block_device_operations md_fops = |
| 5058 | { |
| 5059 | .owner = THIS_MODULE, |
| 5060 | .open = md_open, |
| 5061 | .release = md_release, |
| 5062 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5063 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5064 | .media_changed = md_media_changed, |
| 5065 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | }; |
| 5067 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5068 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | { |
| 5070 | mdk_thread_t *thread = arg; |
| 5071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | /* |
| 5073 | * md_thread is a 'system-thread', it's priority should be very |
| 5074 | * high. We avoid resource deadlocks individually in each |
| 5075 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5076 | * the very same RT priority as kswapd, thus we will never get |
| 5077 | * into a priority inversion deadlock. |
| 5078 | * |
| 5079 | * we definitely have to have equal or higher priority than |
| 5080 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5081 | * many dirty RAID5 blocks. |
| 5082 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5084 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5085 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5087 | /* We need to wait INTERRUPTIBLE so that |
| 5088 | * we don't add to the load-average. |
| 5089 | * That means we need to be sure no signals are |
| 5090 | * pending |
| 5091 | */ |
| 5092 | if (signal_pending(current)) |
| 5093 | flush_signals(current); |
| 5094 | |
| 5095 | wait_event_interruptible_timeout |
| 5096 | (thread->wqueue, |
| 5097 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5098 | || kthread_should_stop(), |
| 5099 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | |
| 5101 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5102 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5103 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | return 0; |
| 5107 | } |
| 5108 | |
| 5109 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5110 | { |
| 5111 | if (thread) { |
| 5112 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5113 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5114 | wake_up(&thread->wqueue); |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5119 | const char *name) |
| 5120 | { |
| 5121 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5123 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | if (!thread) |
| 5125 | return NULL; |
| 5126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | init_waitqueue_head(&thread->wqueue); |
| 5128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | thread->run = run; |
| 5130 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5131 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5132 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5133 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | kfree(thread); |
| 5135 | return NULL; |
| 5136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | return thread; |
| 5138 | } |
| 5139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | void md_unregister_thread(mdk_thread_t *thread) |
| 5141 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5142 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5143 | |
| 5144 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | kfree(thread); |
| 5146 | } |
| 5147 | |
| 5148 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5149 | { |
| 5150 | if (!mddev) { |
| 5151 | MD_BUG(); |
| 5152 | return; |
| 5153 | } |
| 5154 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5155 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5157 | |
| 5158 | if (mddev->external) |
| 5159 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5160 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5162 | mdname(mddev), |
| 5163 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5164 | __builtin_return_address(0),__builtin_return_address(1), |
| 5165 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5166 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5167 | if (!mddev->pers) |
| 5168 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | if (!mddev->pers->error_handler) |
| 5170 | return; |
| 5171 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5172 | if (mddev->degraded) |
| 5173 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5174 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5176 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5177 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5178 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /* seq_file implementation /proc/mdstat */ |
| 5182 | |
| 5183 | static void status_unused(struct seq_file *seq) |
| 5184 | { |
| 5185 | int i = 0; |
| 5186 | mdk_rdev_t *rdev; |
| 5187 | struct list_head *tmp; |
| 5188 | |
| 5189 | seq_printf(seq, "unused devices: "); |
| 5190 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5191 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | char b[BDEVNAME_SIZE]; |
| 5193 | i++; |
| 5194 | seq_printf(seq, "%s ", |
| 5195 | bdevname(rdev->bdev,b)); |
| 5196 | } |
| 5197 | if (!i) |
| 5198 | seq_printf(seq, "<none>"); |
| 5199 | |
| 5200 | seq_printf(seq, "\n"); |
| 5201 | } |
| 5202 | |
| 5203 | |
| 5204 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5205 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5206 | sector_t max_blocks, resync, res; |
| 5207 | unsigned long dt, db, rt; |
| 5208 | int scale; |
| 5209 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | |
| 5211 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5212 | |
| 5213 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5214 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5215 | else |
| 5216 | max_blocks = mddev->size; |
| 5217 | |
| 5218 | /* |
| 5219 | * Should not happen. |
| 5220 | */ |
| 5221 | if (!max_blocks) { |
| 5222 | MD_BUG(); |
| 5223 | return; |
| 5224 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5225 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5226 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5227 | * u32, as those are the requirements for sector_div. |
| 5228 | * Thus 'scale' must be at least 10 |
| 5229 | */ |
| 5230 | scale = 10; |
| 5231 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5232 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5233 | scale++; |
| 5234 | } |
| 5235 | res = (resync>>scale)*1000; |
| 5236 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5237 | |
| 5238 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5240 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | seq_printf(seq, "["); |
| 5242 | for (i = 0; i < x; i++) |
| 5243 | seq_printf(seq, "="); |
| 5244 | seq_printf(seq, ">"); |
| 5245 | for (i = 0; i < y; i++) |
| 5246 | seq_printf(seq, "."); |
| 5247 | seq_printf(seq, "] "); |
| 5248 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5249 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5250 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5251 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5252 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5253 | "check" : |
| 5254 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5255 | "resync" : "recovery"))), |
| 5256 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5257 | (unsigned long long) resync, |
| 5258 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | |
| 5260 | /* |
| 5261 | * We do not want to overflow, so the order of operands and |
| 5262 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5263 | * safe against division by zero. We only estimate anyway. |
| 5264 | * |
| 5265 | * dt: time from mark until now |
| 5266 | * db: blocks written from mark until now |
| 5267 | * rt: remaining time |
| 5268 | */ |
| 5269 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5270 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5271 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5272 | - mddev->resync_mark_cnt; |
| 5273 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | |
| 5275 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5276 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5277 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5281 | { |
| 5282 | struct list_head *tmp; |
| 5283 | loff_t l = *pos; |
| 5284 | mddev_t *mddev; |
| 5285 | |
| 5286 | if (l >= 0x10000) |
| 5287 | return NULL; |
| 5288 | if (!l--) |
| 5289 | /* header */ |
| 5290 | return (void*)1; |
| 5291 | |
| 5292 | spin_lock(&all_mddevs_lock); |
| 5293 | list_for_each(tmp,&all_mddevs) |
| 5294 | if (!l--) { |
| 5295 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5296 | mddev_get(mddev); |
| 5297 | spin_unlock(&all_mddevs_lock); |
| 5298 | return mddev; |
| 5299 | } |
| 5300 | spin_unlock(&all_mddevs_lock); |
| 5301 | if (!l--) |
| 5302 | return (void*)2;/* tail */ |
| 5303 | return NULL; |
| 5304 | } |
| 5305 | |
| 5306 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5307 | { |
| 5308 | struct list_head *tmp; |
| 5309 | mddev_t *next_mddev, *mddev = v; |
| 5310 | |
| 5311 | ++*pos; |
| 5312 | if (v == (void*)2) |
| 5313 | return NULL; |
| 5314 | |
| 5315 | spin_lock(&all_mddevs_lock); |
| 5316 | if (v == (void*)1) |
| 5317 | tmp = all_mddevs.next; |
| 5318 | else |
| 5319 | tmp = mddev->all_mddevs.next; |
| 5320 | if (tmp != &all_mddevs) |
| 5321 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5322 | else { |
| 5323 | next_mddev = (void*)2; |
| 5324 | *pos = 0x10000; |
| 5325 | } |
| 5326 | spin_unlock(&all_mddevs_lock); |
| 5327 | |
| 5328 | if (v != (void*)1) |
| 5329 | mddev_put(mddev); |
| 5330 | return next_mddev; |
| 5331 | |
| 5332 | } |
| 5333 | |
| 5334 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5335 | { |
| 5336 | mddev_t *mddev = v; |
| 5337 | |
| 5338 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5339 | mddev_put(mddev); |
| 5340 | } |
| 5341 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5342 | struct mdstat_info { |
| 5343 | int event; |
| 5344 | }; |
| 5345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5347 | { |
| 5348 | mddev_t *mddev = v; |
| 5349 | sector_t size; |
| 5350 | struct list_head *tmp2; |
| 5351 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5352 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5353 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
| 5355 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5356 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | seq_printf(seq, "Personalities : "); |
| 5358 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5359 | list_for_each_entry(pers, &pers_list, list) |
| 5360 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | |
| 5362 | spin_unlock(&pers_lock); |
| 5363 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5364 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
| 5367 | if (v == (void*)2) { |
| 5368 | status_unused(seq); |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5372 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5376 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5377 | mddev->pers ? "" : "in"); |
| 5378 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5379 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5381 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5382 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | seq_printf(seq, " %s", mddev->pers->name); |
| 5384 | } |
| 5385 | |
| 5386 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5387 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | char b[BDEVNAME_SIZE]; |
| 5389 | seq_printf(seq, " %s[%d]", |
| 5390 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5391 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5392 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5393 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | seq_printf(seq, "(F)"); |
| 5395 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5396 | } else if (rdev->raid_disk < 0) |
| 5397 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | size += rdev->size; |
| 5399 | } |
| 5400 | |
| 5401 | if (!list_empty(&mddev->disks)) { |
| 5402 | if (mddev->pers) |
| 5403 | seq_printf(seq, "\n %llu blocks", |
| 5404 | (unsigned long long)mddev->array_size); |
| 5405 | else |
| 5406 | seq_printf(seq, "\n %llu blocks", |
| 5407 | (unsigned long long)size); |
| 5408 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5409 | if (mddev->persistent) { |
| 5410 | if (mddev->major_version != 0 || |
| 5411 | mddev->minor_version != 90) { |
| 5412 | seq_printf(seq," super %d.%d", |
| 5413 | mddev->major_version, |
| 5414 | mddev->minor_version); |
| 5415 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5416 | } else if (mddev->external) |
| 5417 | seq_printf(seq, " super external:%s", |
| 5418 | mddev->metadata_type); |
| 5419 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5420 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | |
| 5422 | if (mddev->pers) { |
| 5423 | mddev->pers->status (seq, mddev); |
| 5424 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5425 | if (mddev->pers->sync_request) { |
| 5426 | if (mddev->curr_resync > 2) { |
| 5427 | status_resync (seq, mddev); |
| 5428 | seq_printf(seq, "\n "); |
| 5429 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5430 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5431 | else if (mddev->recovery_cp < MaxSector) |
| 5432 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5433 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5434 | } else |
| 5435 | seq_printf(seq, "\n "); |
| 5436 | |
| 5437 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5438 | unsigned long chunk_kb; |
| 5439 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5440 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5441 | chunk_kb = bitmap->chunksize >> 10; |
| 5442 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5443 | "%lu%s chunk", |
| 5444 | bitmap->pages - bitmap->missing_pages, |
| 5445 | bitmap->pages, |
| 5446 | (bitmap->pages - bitmap->missing_pages) |
| 5447 | << (PAGE_SHIFT - 10), |
| 5448 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5449 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5450 | if (bitmap->file) { |
| 5451 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5452 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5453 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5454 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5455 | seq_printf(seq, "\n"); |
| 5456 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | seq_printf(seq, "\n"); |
| 5460 | } |
| 5461 | mddev_unlock(mddev); |
| 5462 | |
| 5463 | return 0; |
| 5464 | } |
| 5465 | |
| 5466 | static struct seq_operations md_seq_ops = { |
| 5467 | .start = md_seq_start, |
| 5468 | .next = md_seq_next, |
| 5469 | .stop = md_seq_stop, |
| 5470 | .show = md_seq_show, |
| 5471 | }; |
| 5472 | |
| 5473 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5474 | { |
| 5475 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5476 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5477 | if (mi == NULL) |
| 5478 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | |
| 5480 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5481 | if (error) |
| 5482 | kfree(mi); |
| 5483 | else { |
| 5484 | struct seq_file *p = file->private_data; |
| 5485 | p->private = mi; |
| 5486 | mi->event = atomic_read(&md_event_count); |
| 5487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | return error; |
| 5489 | } |
| 5490 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5491 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5492 | { |
| 5493 | struct seq_file *m = filp->private_data; |
| 5494 | struct mdstat_info *mi = m->private; |
| 5495 | int mask; |
| 5496 | |
| 5497 | poll_wait(filp, &md_event_waiters, wait); |
| 5498 | |
| 5499 | /* always allow read */ |
| 5500 | mask = POLLIN | POLLRDNORM; |
| 5501 | |
| 5502 | if (mi->event != atomic_read(&md_event_count)) |
| 5503 | mask |= POLLERR | POLLPRI; |
| 5504 | return mask; |
| 5505 | } |
| 5506 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5507 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5508 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | .open = md_seq_open, |
| 5510 | .read = seq_read, |
| 5511 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5512 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5513 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | }; |
| 5515 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5516 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5519 | list_add_tail(&p->list, &pers_list); |
| 5520 | 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] | 5521 | spin_unlock(&pers_lock); |
| 5522 | return 0; |
| 5523 | } |
| 5524 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5525 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5527 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5529 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | spin_unlock(&pers_lock); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | static int is_mddev_idle(mddev_t *mddev) |
| 5535 | { |
| 5536 | mdk_rdev_t * rdev; |
| 5537 | struct list_head *tmp; |
| 5538 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5539 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | |
| 5541 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5542 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5544 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5545 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5547 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5548 | * as sync_io is counted when a request starts, and |
| 5549 | * disk_stats is counted when it completes. |
| 5550 | * So resync activity will cause curr_events to be smaller than |
| 5551 | * when there was no such activity. |
| 5552 | * non-sync IO will cause disk_stat to increase without |
| 5553 | * increasing sync_io so curr_events will (eventually) |
| 5554 | * be larger than it was before. Once it becomes |
| 5555 | * substantially larger, the test below will cause |
| 5556 | * the array to appear non-idle, and resync will slow |
| 5557 | * down. |
| 5558 | * If there is a lot of outstanding resync activity when |
| 5559 | * we set last_event to curr_events, then all that activity |
| 5560 | * completing might cause the array to appear non-idle |
| 5561 | * and resync will be slowed down even though there might |
| 5562 | * not have been non-resync activity. This will only |
| 5563 | * happen once though. 'last_events' will soon reflect |
| 5564 | * the state where there is little or no outstanding |
| 5565 | * resync requests, and further resync activity will |
| 5566 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5567 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5569 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | rdev->last_events = curr_events; |
| 5571 | idle = 0; |
| 5572 | } |
| 5573 | } |
| 5574 | return idle; |
| 5575 | } |
| 5576 | |
| 5577 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5578 | { |
| 5579 | /* another "blocks" (512byte) blocks have been synced */ |
| 5580 | atomic_sub(blocks, &mddev->recovery_active); |
| 5581 | wake_up(&mddev->recovery_wait); |
| 5582 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5583 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | md_wakeup_thread(mddev->thread); |
| 5585 | // stop recovery, signal do_sync .... |
| 5586 | } |
| 5587 | } |
| 5588 | |
| 5589 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5590 | /* md_write_start(mddev, bi) |
| 5591 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5592 | * in superblock) before writing, schedule a superblock update |
| 5593 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5594 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5595 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5597 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5598 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5599 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5600 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5601 | BUG_ON(mddev->ro == 1); |
| 5602 | if (mddev->ro == 2) { |
| 5603 | /* need to switch to read/write */ |
| 5604 | mddev->ro = 0; |
| 5605 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5606 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5607 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5608 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5609 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5610 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5611 | if (mddev->safemode == 1) |
| 5612 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5613 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5614 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5615 | if (mddev->in_sync) { |
| 5616 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5617 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5618 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5619 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5620 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5621 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5622 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5623 | if (did_change) |
| 5624 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5625 | wait_event(mddev->sb_wait, |
| 5626 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5627 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | void md_write_end(mddev_t *mddev) |
| 5631 | { |
| 5632 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5633 | if (mddev->safemode == 2) |
| 5634 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5635 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5637 | } |
| 5638 | } |
| 5639 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5640 | /* md_allow_write(mddev) |
| 5641 | * Calling this ensures that the array is marked 'active' so that writes |
| 5642 | * may proceed without blocking. It is important to call this before |
| 5643 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5644 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5645 | * |
| 5646 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5647 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5648 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5649 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5650 | { |
| 5651 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5652 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5653 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5654 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5655 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5656 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5657 | |
| 5658 | spin_lock_irq(&mddev->write_lock); |
| 5659 | if (mddev->in_sync) { |
| 5660 | mddev->in_sync = 0; |
| 5661 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5662 | if (mddev->safemode_delay && |
| 5663 | mddev->safemode == 0) |
| 5664 | mddev->safemode = 1; |
| 5665 | spin_unlock_irq(&mddev->write_lock); |
| 5666 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5667 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5668 | } else |
| 5669 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5670 | |
| 5671 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5672 | return -EAGAIN; |
| 5673 | else |
| 5674 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5675 | } |
| 5676 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | #define SYNC_MARKS 10 |
| 5679 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5680 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | { |
| 5682 | mddev_t *mddev2; |
| 5683 | unsigned int currspeed = 0, |
| 5684 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5685 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | unsigned long mark[SYNC_MARKS]; |
| 5687 | sector_t mark_cnt[SYNC_MARKS]; |
| 5688 | int last_mark,m; |
| 5689 | struct list_head *tmp; |
| 5690 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5691 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5692 | struct list_head *rtmp; |
| 5693 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5694 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
| 5696 | /* just incase thread restarts... */ |
| 5697 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5698 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5699 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5700 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5702 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5703 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5704 | desc = "data-check"; |
| 5705 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5706 | desc = "requested-resync"; |
| 5707 | else |
| 5708 | desc = "resync"; |
| 5709 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5710 | desc = "reshape"; |
| 5711 | else |
| 5712 | desc = "recovery"; |
| 5713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | /* we overload curr_resync somewhat here. |
| 5715 | * 0 == not engaged in resync at all |
| 5716 | * 2 == checking that there is no conflict with another sync |
| 5717 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5718 | * commense |
| 5719 | * other == active in resync - this many blocks |
| 5720 | * |
| 5721 | * Before starting a resync we must have set curr_resync to |
| 5722 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5723 | * less than ours. When we find one that is the same or higher |
| 5724 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5725 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5726 | * This will mean we have to start checking from the beginning again. |
| 5727 | * |
| 5728 | */ |
| 5729 | |
| 5730 | do { |
| 5731 | mddev->curr_resync = 2; |
| 5732 | |
| 5733 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5734 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5735 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | goto skip; |
| 5737 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5738 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | if (mddev2 == mddev) |
| 5740 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5741 | if (!mddev->parallel_resync |
| 5742 | && mddev2->curr_resync |
| 5743 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | DEFINE_WAIT(wq); |
| 5745 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5746 | /* arbitrarily yield */ |
| 5747 | mddev->curr_resync = 1; |
| 5748 | wake_up(&resync_wait); |
| 5749 | } |
| 5750 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5751 | /* no need to wait here, we can wait the next |
| 5752 | * time 'round when curr_resync == 2 |
| 5753 | */ |
| 5754 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5755 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5756 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5757 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5758 | printk(KERN_INFO "md: delaying %s of %s" |
| 5759 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5761 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | mddev_put(mddev2); |
| 5763 | schedule(); |
| 5764 | finish_wait(&resync_wait, &wq); |
| 5765 | goto try_again; |
| 5766 | } |
| 5767 | finish_wait(&resync_wait, &wq); |
| 5768 | } |
| 5769 | } |
| 5770 | } while (mddev->curr_resync < 2); |
| 5771 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5772 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5773 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5775 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | */ |
| 5777 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5778 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5779 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5780 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5781 | j = mddev->resync_min; |
| 5782 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5783 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5784 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5785 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5786 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5787 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | /* recovery follows the physical size of devices */ |
| 5789 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5790 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5791 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5792 | if (rdev->raid_disk >= 0 && |
| 5793 | !test_bit(Faulty, &rdev->flags) && |
| 5794 | !test_bit(In_sync, &rdev->flags) && |
| 5795 | rdev->recovery_offset < j) |
| 5796 | j = rdev->recovery_offset; |
| 5797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5799 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5800 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5801 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5802 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5803 | "(but not more than %d KB/sec) for %s.\n", |
| 5804 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
| 5806 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5807 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5808 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5810 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5811 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | } |
| 5813 | last_mark = 0; |
| 5814 | mddev->resync_mark = mark[last_mark]; |
| 5815 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5816 | |
| 5817 | /* |
| 5818 | * Tune reconstruction: |
| 5819 | */ |
| 5820 | window = 32*(PAGE_SIZE/512); |
| 5821 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5822 | window/2,(unsigned long long) max_sectors/2); |
| 5823 | |
| 5824 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | last_check = 0; |
| 5826 | |
| 5827 | if (j>2) { |
| 5828 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5829 | "md: resuming %s of %s from checkpoint.\n", |
| 5830 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | mddev->curr_resync = j; |
| 5832 | } |
| 5833 | |
| 5834 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5835 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5837 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5838 | if (j >= mddev->resync_max) { |
| 5839 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5840 | wait_event(mddev->recovery_wait, |
| 5841 | mddev->resync_max > j |
| 5842 | || kthread_should_stop()); |
| 5843 | } |
| 5844 | if (kthread_should_stop()) |
| 5845 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5846 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5847 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5848 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5849 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | goto out; |
| 5851 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5852 | |
| 5853 | if (!skipped) { /* actual IO requested */ |
| 5854 | io_sectors += sectors; |
| 5855 | atomic_add(sectors, &mddev->recovery_active); |
| 5856 | } |
| 5857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | j += sectors; |
| 5859 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5860 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5861 | if (last_check == 0) |
| 5862 | /* this is the earliers that rebuilt will be |
| 5863 | * visible in /proc/mdstat |
| 5864 | */ |
| 5865 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5866 | |
| 5867 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | continue; |
| 5869 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5870 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5872 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | break; |
| 5874 | |
| 5875 | repeat: |
| 5876 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5877 | /* step marks */ |
| 5878 | int next = (last_mark+1) % SYNC_MARKS; |
| 5879 | |
| 5880 | mddev->resync_mark = mark[next]; |
| 5881 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5882 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5883 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | last_mark = next; |
| 5885 | } |
| 5886 | |
| 5887 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5888 | if (kthread_should_stop()) |
| 5889 | goto interrupted; |
| 5890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | /* |
| 5893 | * this loop exits only if either when we are slower than |
| 5894 | * the 'hard' speed limit, or the system was IO-idle for |
| 5895 | * a jiffy. |
| 5896 | * the system might be non-idle CPU-wise, but we only care |
| 5897 | * about not overloading the IO subsystem. (things like an |
| 5898 | * e2fsck being done on the RAID array should execute fast) |
| 5899 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5900 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | cond_resched(); |
| 5902 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5903 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5904 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5906 | if (currspeed > speed_min(mddev)) { |
| 5907 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5909 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | goto repeat; |
| 5911 | } |
| 5912 | } |
| 5913 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5914 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | /* |
| 5916 | * this also signals 'finished resyncing' to md_stop |
| 5917 | */ |
| 5918 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5919 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | |
| 5921 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5922 | |
| 5923 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5924 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5926 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5927 | mddev->curr_resync > 2) { |
| 5928 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5929 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5930 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5931 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5932 | "md: checkpointing %s of %s.\n", |
| 5933 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5934 | mddev->recovery_cp = mddev->curr_resync; |
| 5935 | } |
| 5936 | } else |
| 5937 | mddev->recovery_cp = MaxSector; |
| 5938 | } else { |
| 5939 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5940 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5941 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5942 | if (rdev->raid_disk >= 0 && |
| 5943 | !test_bit(Faulty, &rdev->flags) && |
| 5944 | !test_bit(In_sync, &rdev->flags) && |
| 5945 | rdev->recovery_offset < mddev->curr_resync) |
| 5946 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5949 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | skip: |
| 5952 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5953 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5954 | mddev->resync_max = MaxSector; |
| 5955 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | wake_up(&resync_wait); |
| 5957 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5958 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5959 | return; |
| 5960 | |
| 5961 | interrupted: |
| 5962 | /* |
| 5963 | * got a signal, exit. |
| 5964 | */ |
| 5965 | printk(KERN_INFO |
| 5966 | "md: md_do_sync() got signal ... exiting\n"); |
| 5967 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5968 | goto out; |
| 5969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5971 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | |
| 5973 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5974 | static int remove_and_add_spares(mddev_t *mddev) |
| 5975 | { |
| 5976 | mdk_rdev_t *rdev; |
| 5977 | struct list_head *rtmp; |
| 5978 | int spares = 0; |
| 5979 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5980 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5981 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5982 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5983 | (test_bit(Faulty, &rdev->flags) || |
| 5984 | ! test_bit(In_sync, &rdev->flags)) && |
| 5985 | atomic_read(&rdev->nr_pending)==0) { |
| 5986 | if (mddev->pers->hot_remove_disk( |
| 5987 | mddev, rdev->raid_disk)==0) { |
| 5988 | char nm[20]; |
| 5989 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5990 | sysfs_remove_link(&mddev->kobj, nm); |
| 5991 | rdev->raid_disk = -1; |
| 5992 | } |
| 5993 | } |
| 5994 | |
| 5995 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5996 | rdev_for_each(rdev, rtmp, mddev) { |
| 5997 | if (rdev->raid_disk >= 0 && |
| 5998 | !test_bit(In_sync, &rdev->flags)) |
| 5999 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6000 | if (rdev->raid_disk < 0 |
| 6001 | && !test_bit(Faulty, &rdev->flags)) { |
| 6002 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6003 | if (mddev->pers-> |
| 6004 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6005 | char nm[20]; |
| 6006 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6007 | if (sysfs_create_link(&mddev->kobj, |
| 6008 | &rdev->kobj, nm)) |
| 6009 | printk(KERN_WARNING |
| 6010 | "md: cannot register " |
| 6011 | "%s for %s\n", |
| 6012 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6013 | spares++; |
| 6014 | md_new_event(mddev); |
| 6015 | } else |
| 6016 | break; |
| 6017 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6018 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6019 | } |
| 6020 | return spares; |
| 6021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | /* |
| 6023 | * This routine is regularly called by all per-raid-array threads to |
| 6024 | * deal with generic issues like resync and super-block update. |
| 6025 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6026 | * need this as they never do any recovery or update the superblock. |
| 6027 | * |
| 6028 | * It does not do any resync itself, but rather "forks" off other threads |
| 6029 | * to do that as needed. |
| 6030 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6031 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6032 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | * and wakeups up this thread which will reap the thread and finish up. |
| 6034 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6035 | * |
| 6036 | * The overall approach is: |
| 6037 | * 1/ if the superblock needs updating, update it. |
| 6038 | * 2/ If a recovery thread is running, don't do anything else. |
| 6039 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6040 | * 4/ If there are any faulty devices, remove them. |
| 6041 | * 5/ If array is degraded, try to add spares devices |
| 6042 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6043 | */ |
| 6044 | void md_check_recovery(mddev_t *mddev) |
| 6045 | { |
| 6046 | mdk_rdev_t *rdev; |
| 6047 | struct list_head *rtmp; |
| 6048 | |
| 6049 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6050 | if (mddev->bitmap) |
| 6051 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | |
| 6053 | if (mddev->ro) |
| 6054 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6055 | |
| 6056 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6057 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6058 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6059 | mdname(mddev)); |
| 6060 | mddev->safemode = 2; |
| 6061 | } |
| 6062 | flush_signals(current); |
| 6063 | } |
| 6064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6066 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6068 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6069 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6070 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6071 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | )) |
| 6073 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6074 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6075 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6076 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6077 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6078 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6079 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6080 | spin_lock_irq(&mddev->write_lock); |
| 6081 | if (mddev->safemode && |
| 6082 | !atomic_read(&mddev->writes_pending) && |
| 6083 | !mddev->in_sync && |
| 6084 | mddev->recovery_cp == MaxSector) { |
| 6085 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6086 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6087 | if (mddev->persistent) |
| 6088 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6089 | } |
| 6090 | if (mddev->safemode == 1) |
| 6091 | mddev->safemode = 0; |
| 6092 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6093 | if (did_change) |
| 6094 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6095 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6096 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6097 | if (mddev->flags) |
| 6098 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6099 | |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6100 | rdev_for_each(rdev, rtmp, mddev) |
| 6101 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
| 6102 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6103 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6106 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6107 | /* resync/recovery still happening */ |
| 6108 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6109 | goto unlock; |
| 6110 | } |
| 6111 | if (mddev->sync_thread) { |
| 6112 | /* resync has finished, collect result */ |
| 6113 | md_unregister_thread(mddev->sync_thread); |
| 6114 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6115 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | /* success...*/ |
| 6117 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6118 | if (mddev->pers->spare_active(mddev)) |
| 6119 | sysfs_notify(&mddev->kobj, NULL, |
| 6120 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6122 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6123 | |
| 6124 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6125 | * information must be scrapped |
| 6126 | */ |
| 6127 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6128 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6129 | rdev->saved_raid_disk = -1; |
| 6130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | mddev->recovery = 0; |
| 6132 | /* flag recovery needed just to double check */ |
| 6133 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6134 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6135 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | goto unlock; |
| 6137 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6138 | /* Set RUNNING before clearing NEEDED to avoid |
| 6139 | * any transients in the value of "sync_action". |
| 6140 | */ |
| 6141 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6142 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6143 | /* Clear some bits that don't mean anything, but |
| 6144 | * might be left set |
| 6145 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6146 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6147 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6149 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6150 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | /* no recovery is running. |
| 6152 | * remove any failed drives, then |
| 6153 | * add spares if possible. |
| 6154 | * Spare are also removed and re-added, to allow |
| 6155 | * the personality to fail the re-add. |
| 6156 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6158 | if (mddev->reshape_position != MaxSector) { |
| 6159 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6160 | /* Cannot proceed */ |
| 6161 | goto unlock; |
| 6162 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6163 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6164 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6165 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6166 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6167 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6168 | } else if (mddev->recovery_cp < MaxSector) { |
| 6169 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6170 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6171 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6172 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6176 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6177 | /* We are adding a device or devices to an array |
| 6178 | * which has the bitmap stored on all devices. |
| 6179 | * So make sure all bitmap pages get written |
| 6180 | */ |
| 6181 | bitmap_write_all(mddev->bitmap); |
| 6182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6184 | mddev, |
| 6185 | "%s_resync"); |
| 6186 | if (!mddev->sync_thread) { |
| 6187 | printk(KERN_ERR "%s: could not start resync" |
| 6188 | " thread...\n", |
| 6189 | mdname(mddev)); |
| 6190 | /* leave the spares where they are, it shouldn't hurt */ |
| 6191 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6192 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6194 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6195 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | } |
| 6197 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6198 | if (!mddev->sync_thread) { |
| 6199 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6200 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6201 | &mddev->recovery)) |
| 6202 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
| 6203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | mddev_unlock(mddev); |
| 6205 | } |
| 6206 | } |
| 6207 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6208 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6209 | { |
| 6210 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6211 | wait_event_timeout(rdev->blocked_wait, |
| 6212 | !test_bit(Blocked, &rdev->flags), |
| 6213 | msecs_to_jiffies(5000)); |
| 6214 | rdev_dec_pending(rdev, mddev); |
| 6215 | } |
| 6216 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6217 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6218 | static int md_notify_reboot(struct notifier_block *this, |
| 6219 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | { |
| 6221 | struct list_head *tmp; |
| 6222 | mddev_t *mddev; |
| 6223 | |
| 6224 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6225 | |
| 6226 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6227 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6228 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6229 | if (mddev_trylock(mddev)) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame^] | 6230 | do_md_stop (mddev, 1, 0); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6231 | mddev_unlock(mddev); |
| 6232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | /* |
| 6234 | * certain more exotic SCSI devices are known to be |
| 6235 | * volatile wrt too early system reboots. While the |
| 6236 | * right place to handle this issue is the given |
| 6237 | * driver, we do want to have a safe RAID driver ... |
| 6238 | */ |
| 6239 | mdelay(1000*1); |
| 6240 | } |
| 6241 | return NOTIFY_DONE; |
| 6242 | } |
| 6243 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6244 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | .notifier_call = md_notify_reboot, |
| 6246 | .next = NULL, |
| 6247 | .priority = INT_MAX, /* before any real devices */ |
| 6248 | }; |
| 6249 | |
| 6250 | static void md_geninit(void) |
| 6251 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6253 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6254 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | } |
| 6256 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6257 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | if (register_blkdev(MAJOR_NR, "md")) |
| 6260 | return -1; |
| 6261 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6262 | unregister_blkdev(MAJOR_NR, "md"); |
| 6263 | return -1; |
| 6264 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6265 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6266 | md_probe, NULL, NULL); |
| 6267 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | md_probe, NULL, NULL); |
| 6269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6271 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | |
| 6273 | md_geninit(); |
| 6274 | return (0); |
| 6275 | } |
| 6276 | |
| 6277 | |
| 6278 | #ifndef MODULE |
| 6279 | |
| 6280 | /* |
| 6281 | * Searches all registered partitions for autorun RAID arrays |
| 6282 | * at boot time. |
| 6283 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6284 | |
| 6285 | static LIST_HEAD(all_detected_devices); |
| 6286 | struct detected_devices_node { |
| 6287 | struct list_head list; |
| 6288 | dev_t dev; |
| 6289 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | |
| 6291 | void md_autodetect_dev(dev_t dev) |
| 6292 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6293 | struct detected_devices_node *node_detected_dev; |
| 6294 | |
| 6295 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6296 | if (node_detected_dev) { |
| 6297 | node_detected_dev->dev = dev; |
| 6298 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6299 | } else { |
| 6300 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6301 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | |
| 6306 | static void autostart_arrays(int part) |
| 6307 | { |
| 6308 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6309 | struct detected_devices_node *node_detected_dev; |
| 6310 | dev_t dev; |
| 6311 | int i_scanned, i_passed; |
| 6312 | |
| 6313 | i_scanned = 0; |
| 6314 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | |
| 6316 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6317 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6318 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6319 | i_scanned++; |
| 6320 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6321 | struct detected_devices_node, list); |
| 6322 | list_del(&node_detected_dev->list); |
| 6323 | dev = node_detected_dev->dev; |
| 6324 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6325 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | if (IS_ERR(rdev)) |
| 6327 | continue; |
| 6328 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6329 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | MD_BUG(); |
| 6331 | continue; |
| 6332 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6333 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6335 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6337 | |
| 6338 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6339 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | |
| 6341 | autorun_devices(part); |
| 6342 | } |
| 6343 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6344 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | |
| 6346 | static __exit void md_exit(void) |
| 6347 | { |
| 6348 | mddev_t *mddev; |
| 6349 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6350 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6351 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6352 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | |
| 6354 | unregister_blkdev(MAJOR_NR,"md"); |
| 6355 | unregister_blkdev(mdp_major, "mdp"); |
| 6356 | unregister_reboot_notifier(&md_notifier); |
| 6357 | unregister_sysctl_table(raid_table_header); |
| 6358 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6359 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | struct gendisk *disk = mddev->gendisk; |
| 6361 | if (!disk) |
| 6362 | continue; |
| 6363 | export_array(mddev); |
| 6364 | del_gendisk(disk); |
| 6365 | put_disk(disk); |
| 6366 | mddev->gendisk = NULL; |
| 6367 | mddev_put(mddev); |
| 6368 | } |
| 6369 | } |
| 6370 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6371 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | module_exit(md_exit) |
| 6373 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6374 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6375 | { |
| 6376 | return sprintf(buffer, "%d", start_readonly); |
| 6377 | } |
| 6378 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6379 | { |
| 6380 | char *e; |
| 6381 | int num = simple_strtoul(val, &e, 10); |
| 6382 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6383 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6384 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6385 | } |
| 6386 | return -EINVAL; |
| 6387 | } |
| 6388 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6389 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6390 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6391 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | EXPORT_SYMBOL(register_md_personality); |
| 6394 | EXPORT_SYMBOL(unregister_md_personality); |
| 6395 | EXPORT_SYMBOL(md_error); |
| 6396 | EXPORT_SYMBOL(md_done_sync); |
| 6397 | EXPORT_SYMBOL(md_write_start); |
| 6398 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | EXPORT_SYMBOL(md_register_thread); |
| 6400 | EXPORT_SYMBOL(md_unregister_thread); |
| 6401 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | EXPORT_SYMBOL(md_check_recovery); |
| 6403 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6404 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6405 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |