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 | |
| 2691 | static int do_md_stop(mddev_t * mddev, int ro); |
| 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; |
| 2707 | err = do_md_stop(mddev, 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; |
| 2714 | err = do_md_stop(mddev, 2); |
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) |
| 2722 | err = do_md_stop(mddev, 1); |
| 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) |
| 2732 | err = do_md_stop(mddev, 1); |
| 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 | */ |
| 3821 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | { |
| 3823 | int err = 0; |
| 3824 | struct gendisk *disk = mddev->gendisk; |
| 3825 | |
| 3826 | if (mddev->pers) { |
| 3827 | if (atomic_read(&mddev->active)>2) { |
| 3828 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3829 | return -EBUSY; |
| 3830 | } |
| 3831 | |
| 3832 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3833 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3835 | md_unregister_thread(mddev->sync_thread); |
| 3836 | mddev->sync_thread = NULL; |
| 3837 | } |
| 3838 | |
| 3839 | del_timer_sync(&mddev->safemode_timer); |
| 3840 | |
| 3841 | invalidate_partition(disk, 0); |
| 3842 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3843 | switch(mode) { |
| 3844 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3846 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | goto out; |
| 3848 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3849 | break; |
| 3850 | case 0: /* disassemble */ |
| 3851 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3852 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3853 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | if (mddev->ro) |
| 3855 | set_disk_ro(disk, 0); |
| 3856 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3857 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3858 | mddev->queue->merge_bvec_fn = NULL; |
| 3859 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3860 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3861 | if (mddev->pers->sync_request) |
| 3862 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | module_put(mddev->pers->owner); |
| 3865 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3866 | /* tell userspace to handle 'inactive' */ |
| 3867 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3868 | |
| 3869 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3870 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | if (mddev->ro) |
| 3873 | mddev->ro = 0; |
| 3874 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3875 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | /* mark array as shutdown cleanly */ |
| 3877 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3878 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3880 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3882 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | /* |
| 3886 | * Free resources if final stop |
| 3887 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3888 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3889 | mdk_rdev_t *rdev; |
| 3890 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3893 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3894 | bitmap_destroy(mddev); |
| 3895 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3896 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3897 | fput(mddev->bitmap_file); |
| 3898 | mddev->bitmap_file = NULL; |
| 3899 | } |
| 3900 | mddev->bitmap_offset = 0; |
| 3901 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3902 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3903 | if (rdev->raid_disk >= 0) { |
| 3904 | char nm[20]; |
| 3905 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3906 | sysfs_remove_link(&mddev->kobj, nm); |
| 3907 | } |
| 3908 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3909 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3910 | flush_scheduled_work(); |
| 3911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | export_array(mddev); |
| 3913 | |
| 3914 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3915 | mddev->size = 0; |
| 3916 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3917 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3918 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3919 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3920 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3921 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3922 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3923 | mddev->level = LEVEL_NONE; |
| 3924 | mddev->clevel[0] = 0; |
| 3925 | mddev->flags = 0; |
| 3926 | mddev->ro = 0; |
| 3927 | mddev->metadata_type[0] = 0; |
| 3928 | mddev->chunk_size = 0; |
| 3929 | mddev->ctime = mddev->utime = 0; |
| 3930 | mddev->layout = 0; |
| 3931 | mddev->max_disks = 0; |
| 3932 | mddev->events = 0; |
| 3933 | mddev->delta_disks = 0; |
| 3934 | mddev->new_level = LEVEL_NONE; |
| 3935 | mddev->new_layout = 0; |
| 3936 | mddev->new_chunk = 0; |
| 3937 | mddev->curr_resync = 0; |
| 3938 | mddev->resync_mismatches = 0; |
| 3939 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3940 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3941 | mddev->recovery = 0; |
| 3942 | mddev->in_sync = 0; |
| 3943 | mddev->changed = 0; |
| 3944 | mddev->degraded = 0; |
| 3945 | mddev->barriers_work = 0; |
| 3946 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3947 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3948 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3950 | mdname(mddev)); |
| 3951 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3952 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3953 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | out: |
| 3955 | return err; |
| 3956 | } |
| 3957 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3958 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | static void autorun_array(mddev_t *mddev) |
| 3960 | { |
| 3961 | mdk_rdev_t *rdev; |
| 3962 | struct list_head *tmp; |
| 3963 | int err; |
| 3964 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3965 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | |
| 3968 | printk(KERN_INFO "md: running: "); |
| 3969 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3970 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | char b[BDEVNAME_SIZE]; |
| 3972 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3973 | } |
| 3974 | printk("\n"); |
| 3975 | |
| 3976 | err = do_md_run (mddev); |
| 3977 | if (err) { |
| 3978 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3979 | do_md_stop (mddev, 0); |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | /* |
| 3984 | * lets try to run arrays based on all disks that have arrived |
| 3985 | * until now. (those are in pending_raid_disks) |
| 3986 | * |
| 3987 | * the method: pick the first pending disk, collect all disks with |
| 3988 | * the same UUID, remove all from the pending list and put them into |
| 3989 | * the 'same_array' list. Then order this list based on superblock |
| 3990 | * update time (freshest comes first), kick out 'old' disks and |
| 3991 | * compare superblocks. If everything's fine then run it. |
| 3992 | * |
| 3993 | * If "unit" is allocated, then bump its reference count |
| 3994 | */ |
| 3995 | static void autorun_devices(int part) |
| 3996 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | struct list_head *tmp; |
| 3998 | mdk_rdev_t *rdev0, *rdev; |
| 3999 | mddev_t *mddev; |
| 4000 | char b[BDEVNAME_SIZE]; |
| 4001 | |
| 4002 | printk(KERN_INFO "md: autorun ...\n"); |
| 4003 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4004 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4006 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | rdev0 = list_entry(pending_raid_disks.next, |
| 4008 | mdk_rdev_t, same_set); |
| 4009 | |
| 4010 | printk(KERN_INFO "md: considering %s ...\n", |
| 4011 | bdevname(rdev0->bdev,b)); |
| 4012 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4013 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4015 | printk(KERN_INFO "md: adding %s ...\n", |
| 4016 | bdevname(rdev->bdev,b)); |
| 4017 | list_move(&rdev->same_set, &candidates); |
| 4018 | } |
| 4019 | /* |
| 4020 | * now we have a set of devices, with all of them having |
| 4021 | * mostly sane superblocks. It's time to allocate the |
| 4022 | * mddev. |
| 4023 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4024 | if (part) { |
| 4025 | dev = MKDEV(mdp_major, |
| 4026 | rdev0->preferred_minor << MdpMinorShift); |
| 4027 | unit = MINOR(dev) >> MdpMinorShift; |
| 4028 | } else { |
| 4029 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4030 | unit = MINOR(dev); |
| 4031 | } |
| 4032 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4034 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4035 | break; |
| 4036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | |
| 4038 | md_probe(dev, NULL, NULL); |
| 4039 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4040 | if (!mddev || !mddev->gendisk) { |
| 4041 | if (mddev) |
| 4042 | mddev_put(mddev); |
| 4043 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | "md: cannot allocate memory for md drive.\n"); |
| 4045 | break; |
| 4046 | } |
| 4047 | if (mddev_lock(mddev)) |
| 4048 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4049 | mdname(mddev)); |
| 4050 | else if (mddev->raid_disks || mddev->major_version |
| 4051 | || !list_empty(&mddev->disks)) { |
| 4052 | printk(KERN_WARNING |
| 4053 | "md: %s already running, cannot run %s\n", |
| 4054 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4055 | mddev_unlock(mddev); |
| 4056 | } else { |
| 4057 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4058 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4059 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | list_del_init(&rdev->same_set); |
| 4061 | if (bind_rdev_to_array(rdev, mddev)) |
| 4062 | export_rdev(rdev); |
| 4063 | } |
| 4064 | autorun_array(mddev); |
| 4065 | mddev_unlock(mddev); |
| 4066 | } |
| 4067 | /* on success, candidates will be empty, on error |
| 4068 | * it won't... |
| 4069 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4070 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | export_rdev(rdev); |
| 4072 | mddev_put(mddev); |
| 4073 | } |
| 4074 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4075 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4076 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | static int get_version(void __user * arg) |
| 4079 | { |
| 4080 | mdu_version_t ver; |
| 4081 | |
| 4082 | ver.major = MD_MAJOR_VERSION; |
| 4083 | ver.minor = MD_MINOR_VERSION; |
| 4084 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4085 | |
| 4086 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4087 | return -EFAULT; |
| 4088 | |
| 4089 | return 0; |
| 4090 | } |
| 4091 | |
| 4092 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4093 | { |
| 4094 | mdu_array_info_t info; |
| 4095 | int nr,working,active,failed,spare; |
| 4096 | mdk_rdev_t *rdev; |
| 4097 | struct list_head *tmp; |
| 4098 | |
| 4099 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4100 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4102 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | failed++; |
| 4104 | else { |
| 4105 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4106 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | active++; |
| 4108 | else |
| 4109 | spare++; |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | info.major_version = mddev->major_version; |
| 4114 | info.minor_version = mddev->minor_version; |
| 4115 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4116 | info.ctime = mddev->ctime; |
| 4117 | info.level = mddev->level; |
| 4118 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4119 | if (info.size != mddev->size) /* overflow */ |
| 4120 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | info.nr_disks = nr; |
| 4122 | info.raid_disks = mddev->raid_disks; |
| 4123 | info.md_minor = mddev->md_minor; |
| 4124 | info.not_persistent= !mddev->persistent; |
| 4125 | |
| 4126 | info.utime = mddev->utime; |
| 4127 | info.state = 0; |
| 4128 | if (mddev->in_sync) |
| 4129 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4130 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4131 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | info.active_disks = active; |
| 4133 | info.working_disks = working; |
| 4134 | info.failed_disks = failed; |
| 4135 | info.spare_disks = spare; |
| 4136 | |
| 4137 | info.layout = mddev->layout; |
| 4138 | info.chunk_size = mddev->chunk_size; |
| 4139 | |
| 4140 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4141 | return -EFAULT; |
| 4142 | |
| 4143 | return 0; |
| 4144 | } |
| 4145 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4146 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4147 | { |
| 4148 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4149 | char *ptr, *buf = NULL; |
| 4150 | int err = -ENOMEM; |
| 4151 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4152 | if (md_allow_write(mddev)) |
| 4153 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4154 | else |
| 4155 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4156 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4157 | if (!file) |
| 4158 | goto out; |
| 4159 | |
| 4160 | /* bitmap disabled, zero the first byte and copy out */ |
| 4161 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4162 | file->pathname[0] = '\0'; |
| 4163 | goto copy_out; |
| 4164 | } |
| 4165 | |
| 4166 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4167 | if (!buf) |
| 4168 | goto out; |
| 4169 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4170 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4171 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4172 | goto out; |
| 4173 | |
| 4174 | strcpy(file->pathname, ptr); |
| 4175 | |
| 4176 | copy_out: |
| 4177 | err = 0; |
| 4178 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4179 | err = -EFAULT; |
| 4180 | out: |
| 4181 | kfree(buf); |
| 4182 | kfree(file); |
| 4183 | return err; |
| 4184 | } |
| 4185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4187 | { |
| 4188 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | mdk_rdev_t *rdev; |
| 4190 | |
| 4191 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4192 | return -EFAULT; |
| 4193 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame^] | 4194 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | if (rdev) { |
| 4196 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4197 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4198 | info.raid_disk = rdev->raid_disk; |
| 4199 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4200 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4202 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4204 | info.state |= (1<<MD_DISK_SYNC); |
| 4205 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4206 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4207 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | } else { |
| 4209 | info.major = info.minor = 0; |
| 4210 | info.raid_disk = -1; |
| 4211 | info.state = (1<<MD_DISK_REMOVED); |
| 4212 | } |
| 4213 | |
| 4214 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4215 | return -EFAULT; |
| 4216 | |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4221 | { |
| 4222 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4223 | mdk_rdev_t *rdev; |
| 4224 | dev_t dev = MKDEV(info->major,info->minor); |
| 4225 | |
| 4226 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4227 | return -EOVERFLOW; |
| 4228 | |
| 4229 | if (!mddev->raid_disks) { |
| 4230 | int err; |
| 4231 | /* expecting a device which has a superblock */ |
| 4232 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4233 | if (IS_ERR(rdev)) { |
| 4234 | printk(KERN_WARNING |
| 4235 | "md: md_import_device returned %ld\n", |
| 4236 | PTR_ERR(rdev)); |
| 4237 | return PTR_ERR(rdev); |
| 4238 | } |
| 4239 | if (!list_empty(&mddev->disks)) { |
| 4240 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4241 | mdk_rdev_t, same_set); |
| 4242 | int err = super_types[mddev->major_version] |
| 4243 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4244 | if (err < 0) { |
| 4245 | printk(KERN_WARNING |
| 4246 | "md: %s has different UUID to %s\n", |
| 4247 | bdevname(rdev->bdev,b), |
| 4248 | bdevname(rdev0->bdev,b2)); |
| 4249 | export_rdev(rdev); |
| 4250 | return -EINVAL; |
| 4251 | } |
| 4252 | } |
| 4253 | err = bind_rdev_to_array(rdev, mddev); |
| 4254 | if (err) |
| 4255 | export_rdev(rdev); |
| 4256 | return err; |
| 4257 | } |
| 4258 | |
| 4259 | /* |
| 4260 | * add_new_disk can be used once the array is assembled |
| 4261 | * to add "hot spares". They must already have a superblock |
| 4262 | * written |
| 4263 | */ |
| 4264 | if (mddev->pers) { |
| 4265 | int err; |
| 4266 | if (!mddev->pers->hot_add_disk) { |
| 4267 | printk(KERN_WARNING |
| 4268 | "%s: personality does not support diskops!\n", |
| 4269 | mdname(mddev)); |
| 4270 | return -EINVAL; |
| 4271 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4272 | if (mddev->persistent) |
| 4273 | rdev = md_import_device(dev, mddev->major_version, |
| 4274 | mddev->minor_version); |
| 4275 | else |
| 4276 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | if (IS_ERR(rdev)) { |
| 4278 | printk(KERN_WARNING |
| 4279 | "md: md_import_device returned %ld\n", |
| 4280 | PTR_ERR(rdev)); |
| 4281 | return PTR_ERR(rdev); |
| 4282 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4283 | /* set save_raid_disk if appropriate */ |
| 4284 | if (!mddev->persistent) { |
| 4285 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4286 | info->raid_disk < mddev->raid_disks) |
| 4287 | rdev->raid_disk = info->raid_disk; |
| 4288 | else |
| 4289 | rdev->raid_disk = -1; |
| 4290 | } else |
| 4291 | super_types[mddev->major_version]. |
| 4292 | validate_super(mddev, rdev); |
| 4293 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4294 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4295 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4296 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4297 | set_bit(WriteMostly, &rdev->flags); |
| 4298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | rdev->raid_disk = -1; |
| 4300 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4301 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4302 | /* If there is hot_add_disk but no hot_remove_disk |
| 4303 | * then added disks for geometry changes, |
| 4304 | * and should be added immediately. |
| 4305 | */ |
| 4306 | super_types[mddev->major_version]. |
| 4307 | validate_super(mddev, rdev); |
| 4308 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4309 | if (err) |
| 4310 | unbind_rdev_from_array(rdev); |
| 4311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | if (err) |
| 4313 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4314 | else |
| 4315 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4316 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4317 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4318 | if (mddev->degraded) |
| 4319 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4320 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4321 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | return err; |
| 4323 | } |
| 4324 | |
| 4325 | /* otherwise, add_new_disk is only allowed |
| 4326 | * for major_version==0 superblocks |
| 4327 | */ |
| 4328 | if (mddev->major_version != 0) { |
| 4329 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4330 | mdname(mddev)); |
| 4331 | return -EINVAL; |
| 4332 | } |
| 4333 | |
| 4334 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4335 | int err; |
| 4336 | rdev = md_import_device (dev, -1, 0); |
| 4337 | if (IS_ERR(rdev)) { |
| 4338 | printk(KERN_WARNING |
| 4339 | "md: error, md_import_device() returned %ld\n", |
| 4340 | PTR_ERR(rdev)); |
| 4341 | return PTR_ERR(rdev); |
| 4342 | } |
| 4343 | rdev->desc_nr = info->number; |
| 4344 | if (info->raid_disk < mddev->raid_disks) |
| 4345 | rdev->raid_disk = info->raid_disk; |
| 4346 | else |
| 4347 | rdev->raid_disk = -1; |
| 4348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4350 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4351 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4353 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4354 | set_bit(WriteMostly, &rdev->flags); |
| 4355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | if (!mddev->persistent) { |
| 4357 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4358 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4359 | } else |
| 4360 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4361 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4362 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4363 | err = bind_rdev_to_array(rdev, mddev); |
| 4364 | if (err) { |
| 4365 | export_rdev(rdev); |
| 4366 | return err; |
| 4367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
| 4373 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4374 | { |
| 4375 | char b[BDEVNAME_SIZE]; |
| 4376 | mdk_rdev_t *rdev; |
| 4377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | rdev = find_rdev(mddev, dev); |
| 4379 | if (!rdev) |
| 4380 | return -ENXIO; |
| 4381 | |
| 4382 | if (rdev->raid_disk >= 0) |
| 4383 | goto busy; |
| 4384 | |
| 4385 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4386 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4387 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
| 4389 | return 0; |
| 4390 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4391 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4393 | return -EBUSY; |
| 4394 | } |
| 4395 | |
| 4396 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4397 | { |
| 4398 | char b[BDEVNAME_SIZE]; |
| 4399 | int err; |
| 4400 | unsigned int size; |
| 4401 | mdk_rdev_t *rdev; |
| 4402 | |
| 4403 | if (!mddev->pers) |
| 4404 | return -ENODEV; |
| 4405 | |
| 4406 | if (mddev->major_version != 0) { |
| 4407 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4408 | " version-0 superblocks.\n", |
| 4409 | mdname(mddev)); |
| 4410 | return -EINVAL; |
| 4411 | } |
| 4412 | if (!mddev->pers->hot_add_disk) { |
| 4413 | printk(KERN_WARNING |
| 4414 | "%s: personality does not support diskops!\n", |
| 4415 | mdname(mddev)); |
| 4416 | return -EINVAL; |
| 4417 | } |
| 4418 | |
| 4419 | rdev = md_import_device (dev, -1, 0); |
| 4420 | if (IS_ERR(rdev)) { |
| 4421 | printk(KERN_WARNING |
| 4422 | "md: error, md_import_device() returned %ld\n", |
| 4423 | PTR_ERR(rdev)); |
| 4424 | return -EINVAL; |
| 4425 | } |
| 4426 | |
| 4427 | if (mddev->persistent) |
| 4428 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4429 | else |
| 4430 | rdev->sb_offset = |
| 4431 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4432 | |
| 4433 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4434 | rdev->size = size; |
| 4435 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4436 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | printk(KERN_WARNING |
| 4438 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4439 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4440 | err = -EINVAL; |
| 4441 | goto abort_export; |
| 4442 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4443 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4445 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4446 | err = bind_rdev_to_array(rdev, mddev); |
| 4447 | if (err) |
| 4448 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
| 4450 | /* |
| 4451 | * The rest should better be atomic, we can have disk failures |
| 4452 | * noticed in interrupt contexts ... |
| 4453 | */ |
| 4454 | |
| 4455 | if (rdev->desc_nr == mddev->max_disks) { |
| 4456 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4457 | mdname(mddev)); |
| 4458 | err = -EBUSY; |
| 4459 | goto abort_unbind_export; |
| 4460 | } |
| 4461 | |
| 4462 | rdev->raid_disk = -1; |
| 4463 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4464 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | |
| 4466 | /* |
| 4467 | * Kick recovery, maybe this spare has to be added to the |
| 4468 | * array immediately. |
| 4469 | */ |
| 4470 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4471 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4472 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | return 0; |
| 4474 | |
| 4475 | abort_unbind_export: |
| 4476 | unbind_rdev_from_array(rdev); |
| 4477 | |
| 4478 | abort_export: |
| 4479 | export_rdev(rdev); |
| 4480 | return err; |
| 4481 | } |
| 4482 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4483 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4484 | { |
| 4485 | int err; |
| 4486 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4487 | if (mddev->pers) { |
| 4488 | if (!mddev->pers->quiesce) |
| 4489 | return -EBUSY; |
| 4490 | if (mddev->recovery || mddev->sync_thread) |
| 4491 | return -EBUSY; |
| 4492 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4493 | } |
| 4494 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4495 | |
| 4496 | if (fd >= 0) { |
| 4497 | if (mddev->bitmap) |
| 4498 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4499 | mddev->bitmap_file = fget(fd); |
| 4500 | |
| 4501 | if (mddev->bitmap_file == NULL) { |
| 4502 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4503 | mdname(mddev)); |
| 4504 | return -EBADF; |
| 4505 | } |
| 4506 | |
| 4507 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4508 | if (err) { |
| 4509 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4510 | mdname(mddev)); |
| 4511 | fput(mddev->bitmap_file); |
| 4512 | mddev->bitmap_file = NULL; |
| 4513 | return err; |
| 4514 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4515 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4516 | } else if (mddev->bitmap == NULL) |
| 4517 | return -ENOENT; /* cannot remove what isn't there */ |
| 4518 | err = 0; |
| 4519 | if (mddev->pers) { |
| 4520 | mddev->pers->quiesce(mddev, 1); |
| 4521 | if (fd >= 0) |
| 4522 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4523 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4524 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4525 | fd = -1; /* make sure to put the file */ |
| 4526 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4527 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4528 | } |
| 4529 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4530 | if (mddev->bitmap_file) { |
| 4531 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4532 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4533 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4534 | mddev->bitmap_file = NULL; |
| 4535 | } |
| 4536 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4537 | return err; |
| 4538 | } |
| 4539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | /* |
| 4541 | * set_array_info is used two different ways |
| 4542 | * The original usage is when creating a new array. |
| 4543 | * In this usage, raid_disks is > 0 and it together with |
| 4544 | * level, size, not_persistent,layout,chunksize determine the |
| 4545 | * shape of the array. |
| 4546 | * This will always create an array with a type-0.90.0 superblock. |
| 4547 | * The newer usage is when assembling an array. |
| 4548 | * In this case raid_disks will be 0, and the major_version field is |
| 4549 | * use to determine which style super-blocks are to be found on the devices. |
| 4550 | * The minor and patch _version numbers are also kept incase the |
| 4551 | * super_block handler wishes to interpret them. |
| 4552 | */ |
| 4553 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4554 | { |
| 4555 | |
| 4556 | if (info->raid_disks == 0) { |
| 4557 | /* just setting version number for superblock loading */ |
| 4558 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4559 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | super_types[info->major_version].name == NULL) { |
| 4561 | /* maybe try to auto-load a module? */ |
| 4562 | printk(KERN_INFO |
| 4563 | "md: superblock version %d not known\n", |
| 4564 | info->major_version); |
| 4565 | return -EINVAL; |
| 4566 | } |
| 4567 | mddev->major_version = info->major_version; |
| 4568 | mddev->minor_version = info->minor_version; |
| 4569 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4570 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | return 0; |
| 4572 | } |
| 4573 | mddev->major_version = MD_MAJOR_VERSION; |
| 4574 | mddev->minor_version = MD_MINOR_VERSION; |
| 4575 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4576 | mddev->ctime = get_seconds(); |
| 4577 | |
| 4578 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4579 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | mddev->size = info->size; |
| 4581 | mddev->raid_disks = info->raid_disks; |
| 4582 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4583 | * openned |
| 4584 | */ |
| 4585 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4586 | mddev->recovery_cp = MaxSector; |
| 4587 | else |
| 4588 | mddev->recovery_cp = 0; |
| 4589 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4590 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | |
| 4592 | mddev->layout = info->layout; |
| 4593 | mddev->chunk_size = info->chunk_size; |
| 4594 | |
| 4595 | mddev->max_disks = MD_SB_DISKS; |
| 4596 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4597 | if (mddev->persistent) |
| 4598 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4599 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4601 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4602 | mddev->bitmap_offset = 0; |
| 4603 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4604 | mddev->reshape_position = MaxSector; |
| 4605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | /* |
| 4607 | * Generate a 128 bit UUID |
| 4608 | */ |
| 4609 | get_random_bytes(mddev->uuid, 16); |
| 4610 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4611 | mddev->new_level = mddev->level; |
| 4612 | mddev->new_chunk = mddev->chunk_size; |
| 4613 | mddev->new_layout = mddev->layout; |
| 4614 | mddev->delta_disks = 0; |
| 4615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | return 0; |
| 4617 | } |
| 4618 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4619 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4620 | { |
| 4621 | mdk_rdev_t * rdev; |
| 4622 | int rv; |
| 4623 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4624 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4625 | |
| 4626 | if (mddev->pers->resize == NULL) |
| 4627 | return -EINVAL; |
| 4628 | /* The "size" is the amount of each device that is used. |
| 4629 | * This can only make sense for arrays with redundancy. |
| 4630 | * linear and raid0 always use whatever space is available |
| 4631 | * We can only consider changing the size if no resync |
| 4632 | * or reconstruction is happening, and if the new size |
| 4633 | * is acceptable. It must fit before the sb_offset or, |
| 4634 | * if that is <data_offset, it must fit before the |
| 4635 | * size of each device. |
| 4636 | * If size is zero, we find the largest size that fits. |
| 4637 | */ |
| 4638 | if (mddev->sync_thread) |
| 4639 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4640 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4641 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4642 | avail = rdev->size * 2; |
| 4643 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4644 | if (fit && (size == 0 || size > avail/2)) |
| 4645 | size = avail/2; |
| 4646 | if (avail < ((sector_t)size << 1)) |
| 4647 | return -ENOSPC; |
| 4648 | } |
| 4649 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4650 | if (!rv) { |
| 4651 | struct block_device *bdev; |
| 4652 | |
| 4653 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4654 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4655 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4656 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4657 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4658 | bdput(bdev); |
| 4659 | } |
| 4660 | } |
| 4661 | return rv; |
| 4662 | } |
| 4663 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4664 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4665 | { |
| 4666 | int rv; |
| 4667 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4668 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4669 | return -EINVAL; |
| 4670 | if (raid_disks <= 0 || |
| 4671 | raid_disks >= mddev->max_disks) |
| 4672 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4673 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4674 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4675 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4676 | |
| 4677 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4678 | return rv; |
| 4679 | } |
| 4680 | |
| 4681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | /* |
| 4683 | * update_array_info is used to change the configuration of an |
| 4684 | * on-line array. |
| 4685 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4686 | * fields in the info are checked against the array. |
| 4687 | * Any differences that cannot be handled will cause an error. |
| 4688 | * Normally, only one change can be managed at a time. |
| 4689 | */ |
| 4690 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4691 | { |
| 4692 | int rv = 0; |
| 4693 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4694 | int state = 0; |
| 4695 | |
| 4696 | /* calculate expected state,ignoring low bits */ |
| 4697 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4698 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | |
| 4700 | if (mddev->major_version != info->major_version || |
| 4701 | mddev->minor_version != info->minor_version || |
| 4702 | /* mddev->patch_version != info->patch_version || */ |
| 4703 | mddev->ctime != info->ctime || |
| 4704 | mddev->level != info->level || |
| 4705 | /* mddev->layout != info->layout || */ |
| 4706 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4707 | mddev->chunk_size != info->chunk_size || |
| 4708 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4709 | ((state^info->state) & 0xfffffe00) |
| 4710 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | return -EINVAL; |
| 4712 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4713 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4715 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4716 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | if (cnt == 0) return 0; |
| 4718 | if (cnt > 1) return -EINVAL; |
| 4719 | |
| 4720 | if (mddev->layout != info->layout) { |
| 4721 | /* Change layout |
| 4722 | * we don't need to do anything at the md level, the |
| 4723 | * personality will take care of it all. |
| 4724 | */ |
| 4725 | if (mddev->pers->reconfig == NULL) |
| 4726 | return -EINVAL; |
| 4727 | else |
| 4728 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4729 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4730 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4731 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4733 | if (mddev->raid_disks != info->raid_disks) |
| 4734 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4736 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4737 | if (mddev->pers->quiesce == NULL) |
| 4738 | return -EINVAL; |
| 4739 | if (mddev->recovery || mddev->sync_thread) |
| 4740 | return -EBUSY; |
| 4741 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4742 | /* add the bitmap */ |
| 4743 | if (mddev->bitmap) |
| 4744 | return -EEXIST; |
| 4745 | if (mddev->default_bitmap_offset == 0) |
| 4746 | return -EINVAL; |
| 4747 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4748 | mddev->pers->quiesce(mddev, 1); |
| 4749 | rv = bitmap_create(mddev); |
| 4750 | if (rv) |
| 4751 | bitmap_destroy(mddev); |
| 4752 | mddev->pers->quiesce(mddev, 0); |
| 4753 | } else { |
| 4754 | /* remove the bitmap */ |
| 4755 | if (!mddev->bitmap) |
| 4756 | return -ENOENT; |
| 4757 | if (mddev->bitmap->file) |
| 4758 | return -EINVAL; |
| 4759 | mddev->pers->quiesce(mddev, 1); |
| 4760 | bitmap_destroy(mddev); |
| 4761 | mddev->pers->quiesce(mddev, 0); |
| 4762 | mddev->bitmap_offset = 0; |
| 4763 | } |
| 4764 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4765 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | return rv; |
| 4767 | } |
| 4768 | |
| 4769 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4770 | { |
| 4771 | mdk_rdev_t *rdev; |
| 4772 | |
| 4773 | if (mddev->pers == NULL) |
| 4774 | return -ENODEV; |
| 4775 | |
| 4776 | rdev = find_rdev(mddev, dev); |
| 4777 | if (!rdev) |
| 4778 | return -ENODEV; |
| 4779 | |
| 4780 | md_error(mddev, rdev); |
| 4781 | return 0; |
| 4782 | } |
| 4783 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 4784 | /* |
| 4785 | * We have a problem here : there is no easy way to give a CHS |
| 4786 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4787 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4788 | * dosfs just mad... ;-) |
| 4789 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4790 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4791 | { |
| 4792 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4793 | |
| 4794 | geo->heads = 2; |
| 4795 | geo->sectors = 4; |
| 4796 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4801 | unsigned int cmd, unsigned long arg) |
| 4802 | { |
| 4803 | int err = 0; |
| 4804 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | mddev_t *mddev = NULL; |
| 4806 | |
| 4807 | if (!capable(CAP_SYS_ADMIN)) |
| 4808 | return -EACCES; |
| 4809 | |
| 4810 | /* |
| 4811 | * Commands dealing with the RAID driver but not any |
| 4812 | * particular array: |
| 4813 | */ |
| 4814 | switch (cmd) |
| 4815 | { |
| 4816 | case RAID_VERSION: |
| 4817 | err = get_version(argp); |
| 4818 | goto done; |
| 4819 | |
| 4820 | case PRINT_RAID_DEBUG: |
| 4821 | err = 0; |
| 4822 | md_print_devices(); |
| 4823 | goto done; |
| 4824 | |
| 4825 | #ifndef MODULE |
| 4826 | case RAID_AUTORUN: |
| 4827 | err = 0; |
| 4828 | autostart_arrays(arg); |
| 4829 | goto done; |
| 4830 | #endif |
| 4831 | default:; |
| 4832 | } |
| 4833 | |
| 4834 | /* |
| 4835 | * Commands creating/starting a new array: |
| 4836 | */ |
| 4837 | |
| 4838 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4839 | |
| 4840 | if (!mddev) { |
| 4841 | BUG(); |
| 4842 | goto abort; |
| 4843 | } |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | err = mddev_lock(mddev); |
| 4846 | if (err) { |
| 4847 | printk(KERN_INFO |
| 4848 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4849 | err, cmd); |
| 4850 | goto abort; |
| 4851 | } |
| 4852 | |
| 4853 | switch (cmd) |
| 4854 | { |
| 4855 | case SET_ARRAY_INFO: |
| 4856 | { |
| 4857 | mdu_array_info_t info; |
| 4858 | if (!arg) |
| 4859 | memset(&info, 0, sizeof(info)); |
| 4860 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4861 | err = -EFAULT; |
| 4862 | goto abort_unlock; |
| 4863 | } |
| 4864 | if (mddev->pers) { |
| 4865 | err = update_array_info(mddev, &info); |
| 4866 | if (err) { |
| 4867 | printk(KERN_WARNING "md: couldn't update" |
| 4868 | " array info. %d\n", err); |
| 4869 | goto abort_unlock; |
| 4870 | } |
| 4871 | goto done_unlock; |
| 4872 | } |
| 4873 | if (!list_empty(&mddev->disks)) { |
| 4874 | printk(KERN_WARNING |
| 4875 | "md: array %s already has disks!\n", |
| 4876 | mdname(mddev)); |
| 4877 | err = -EBUSY; |
| 4878 | goto abort_unlock; |
| 4879 | } |
| 4880 | if (mddev->raid_disks) { |
| 4881 | printk(KERN_WARNING |
| 4882 | "md: array %s already initialised!\n", |
| 4883 | mdname(mddev)); |
| 4884 | err = -EBUSY; |
| 4885 | goto abort_unlock; |
| 4886 | } |
| 4887 | err = set_array_info(mddev, &info); |
| 4888 | if (err) { |
| 4889 | printk(KERN_WARNING "md: couldn't set" |
| 4890 | " array info. %d\n", err); |
| 4891 | goto abort_unlock; |
| 4892 | } |
| 4893 | } |
| 4894 | goto done_unlock; |
| 4895 | |
| 4896 | default:; |
| 4897 | } |
| 4898 | |
| 4899 | /* |
| 4900 | * Commands querying/configuring an existing array: |
| 4901 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4902 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4903 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4904 | if ((!mddev->raid_disks && !mddev->external) |
| 4905 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4906 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4907 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | err = -ENODEV; |
| 4909 | goto abort_unlock; |
| 4910 | } |
| 4911 | |
| 4912 | /* |
| 4913 | * Commands even a read-only array can execute: |
| 4914 | */ |
| 4915 | switch (cmd) |
| 4916 | { |
| 4917 | case GET_ARRAY_INFO: |
| 4918 | err = get_array_info(mddev, argp); |
| 4919 | goto done_unlock; |
| 4920 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4921 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4922 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4923 | goto done_unlock; |
| 4924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | case GET_DISK_INFO: |
| 4926 | err = get_disk_info(mddev, argp); |
| 4927 | goto done_unlock; |
| 4928 | |
| 4929 | case RESTART_ARRAY_RW: |
| 4930 | err = restart_array(mddev); |
| 4931 | goto done_unlock; |
| 4932 | |
| 4933 | case STOP_ARRAY: |
| 4934 | err = do_md_stop (mddev, 0); |
| 4935 | goto done_unlock; |
| 4936 | |
| 4937 | case STOP_ARRAY_RO: |
| 4938 | err = do_md_stop (mddev, 1); |
| 4939 | goto done_unlock; |
| 4940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /* |
| 4944 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4945 | * superblock, so we do not allow them on read-only arrays. |
| 4946 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4947 | * here and hit the 'default' below, so only disallow |
| 4948 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 4950 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4951 | if (mddev->ro == 2) { |
| 4952 | mddev->ro = 0; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4953 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 4954 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4955 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4956 | } else { |
| 4957 | err = -EROFS; |
| 4958 | goto abort_unlock; |
| 4959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | switch (cmd) |
| 4963 | { |
| 4964 | case ADD_NEW_DISK: |
| 4965 | { |
| 4966 | mdu_disk_info_t info; |
| 4967 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4968 | err = -EFAULT; |
| 4969 | else |
| 4970 | err = add_new_disk(mddev, &info); |
| 4971 | goto done_unlock; |
| 4972 | } |
| 4973 | |
| 4974 | case HOT_REMOVE_DISK: |
| 4975 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4976 | goto done_unlock; |
| 4977 | |
| 4978 | case HOT_ADD_DISK: |
| 4979 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4980 | goto done_unlock; |
| 4981 | |
| 4982 | case SET_DISK_FAULTY: |
| 4983 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4984 | goto done_unlock; |
| 4985 | |
| 4986 | case RUN_ARRAY: |
| 4987 | err = do_md_run (mddev); |
| 4988 | goto done_unlock; |
| 4989 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4990 | case SET_BITMAP_FILE: |
| 4991 | err = set_bitmap_file(mddev, (int)arg); |
| 4992 | goto done_unlock; |
| 4993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | err = -EINVAL; |
| 4996 | goto abort_unlock; |
| 4997 | } |
| 4998 | |
| 4999 | done_unlock: |
| 5000 | abort_unlock: |
| 5001 | mddev_unlock(mddev); |
| 5002 | |
| 5003 | return err; |
| 5004 | done: |
| 5005 | if (err) |
| 5006 | MD_BUG(); |
| 5007 | abort: |
| 5008 | return err; |
| 5009 | } |
| 5010 | |
| 5011 | static int md_open(struct inode *inode, struct file *file) |
| 5012 | { |
| 5013 | /* |
| 5014 | * Succeed if we can lock the mddev, which confirms that |
| 5015 | * it isn't being stopped right now. |
| 5016 | */ |
| 5017 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5018 | int err; |
| 5019 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5020 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | goto out; |
| 5022 | |
| 5023 | err = 0; |
| 5024 | mddev_get(mddev); |
| 5025 | mddev_unlock(mddev); |
| 5026 | |
| 5027 | check_disk_change(inode->i_bdev); |
| 5028 | out: |
| 5029 | return err; |
| 5030 | } |
| 5031 | |
| 5032 | static int md_release(struct inode *inode, struct file * file) |
| 5033 | { |
| 5034 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5035 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5036 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | mddev_put(mddev); |
| 5038 | |
| 5039 | return 0; |
| 5040 | } |
| 5041 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5042 | static int md_media_changed(struct gendisk *disk) |
| 5043 | { |
| 5044 | mddev_t *mddev = disk->private_data; |
| 5045 | |
| 5046 | return mddev->changed; |
| 5047 | } |
| 5048 | |
| 5049 | static int md_revalidate(struct gendisk *disk) |
| 5050 | { |
| 5051 | mddev_t *mddev = disk->private_data; |
| 5052 | |
| 5053 | mddev->changed = 0; |
| 5054 | return 0; |
| 5055 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | static struct block_device_operations md_fops = |
| 5057 | { |
| 5058 | .owner = THIS_MODULE, |
| 5059 | .open = md_open, |
| 5060 | .release = md_release, |
| 5061 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5062 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5063 | .media_changed = md_media_changed, |
| 5064 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | }; |
| 5066 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5067 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | { |
| 5069 | mdk_thread_t *thread = arg; |
| 5070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | /* |
| 5072 | * md_thread is a 'system-thread', it's priority should be very |
| 5073 | * high. We avoid resource deadlocks individually in each |
| 5074 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5075 | * the very same RT priority as kswapd, thus we will never get |
| 5076 | * into a priority inversion deadlock. |
| 5077 | * |
| 5078 | * we definitely have to have equal or higher priority than |
| 5079 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5080 | * many dirty RAID5 blocks. |
| 5081 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5083 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5084 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5086 | /* We need to wait INTERRUPTIBLE so that |
| 5087 | * we don't add to the load-average. |
| 5088 | * That means we need to be sure no signals are |
| 5089 | * pending |
| 5090 | */ |
| 5091 | if (signal_pending(current)) |
| 5092 | flush_signals(current); |
| 5093 | |
| 5094 | wait_event_interruptible_timeout |
| 5095 | (thread->wqueue, |
| 5096 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5097 | || kthread_should_stop(), |
| 5098 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | |
| 5100 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5101 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5102 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | return 0; |
| 5106 | } |
| 5107 | |
| 5108 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5109 | { |
| 5110 | if (thread) { |
| 5111 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5112 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5113 | wake_up(&thread->wqueue); |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5118 | const char *name) |
| 5119 | { |
| 5120 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5122 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | if (!thread) |
| 5124 | return NULL; |
| 5125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | init_waitqueue_head(&thread->wqueue); |
| 5127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | thread->run = run; |
| 5129 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5130 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5131 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5132 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | kfree(thread); |
| 5134 | return NULL; |
| 5135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | return thread; |
| 5137 | } |
| 5138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | void md_unregister_thread(mdk_thread_t *thread) |
| 5140 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5141 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5142 | |
| 5143 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | kfree(thread); |
| 5145 | } |
| 5146 | |
| 5147 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5148 | { |
| 5149 | if (!mddev) { |
| 5150 | MD_BUG(); |
| 5151 | return; |
| 5152 | } |
| 5153 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5154 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5156 | |
| 5157 | if (mddev->external) |
| 5158 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5159 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5161 | mdname(mddev), |
| 5162 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5163 | __builtin_return_address(0),__builtin_return_address(1), |
| 5164 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5165 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5166 | if (!mddev->pers) |
| 5167 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | if (!mddev->pers->error_handler) |
| 5169 | return; |
| 5170 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5171 | if (mddev->degraded) |
| 5172 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5173 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5175 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5176 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5177 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
| 5180 | /* seq_file implementation /proc/mdstat */ |
| 5181 | |
| 5182 | static void status_unused(struct seq_file *seq) |
| 5183 | { |
| 5184 | int i = 0; |
| 5185 | mdk_rdev_t *rdev; |
| 5186 | struct list_head *tmp; |
| 5187 | |
| 5188 | seq_printf(seq, "unused devices: "); |
| 5189 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5190 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | char b[BDEVNAME_SIZE]; |
| 5192 | i++; |
| 5193 | seq_printf(seq, "%s ", |
| 5194 | bdevname(rdev->bdev,b)); |
| 5195 | } |
| 5196 | if (!i) |
| 5197 | seq_printf(seq, "<none>"); |
| 5198 | |
| 5199 | seq_printf(seq, "\n"); |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5204 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5205 | sector_t max_blocks, resync, res; |
| 5206 | unsigned long dt, db, rt; |
| 5207 | int scale; |
| 5208 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | |
| 5210 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5211 | |
| 5212 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5213 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5214 | else |
| 5215 | max_blocks = mddev->size; |
| 5216 | |
| 5217 | /* |
| 5218 | * Should not happen. |
| 5219 | */ |
| 5220 | if (!max_blocks) { |
| 5221 | MD_BUG(); |
| 5222 | return; |
| 5223 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5224 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5225 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5226 | * u32, as those are the requirements for sector_div. |
| 5227 | * Thus 'scale' must be at least 10 |
| 5228 | */ |
| 5229 | scale = 10; |
| 5230 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5231 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5232 | scale++; |
| 5233 | } |
| 5234 | res = (resync>>scale)*1000; |
| 5235 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5236 | |
| 5237 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5239 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | seq_printf(seq, "["); |
| 5241 | for (i = 0; i < x; i++) |
| 5242 | seq_printf(seq, "="); |
| 5243 | seq_printf(seq, ">"); |
| 5244 | for (i = 0; i < y; i++) |
| 5245 | seq_printf(seq, "."); |
| 5246 | seq_printf(seq, "] "); |
| 5247 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5248 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5249 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5250 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5251 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5252 | "check" : |
| 5253 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5254 | "resync" : "recovery"))), |
| 5255 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5256 | (unsigned long long) resync, |
| 5257 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | |
| 5259 | /* |
| 5260 | * We do not want to overflow, so the order of operands and |
| 5261 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5262 | * safe against division by zero. We only estimate anyway. |
| 5263 | * |
| 5264 | * dt: time from mark until now |
| 5265 | * db: blocks written from mark until now |
| 5266 | * rt: remaining time |
| 5267 | */ |
| 5268 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5269 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5270 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5271 | - mddev->resync_mark_cnt; |
| 5272 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | |
| 5274 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5275 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5276 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5280 | { |
| 5281 | struct list_head *tmp; |
| 5282 | loff_t l = *pos; |
| 5283 | mddev_t *mddev; |
| 5284 | |
| 5285 | if (l >= 0x10000) |
| 5286 | return NULL; |
| 5287 | if (!l--) |
| 5288 | /* header */ |
| 5289 | return (void*)1; |
| 5290 | |
| 5291 | spin_lock(&all_mddevs_lock); |
| 5292 | list_for_each(tmp,&all_mddevs) |
| 5293 | if (!l--) { |
| 5294 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5295 | mddev_get(mddev); |
| 5296 | spin_unlock(&all_mddevs_lock); |
| 5297 | return mddev; |
| 5298 | } |
| 5299 | spin_unlock(&all_mddevs_lock); |
| 5300 | if (!l--) |
| 5301 | return (void*)2;/* tail */ |
| 5302 | return NULL; |
| 5303 | } |
| 5304 | |
| 5305 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5306 | { |
| 5307 | struct list_head *tmp; |
| 5308 | mddev_t *next_mddev, *mddev = v; |
| 5309 | |
| 5310 | ++*pos; |
| 5311 | if (v == (void*)2) |
| 5312 | return NULL; |
| 5313 | |
| 5314 | spin_lock(&all_mddevs_lock); |
| 5315 | if (v == (void*)1) |
| 5316 | tmp = all_mddevs.next; |
| 5317 | else |
| 5318 | tmp = mddev->all_mddevs.next; |
| 5319 | if (tmp != &all_mddevs) |
| 5320 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5321 | else { |
| 5322 | next_mddev = (void*)2; |
| 5323 | *pos = 0x10000; |
| 5324 | } |
| 5325 | spin_unlock(&all_mddevs_lock); |
| 5326 | |
| 5327 | if (v != (void*)1) |
| 5328 | mddev_put(mddev); |
| 5329 | return next_mddev; |
| 5330 | |
| 5331 | } |
| 5332 | |
| 5333 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5334 | { |
| 5335 | mddev_t *mddev = v; |
| 5336 | |
| 5337 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5338 | mddev_put(mddev); |
| 5339 | } |
| 5340 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5341 | struct mdstat_info { |
| 5342 | int event; |
| 5343 | }; |
| 5344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5346 | { |
| 5347 | mddev_t *mddev = v; |
| 5348 | sector_t size; |
| 5349 | struct list_head *tmp2; |
| 5350 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5351 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5352 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | |
| 5354 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5355 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | seq_printf(seq, "Personalities : "); |
| 5357 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5358 | list_for_each_entry(pers, &pers_list, list) |
| 5359 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | |
| 5361 | spin_unlock(&pers_lock); |
| 5362 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5363 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | return 0; |
| 5365 | } |
| 5366 | if (v == (void*)2) { |
| 5367 | status_unused(seq); |
| 5368 | return 0; |
| 5369 | } |
| 5370 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5371 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5375 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5376 | mddev->pers ? "" : "in"); |
| 5377 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5378 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5380 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5381 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | seq_printf(seq, " %s", mddev->pers->name); |
| 5383 | } |
| 5384 | |
| 5385 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5386 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | char b[BDEVNAME_SIZE]; |
| 5388 | seq_printf(seq, " %s[%d]", |
| 5389 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5391 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5392 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | seq_printf(seq, "(F)"); |
| 5394 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5395 | } else if (rdev->raid_disk < 0) |
| 5396 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | size += rdev->size; |
| 5398 | } |
| 5399 | |
| 5400 | if (!list_empty(&mddev->disks)) { |
| 5401 | if (mddev->pers) |
| 5402 | seq_printf(seq, "\n %llu blocks", |
| 5403 | (unsigned long long)mddev->array_size); |
| 5404 | else |
| 5405 | seq_printf(seq, "\n %llu blocks", |
| 5406 | (unsigned long long)size); |
| 5407 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5408 | if (mddev->persistent) { |
| 5409 | if (mddev->major_version != 0 || |
| 5410 | mddev->minor_version != 90) { |
| 5411 | seq_printf(seq," super %d.%d", |
| 5412 | mddev->major_version, |
| 5413 | mddev->minor_version); |
| 5414 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5415 | } else if (mddev->external) |
| 5416 | seq_printf(seq, " super external:%s", |
| 5417 | mddev->metadata_type); |
| 5418 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5419 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | |
| 5421 | if (mddev->pers) { |
| 5422 | mddev->pers->status (seq, mddev); |
| 5423 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5424 | if (mddev->pers->sync_request) { |
| 5425 | if (mddev->curr_resync > 2) { |
| 5426 | status_resync (seq, mddev); |
| 5427 | seq_printf(seq, "\n "); |
| 5428 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5429 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5430 | else if (mddev->recovery_cp < MaxSector) |
| 5431 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5432 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5433 | } else |
| 5434 | seq_printf(seq, "\n "); |
| 5435 | |
| 5436 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5437 | unsigned long chunk_kb; |
| 5438 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5439 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5440 | chunk_kb = bitmap->chunksize >> 10; |
| 5441 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5442 | "%lu%s chunk", |
| 5443 | bitmap->pages - bitmap->missing_pages, |
| 5444 | bitmap->pages, |
| 5445 | (bitmap->pages - bitmap->missing_pages) |
| 5446 | << (PAGE_SHIFT - 10), |
| 5447 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5448 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5449 | if (bitmap->file) { |
| 5450 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5451 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5452 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5453 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5454 | seq_printf(seq, "\n"); |
| 5455 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | seq_printf(seq, "\n"); |
| 5459 | } |
| 5460 | mddev_unlock(mddev); |
| 5461 | |
| 5462 | return 0; |
| 5463 | } |
| 5464 | |
| 5465 | static struct seq_operations md_seq_ops = { |
| 5466 | .start = md_seq_start, |
| 5467 | .next = md_seq_next, |
| 5468 | .stop = md_seq_stop, |
| 5469 | .show = md_seq_show, |
| 5470 | }; |
| 5471 | |
| 5472 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5473 | { |
| 5474 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5475 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5476 | if (mi == NULL) |
| 5477 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | |
| 5479 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5480 | if (error) |
| 5481 | kfree(mi); |
| 5482 | else { |
| 5483 | struct seq_file *p = file->private_data; |
| 5484 | p->private = mi; |
| 5485 | mi->event = atomic_read(&md_event_count); |
| 5486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | return error; |
| 5488 | } |
| 5489 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5490 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5491 | { |
| 5492 | struct seq_file *m = filp->private_data; |
| 5493 | struct mdstat_info *mi = m->private; |
| 5494 | int mask; |
| 5495 | |
| 5496 | poll_wait(filp, &md_event_waiters, wait); |
| 5497 | |
| 5498 | /* always allow read */ |
| 5499 | mask = POLLIN | POLLRDNORM; |
| 5500 | |
| 5501 | if (mi->event != atomic_read(&md_event_count)) |
| 5502 | mask |= POLLERR | POLLPRI; |
| 5503 | return mask; |
| 5504 | } |
| 5505 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5506 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5507 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | .open = md_seq_open, |
| 5509 | .read = seq_read, |
| 5510 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5511 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5512 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | }; |
| 5514 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5515 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5518 | list_add_tail(&p->list, &pers_list); |
| 5519 | 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] | 5520 | spin_unlock(&pers_lock); |
| 5521 | return 0; |
| 5522 | } |
| 5523 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5524 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5526 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5528 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | spin_unlock(&pers_lock); |
| 5530 | return 0; |
| 5531 | } |
| 5532 | |
| 5533 | static int is_mddev_idle(mddev_t *mddev) |
| 5534 | { |
| 5535 | mdk_rdev_t * rdev; |
| 5536 | struct list_head *tmp; |
| 5537 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5538 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | |
| 5540 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5541 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5543 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5544 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5546 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5547 | * as sync_io is counted when a request starts, and |
| 5548 | * disk_stats is counted when it completes. |
| 5549 | * So resync activity will cause curr_events to be smaller than |
| 5550 | * when there was no such activity. |
| 5551 | * non-sync IO will cause disk_stat to increase without |
| 5552 | * increasing sync_io so curr_events will (eventually) |
| 5553 | * be larger than it was before. Once it becomes |
| 5554 | * substantially larger, the test below will cause |
| 5555 | * the array to appear non-idle, and resync will slow |
| 5556 | * down. |
| 5557 | * If there is a lot of outstanding resync activity when |
| 5558 | * we set last_event to curr_events, then all that activity |
| 5559 | * completing might cause the array to appear non-idle |
| 5560 | * and resync will be slowed down even though there might |
| 5561 | * not have been non-resync activity. This will only |
| 5562 | * happen once though. 'last_events' will soon reflect |
| 5563 | * the state where there is little or no outstanding |
| 5564 | * resync requests, and further resync activity will |
| 5565 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5566 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5568 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | rdev->last_events = curr_events; |
| 5570 | idle = 0; |
| 5571 | } |
| 5572 | } |
| 5573 | return idle; |
| 5574 | } |
| 5575 | |
| 5576 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5577 | { |
| 5578 | /* another "blocks" (512byte) blocks have been synced */ |
| 5579 | atomic_sub(blocks, &mddev->recovery_active); |
| 5580 | wake_up(&mddev->recovery_wait); |
| 5581 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5582 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | md_wakeup_thread(mddev->thread); |
| 5584 | // stop recovery, signal do_sync .... |
| 5585 | } |
| 5586 | } |
| 5587 | |
| 5588 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5589 | /* md_write_start(mddev, bi) |
| 5590 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5591 | * in superblock) before writing, schedule a superblock update |
| 5592 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5593 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5594 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5596 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5597 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5598 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5599 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5600 | BUG_ON(mddev->ro == 1); |
| 5601 | if (mddev->ro == 2) { |
| 5602 | /* need to switch to read/write */ |
| 5603 | mddev->ro = 0; |
| 5604 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5605 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5606 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5607 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5608 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5609 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5610 | if (mddev->safemode == 1) |
| 5611 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5612 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5613 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5614 | if (mddev->in_sync) { |
| 5615 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5616 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5617 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5618 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5619 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5620 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5621 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5622 | if (did_change) |
| 5623 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5624 | wait_event(mddev->sb_wait, |
| 5625 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5626 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | void md_write_end(mddev_t *mddev) |
| 5630 | { |
| 5631 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5632 | if (mddev->safemode == 2) |
| 5633 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5634 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5636 | } |
| 5637 | } |
| 5638 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5639 | /* md_allow_write(mddev) |
| 5640 | * Calling this ensures that the array is marked 'active' so that writes |
| 5641 | * may proceed without blocking. It is important to call this before |
| 5642 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5643 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5644 | * |
| 5645 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5646 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5647 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5648 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5649 | { |
| 5650 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5651 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5652 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5653 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5654 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5655 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5656 | |
| 5657 | spin_lock_irq(&mddev->write_lock); |
| 5658 | if (mddev->in_sync) { |
| 5659 | mddev->in_sync = 0; |
| 5660 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5661 | if (mddev->safemode_delay && |
| 5662 | mddev->safemode == 0) |
| 5663 | mddev->safemode = 1; |
| 5664 | spin_unlock_irq(&mddev->write_lock); |
| 5665 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5666 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5667 | } else |
| 5668 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5669 | |
| 5670 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5671 | return -EAGAIN; |
| 5672 | else |
| 5673 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5674 | } |
| 5675 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | #define SYNC_MARKS 10 |
| 5678 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5679 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | { |
| 5681 | mddev_t *mddev2; |
| 5682 | unsigned int currspeed = 0, |
| 5683 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5684 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | unsigned long mark[SYNC_MARKS]; |
| 5686 | sector_t mark_cnt[SYNC_MARKS]; |
| 5687 | int last_mark,m; |
| 5688 | struct list_head *tmp; |
| 5689 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5690 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5691 | struct list_head *rtmp; |
| 5692 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5693 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | |
| 5695 | /* just incase thread restarts... */ |
| 5696 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5697 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5698 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5699 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5701 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5702 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5703 | desc = "data-check"; |
| 5704 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5705 | desc = "requested-resync"; |
| 5706 | else |
| 5707 | desc = "resync"; |
| 5708 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5709 | desc = "reshape"; |
| 5710 | else |
| 5711 | desc = "recovery"; |
| 5712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | /* we overload curr_resync somewhat here. |
| 5714 | * 0 == not engaged in resync at all |
| 5715 | * 2 == checking that there is no conflict with another sync |
| 5716 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5717 | * commense |
| 5718 | * other == active in resync - this many blocks |
| 5719 | * |
| 5720 | * Before starting a resync we must have set curr_resync to |
| 5721 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5722 | * less than ours. When we find one that is the same or higher |
| 5723 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5724 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5725 | * This will mean we have to start checking from the beginning again. |
| 5726 | * |
| 5727 | */ |
| 5728 | |
| 5729 | do { |
| 5730 | mddev->curr_resync = 2; |
| 5731 | |
| 5732 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5733 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5734 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | goto skip; |
| 5736 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5737 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | if (mddev2 == mddev) |
| 5739 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5740 | if (!mddev->parallel_resync |
| 5741 | && mddev2->curr_resync |
| 5742 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | DEFINE_WAIT(wq); |
| 5744 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5745 | /* arbitrarily yield */ |
| 5746 | mddev->curr_resync = 1; |
| 5747 | wake_up(&resync_wait); |
| 5748 | } |
| 5749 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5750 | /* no need to wait here, we can wait the next |
| 5751 | * time 'round when curr_resync == 2 |
| 5752 | */ |
| 5753 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5754 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5755 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5756 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5757 | printk(KERN_INFO "md: delaying %s of %s" |
| 5758 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5760 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | mddev_put(mddev2); |
| 5762 | schedule(); |
| 5763 | finish_wait(&resync_wait, &wq); |
| 5764 | goto try_again; |
| 5765 | } |
| 5766 | finish_wait(&resync_wait, &wq); |
| 5767 | } |
| 5768 | } |
| 5769 | } while (mddev->curr_resync < 2); |
| 5770 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5771 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5772 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5774 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | */ |
| 5776 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5777 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5778 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5779 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5780 | j = mddev->resync_min; |
| 5781 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5782 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5783 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5784 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5785 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5786 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | /* recovery follows the physical size of devices */ |
| 5788 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5789 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5790 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5791 | if (rdev->raid_disk >= 0 && |
| 5792 | !test_bit(Faulty, &rdev->flags) && |
| 5793 | !test_bit(In_sync, &rdev->flags) && |
| 5794 | rdev->recovery_offset < j) |
| 5795 | j = rdev->recovery_offset; |
| 5796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5798 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5799 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5800 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5801 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5802 | "(but not more than %d KB/sec) for %s.\n", |
| 5803 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | |
| 5805 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5806 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5807 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5809 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5810 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | } |
| 5812 | last_mark = 0; |
| 5813 | mddev->resync_mark = mark[last_mark]; |
| 5814 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5815 | |
| 5816 | /* |
| 5817 | * Tune reconstruction: |
| 5818 | */ |
| 5819 | window = 32*(PAGE_SIZE/512); |
| 5820 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5821 | window/2,(unsigned long long) max_sectors/2); |
| 5822 | |
| 5823 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | last_check = 0; |
| 5825 | |
| 5826 | if (j>2) { |
| 5827 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5828 | "md: resuming %s of %s from checkpoint.\n", |
| 5829 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | mddev->curr_resync = j; |
| 5831 | } |
| 5832 | |
| 5833 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5834 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5836 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5837 | if (j >= mddev->resync_max) { |
| 5838 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5839 | wait_event(mddev->recovery_wait, |
| 5840 | mddev->resync_max > j |
| 5841 | || kthread_should_stop()); |
| 5842 | } |
| 5843 | if (kthread_should_stop()) |
| 5844 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5845 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5846 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5847 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5848 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | goto out; |
| 5850 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5851 | |
| 5852 | if (!skipped) { /* actual IO requested */ |
| 5853 | io_sectors += sectors; |
| 5854 | atomic_add(sectors, &mddev->recovery_active); |
| 5855 | } |
| 5856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | j += sectors; |
| 5858 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5859 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5860 | if (last_check == 0) |
| 5861 | /* this is the earliers that rebuilt will be |
| 5862 | * visible in /proc/mdstat |
| 5863 | */ |
| 5864 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5865 | |
| 5866 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | continue; |
| 5868 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5869 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5871 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | break; |
| 5873 | |
| 5874 | repeat: |
| 5875 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5876 | /* step marks */ |
| 5877 | int next = (last_mark+1) % SYNC_MARKS; |
| 5878 | |
| 5879 | mddev->resync_mark = mark[next]; |
| 5880 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5881 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5882 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | last_mark = next; |
| 5884 | } |
| 5885 | |
| 5886 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5887 | if (kthread_should_stop()) |
| 5888 | goto interrupted; |
| 5889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | |
| 5891 | /* |
| 5892 | * this loop exits only if either when we are slower than |
| 5893 | * the 'hard' speed limit, or the system was IO-idle for |
| 5894 | * a jiffy. |
| 5895 | * the system might be non-idle CPU-wise, but we only care |
| 5896 | * about not overloading the IO subsystem. (things like an |
| 5897 | * e2fsck being done on the RAID array should execute fast) |
| 5898 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5899 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | cond_resched(); |
| 5901 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5902 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5903 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5905 | if (currspeed > speed_min(mddev)) { |
| 5906 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5908 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | goto repeat; |
| 5910 | } |
| 5911 | } |
| 5912 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5913 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | /* |
| 5915 | * this also signals 'finished resyncing' to md_stop |
| 5916 | */ |
| 5917 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5918 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | |
| 5920 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5921 | |
| 5922 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5923 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5925 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5926 | mddev->curr_resync > 2) { |
| 5927 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5928 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5929 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5930 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5931 | "md: checkpointing %s of %s.\n", |
| 5932 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5933 | mddev->recovery_cp = mddev->curr_resync; |
| 5934 | } |
| 5935 | } else |
| 5936 | mddev->recovery_cp = MaxSector; |
| 5937 | } else { |
| 5938 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5939 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5940 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5941 | if (rdev->raid_disk >= 0 && |
| 5942 | !test_bit(Faulty, &rdev->flags) && |
| 5943 | !test_bit(In_sync, &rdev->flags) && |
| 5944 | rdev->recovery_offset < mddev->curr_resync) |
| 5945 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5948 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | skip: |
| 5951 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5952 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5953 | mddev->resync_max = MaxSector; |
| 5954 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | wake_up(&resync_wait); |
| 5956 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5957 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5958 | return; |
| 5959 | |
| 5960 | interrupted: |
| 5961 | /* |
| 5962 | * got a signal, exit. |
| 5963 | */ |
| 5964 | printk(KERN_INFO |
| 5965 | "md: md_do_sync() got signal ... exiting\n"); |
| 5966 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5967 | goto out; |
| 5968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5970 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | |
| 5972 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5973 | static int remove_and_add_spares(mddev_t *mddev) |
| 5974 | { |
| 5975 | mdk_rdev_t *rdev; |
| 5976 | struct list_head *rtmp; |
| 5977 | int spares = 0; |
| 5978 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5979 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5980 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5981 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5982 | (test_bit(Faulty, &rdev->flags) || |
| 5983 | ! test_bit(In_sync, &rdev->flags)) && |
| 5984 | atomic_read(&rdev->nr_pending)==0) { |
| 5985 | if (mddev->pers->hot_remove_disk( |
| 5986 | mddev, rdev->raid_disk)==0) { |
| 5987 | char nm[20]; |
| 5988 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5989 | sysfs_remove_link(&mddev->kobj, nm); |
| 5990 | rdev->raid_disk = -1; |
| 5991 | } |
| 5992 | } |
| 5993 | |
| 5994 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5995 | rdev_for_each(rdev, rtmp, mddev) { |
| 5996 | if (rdev->raid_disk >= 0 && |
| 5997 | !test_bit(In_sync, &rdev->flags)) |
| 5998 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5999 | if (rdev->raid_disk < 0 |
| 6000 | && !test_bit(Faulty, &rdev->flags)) { |
| 6001 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6002 | if (mddev->pers-> |
| 6003 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6004 | char nm[20]; |
| 6005 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6006 | if (sysfs_create_link(&mddev->kobj, |
| 6007 | &rdev->kobj, nm)) |
| 6008 | printk(KERN_WARNING |
| 6009 | "md: cannot register " |
| 6010 | "%s for %s\n", |
| 6011 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6012 | spares++; |
| 6013 | md_new_event(mddev); |
| 6014 | } else |
| 6015 | break; |
| 6016 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6017 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6018 | } |
| 6019 | return spares; |
| 6020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | /* |
| 6022 | * This routine is regularly called by all per-raid-array threads to |
| 6023 | * deal with generic issues like resync and super-block update. |
| 6024 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6025 | * need this as they never do any recovery or update the superblock. |
| 6026 | * |
| 6027 | * It does not do any resync itself, but rather "forks" off other threads |
| 6028 | * to do that as needed. |
| 6029 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6030 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6031 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | * and wakeups up this thread which will reap the thread and finish up. |
| 6033 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6034 | * |
| 6035 | * The overall approach is: |
| 6036 | * 1/ if the superblock needs updating, update it. |
| 6037 | * 2/ If a recovery thread is running, don't do anything else. |
| 6038 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6039 | * 4/ If there are any faulty devices, remove them. |
| 6040 | * 5/ If array is degraded, try to add spares devices |
| 6041 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6042 | */ |
| 6043 | void md_check_recovery(mddev_t *mddev) |
| 6044 | { |
| 6045 | mdk_rdev_t *rdev; |
| 6046 | struct list_head *rtmp; |
| 6047 | |
| 6048 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6049 | if (mddev->bitmap) |
| 6050 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | |
| 6052 | if (mddev->ro) |
| 6053 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6054 | |
| 6055 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6056 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6057 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6058 | mdname(mddev)); |
| 6059 | mddev->safemode = 2; |
| 6060 | } |
| 6061 | flush_signals(current); |
| 6062 | } |
| 6063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6065 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6067 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6068 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6069 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6070 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | )) |
| 6072 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6073 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6074 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6075 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6076 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6077 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6078 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6079 | spin_lock_irq(&mddev->write_lock); |
| 6080 | if (mddev->safemode && |
| 6081 | !atomic_read(&mddev->writes_pending) && |
| 6082 | !mddev->in_sync && |
| 6083 | mddev->recovery_cp == MaxSector) { |
| 6084 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6085 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6086 | if (mddev->persistent) |
| 6087 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6088 | } |
| 6089 | if (mddev->safemode == 1) |
| 6090 | mddev->safemode = 0; |
| 6091 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6092 | if (did_change) |
| 6093 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6094 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6095 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6096 | if (mddev->flags) |
| 6097 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6098 | |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6099 | rdev_for_each(rdev, rtmp, mddev) |
| 6100 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
| 6101 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6102 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6105 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6106 | /* resync/recovery still happening */ |
| 6107 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6108 | goto unlock; |
| 6109 | } |
| 6110 | if (mddev->sync_thread) { |
| 6111 | /* resync has finished, collect result */ |
| 6112 | md_unregister_thread(mddev->sync_thread); |
| 6113 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6114 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | /* success...*/ |
| 6116 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6117 | if (mddev->pers->spare_active(mddev)) |
| 6118 | sysfs_notify(&mddev->kobj, NULL, |
| 6119 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6121 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6122 | |
| 6123 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6124 | * information must be scrapped |
| 6125 | */ |
| 6126 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6127 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6128 | rdev->saved_raid_disk = -1; |
| 6129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | mddev->recovery = 0; |
| 6131 | /* flag recovery needed just to double check */ |
| 6132 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6133 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6134 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | goto unlock; |
| 6136 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6137 | /* Set RUNNING before clearing NEEDED to avoid |
| 6138 | * any transients in the value of "sync_action". |
| 6139 | */ |
| 6140 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6141 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6142 | /* Clear some bits that don't mean anything, but |
| 6143 | * might be left set |
| 6144 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6145 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6146 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6148 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6149 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | /* no recovery is running. |
| 6151 | * remove any failed drives, then |
| 6152 | * add spares if possible. |
| 6153 | * Spare are also removed and re-added, to allow |
| 6154 | * the personality to fail the re-add. |
| 6155 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6157 | if (mddev->reshape_position != MaxSector) { |
| 6158 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6159 | /* Cannot proceed */ |
| 6160 | goto unlock; |
| 6161 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6162 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6163 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6164 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6165 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6166 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6167 | } else if (mddev->recovery_cp < MaxSector) { |
| 6168 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6169 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6170 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6171 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6175 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6176 | /* We are adding a device or devices to an array |
| 6177 | * which has the bitmap stored on all devices. |
| 6178 | * So make sure all bitmap pages get written |
| 6179 | */ |
| 6180 | bitmap_write_all(mddev->bitmap); |
| 6181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6183 | mddev, |
| 6184 | "%s_resync"); |
| 6185 | if (!mddev->sync_thread) { |
| 6186 | printk(KERN_ERR "%s: could not start resync" |
| 6187 | " thread...\n", |
| 6188 | mdname(mddev)); |
| 6189 | /* leave the spares where they are, it shouldn't hurt */ |
| 6190 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6191 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6193 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6194 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | } |
| 6196 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6197 | if (!mddev->sync_thread) { |
| 6198 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6199 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6200 | &mddev->recovery)) |
| 6201 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
| 6202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | mddev_unlock(mddev); |
| 6204 | } |
| 6205 | } |
| 6206 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6207 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6208 | { |
| 6209 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6210 | wait_event_timeout(rdev->blocked_wait, |
| 6211 | !test_bit(Blocked, &rdev->flags), |
| 6212 | msecs_to_jiffies(5000)); |
| 6213 | rdev_dec_pending(rdev, mddev); |
| 6214 | } |
| 6215 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6216 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6217 | static int md_notify_reboot(struct notifier_block *this, |
| 6218 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | { |
| 6220 | struct list_head *tmp; |
| 6221 | mddev_t *mddev; |
| 6222 | |
| 6223 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6224 | |
| 6225 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6226 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6227 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6228 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6230 | mddev_unlock(mddev); |
| 6231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | /* |
| 6233 | * certain more exotic SCSI devices are known to be |
| 6234 | * volatile wrt too early system reboots. While the |
| 6235 | * right place to handle this issue is the given |
| 6236 | * driver, we do want to have a safe RAID driver ... |
| 6237 | */ |
| 6238 | mdelay(1000*1); |
| 6239 | } |
| 6240 | return NOTIFY_DONE; |
| 6241 | } |
| 6242 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6243 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | .notifier_call = md_notify_reboot, |
| 6245 | .next = NULL, |
| 6246 | .priority = INT_MAX, /* before any real devices */ |
| 6247 | }; |
| 6248 | |
| 6249 | static void md_geninit(void) |
| 6250 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6252 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6253 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | } |
| 6255 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6256 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | if (register_blkdev(MAJOR_NR, "md")) |
| 6259 | return -1; |
| 6260 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6261 | unregister_blkdev(MAJOR_NR, "md"); |
| 6262 | return -1; |
| 6263 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6264 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6265 | md_probe, NULL, NULL); |
| 6266 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | md_probe, NULL, NULL); |
| 6268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6270 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | |
| 6272 | md_geninit(); |
| 6273 | return (0); |
| 6274 | } |
| 6275 | |
| 6276 | |
| 6277 | #ifndef MODULE |
| 6278 | |
| 6279 | /* |
| 6280 | * Searches all registered partitions for autorun RAID arrays |
| 6281 | * at boot time. |
| 6282 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6283 | |
| 6284 | static LIST_HEAD(all_detected_devices); |
| 6285 | struct detected_devices_node { |
| 6286 | struct list_head list; |
| 6287 | dev_t dev; |
| 6288 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | |
| 6290 | void md_autodetect_dev(dev_t dev) |
| 6291 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6292 | struct detected_devices_node *node_detected_dev; |
| 6293 | |
| 6294 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6295 | if (node_detected_dev) { |
| 6296 | node_detected_dev->dev = dev; |
| 6297 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6298 | } else { |
| 6299 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6300 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | |
| 6305 | static void autostart_arrays(int part) |
| 6306 | { |
| 6307 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6308 | struct detected_devices_node *node_detected_dev; |
| 6309 | dev_t dev; |
| 6310 | int i_scanned, i_passed; |
| 6311 | |
| 6312 | i_scanned = 0; |
| 6313 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | |
| 6315 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6316 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6317 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6318 | i_scanned++; |
| 6319 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6320 | struct detected_devices_node, list); |
| 6321 | list_del(&node_detected_dev->list); |
| 6322 | dev = node_detected_dev->dev; |
| 6323 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6324 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | if (IS_ERR(rdev)) |
| 6326 | continue; |
| 6327 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6328 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6329 | MD_BUG(); |
| 6330 | continue; |
| 6331 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6332 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6334 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6336 | |
| 6337 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6338 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | |
| 6340 | autorun_devices(part); |
| 6341 | } |
| 6342 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6343 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | |
| 6345 | static __exit void md_exit(void) |
| 6346 | { |
| 6347 | mddev_t *mddev; |
| 6348 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6349 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6350 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6351 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | |
| 6353 | unregister_blkdev(MAJOR_NR,"md"); |
| 6354 | unregister_blkdev(mdp_major, "mdp"); |
| 6355 | unregister_reboot_notifier(&md_notifier); |
| 6356 | unregister_sysctl_table(raid_table_header); |
| 6357 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6358 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | struct gendisk *disk = mddev->gendisk; |
| 6360 | if (!disk) |
| 6361 | continue; |
| 6362 | export_array(mddev); |
| 6363 | del_gendisk(disk); |
| 6364 | put_disk(disk); |
| 6365 | mddev->gendisk = NULL; |
| 6366 | mddev_put(mddev); |
| 6367 | } |
| 6368 | } |
| 6369 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6370 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | module_exit(md_exit) |
| 6372 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6373 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6374 | { |
| 6375 | return sprintf(buffer, "%d", start_readonly); |
| 6376 | } |
| 6377 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6378 | { |
| 6379 | char *e; |
| 6380 | int num = simple_strtoul(val, &e, 10); |
| 6381 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6382 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6383 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6384 | } |
| 6385 | return -EINVAL; |
| 6386 | } |
| 6387 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6388 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6389 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6390 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | EXPORT_SYMBOL(register_md_personality); |
| 6393 | EXPORT_SYMBOL(unregister_md_personality); |
| 6394 | EXPORT_SYMBOL(md_error); |
| 6395 | EXPORT_SYMBOL(md_done_sync); |
| 6396 | EXPORT_SYMBOL(md_write_start); |
| 6397 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | EXPORT_SYMBOL(md_register_thread); |
| 6399 | EXPORT_SYMBOL(md_unregister_thread); |
| 6400 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | EXPORT_SYMBOL(md_check_recovery); |
| 6402 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6403 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6404 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |