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); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 172 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 173 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 174 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 175 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 176 | /* Alternate version that can be called from interrupts |
| 177 | * when calling sysfs_notify isn't needed. |
| 178 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 179 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 180 | { |
| 181 | atomic_inc(&md_event_count); |
| 182 | wake_up(&md_event_waiters); |
| 183 | } |
| 184 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 185 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * Enables to iterate over all existing md arrays |
| 187 | * all_mddevs_lock protects this list. |
| 188 | */ |
| 189 | static LIST_HEAD(all_mddevs); |
| 190 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 191 | |
| 192 | |
| 193 | /* |
| 194 | * iterates through all used mddevs in the system. |
| 195 | * We take care to grab the all_mddevs_lock whenever navigating |
| 196 | * the list, and to always hold a refcount when unlocked. |
| 197 | * Any code which breaks out of this loop while own |
| 198 | * a reference to the current mddev and must mddev_put it. |
| 199 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 200 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | \ |
| 202 | for (({ spin_lock(&all_mddevs_lock); \ |
| 203 | tmp = all_mddevs.next; \ |
| 204 | mddev = NULL;}); \ |
| 205 | ({ if (tmp != &all_mddevs) \ |
| 206 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 207 | spin_unlock(&all_mddevs_lock); \ |
| 208 | if (mddev) mddev_put(mddev); \ |
| 209 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 210 | tmp != &all_mddevs;}); \ |
| 211 | ({ spin_lock(&all_mddevs_lock); \ |
| 212 | tmp = tmp->next;}) \ |
| 213 | ) |
| 214 | |
| 215 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 216 | static int md_fail_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 218 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 223 | { |
| 224 | atomic_inc(&mddev->active); |
| 225 | return mddev; |
| 226 | } |
| 227 | |
| 228 | static void mddev_put(mddev_t *mddev) |
| 229 | { |
| 230 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 231 | return; |
| 232 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 233 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 234 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 235 | blk_cleanup_queue(mddev->queue); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 236 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 237 | } else |
| 238 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static mddev_t * mddev_find(dev_t unit) |
| 242 | { |
| 243 | mddev_t *mddev, *new = NULL; |
| 244 | |
| 245 | retry: |
| 246 | spin_lock(&all_mddevs_lock); |
| 247 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 248 | if (mddev->unit == unit) { |
| 249 | mddev_get(mddev); |
| 250 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 251 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return mddev; |
| 253 | } |
| 254 | |
| 255 | if (new) { |
| 256 | list_add(&new->all_mddevs, &all_mddevs); |
| 257 | spin_unlock(&all_mddevs_lock); |
| 258 | return new; |
| 259 | } |
| 260 | spin_unlock(&all_mddevs_lock); |
| 261 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 262 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!new) |
| 264 | return NULL; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | new->unit = unit; |
| 267 | if (MAJOR(unit) == MD_MAJOR) |
| 268 | new->md_minor = MINOR(unit); |
| 269 | else |
| 270 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 271 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 272 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | INIT_LIST_HEAD(&new->disks); |
| 274 | INIT_LIST_HEAD(&new->all_mddevs); |
| 275 | init_timer(&new->safemode_timer); |
| 276 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 277 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 278 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame^] | 279 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 280 | new->reshape_position = MaxSector; |
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"); |
| 375 | return -EINVAL; |
| 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 | { |
| 546 | if ( (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)) |
| 550 | |
| 551 | return 1; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 558 | { |
| 559 | int ret; |
| 560 | mdp_super_t *tmp1, *tmp2; |
| 561 | |
| 562 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 563 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 564 | |
| 565 | if (!tmp1 || !tmp2) { |
| 566 | ret = 0; |
| 567 | printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n"); |
| 568 | goto abort; |
| 569 | } |
| 570 | |
| 571 | *tmp1 = *sb1; |
| 572 | *tmp2 = *sb2; |
| 573 | |
| 574 | /* |
| 575 | * nr_disks is not constant |
| 576 | */ |
| 577 | tmp1->nr_disks = 0; |
| 578 | tmp2->nr_disks = 0; |
| 579 | |
| 580 | if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4)) |
| 581 | ret = 0; |
| 582 | else |
| 583 | ret = 1; |
| 584 | |
| 585 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 586 | kfree(tmp1); |
| 587 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return ret; |
| 589 | } |
| 590 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 591 | |
| 592 | static u32 md_csum_fold(u32 csum) |
| 593 | { |
| 594 | csum = (csum & 0xffff) + (csum >> 16); |
| 595 | return (csum & 0xffff) + (csum >> 16); |
| 596 | } |
| 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 599 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 600 | u64 newcsum = 0; |
| 601 | u32 *sb32 = (u32*)sb; |
| 602 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | unsigned int disk_csum, csum; |
| 604 | |
| 605 | disk_csum = sb->sb_csum; |
| 606 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 607 | |
| 608 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 609 | newcsum += sb32[i]; |
| 610 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 611 | |
| 612 | |
| 613 | #ifdef CONFIG_ALPHA |
| 614 | /* This used to use csum_partial, which was wrong for several |
| 615 | * reasons including that different results are returned on |
| 616 | * different architectures. It isn't critical that we get exactly |
| 617 | * the same return value as before (we always csum_fold before |
| 618 | * testing, and that removes any differences). However as we |
| 619 | * know that csum_partial always returned a 16bit value on |
| 620 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 621 | */ |
| 622 | sb->sb_csum = md_csum_fold(disk_csum); |
| 623 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 625 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return csum; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | /* |
| 631 | * Handle superblock details. |
| 632 | * We want to be able to handle multiple superblock formats |
| 633 | * so we have a common interface to them all, and an array of |
| 634 | * different handlers. |
| 635 | * We rely on user-space to write the initial superblock, and support |
| 636 | * reading and updating of superblocks. |
| 637 | * Interface methods are: |
| 638 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 639 | * loads and validates a superblock on dev. |
| 640 | * if refdev != NULL, compare superblocks on both devices |
| 641 | * Return: |
| 642 | * 0 - dev has a superblock that is compatible with refdev |
| 643 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 644 | * so dev should be used as the refdev in future |
| 645 | * -EINVAL superblock incompatible or invalid |
| 646 | * -othererror e.g. -EIO |
| 647 | * |
| 648 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 649 | * Verify that dev is acceptable into mddev. |
| 650 | * The first time, mddev->raid_disks will be 0, and data from |
| 651 | * dev should be merged in. Subsequent calls check that dev |
| 652 | * is new enough. Return 0 or -EINVAL |
| 653 | * |
| 654 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 655 | * Update the superblock for rdev with data in mddev |
| 656 | * This does not write to disc. |
| 657 | * |
| 658 | */ |
| 659 | |
| 660 | struct super_type { |
| 661 | char *name; |
| 662 | struct module *owner; |
| 663 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version); |
| 664 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 665 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 666 | }; |
| 667 | |
| 668 | /* |
| 669 | * load_super for 0.90.0 |
| 670 | */ |
| 671 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 672 | { |
| 673 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 674 | mdp_super_t *sb; |
| 675 | int ret; |
| 676 | sector_t sb_offset; |
| 677 | |
| 678 | /* |
| 679 | * Calculate the position of the superblock, |
| 680 | * it's at the end of the disk. |
| 681 | * |
| 682 | * It also happens to be a multiple of 4Kb. |
| 683 | */ |
| 684 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 685 | rdev->sb_offset = sb_offset; |
| 686 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 687 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | if (ret) return ret; |
| 689 | |
| 690 | ret = -EINVAL; |
| 691 | |
| 692 | bdevname(rdev->bdev, b); |
| 693 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 694 | |
| 695 | if (sb->md_magic != MD_SB_MAGIC) { |
| 696 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 697 | b); |
| 698 | goto abort; |
| 699 | } |
| 700 | |
| 701 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 702 | sb->minor_version < 90 || |
| 703 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 705 | sb->major_version, sb->minor_version, |
| 706 | b); |
| 707 | goto abort; |
| 708 | } |
| 709 | |
| 710 | if (sb->raid_disks <= 0) |
| 711 | goto abort; |
| 712 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 713 | 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] | 714 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 715 | b); |
| 716 | goto abort; |
| 717 | } |
| 718 | |
| 719 | rdev->preferred_minor = sb->md_minor; |
| 720 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 721 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 723 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 724 | if (sb->level != 1 && sb->level != 4 |
| 725 | && sb->level != 5 && sb->level != 6 |
| 726 | && sb->level != 10) { |
| 727 | /* FIXME use a better test */ |
| 728 | printk(KERN_WARNING |
| 729 | "md: bitmaps not supported for this level.\n"); |
| 730 | goto abort; |
| 731 | } |
| 732 | } |
| 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | if (sb->level == LEVEL_MULTIPATH) |
| 735 | rdev->desc_nr = -1; |
| 736 | else |
| 737 | rdev->desc_nr = sb->this_disk.number; |
| 738 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 739 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 741 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | __u64 ev1, ev2; |
| 743 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 744 | if (!uuid_equal(refsb, sb)) { |
| 745 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 746 | b, bdevname(refdev->bdev,b2)); |
| 747 | goto abort; |
| 748 | } |
| 749 | if (!sb_equal(refsb, sb)) { |
| 750 | printk(KERN_WARNING "md: %s has same UUID" |
| 751 | " but different superblock to %s\n", |
| 752 | b, bdevname(refdev->bdev, b2)); |
| 753 | goto abort; |
| 754 | } |
| 755 | ev1 = md_event(sb); |
| 756 | ev2 = md_event(refsb); |
| 757 | if (ev1 > ev2) |
| 758 | ret = 1; |
| 759 | else |
| 760 | ret = 0; |
| 761 | } |
| 762 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 763 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 764 | if (rdev->size < sb->size && sb->level > 1) |
| 765 | /* "this cannot possibly happen" ... */ |
| 766 | ret = -EINVAL; |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | abort: |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * validate_super for 0.90.0 |
| 774 | */ |
| 775 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 776 | { |
| 777 | mdp_disk_t *desc; |
| 778 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 779 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 781 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 782 | clear_bit(Faulty, &rdev->flags); |
| 783 | clear_bit(In_sync, &rdev->flags); |
| 784 | clear_bit(WriteMostly, &rdev->flags); |
| 785 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | if (mddev->raid_disks == 0) { |
| 788 | mddev->major_version = 0; |
| 789 | mddev->minor_version = sb->minor_version; |
| 790 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 791 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | mddev->chunk_size = sb->chunk_size; |
| 793 | mddev->ctime = sb->ctime; |
| 794 | mddev->utime = sb->utime; |
| 795 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 796 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | mddev->layout = sb->layout; |
| 798 | mddev->raid_disks = sb->raid_disks; |
| 799 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 800 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 801 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 802 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 804 | if (mddev->minor_version >= 91) { |
| 805 | mddev->reshape_position = sb->reshape_position; |
| 806 | mddev->delta_disks = sb->delta_disks; |
| 807 | mddev->new_level = sb->new_level; |
| 808 | mddev->new_layout = sb->new_layout; |
| 809 | mddev->new_chunk = sb->new_chunk; |
| 810 | } else { |
| 811 | mddev->reshape_position = MaxSector; |
| 812 | mddev->delta_disks = 0; |
| 813 | mddev->new_level = mddev->level; |
| 814 | mddev->new_layout = mddev->layout; |
| 815 | mddev->new_chunk = mddev->chunk_size; |
| 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 819 | mddev->recovery_cp = MaxSector; |
| 820 | else { |
| 821 | if (sb->events_hi == sb->cp_events_hi && |
| 822 | sb->events_lo == sb->cp_events_lo) { |
| 823 | mddev->recovery_cp = sb->recovery_cp; |
| 824 | } else |
| 825 | mddev->recovery_cp = 0; |
| 826 | } |
| 827 | |
| 828 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 829 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 830 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 831 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 832 | |
| 833 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 834 | |
| 835 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 836 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 837 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 838 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 839 | } else if (mddev->pers == NULL) { |
| 840 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | ++ev1; |
| 842 | if (ev1 < mddev->events) |
| 843 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 844 | } else if (mddev->bitmap) { |
| 845 | /* if adding to array with a bitmap, then we can accept an |
| 846 | * older device ... but not too old. |
| 847 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 848 | if (ev1 < mddev->bitmap->events_cleared) |
| 849 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 850 | } else { |
| 851 | if (ev1 < mddev->events) |
| 852 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 853 | return 0; |
| 854 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | desc = sb->disks + rdev->desc_nr; |
| 858 | |
| 859 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 860 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 861 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 862 | desc->raid_disk < mddev->raid_disks */) { |
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 | rdev->raid_disk = desc->raid_disk; |
| 865 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 866 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 867 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 868 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 869 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * sync_super for 0.90.0 |
| 875 | */ |
| 876 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 877 | { |
| 878 | mdp_super_t *sb; |
| 879 | struct list_head *tmp; |
| 880 | mdk_rdev_t *rdev2; |
| 881 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | /* make rdev->sb match mddev data.. |
| 885 | * |
| 886 | * 1/ zero out disks |
| 887 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 888 | * 3/ any empty disks < next_spare become removed |
| 889 | * |
| 890 | * disks[0] gets initialised to REMOVED because |
| 891 | * we cannot be sure from other fields if it has |
| 892 | * been initialised or not. |
| 893 | */ |
| 894 | int i; |
| 895 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 896 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 897 | rdev->sb_size = MD_SB_BYTES; |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 900 | |
| 901 | memset(sb, 0, sizeof(*sb)); |
| 902 | |
| 903 | sb->md_magic = MD_SB_MAGIC; |
| 904 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | sb->patch_version = mddev->patch_version; |
| 906 | sb->gvalid_words = 0; /* ignored */ |
| 907 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 908 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 909 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 910 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 911 | |
| 912 | sb->ctime = mddev->ctime; |
| 913 | sb->level = mddev->level; |
| 914 | sb->size = mddev->size; |
| 915 | sb->raid_disks = mddev->raid_disks; |
| 916 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 917 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | sb->utime = mddev->utime; |
| 919 | sb->state = 0; |
| 920 | sb->events_hi = (mddev->events>>32); |
| 921 | sb->events_lo = (u32)mddev->events; |
| 922 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 923 | if (mddev->reshape_position == MaxSector) |
| 924 | sb->minor_version = 90; |
| 925 | else { |
| 926 | sb->minor_version = 91; |
| 927 | sb->reshape_position = mddev->reshape_position; |
| 928 | sb->new_level = mddev->new_level; |
| 929 | sb->delta_disks = mddev->delta_disks; |
| 930 | sb->new_layout = mddev->new_layout; |
| 931 | sb->new_chunk = mddev->new_chunk; |
| 932 | } |
| 933 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | if (mddev->in_sync) |
| 935 | { |
| 936 | sb->recovery_cp = mddev->recovery_cp; |
| 937 | sb->cp_events_hi = (mddev->events>>32); |
| 938 | sb->cp_events_lo = (u32)mddev->events; |
| 939 | if (mddev->recovery_cp == MaxSector) |
| 940 | sb->state = (1<< MD_SB_CLEAN); |
| 941 | } else |
| 942 | sb->recovery_cp = 0; |
| 943 | |
| 944 | sb->layout = mddev->layout; |
| 945 | sb->chunk_size = mddev->chunk_size; |
| 946 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 947 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 948 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 951 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 953 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 954 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 955 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 956 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 958 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 959 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | d = &sb->disks[rdev2->desc_nr]; |
| 961 | nr_disks++; |
| 962 | d->number = rdev2->desc_nr; |
| 963 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 964 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 965 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 966 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | d->raid_disk = rdev2->raid_disk; |
| 968 | else |
| 969 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 970 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 972 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | d->state = (1<<MD_DISK_ACTIVE); |
| 974 | d->state |= (1<<MD_DISK_SYNC); |
| 975 | active++; |
| 976 | working++; |
| 977 | } else { |
| 978 | d->state = 0; |
| 979 | spare++; |
| 980 | working++; |
| 981 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 982 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 983 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 986 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 987 | mdp_disk_t *d = &sb->disks[i]; |
| 988 | if (d->state == 0 && d->number == 0) { |
| 989 | d->number = i; |
| 990 | d->raid_disk = i; |
| 991 | d->state = (1<<MD_DISK_REMOVED); |
| 992 | d->state |= (1<<MD_DISK_FAULTY); |
| 993 | failed++; |
| 994 | } |
| 995 | } |
| 996 | sb->nr_disks = nr_disks; |
| 997 | sb->active_disks = active; |
| 998 | sb->working_disks = working; |
| 999 | sb->failed_disks = failed; |
| 1000 | sb->spare_disks = spare; |
| 1001 | |
| 1002 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1003 | sb->sb_csum = calc_sb_csum(sb); |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * version 1 superblock |
| 1008 | */ |
| 1009 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1010 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1012 | __le32 disk_csum; |
| 1013 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | unsigned long long newcsum; |
| 1015 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1016 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | int i; |
| 1018 | |
| 1019 | disk_csum = sb->sb_csum; |
| 1020 | sb->sb_csum = 0; |
| 1021 | newcsum = 0; |
| 1022 | for (i=0; size>=4; size -= 4 ) |
| 1023 | newcsum += le32_to_cpu(*isuper++); |
| 1024 | |
| 1025 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1026 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1029 | sb->sb_csum = disk_csum; |
| 1030 | return cpu_to_le32(csum); |
| 1031 | } |
| 1032 | |
| 1033 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1034 | { |
| 1035 | struct mdp_superblock_1 *sb; |
| 1036 | int ret; |
| 1037 | sector_t sb_offset; |
| 1038 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1039 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | /* |
| 1042 | * Calculate the position of the superblock. |
| 1043 | * It is always aligned to a 4K boundary and |
| 1044 | * depeding on minor_version, it can be: |
| 1045 | * 0: At least 8K, but less than 12K, from end of device |
| 1046 | * 1: At start of device |
| 1047 | * 2: 4K from start of device. |
| 1048 | */ |
| 1049 | switch(minor_version) { |
| 1050 | case 0: |
| 1051 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 1052 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1053 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | /* convert from sectors to K */ |
| 1055 | sb_offset /= 2; |
| 1056 | break; |
| 1057 | case 1: |
| 1058 | sb_offset = 0; |
| 1059 | break; |
| 1060 | case 2: |
| 1061 | sb_offset = 4; |
| 1062 | break; |
| 1063 | default: |
| 1064 | return -EINVAL; |
| 1065 | } |
| 1066 | rdev->sb_offset = sb_offset; |
| 1067 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1068 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1069 | * and it is safe to read 4k, so we do that |
| 1070 | */ |
| 1071 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | if (ret) return ret; |
| 1073 | |
| 1074 | |
| 1075 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1076 | |
| 1077 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1078 | sb->major_version != cpu_to_le32(1) || |
| 1079 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 1080 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1081 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | |
| 1084 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1085 | printk("md: invalid superblock checksum on %s\n", |
| 1086 | bdevname(rdev->bdev,b)); |
| 1087 | return -EINVAL; |
| 1088 | } |
| 1089 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1090 | printk("md: data_size too small on %s\n", |
| 1091 | bdevname(rdev->bdev,b)); |
| 1092 | return -EINVAL; |
| 1093 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1094 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1095 | if (sb->level != cpu_to_le32(1) && |
| 1096 | sb->level != cpu_to_le32(4) && |
| 1097 | sb->level != cpu_to_le32(5) && |
| 1098 | sb->level != cpu_to_le32(6) && |
| 1099 | sb->level != cpu_to_le32(10)) { |
| 1100 | printk(KERN_WARNING |
| 1101 | "md: bitmaps not supported for this level.\n"); |
| 1102 | return -EINVAL; |
| 1103 | } |
| 1104 | } |
| 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | rdev->preferred_minor = 0xffff; |
| 1107 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1108 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1110 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1111 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1112 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1113 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1114 | |
| 1115 | if (minor_version |
| 1116 | && rdev->data_offset < sb_offset + (rdev->sb_size/512)) |
| 1117 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1118 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1119 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1120 | rdev->desc_nr = -1; |
| 1121 | else |
| 1122 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1123 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1124 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1125 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1126 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | __u64 ev1, ev2; |
| 1128 | struct mdp_superblock_1 *refsb = |
| 1129 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1130 | |
| 1131 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1132 | sb->level != refsb->level || |
| 1133 | sb->layout != refsb->layout || |
| 1134 | sb->chunksize != refsb->chunksize) { |
| 1135 | printk(KERN_WARNING "md: %s has strangely different" |
| 1136 | " superblock to %s\n", |
| 1137 | bdevname(rdev->bdev,b), |
| 1138 | bdevname(refdev->bdev,b2)); |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | ev1 = le64_to_cpu(sb->events); |
| 1142 | ev2 = le64_to_cpu(refsb->events); |
| 1143 | |
| 1144 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1145 | ret = 1; |
| 1146 | else |
| 1147 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1149 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1151 | else |
| 1152 | rdev->size = rdev->sb_offset; |
| 1153 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1154 | return -EINVAL; |
| 1155 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1156 | if (le32_to_cpu(sb->chunksize)) |
| 1157 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1158 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1159 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1160 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1161 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1165 | { |
| 1166 | 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] | 1167 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1169 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1170 | clear_bit(Faulty, &rdev->flags); |
| 1171 | clear_bit(In_sync, &rdev->flags); |
| 1172 | clear_bit(WriteMostly, &rdev->flags); |
| 1173 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | if (mddev->raid_disks == 0) { |
| 1176 | mddev->major_version = 1; |
| 1177 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1178 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1180 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1181 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1182 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1183 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | mddev->layout = le32_to_cpu(sb->layout); |
| 1185 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1186 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1187 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1188 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1189 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1192 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1193 | |
| 1194 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1195 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1196 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1197 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1198 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1199 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1200 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1201 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1202 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1203 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1204 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1205 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1206 | } else { |
| 1207 | mddev->reshape_position = MaxSector; |
| 1208 | mddev->delta_disks = 0; |
| 1209 | mddev->new_level = mddev->level; |
| 1210 | mddev->new_layout = mddev->layout; |
| 1211 | mddev->new_chunk = mddev->chunk_size; |
| 1212 | } |
| 1213 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1214 | } else if (mddev->pers == NULL) { |
| 1215 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | ++ev1; |
| 1217 | if (ev1 < mddev->events) |
| 1218 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1219 | } else if (mddev->bitmap) { |
| 1220 | /* If adding to array with a bitmap, then we can accept an |
| 1221 | * older device, but not too old. |
| 1222 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1223 | if (ev1 < mddev->bitmap->events_cleared) |
| 1224 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1225 | } else { |
| 1226 | if (ev1 < mddev->events) |
| 1227 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1228 | return 0; |
| 1229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1231 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1233 | switch(role) { |
| 1234 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | break; |
| 1236 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1237 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | break; |
| 1239 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1240 | if ((le32_to_cpu(sb->feature_map) & |
| 1241 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1242 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1243 | else |
| 1244 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | rdev->raid_disk = role; |
| 1246 | break; |
| 1247 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1248 | if (sb->devflags & WriteMostly1) |
| 1249 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1250 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1251 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1257 | { |
| 1258 | struct mdp_superblock_1 *sb; |
| 1259 | struct list_head *tmp; |
| 1260 | mdk_rdev_t *rdev2; |
| 1261 | int max_dev, i; |
| 1262 | /* make rdev->sb match mddev and rdev data. */ |
| 1263 | |
| 1264 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1265 | |
| 1266 | sb->feature_map = 0; |
| 1267 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1268 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1270 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1271 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1272 | |
| 1273 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1274 | sb->events = cpu_to_le64(mddev->events); |
| 1275 | if (mddev->in_sync) |
| 1276 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1277 | else |
| 1278 | sb->resync_offset = cpu_to_le64(0); |
| 1279 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1280 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1281 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1282 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1283 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1284 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1285 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1286 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1287 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1288 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1289 | |
| 1290 | if (rdev->raid_disk >= 0 && |
| 1291 | !test_bit(In_sync, &rdev->flags) && |
| 1292 | rdev->recovery_offset > 0) { |
| 1293 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1294 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1295 | } |
| 1296 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1297 | if (mddev->reshape_position != MaxSector) { |
| 1298 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1299 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1300 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1301 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1302 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1303 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1304 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | max_dev = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1307 | rdev_for_each(rdev2, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | if (rdev2->desc_nr+1 > max_dev) |
| 1309 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1312 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | for (i=0; i<max_dev;i++) |
| 1314 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1315 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1316 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1318 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1320 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1322 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1323 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | else |
| 1325 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1326 | } |
| 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1329 | } |
| 1330 | |
| 1331 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1332 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | [0] = { |
| 1334 | .name = "0.90.0", |
| 1335 | .owner = THIS_MODULE, |
| 1336 | .load_super = super_90_load, |
| 1337 | .validate_super = super_90_validate, |
| 1338 | .sync_super = super_90_sync, |
| 1339 | }, |
| 1340 | [1] = { |
| 1341 | .name = "md-1", |
| 1342 | .owner = THIS_MODULE, |
| 1343 | .load_super = super_1_load, |
| 1344 | .validate_super = super_1_validate, |
| 1345 | .sync_super = super_1_sync, |
| 1346 | }, |
| 1347 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1350 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1351 | struct list_head *tmp, *tmp2; |
| 1352 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1354 | rdev_for_each(rdev, tmp, mddev1) |
| 1355 | rdev_for_each(rdev2, tmp2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1356 | if (rdev->bdev->bd_contains == |
| 1357 | rdev2->bdev->bd_contains) |
| 1358 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static LIST_HEAD(pending_raid_disks); |
| 1364 | |
| 1365 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1366 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1367 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1368 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1369 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1370 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | if (rdev->mddev) { |
| 1373 | MD_BUG(); |
| 1374 | return -EINVAL; |
| 1375 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1376 | |
| 1377 | /* prevent duplicates */ |
| 1378 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1379 | return -EEXIST; |
| 1380 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1381 | /* make sure rdev->size exceeds mddev->size */ |
| 1382 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1383 | if (mddev->pers) { |
| 1384 | /* Cannot change size, so fail |
| 1385 | * If mddev->level <= 0, then we don't care |
| 1386 | * about aligning sizes (e.g. linear) |
| 1387 | */ |
| 1388 | if (mddev->level > 0) |
| 1389 | return -ENOSPC; |
| 1390 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1391 | mddev->size = rdev->size; |
| 1392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | /* Verify rdev->desc_nr is unique. |
| 1395 | * If it is -1, assign a free number, else |
| 1396 | * check number is not in use |
| 1397 | */ |
| 1398 | if (rdev->desc_nr < 0) { |
| 1399 | int choice = 0; |
| 1400 | if (mddev->pers) choice = mddev->raid_disks; |
| 1401 | while (find_rdev_nr(mddev, choice)) |
| 1402 | choice++; |
| 1403 | rdev->desc_nr = choice; |
| 1404 | } else { |
| 1405 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1406 | return -EBUSY; |
| 1407 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1408 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1409 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1410 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1413 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1414 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1415 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1416 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1417 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1418 | if (rdev->bdev->bd_part) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1419 | ko = &rdev->bdev->bd_part->dev.kobj; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1420 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1421 | ko = &rdev->bdev->bd_disk->dev.kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1422 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1423 | kobject_del(&rdev->kobj); |
| 1424 | goto fail; |
| 1425 | } |
| 1426 | list_add(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1427 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1429 | |
| 1430 | fail: |
| 1431 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1432 | b, mdname(mddev)); |
| 1433 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1436 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1437 | { |
| 1438 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1439 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1440 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1444 | { |
| 1445 | char b[BDEVNAME_SIZE]; |
| 1446 | if (!rdev->mddev) { |
| 1447 | MD_BUG(); |
| 1448 | return; |
| 1449 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1450 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | list_del_init(&rdev->same_set); |
| 1452 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1453 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1454 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1455 | |
| 1456 | /* We need to delay this, otherwise we can deadlock when |
| 1457 | * writing to 'remove' to "dev/state" |
| 1458 | */ |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1459 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1460 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1461 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * prevent the device from being mounted, repartitioned or |
| 1466 | * otherwise reused by a RAID array (or any other kernel |
| 1467 | * subsystem), by bd_claiming the device. |
| 1468 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1469 | 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] | 1470 | { |
| 1471 | int err = 0; |
| 1472 | struct block_device *bdev; |
| 1473 | char b[BDEVNAME_SIZE]; |
| 1474 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1475 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | if (IS_ERR(bdev)) { |
| 1477 | printk(KERN_ERR "md: could not open %s.\n", |
| 1478 | __bdevname(dev, b)); |
| 1479 | return PTR_ERR(bdev); |
| 1480 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1481 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | if (err) { |
| 1483 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1484 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1485 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return err; |
| 1487 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1488 | if (!shared) |
| 1489 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | rdev->bdev = bdev; |
| 1491 | return err; |
| 1492 | } |
| 1493 | |
| 1494 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1495 | { |
| 1496 | struct block_device *bdev = rdev->bdev; |
| 1497 | rdev->bdev = NULL; |
| 1498 | if (!bdev) |
| 1499 | MD_BUG(); |
| 1500 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1501 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | void md_autodetect_dev(dev_t dev); |
| 1505 | |
| 1506 | static void export_rdev(mdk_rdev_t * rdev) |
| 1507 | { |
| 1508 | char b[BDEVNAME_SIZE]; |
| 1509 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1510 | bdevname(rdev->bdev,b)); |
| 1511 | if (rdev->mddev) |
| 1512 | MD_BUG(); |
| 1513 | free_disk_sb(rdev); |
| 1514 | list_del_init(&rdev->same_set); |
| 1515 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1516 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1517 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | #endif |
| 1519 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1520 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1524 | { |
| 1525 | unbind_rdev_from_array(rdev); |
| 1526 | export_rdev(rdev); |
| 1527 | } |
| 1528 | |
| 1529 | static void export_array(mddev_t *mddev) |
| 1530 | { |
| 1531 | struct list_head *tmp; |
| 1532 | mdk_rdev_t *rdev; |
| 1533 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1534 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (!rdev->mddev) { |
| 1536 | MD_BUG(); |
| 1537 | continue; |
| 1538 | } |
| 1539 | kick_rdev_from_array(rdev); |
| 1540 | } |
| 1541 | if (!list_empty(&mddev->disks)) |
| 1542 | MD_BUG(); |
| 1543 | mddev->raid_disks = 0; |
| 1544 | mddev->major_version = 0; |
| 1545 | } |
| 1546 | |
| 1547 | static void print_desc(mdp_disk_t *desc) |
| 1548 | { |
| 1549 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1550 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1551 | } |
| 1552 | |
| 1553 | static void print_sb(mdp_super_t *sb) |
| 1554 | { |
| 1555 | int i; |
| 1556 | |
| 1557 | printk(KERN_INFO |
| 1558 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1559 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1560 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1561 | sb->ctime); |
| 1562 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1563 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1564 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1565 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1566 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1567 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1568 | sb->failed_disks, sb->spare_disks, |
| 1569 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1570 | |
| 1571 | printk(KERN_INFO); |
| 1572 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1573 | mdp_disk_t *desc; |
| 1574 | |
| 1575 | desc = sb->disks + i; |
| 1576 | if (desc->number || desc->major || desc->minor || |
| 1577 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1578 | printk(" D %2d: ", i); |
| 1579 | print_desc(desc); |
| 1580 | } |
| 1581 | } |
| 1582 | printk(KERN_INFO "md: THIS: "); |
| 1583 | print_desc(&sb->this_disk); |
| 1584 | |
| 1585 | } |
| 1586 | |
| 1587 | static void print_rdev(mdk_rdev_t *rdev) |
| 1588 | { |
| 1589 | char b[BDEVNAME_SIZE]; |
| 1590 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1591 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1592 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1593 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | if (rdev->sb_loaded) { |
| 1595 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1596 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1597 | } else |
| 1598 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1599 | } |
| 1600 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1601 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | { |
| 1603 | struct list_head *tmp, *tmp2; |
| 1604 | mdk_rdev_t *rdev; |
| 1605 | mddev_t *mddev; |
| 1606 | char b[BDEVNAME_SIZE]; |
| 1607 | |
| 1608 | printk("\n"); |
| 1609 | printk("md: **********************************\n"); |
| 1610 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1611 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1612 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1614 | if (mddev->bitmap) |
| 1615 | bitmap_print_sb(mddev->bitmap); |
| 1616 | else |
| 1617 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1618 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1620 | printk("\n"); |
| 1621 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1622 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | print_rdev(rdev); |
| 1624 | } |
| 1625 | printk("md: **********************************\n"); |
| 1626 | printk("\n"); |
| 1627 | } |
| 1628 | |
| 1629 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1630 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1632 | /* Update each superblock (in-memory image), but |
| 1633 | * if we are allowed to, skip spares which already |
| 1634 | * have the right event counter, or have one earlier |
| 1635 | * (which would mean they aren't being marked as dirty |
| 1636 | * with the rest of the array) |
| 1637 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | mdk_rdev_t *rdev; |
| 1639 | struct list_head *tmp; |
| 1640 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1641 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1642 | if (rdev->sb_events == mddev->events || |
| 1643 | (nospares && |
| 1644 | rdev->raid_disk < 0 && |
| 1645 | (rdev->sb_events&1)==0 && |
| 1646 | rdev->sb_events+1 == mddev->events)) { |
| 1647 | /* Don't update this superblock */ |
| 1648 | rdev->sb_loaded = 2; |
| 1649 | } else { |
| 1650 | super_types[mddev->major_version]. |
| 1651 | sync_super(mddev, rdev); |
| 1652 | rdev->sb_loaded = 1; |
| 1653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1657 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | struct list_head *tmp; |
| 1660 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1661 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1662 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1664 | if (mddev->external) |
| 1665 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1667 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1668 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1669 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1670 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1671 | force_change = 1; |
| 1672 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1673 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1674 | * though if events isn't the right even/odd, we will have to do |
| 1675 | * spares after all |
| 1676 | */ |
| 1677 | nospares = 1; |
| 1678 | if (force_change) |
| 1679 | nospares = 0; |
| 1680 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1681 | /* If the array is degraded, then skipping spares is both |
| 1682 | * dangerous and fairly pointless. |
| 1683 | * Dangerous because a device that was removed from the array |
| 1684 | * might have a event_count that still looks up-to-date, |
| 1685 | * so it can be re-added without a resync. |
| 1686 | * Pointless because if there are any spares to skip, |
| 1687 | * then a recovery will happen and soon that array won't |
| 1688 | * be degraded any more and the spare can go back to sleep then. |
| 1689 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1690 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1691 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1692 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1694 | |
| 1695 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1696 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1697 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1698 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1699 | && (mddev->events & 1) |
| 1700 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1701 | mddev->events--; |
| 1702 | else { |
| 1703 | /* otherwise we have to go forward and ... */ |
| 1704 | mddev->events ++; |
| 1705 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1706 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1707 | if ((mddev->events&1)==0) { |
| 1708 | mddev->events++; |
| 1709 | nospares = 0; |
| 1710 | } |
| 1711 | } else { |
| 1712 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1713 | if ((mddev->events&1)) { |
| 1714 | mddev->events++; |
| 1715 | nospares = 0; |
| 1716 | } |
| 1717 | } |
| 1718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
| 1720 | if (!mddev->events) { |
| 1721 | /* |
| 1722 | * oops, this 64-bit counter should never wrap. |
| 1723 | * Either we are in around ~1 trillion A.C., assuming |
| 1724 | * 1 reboot per second, or we have a bug: |
| 1725 | */ |
| 1726 | MD_BUG(); |
| 1727 | mddev->events --; |
| 1728 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
| 1730 | /* |
| 1731 | * do not write anything to disk if using |
| 1732 | * nonpersistent superblocks |
| 1733 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1734 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1735 | if (!mddev->external) |
| 1736 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1737 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1738 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1739 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1741 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1742 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1743 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
| 1745 | dprintk(KERN_INFO |
| 1746 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1747 | mdname(mddev),mddev->in_sync); |
| 1748 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1749 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1750 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | char b[BDEVNAME_SIZE]; |
| 1752 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1753 | if (rdev->sb_loaded != 1) |
| 1754 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1755 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | dprintk("(skipping faulty "); |
| 1757 | |
| 1758 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1759 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1760 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1761 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1762 | rdev->sb_page); |
| 1763 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1764 | bdevname(rdev->bdev,b), |
| 1765 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1766 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } else |
| 1769 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1770 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | /* only need to write one superblock... */ |
| 1772 | break; |
| 1773 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1774 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1775 | /* 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] | 1776 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1777 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1778 | if (mddev->in_sync != sync_req || |
| 1779 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1780 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1781 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1782 | goto repeat; |
| 1783 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1784 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1785 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1786 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1790 | /* words written to sysfs files may, or my not, be \n terminated. |
| 1791 | * We want to accept with case. For this we use cmd_match. |
| 1792 | */ |
| 1793 | static int cmd_match(const char *cmd, const char *str) |
| 1794 | { |
| 1795 | /* See if cmd, written into a sysfs file, matches |
| 1796 | * str. They must either be the same, or cmd can |
| 1797 | * have a trailing newline |
| 1798 | */ |
| 1799 | while (*cmd && *str && *cmd == *str) { |
| 1800 | cmd++; |
| 1801 | str++; |
| 1802 | } |
| 1803 | if (*cmd == '\n') |
| 1804 | cmd++; |
| 1805 | if (*str || *cmd) |
| 1806 | return 0; |
| 1807 | return 1; |
| 1808 | } |
| 1809 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1810 | struct rdev_sysfs_entry { |
| 1811 | struct attribute attr; |
| 1812 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1813 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1814 | }; |
| 1815 | |
| 1816 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1817 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1818 | { |
| 1819 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1820 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1821 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1822 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1823 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1824 | sep = ","; |
| 1825 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1826 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1827 | len += sprintf(page+len, "%sin_sync",sep); |
| 1828 | sep = ","; |
| 1829 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1830 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1831 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1832 | sep = ","; |
| 1833 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1834 | if (test_bit(Blocked, &rdev->flags)) { |
| 1835 | len += sprintf(page+len, "%sblocked", sep); |
| 1836 | sep = ","; |
| 1837 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1838 | if (!test_bit(Faulty, &rdev->flags) && |
| 1839 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1840 | len += sprintf(page+len, "%sspare", sep); |
| 1841 | sep = ","; |
| 1842 | } |
| 1843 | return len+sprintf(page+len, "\n"); |
| 1844 | } |
| 1845 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1846 | static ssize_t |
| 1847 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1848 | { |
| 1849 | /* can write |
| 1850 | * faulty - simulates and error |
| 1851 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1852 | * writemostly - sets write_mostly |
| 1853 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1854 | * blocked - sets the Blocked flag |
| 1855 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1856 | */ |
| 1857 | int err = -EINVAL; |
| 1858 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1859 | md_error(rdev->mddev, rdev); |
| 1860 | err = 0; |
| 1861 | } else if (cmd_match(buf, "remove")) { |
| 1862 | if (rdev->raid_disk >= 0) |
| 1863 | err = -EBUSY; |
| 1864 | else { |
| 1865 | mddev_t *mddev = rdev->mddev; |
| 1866 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1867 | if (mddev->pers) |
| 1868 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1869 | md_new_event(mddev); |
| 1870 | err = 0; |
| 1871 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1872 | } else if (cmd_match(buf, "writemostly")) { |
| 1873 | set_bit(WriteMostly, &rdev->flags); |
| 1874 | err = 0; |
| 1875 | } else if (cmd_match(buf, "-writemostly")) { |
| 1876 | clear_bit(WriteMostly, &rdev->flags); |
| 1877 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1878 | } else if (cmd_match(buf, "blocked")) { |
| 1879 | set_bit(Blocked, &rdev->flags); |
| 1880 | err = 0; |
| 1881 | } else if (cmd_match(buf, "-blocked")) { |
| 1882 | clear_bit(Blocked, &rdev->flags); |
| 1883 | wake_up(&rdev->blocked_wait); |
| 1884 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1885 | md_wakeup_thread(rdev->mddev->thread); |
| 1886 | |
| 1887 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1888 | } |
| 1889 | return err ? err : len; |
| 1890 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1891 | static struct rdev_sysfs_entry rdev_state = |
| 1892 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1893 | |
| 1894 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1895 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1896 | { |
| 1897 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1898 | } |
| 1899 | |
| 1900 | static ssize_t |
| 1901 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1902 | { |
| 1903 | char *e; |
| 1904 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1905 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1906 | atomic_set(&rdev->corrected_errors, n); |
| 1907 | return len; |
| 1908 | } |
| 1909 | return -EINVAL; |
| 1910 | } |
| 1911 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1912 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1913 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1914 | static ssize_t |
| 1915 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1916 | { |
| 1917 | if (rdev->raid_disk < 0) |
| 1918 | return sprintf(page, "none\n"); |
| 1919 | else |
| 1920 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1921 | } |
| 1922 | |
| 1923 | static ssize_t |
| 1924 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1925 | { |
| 1926 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1927 | int err; |
| 1928 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1929 | int slot = simple_strtoul(buf, &e, 10); |
| 1930 | if (strncmp(buf, "none", 4)==0) |
| 1931 | slot = -1; |
| 1932 | else if (e==buf || (*e && *e!= '\n')) |
| 1933 | return -EINVAL; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1934 | if (rdev->mddev->pers) { |
| 1935 | /* Setting 'slot' on an active array requires also |
| 1936 | * updating the 'rd%d' link, and communicating |
| 1937 | * with the personality with ->hot_*_disk. |
| 1938 | * For now we only support removing |
| 1939 | * failed/spare devices. This normally happens automatically, |
| 1940 | * but not when the metadata is externally managed. |
| 1941 | */ |
| 1942 | if (slot != -1) |
| 1943 | return -EBUSY; |
| 1944 | if (rdev->raid_disk == -1) |
| 1945 | return -EEXIST; |
| 1946 | /* personality does all needed checks */ |
| 1947 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1948 | return -EINVAL; |
| 1949 | err = rdev->mddev->pers-> |
| 1950 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 1951 | if (err) |
| 1952 | return err; |
| 1953 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1954 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 1955 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1956 | md_wakeup_thread(rdev->mddev->thread); |
| 1957 | } else { |
| 1958 | if (slot >= rdev->mddev->raid_disks) |
| 1959 | return -ENOSPC; |
| 1960 | rdev->raid_disk = slot; |
| 1961 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1962 | clear_bit(Faulty, &rdev->flags); |
| 1963 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1964 | set_bit(In_sync, &rdev->flags); |
| 1965 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1966 | return len; |
| 1967 | } |
| 1968 | |
| 1969 | |
| 1970 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1971 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1972 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1973 | static ssize_t |
| 1974 | offset_show(mdk_rdev_t *rdev, char *page) |
| 1975 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 1976 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | static ssize_t |
| 1980 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1981 | { |
| 1982 | char *e; |
| 1983 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 1984 | if (e==buf || (*e && *e != '\n')) |
| 1985 | return -EINVAL; |
| 1986 | if (rdev->mddev->pers) |
| 1987 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1988 | if (rdev->size && rdev->mddev->external) |
| 1989 | /* Must set offset before size, so overlap checks |
| 1990 | * can be sane */ |
| 1991 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1992 | rdev->data_offset = offset; |
| 1993 | return len; |
| 1994 | } |
| 1995 | |
| 1996 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1997 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1998 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1999 | static ssize_t |
| 2000 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2001 | { |
| 2002 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2003 | } |
| 2004 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2005 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2006 | { |
| 2007 | /* check if two start/length pairs overlap */ |
| 2008 | if (s1+l1 <= s2) |
| 2009 | return 0; |
| 2010 | if (s2+l2 <= s1) |
| 2011 | return 0; |
| 2012 | return 1; |
| 2013 | } |
| 2014 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2015 | static ssize_t |
| 2016 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2017 | { |
| 2018 | char *e; |
| 2019 | unsigned long long size = simple_strtoull(buf, &e, 10); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2020 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2021 | mddev_t *my_mddev = rdev->mddev; |
| 2022 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2023 | if (e==buf || (*e && *e != '\n')) |
| 2024 | return -EINVAL; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2025 | if (my_mddev->pers) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2026 | return -EBUSY; |
| 2027 | rdev->size = size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2028 | if (size > oldsize && rdev->mddev->external) { |
| 2029 | /* need to check that all other rdevs with the same ->bdev |
| 2030 | * do not overlap. We need to unlock the mddev to avoid |
| 2031 | * a deadlock. We have already changed rdev->size, and if |
| 2032 | * we have to change it back, we will have the lock again. |
| 2033 | */ |
| 2034 | mddev_t *mddev; |
| 2035 | int overlap = 0; |
| 2036 | struct list_head *tmp, *tmp2; |
| 2037 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2038 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2039 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2040 | mdk_rdev_t *rdev2; |
| 2041 | |
| 2042 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2043 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2044 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2045 | (rdev->bdev == rdev2->bdev && |
| 2046 | rdev != rdev2 && |
| 2047 | overlaps(rdev->data_offset, rdev->size, |
| 2048 | rdev2->data_offset, rdev2->size))) { |
| 2049 | overlap = 1; |
| 2050 | break; |
| 2051 | } |
| 2052 | mddev_unlock(mddev); |
| 2053 | if (overlap) { |
| 2054 | mddev_put(mddev); |
| 2055 | break; |
| 2056 | } |
| 2057 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2058 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2059 | if (overlap) { |
| 2060 | /* Someone else could have slipped in a size |
| 2061 | * change here, but doing so is just silly. |
| 2062 | * We put oldsize back because we *know* it is |
| 2063 | * safe, and trust userspace not to race with |
| 2064 | * itself |
| 2065 | */ |
| 2066 | rdev->size = oldsize; |
| 2067 | return -EBUSY; |
| 2068 | } |
| 2069 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2070 | if (size < my_mddev->size || my_mddev->size == 0) |
| 2071 | my_mddev->size = size; |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2072 | return len; |
| 2073 | } |
| 2074 | |
| 2075 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2076 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2077 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2078 | static struct attribute *rdev_default_attrs[] = { |
| 2079 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2080 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2081 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2082 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2083 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2084 | NULL, |
| 2085 | }; |
| 2086 | static ssize_t |
| 2087 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2088 | { |
| 2089 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2090 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2091 | mddev_t *mddev = rdev->mddev; |
| 2092 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2093 | |
| 2094 | if (!entry->show) |
| 2095 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2096 | |
| 2097 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2098 | if (!rv) { |
| 2099 | if (rdev->mddev == NULL) |
| 2100 | rv = -EBUSY; |
| 2101 | else |
| 2102 | rv = entry->show(rdev, page); |
| 2103 | mddev_unlock(mddev); |
| 2104 | } |
| 2105 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | static ssize_t |
| 2109 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2110 | const char *page, size_t length) |
| 2111 | { |
| 2112 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2113 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2114 | ssize_t rv; |
| 2115 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2116 | |
| 2117 | if (!entry->store) |
| 2118 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2119 | if (!capable(CAP_SYS_ADMIN)) |
| 2120 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2121 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2122 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2123 | if (rdev->mddev == NULL) |
| 2124 | rv = -EBUSY; |
| 2125 | else |
| 2126 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2127 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2128 | } |
| 2129 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | static void rdev_free(struct kobject *ko) |
| 2133 | { |
| 2134 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2135 | kfree(rdev); |
| 2136 | } |
| 2137 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2138 | .show = rdev_attr_show, |
| 2139 | .store = rdev_attr_store, |
| 2140 | }; |
| 2141 | static struct kobj_type rdev_ktype = { |
| 2142 | .release = rdev_free, |
| 2143 | .sysfs_ops = &rdev_sysfs_ops, |
| 2144 | .default_attrs = rdev_default_attrs, |
| 2145 | }; |
| 2146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | /* |
| 2148 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2149 | * |
| 2150 | * mark the device faulty if: |
| 2151 | * |
| 2152 | * - the device is nonexistent (zero size) |
| 2153 | * - the device has no valid superblock |
| 2154 | * |
| 2155 | * a faulty rdev _never_ has rdev->sb set. |
| 2156 | */ |
| 2157 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2158 | { |
| 2159 | char b[BDEVNAME_SIZE]; |
| 2160 | int err; |
| 2161 | mdk_rdev_t *rdev; |
| 2162 | sector_t size; |
| 2163 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2164 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | if (!rdev) { |
| 2166 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2167 | return ERR_PTR(-ENOMEM); |
| 2168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
| 2170 | if ((err = alloc_disk_sb(rdev))) |
| 2171 | goto abort_free; |
| 2172 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2173 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | if (err) |
| 2175 | goto abort_free; |
| 2176 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2177 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2180 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2181 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2182 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2184 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2186 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2187 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | |
| 2189 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2190 | if (!size) { |
| 2191 | printk(KERN_WARNING |
| 2192 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2193 | bdevname(rdev->bdev,b)); |
| 2194 | err = -EINVAL; |
| 2195 | goto abort_free; |
| 2196 | } |
| 2197 | |
| 2198 | if (super_format >= 0) { |
| 2199 | err = super_types[super_format]. |
| 2200 | load_super(rdev, NULL, super_minor); |
| 2201 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2202 | printk(KERN_WARNING |
| 2203 | "md: %s does not have a valid v%d.%d " |
| 2204 | "superblock, not importing!\n", |
| 2205 | bdevname(rdev->bdev,b), |
| 2206 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | goto abort_free; |
| 2208 | } |
| 2209 | if (err < 0) { |
| 2210 | printk(KERN_WARNING |
| 2211 | "md: could not read %s's sb, not importing!\n", |
| 2212 | bdevname(rdev->bdev,b)); |
| 2213 | goto abort_free; |
| 2214 | } |
| 2215 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2218 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
| 2220 | return rdev; |
| 2221 | |
| 2222 | abort_free: |
| 2223 | if (rdev->sb_page) { |
| 2224 | if (rdev->bdev) |
| 2225 | unlock_rdev(rdev); |
| 2226 | free_disk_sb(rdev); |
| 2227 | } |
| 2228 | kfree(rdev); |
| 2229 | return ERR_PTR(err); |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * Check a full RAID array for plausibility |
| 2234 | */ |
| 2235 | |
| 2236 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2237 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | { |
| 2239 | int i; |
| 2240 | struct list_head *tmp; |
| 2241 | mdk_rdev_t *rdev, *freshest; |
| 2242 | char b[BDEVNAME_SIZE]; |
| 2243 | |
| 2244 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2245 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | switch (super_types[mddev->major_version]. |
| 2247 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2248 | case 1: |
| 2249 | freshest = rdev; |
| 2250 | break; |
| 2251 | case 0: |
| 2252 | break; |
| 2253 | default: |
| 2254 | printk( KERN_ERR \ |
| 2255 | "md: fatal superblock inconsistency in %s" |
| 2256 | " -- removing from array\n", |
| 2257 | bdevname(rdev->bdev,b)); |
| 2258 | kick_rdev_from_array(rdev); |
| 2259 | } |
| 2260 | |
| 2261 | |
| 2262 | super_types[mddev->major_version]. |
| 2263 | validate_super(mddev, freshest); |
| 2264 | |
| 2265 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2266 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | if (rdev != freshest) |
| 2268 | if (super_types[mddev->major_version]. |
| 2269 | validate_super(mddev, rdev)) { |
| 2270 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2271 | " from array!\n", |
| 2272 | bdevname(rdev->bdev,b)); |
| 2273 | kick_rdev_from_array(rdev); |
| 2274 | continue; |
| 2275 | } |
| 2276 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2277 | rdev->desc_nr = i++; |
| 2278 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2279 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2280 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2281 | rdev->raid_disk = -1; |
| 2282 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | if (mddev->recovery_cp != MaxSector && |
| 2289 | mddev->level >= 1) |
| 2290 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2291 | " -- starting background reconstruction\n", |
| 2292 | mdname(mddev)); |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2296 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2297 | safe_delay_show(mddev_t *mddev, char *page) |
| 2298 | { |
| 2299 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2300 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2301 | } |
| 2302 | static ssize_t |
| 2303 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2304 | { |
| 2305 | int scale=1; |
| 2306 | int dot=0; |
| 2307 | int i; |
| 2308 | unsigned long msec; |
| 2309 | char buf[30]; |
| 2310 | char *e; |
| 2311 | /* remove a period, and count digits after it */ |
| 2312 | if (len >= sizeof(buf)) |
| 2313 | return -EINVAL; |
| 2314 | strlcpy(buf, cbuf, len); |
| 2315 | buf[len] = 0; |
| 2316 | for (i=0; i<len; i++) { |
| 2317 | if (dot) { |
| 2318 | if (isdigit(buf[i])) { |
| 2319 | buf[i-1] = buf[i]; |
| 2320 | scale *= 10; |
| 2321 | } |
| 2322 | buf[i] = 0; |
| 2323 | } else if (buf[i] == '.') { |
| 2324 | dot=1; |
| 2325 | buf[i] = 0; |
| 2326 | } |
| 2327 | } |
| 2328 | msec = simple_strtoul(buf, &e, 10); |
| 2329 | if (e == buf || (*e && *e != '\n')) |
| 2330 | return -EINVAL; |
| 2331 | msec = (msec * 1000) / scale; |
| 2332 | if (msec == 0) |
| 2333 | mddev->safemode_delay = 0; |
| 2334 | else { |
| 2335 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2336 | if (mddev->safemode_delay == 0) |
| 2337 | mddev->safemode_delay = 1; |
| 2338 | } |
| 2339 | return len; |
| 2340 | } |
| 2341 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2342 | __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] | 2343 | |
| 2344 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2345 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2346 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2347 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2348 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2349 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2350 | else if (mddev->clevel[0]) |
| 2351 | return sprintf(page, "%s\n", mddev->clevel); |
| 2352 | else if (mddev->level != LEVEL_NONE) |
| 2353 | return sprintf(page, "%d\n", mddev->level); |
| 2354 | else |
| 2355 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2358 | static ssize_t |
| 2359 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2360 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2361 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2362 | if (mddev->pers) |
| 2363 | return -EBUSY; |
| 2364 | if (len == 0) |
| 2365 | return 0; |
| 2366 | if (len >= sizeof(mddev->clevel)) |
| 2367 | return -ENOSPC; |
| 2368 | strncpy(mddev->clevel, buf, len); |
| 2369 | if (mddev->clevel[len-1] == '\n') |
| 2370 | len--; |
| 2371 | mddev->clevel[len] = 0; |
| 2372 | mddev->level = LEVEL_NONE; |
| 2373 | return rv; |
| 2374 | } |
| 2375 | |
| 2376 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2377 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2378 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2379 | |
| 2380 | static ssize_t |
| 2381 | layout_show(mddev_t *mddev, char *page) |
| 2382 | { |
| 2383 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2384 | if (mddev->reshape_position != MaxSector && |
| 2385 | mddev->layout != mddev->new_layout) |
| 2386 | return sprintf(page, "%d (%d)\n", |
| 2387 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2388 | return sprintf(page, "%d\n", mddev->layout); |
| 2389 | } |
| 2390 | |
| 2391 | static ssize_t |
| 2392 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2393 | { |
| 2394 | char *e; |
| 2395 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2396 | |
| 2397 | if (!*buf || (*e && *e != '\n')) |
| 2398 | return -EINVAL; |
| 2399 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2400 | if (mddev->pers) |
| 2401 | return -EBUSY; |
| 2402 | if (mddev->reshape_position != MaxSector) |
| 2403 | mddev->new_layout = n; |
| 2404 | else |
| 2405 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2406 | return len; |
| 2407 | } |
| 2408 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2409 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2410 | |
| 2411 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2412 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2413 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2414 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2415 | if (mddev->raid_disks == 0) |
| 2416 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2417 | if (mddev->reshape_position != MaxSector && |
| 2418 | mddev->delta_disks != 0) |
| 2419 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2420 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2421 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2422 | } |
| 2423 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2424 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2425 | |
| 2426 | static ssize_t |
| 2427 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2428 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2429 | char *e; |
| 2430 | int rv = 0; |
| 2431 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2432 | |
| 2433 | if (!*buf || (*e && *e != '\n')) |
| 2434 | return -EINVAL; |
| 2435 | |
| 2436 | if (mddev->pers) |
| 2437 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2438 | else if (mddev->reshape_position != MaxSector) { |
| 2439 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2440 | mddev->delta_disks = n - olddisks; |
| 2441 | mddev->raid_disks = n; |
| 2442 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2443 | mddev->raid_disks = n; |
| 2444 | return rv ? rv : len; |
| 2445 | } |
| 2446 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2447 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2448 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2449 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2450 | chunk_size_show(mddev_t *mddev, char *page) |
| 2451 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2452 | if (mddev->reshape_position != MaxSector && |
| 2453 | mddev->chunk_size != mddev->new_chunk) |
| 2454 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2455 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2456 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2457 | } |
| 2458 | |
| 2459 | static ssize_t |
| 2460 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2461 | { |
| 2462 | /* can only set chunk_size if array is not yet active */ |
| 2463 | char *e; |
| 2464 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2465 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2466 | if (!*buf || (*e && *e != '\n')) |
| 2467 | return -EINVAL; |
| 2468 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2469 | if (mddev->pers) |
| 2470 | return -EBUSY; |
| 2471 | else if (mddev->reshape_position != MaxSector) |
| 2472 | mddev->new_chunk = n; |
| 2473 | else |
| 2474 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2475 | return len; |
| 2476 | } |
| 2477 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2478 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2479 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2480 | static ssize_t |
| 2481 | resync_start_show(mddev_t *mddev, char *page) |
| 2482 | { |
| 2483 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2484 | } |
| 2485 | |
| 2486 | static ssize_t |
| 2487 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2488 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2489 | char *e; |
| 2490 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2491 | |
| 2492 | if (mddev->pers) |
| 2493 | return -EBUSY; |
| 2494 | if (!*buf || (*e && *e != '\n')) |
| 2495 | return -EINVAL; |
| 2496 | |
| 2497 | mddev->recovery_cp = n; |
| 2498 | return len; |
| 2499 | } |
| 2500 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2501 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2502 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2503 | /* |
| 2504 | * The array state can be: |
| 2505 | * |
| 2506 | * clear |
| 2507 | * No devices, no size, no level |
| 2508 | * Equivalent to STOP_ARRAY ioctl |
| 2509 | * inactive |
| 2510 | * May have some settings, but array is not active |
| 2511 | * all IO results in error |
| 2512 | * When written, doesn't tear down array, but just stops it |
| 2513 | * suspended (not supported yet) |
| 2514 | * All IO requests will block. The array can be reconfigured. |
| 2515 | * Writing this, if accepted, will block until array is quiessent |
| 2516 | * readonly |
| 2517 | * no resync can happen. no superblocks get written. |
| 2518 | * write requests fail |
| 2519 | * read-auto |
| 2520 | * like readonly, but behaves like 'clean' on a write request. |
| 2521 | * |
| 2522 | * clean - no pending writes, but otherwise active. |
| 2523 | * When written to inactive array, starts without resync |
| 2524 | * If a write request arrives then |
| 2525 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2526 | * if not known, block and switch to write-pending |
| 2527 | * If written to an active array that has pending writes, then fails. |
| 2528 | * active |
| 2529 | * fully active: IO and resync can be happening. |
| 2530 | * When written to inactive array, starts with resync |
| 2531 | * |
| 2532 | * write-pending |
| 2533 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2534 | * |
| 2535 | * active-idle |
| 2536 | * like active, but no writes have been seen for a while (100msec). |
| 2537 | * |
| 2538 | */ |
| 2539 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2540 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2541 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2542 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2543 | "write-pending", "active-idle", NULL }; |
| 2544 | |
| 2545 | static int match_word(const char *word, char **list) |
| 2546 | { |
| 2547 | int n; |
| 2548 | for (n=0; list[n]; n++) |
| 2549 | if (cmd_match(word, list[n])) |
| 2550 | break; |
| 2551 | return n; |
| 2552 | } |
| 2553 | |
| 2554 | static ssize_t |
| 2555 | array_state_show(mddev_t *mddev, char *page) |
| 2556 | { |
| 2557 | enum array_state st = inactive; |
| 2558 | |
| 2559 | if (mddev->pers) |
| 2560 | switch(mddev->ro) { |
| 2561 | case 1: |
| 2562 | st = readonly; |
| 2563 | break; |
| 2564 | case 2: |
| 2565 | st = read_auto; |
| 2566 | break; |
| 2567 | case 0: |
| 2568 | if (mddev->in_sync) |
| 2569 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2570 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2571 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2572 | else if (mddev->safemode) |
| 2573 | st = active_idle; |
| 2574 | else |
| 2575 | st = active; |
| 2576 | } |
| 2577 | else { |
| 2578 | if (list_empty(&mddev->disks) && |
| 2579 | mddev->raid_disks == 0 && |
| 2580 | mddev->size == 0) |
| 2581 | st = clear; |
| 2582 | else |
| 2583 | st = inactive; |
| 2584 | } |
| 2585 | return sprintf(page, "%s\n", array_states[st]); |
| 2586 | } |
| 2587 | |
| 2588 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2589 | static int do_md_run(mddev_t * mddev); |
| 2590 | static int restart_array(mddev_t *mddev); |
| 2591 | |
| 2592 | static ssize_t |
| 2593 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2594 | { |
| 2595 | int err = -EINVAL; |
| 2596 | enum array_state st = match_word(buf, array_states); |
| 2597 | switch(st) { |
| 2598 | case bad_word: |
| 2599 | break; |
| 2600 | case clear: |
| 2601 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2602 | if (atomic_read(&mddev->active) > 1) |
| 2603 | return -EBUSY; |
| 2604 | err = do_md_stop(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2605 | break; |
| 2606 | case inactive: |
| 2607 | /* stopping an active array */ |
| 2608 | if (mddev->pers) { |
| 2609 | if (atomic_read(&mddev->active) > 1) |
| 2610 | return -EBUSY; |
| 2611 | err = do_md_stop(mddev, 2); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2612 | } else |
| 2613 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2614 | break; |
| 2615 | case suspended: |
| 2616 | break; /* not supported yet */ |
| 2617 | case readonly: |
| 2618 | if (mddev->pers) |
| 2619 | err = do_md_stop(mddev, 1); |
| 2620 | else { |
| 2621 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2622 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2623 | err = do_md_run(mddev); |
| 2624 | } |
| 2625 | break; |
| 2626 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2627 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2628 | if (mddev->ro != 1) |
| 2629 | err = do_md_stop(mddev, 1); |
| 2630 | else |
| 2631 | err = restart_array(mddev); |
| 2632 | if (err == 0) { |
| 2633 | mddev->ro = 2; |
| 2634 | set_disk_ro(mddev->gendisk, 0); |
| 2635 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2636 | } else { |
| 2637 | mddev->ro = 2; |
| 2638 | err = do_md_run(mddev); |
| 2639 | } |
| 2640 | break; |
| 2641 | case clean: |
| 2642 | if (mddev->pers) { |
| 2643 | restart_array(mddev); |
| 2644 | spin_lock_irq(&mddev->write_lock); |
| 2645 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2646 | if (mddev->in_sync == 0) { |
| 2647 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2648 | if (mddev->safemode == 1) |
| 2649 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2650 | if (mddev->persistent) |
| 2651 | set_bit(MD_CHANGE_CLEAN, |
| 2652 | &mddev->flags); |
| 2653 | } |
| 2654 | err = 0; |
| 2655 | } else |
| 2656 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2657 | spin_unlock_irq(&mddev->write_lock); |
| 2658 | } else { |
| 2659 | mddev->ro = 0; |
| 2660 | mddev->recovery_cp = MaxSector; |
| 2661 | err = do_md_run(mddev); |
| 2662 | } |
| 2663 | break; |
| 2664 | case active: |
| 2665 | if (mddev->pers) { |
| 2666 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2667 | if (mddev->external) |
| 2668 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2669 | wake_up(&mddev->sb_wait); |
| 2670 | err = 0; |
| 2671 | } else { |
| 2672 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2673 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2674 | err = do_md_run(mddev); |
| 2675 | } |
| 2676 | break; |
| 2677 | case write_pending: |
| 2678 | case active_idle: |
| 2679 | /* these cannot be set */ |
| 2680 | break; |
| 2681 | } |
| 2682 | if (err) |
| 2683 | return err; |
| 2684 | else |
| 2685 | return len; |
| 2686 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2687 | static struct md_sysfs_entry md_array_state = |
| 2688 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2689 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2690 | static ssize_t |
| 2691 | null_show(mddev_t *mddev, char *page) |
| 2692 | { |
| 2693 | return -EINVAL; |
| 2694 | } |
| 2695 | |
| 2696 | static ssize_t |
| 2697 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2698 | { |
| 2699 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2700 | /* The new device is added to the array. |
| 2701 | * If the array has a persistent superblock, we read the |
| 2702 | * superblock to initialise info and check validity. |
| 2703 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2704 | * which mainly checks size. |
| 2705 | */ |
| 2706 | char *e; |
| 2707 | int major = simple_strtoul(buf, &e, 10); |
| 2708 | int minor; |
| 2709 | dev_t dev; |
| 2710 | mdk_rdev_t *rdev; |
| 2711 | int err; |
| 2712 | |
| 2713 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2714 | return -EINVAL; |
| 2715 | minor = simple_strtoul(e+1, &e, 10); |
| 2716 | if (*e && *e != '\n') |
| 2717 | return -EINVAL; |
| 2718 | dev = MKDEV(major, minor); |
| 2719 | if (major != MAJOR(dev) || |
| 2720 | minor != MINOR(dev)) |
| 2721 | return -EOVERFLOW; |
| 2722 | |
| 2723 | |
| 2724 | if (mddev->persistent) { |
| 2725 | rdev = md_import_device(dev, mddev->major_version, |
| 2726 | mddev->minor_version); |
| 2727 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2728 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2729 | mdk_rdev_t, same_set); |
| 2730 | err = super_types[mddev->major_version] |
| 2731 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2732 | if (err < 0) |
| 2733 | goto out; |
| 2734 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2735 | } else if (mddev->external) |
| 2736 | rdev = md_import_device(dev, -2, -1); |
| 2737 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2738 | rdev = md_import_device(dev, -1, -1); |
| 2739 | |
| 2740 | if (IS_ERR(rdev)) |
| 2741 | return PTR_ERR(rdev); |
| 2742 | err = bind_rdev_to_array(rdev, mddev); |
| 2743 | out: |
| 2744 | if (err) |
| 2745 | export_rdev(rdev); |
| 2746 | return err ? err : len; |
| 2747 | } |
| 2748 | |
| 2749 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2750 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2751 | |
| 2752 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2753 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2754 | { |
| 2755 | char *end; |
| 2756 | unsigned long chunk, end_chunk; |
| 2757 | |
| 2758 | if (!mddev->bitmap) |
| 2759 | goto out; |
| 2760 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2761 | while (*buf) { |
| 2762 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2763 | if (buf == end) break; |
| 2764 | if (*end == '-') { /* range */ |
| 2765 | buf = end + 1; |
| 2766 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2767 | if (buf == end) break; |
| 2768 | } |
| 2769 | if (*end && !isspace(*end)) break; |
| 2770 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2771 | buf = end; |
| 2772 | while (isspace(*buf)) buf++; |
| 2773 | } |
| 2774 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2775 | out: |
| 2776 | return len; |
| 2777 | } |
| 2778 | |
| 2779 | static struct md_sysfs_entry md_bitmap = |
| 2780 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2781 | |
| 2782 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2783 | size_show(mddev_t *mddev, char *page) |
| 2784 | { |
| 2785 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2786 | } |
| 2787 | |
| 2788 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2789 | |
| 2790 | static ssize_t |
| 2791 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2792 | { |
| 2793 | /* If array is inactive, we can reduce the component size, but |
| 2794 | * not increase it (except from 0). |
| 2795 | * If array is active, we can try an on-line resize |
| 2796 | */ |
| 2797 | char *e; |
| 2798 | int err = 0; |
| 2799 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2800 | if (!*buf || *buf == '\n' || |
| 2801 | (*e && *e != '\n')) |
| 2802 | return -EINVAL; |
| 2803 | |
| 2804 | if (mddev->pers) { |
| 2805 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2806 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2807 | } else { |
| 2808 | if (mddev->size == 0 || |
| 2809 | mddev->size > size) |
| 2810 | mddev->size = size; |
| 2811 | else |
| 2812 | err = -ENOSPC; |
| 2813 | } |
| 2814 | return err ? err : len; |
| 2815 | } |
| 2816 | |
| 2817 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2818 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2819 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2820 | |
| 2821 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2822 | * This is one of |
| 2823 | * 'none' for arrays with no metadata (good luck...) |
| 2824 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2825 | * or N.M for internally known formats |
| 2826 | */ |
| 2827 | static ssize_t |
| 2828 | metadata_show(mddev_t *mddev, char *page) |
| 2829 | { |
| 2830 | if (mddev->persistent) |
| 2831 | return sprintf(page, "%d.%d\n", |
| 2832 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2833 | else if (mddev->external) |
| 2834 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2835 | else |
| 2836 | return sprintf(page, "none\n"); |
| 2837 | } |
| 2838 | |
| 2839 | static ssize_t |
| 2840 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2841 | { |
| 2842 | int major, minor; |
| 2843 | char *e; |
| 2844 | if (!list_empty(&mddev->disks)) |
| 2845 | return -EBUSY; |
| 2846 | |
| 2847 | if (cmd_match(buf, "none")) { |
| 2848 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2849 | mddev->external = 0; |
| 2850 | mddev->major_version = 0; |
| 2851 | mddev->minor_version = 90; |
| 2852 | return len; |
| 2853 | } |
| 2854 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2855 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2856 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2857 | namelen = sizeof(mddev->metadata_type)-1; |
| 2858 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2859 | mddev->metadata_type[namelen] = 0; |
| 2860 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2861 | mddev->metadata_type[--namelen] = 0; |
| 2862 | mddev->persistent = 0; |
| 2863 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2864 | mddev->major_version = 0; |
| 2865 | mddev->minor_version = 90; |
| 2866 | return len; |
| 2867 | } |
| 2868 | major = simple_strtoul(buf, &e, 10); |
| 2869 | if (e==buf || *e != '.') |
| 2870 | return -EINVAL; |
| 2871 | buf = e+1; |
| 2872 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2873 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2874 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2875 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2876 | return -ENOENT; |
| 2877 | mddev->major_version = major; |
| 2878 | mddev->minor_version = minor; |
| 2879 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2880 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2881 | return len; |
| 2882 | } |
| 2883 | |
| 2884 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2885 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2886 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2887 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2888 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2889 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2890 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2891 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2892 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2893 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2894 | type = "reshape"; |
| 2895 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2896 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2897 | type = "resync"; |
| 2898 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2899 | type = "check"; |
| 2900 | else |
| 2901 | type = "repair"; |
| 2902 | } else |
| 2903 | type = "recover"; |
| 2904 | } |
| 2905 | return sprintf(page, "%s\n", type); |
| 2906 | } |
| 2907 | |
| 2908 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2909 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2910 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2911 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2912 | return -EINVAL; |
| 2913 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2914 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2915 | if (mddev->sync_thread) { |
| 2916 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2917 | md_unregister_thread(mddev->sync_thread); |
| 2918 | mddev->sync_thread = NULL; |
| 2919 | mddev->recovery = 0; |
| 2920 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2921 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2922 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2923 | return -EBUSY; |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2924 | else if (cmd_match(page, "resync") || cmd_match(page, "recover")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2925 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2926 | else if (cmd_match(page, "reshape")) { |
| 2927 | int err; |
| 2928 | if (mddev->pers->start_reshape == NULL) |
| 2929 | return -EINVAL; |
| 2930 | err = mddev->pers->start_reshape(mddev); |
| 2931 | if (err) |
| 2932 | return err; |
| 2933 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2934 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2935 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2936 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2937 | return -EINVAL; |
| 2938 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2939 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2940 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2941 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2942 | md_wakeup_thread(mddev->thread); |
| 2943 | return len; |
| 2944 | } |
| 2945 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2946 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2947 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2948 | { |
| 2949 | return sprintf(page, "%llu\n", |
| 2950 | (unsigned long long) mddev->resync_mismatches); |
| 2951 | } |
| 2952 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2953 | static struct md_sysfs_entry md_scan_mode = |
| 2954 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2955 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2956 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2957 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2958 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2959 | static ssize_t |
| 2960 | sync_min_show(mddev_t *mddev, char *page) |
| 2961 | { |
| 2962 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 2963 | mddev->sync_speed_min ? "local": "system"); |
| 2964 | } |
| 2965 | |
| 2966 | static ssize_t |
| 2967 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 2968 | { |
| 2969 | int min; |
| 2970 | char *e; |
| 2971 | if (strncmp(buf, "system", 6)==0) { |
| 2972 | mddev->sync_speed_min = 0; |
| 2973 | return len; |
| 2974 | } |
| 2975 | min = simple_strtoul(buf, &e, 10); |
| 2976 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 2977 | return -EINVAL; |
| 2978 | mddev->sync_speed_min = min; |
| 2979 | return len; |
| 2980 | } |
| 2981 | |
| 2982 | static struct md_sysfs_entry md_sync_min = |
| 2983 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 2984 | |
| 2985 | static ssize_t |
| 2986 | sync_max_show(mddev_t *mddev, char *page) |
| 2987 | { |
| 2988 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 2989 | mddev->sync_speed_max ? "local": "system"); |
| 2990 | } |
| 2991 | |
| 2992 | static ssize_t |
| 2993 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 2994 | { |
| 2995 | int max; |
| 2996 | char *e; |
| 2997 | if (strncmp(buf, "system", 6)==0) { |
| 2998 | mddev->sync_speed_max = 0; |
| 2999 | return len; |
| 3000 | } |
| 3001 | max = simple_strtoul(buf, &e, 10); |
| 3002 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3003 | return -EINVAL; |
| 3004 | mddev->sync_speed_max = max; |
| 3005 | return len; |
| 3006 | } |
| 3007 | |
| 3008 | static struct md_sysfs_entry md_sync_max = |
| 3009 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3010 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3011 | static ssize_t |
| 3012 | degraded_show(mddev_t *mddev, char *page) |
| 3013 | { |
| 3014 | return sprintf(page, "%d\n", mddev->degraded); |
| 3015 | } |
| 3016 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3017 | |
| 3018 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3019 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3020 | { |
| 3021 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3022 | } |
| 3023 | |
| 3024 | static ssize_t |
| 3025 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3026 | { |
| 3027 | long n; |
| 3028 | |
| 3029 | if (strict_strtol(buf, 10, &n)) |
| 3030 | return -EINVAL; |
| 3031 | |
| 3032 | if (n != 0 && n != 1) |
| 3033 | return -EINVAL; |
| 3034 | |
| 3035 | mddev->parallel_resync = n; |
| 3036 | |
| 3037 | if (mddev->sync_thread) |
| 3038 | wake_up(&resync_wait); |
| 3039 | |
| 3040 | return len; |
| 3041 | } |
| 3042 | |
| 3043 | /* force parallel resync, even with shared block devices */ |
| 3044 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3045 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3046 | sync_force_parallel_show, sync_force_parallel_store); |
| 3047 | |
| 3048 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3049 | sync_speed_show(mddev_t *mddev, char *page) |
| 3050 | { |
| 3051 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 3052 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3053 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 3054 | if (!dt) dt++; |
| 3055 | db = resync - (mddev->resync_mark_cnt); |
| 3056 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 3057 | } |
| 3058 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3059 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3060 | |
| 3061 | static ssize_t |
| 3062 | sync_completed_show(mddev_t *mddev, char *page) |
| 3063 | { |
| 3064 | unsigned long max_blocks, resync; |
| 3065 | |
| 3066 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3067 | max_blocks = mddev->resync_max_sectors; |
| 3068 | else |
| 3069 | max_blocks = mddev->size << 1; |
| 3070 | |
| 3071 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3072 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3073 | } |
| 3074 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3075 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3076 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3077 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3078 | max_sync_show(mddev_t *mddev, char *page) |
| 3079 | { |
| 3080 | if (mddev->resync_max == MaxSector) |
| 3081 | return sprintf(page, "max\n"); |
| 3082 | else |
| 3083 | return sprintf(page, "%llu\n", |
| 3084 | (unsigned long long)mddev->resync_max); |
| 3085 | } |
| 3086 | static ssize_t |
| 3087 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3088 | { |
| 3089 | if (strncmp(buf, "max", 3) == 0) |
| 3090 | mddev->resync_max = MaxSector; |
| 3091 | else { |
| 3092 | char *ep; |
| 3093 | unsigned long long max = simple_strtoull(buf, &ep, 10); |
| 3094 | if (ep == buf || (*ep != 0 && *ep != '\n')) |
| 3095 | return -EINVAL; |
| 3096 | if (max < mddev->resync_max && |
| 3097 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3098 | return -EBUSY; |
| 3099 | |
| 3100 | /* Must be a multiple of chunk_size */ |
| 3101 | if (mddev->chunk_size) { |
| 3102 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3103 | return -EINVAL; |
| 3104 | } |
| 3105 | mddev->resync_max = max; |
| 3106 | } |
| 3107 | wake_up(&mddev->recovery_wait); |
| 3108 | return len; |
| 3109 | } |
| 3110 | |
| 3111 | static struct md_sysfs_entry md_max_sync = |
| 3112 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3113 | |
| 3114 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3115 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3116 | { |
| 3117 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3118 | } |
| 3119 | |
| 3120 | static ssize_t |
| 3121 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3122 | { |
| 3123 | char *e; |
| 3124 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3125 | |
| 3126 | if (mddev->pers->quiesce == NULL) |
| 3127 | return -EINVAL; |
| 3128 | if (buf == e || (*e && *e != '\n')) |
| 3129 | return -EINVAL; |
| 3130 | if (new >= mddev->suspend_hi || |
| 3131 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3132 | mddev->suspend_lo = new; |
| 3133 | mddev->pers->quiesce(mddev, 2); |
| 3134 | return len; |
| 3135 | } else |
| 3136 | return -EINVAL; |
| 3137 | } |
| 3138 | static struct md_sysfs_entry md_suspend_lo = |
| 3139 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3140 | |
| 3141 | |
| 3142 | static ssize_t |
| 3143 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3144 | { |
| 3145 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3146 | } |
| 3147 | |
| 3148 | static ssize_t |
| 3149 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3150 | { |
| 3151 | char *e; |
| 3152 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3153 | |
| 3154 | if (mddev->pers->quiesce == NULL) |
| 3155 | return -EINVAL; |
| 3156 | if (buf == e || (*e && *e != '\n')) |
| 3157 | return -EINVAL; |
| 3158 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3159 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3160 | mddev->suspend_hi = new; |
| 3161 | mddev->pers->quiesce(mddev, 1); |
| 3162 | mddev->pers->quiesce(mddev, 0); |
| 3163 | return len; |
| 3164 | } else |
| 3165 | return -EINVAL; |
| 3166 | } |
| 3167 | static struct md_sysfs_entry md_suspend_hi = |
| 3168 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3169 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3170 | static ssize_t |
| 3171 | reshape_position_show(mddev_t *mddev, char *page) |
| 3172 | { |
| 3173 | if (mddev->reshape_position != MaxSector) |
| 3174 | return sprintf(page, "%llu\n", |
| 3175 | (unsigned long long)mddev->reshape_position); |
| 3176 | strcpy(page, "none\n"); |
| 3177 | return 5; |
| 3178 | } |
| 3179 | |
| 3180 | static ssize_t |
| 3181 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3182 | { |
| 3183 | char *e; |
| 3184 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3185 | if (mddev->pers) |
| 3186 | return -EBUSY; |
| 3187 | if (buf == e || (*e && *e != '\n')) |
| 3188 | return -EINVAL; |
| 3189 | mddev->reshape_position = new; |
| 3190 | mddev->delta_disks = 0; |
| 3191 | mddev->new_level = mddev->level; |
| 3192 | mddev->new_layout = mddev->layout; |
| 3193 | mddev->new_chunk = mddev->chunk_size; |
| 3194 | return len; |
| 3195 | } |
| 3196 | |
| 3197 | static struct md_sysfs_entry md_reshape_position = |
| 3198 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3199 | reshape_position_store); |
| 3200 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3201 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3202 | static struct attribute *md_default_attrs[] = { |
| 3203 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3204 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3205 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3206 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3207 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3208 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3209 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3210 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3211 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3212 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3213 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3214 | NULL, |
| 3215 | }; |
| 3216 | |
| 3217 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3218 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3219 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3220 | &md_sync_min.attr, |
| 3221 | &md_sync_max.attr, |
| 3222 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3223 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3224 | &md_sync_completed.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3225 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3226 | &md_suspend_lo.attr, |
| 3227 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3228 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3229 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3230 | NULL, |
| 3231 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3232 | static struct attribute_group md_redundancy_group = { |
| 3233 | .name = NULL, |
| 3234 | .attrs = md_redundancy_attrs, |
| 3235 | }; |
| 3236 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3237 | |
| 3238 | static ssize_t |
| 3239 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3240 | { |
| 3241 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3242 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3243 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3244 | |
| 3245 | if (!entry->show) |
| 3246 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3247 | rv = mddev_lock(mddev); |
| 3248 | if (!rv) { |
| 3249 | rv = entry->show(mddev, page); |
| 3250 | mddev_unlock(mddev); |
| 3251 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3252 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | static ssize_t |
| 3256 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3257 | const char *page, size_t length) |
| 3258 | { |
| 3259 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3260 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3261 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3262 | |
| 3263 | if (!entry->store) |
| 3264 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3265 | if (!capable(CAP_SYS_ADMIN)) |
| 3266 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3267 | rv = mddev_lock(mddev); |
| 3268 | if (!rv) { |
| 3269 | rv = entry->store(mddev, page, length); |
| 3270 | mddev_unlock(mddev); |
| 3271 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3272 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | static void md_free(struct kobject *ko) |
| 3276 | { |
| 3277 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3278 | kfree(mddev); |
| 3279 | } |
| 3280 | |
| 3281 | static struct sysfs_ops md_sysfs_ops = { |
| 3282 | .show = md_attr_show, |
| 3283 | .store = md_attr_store, |
| 3284 | }; |
| 3285 | static struct kobj_type md_ktype = { |
| 3286 | .release = md_free, |
| 3287 | .sysfs_ops = &md_sysfs_ops, |
| 3288 | .default_attrs = md_default_attrs, |
| 3289 | }; |
| 3290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | int mdp_major = 0; |
| 3292 | |
| 3293 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3294 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3295 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | mddev_t *mddev = mddev_find(dev); |
| 3297 | struct gendisk *disk; |
| 3298 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3299 | int shift = partitioned ? MdpMinorShift : 0; |
| 3300 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3301 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | |
| 3303 | if (!mddev) |
| 3304 | return NULL; |
| 3305 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3306 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3308 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | mddev_put(mddev); |
| 3310 | return NULL; |
| 3311 | } |
| 3312 | disk = alloc_disk(1 << shift); |
| 3313 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3314 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | mddev_put(mddev); |
| 3316 | return NULL; |
| 3317 | } |
| 3318 | disk->major = MAJOR(dev); |
| 3319 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3320 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3322 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | disk->fops = &md_fops; |
| 3325 | disk->private_data = mddev; |
| 3326 | disk->queue = mddev->queue; |
| 3327 | add_disk(disk); |
| 3328 | mddev->gendisk = disk; |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3329 | mutex_unlock(&disks_mutex); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3330 | 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] | 3331 | "%s", "md"); |
| 3332 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3333 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3334 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3335 | else |
| 3336 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | return NULL; |
| 3338 | } |
| 3339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | static void md_safemode_timeout(unsigned long data) |
| 3341 | { |
| 3342 | mddev_t *mddev = (mddev_t *) data; |
| 3343 | |
| 3344 | mddev->safemode = 1; |
| 3345 | md_wakeup_thread(mddev->thread); |
| 3346 | } |
| 3347 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3348 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | |
| 3350 | static int do_md_run(mddev_t * mddev) |
| 3351 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3352 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | int chunk_size; |
| 3354 | struct list_head *tmp; |
| 3355 | mdk_rdev_t *rdev; |
| 3356 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3357 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | char b[BDEVNAME_SIZE]; |
| 3359 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3360 | if (list_empty(&mddev->disks)) |
| 3361 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | |
| 3364 | if (mddev->pers) |
| 3365 | return -EBUSY; |
| 3366 | |
| 3367 | /* |
| 3368 | * Analyze all RAID superblock(s) |
| 3369 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3370 | if (!mddev->raid_disks) { |
| 3371 | if (!mddev->persistent) |
| 3372 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3373 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | |
| 3376 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3378 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3380 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3381 | chunk_size, MAX_CHUNK_SIZE); |
| 3382 | return -EINVAL; |
| 3383 | } |
| 3384 | /* |
| 3385 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3386 | */ |
| 3387 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3388 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | return -EINVAL; |
| 3390 | } |
| 3391 | if (chunk_size < PAGE_SIZE) { |
| 3392 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3393 | chunk_size, PAGE_SIZE); |
| 3394 | return -EINVAL; |
| 3395 | } |
| 3396 | |
| 3397 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3398 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3399 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | continue; |
| 3401 | if (rdev->size < chunk_size / 1024) { |
| 3402 | printk(KERN_WARNING |
| 3403 | "md: Dev %s smaller than chunk_size:" |
| 3404 | " %lluk < %dk\n", |
| 3405 | bdevname(rdev->bdev,b), |
| 3406 | (unsigned long long)rdev->size, |
| 3407 | chunk_size / 1024); |
| 3408 | return -EINVAL; |
| 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3414 | if (mddev->level != LEVEL_NONE) |
| 3415 | request_module("md-level-%d", mddev->level); |
| 3416 | else if (mddev->clevel[0]) |
| 3417 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | #endif |
| 3419 | |
| 3420 | /* |
| 3421 | * Drop all container device buffers, from now on |
| 3422 | * the only valid external interface is through the md |
| 3423 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3425 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3426 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | continue; |
| 3428 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3429 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3430 | |
| 3431 | /* perform some consistency tests on the device. |
| 3432 | * We don't want the data to overlap the metadata, |
| 3433 | * Internal Bitmap issues has handled elsewhere. |
| 3434 | */ |
| 3435 | if (rdev->data_offset < rdev->sb_offset) { |
| 3436 | if (mddev->size && |
| 3437 | rdev->data_offset + mddev->size*2 |
| 3438 | > rdev->sb_offset*2) { |
| 3439 | printk("md: %s: data overlaps metadata\n", |
| 3440 | mdname(mddev)); |
| 3441 | return -EINVAL; |
| 3442 | } |
| 3443 | } else { |
| 3444 | if (rdev->sb_offset*2 + rdev->sb_size/512 |
| 3445 | > rdev->data_offset) { |
| 3446 | printk("md: %s: metadata overlaps data\n", |
| 3447 | mdname(mddev)); |
| 3448 | return -EINVAL; |
| 3449 | } |
| 3450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | md_probe(mddev->unit, NULL, NULL); |
| 3454 | disk = mddev->gendisk; |
| 3455 | if (!disk) |
| 3456 | return -ENOMEM; |
| 3457 | |
| 3458 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3459 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3460 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3462 | if (mddev->level != LEVEL_NONE) |
| 3463 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3464 | mddev->level); |
| 3465 | else |
| 3466 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3467 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | return -EINVAL; |
| 3469 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3470 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3472 | mddev->level = pers->level; |
| 3473 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3475 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3476 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3477 | /* This personality cannot handle reshaping... */ |
| 3478 | mddev->pers = NULL; |
| 3479 | module_put(pers->owner); |
| 3480 | return -EINVAL; |
| 3481 | } |
| 3482 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3483 | if (pers->sync_request) { |
| 3484 | /* Warn if this is a potentially silly |
| 3485 | * configuration. |
| 3486 | */ |
| 3487 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3488 | mdk_rdev_t *rdev2; |
| 3489 | struct list_head *tmp2; |
| 3490 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3491 | rdev_for_each(rdev, tmp, mddev) { |
| 3492 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3493 | if (rdev < rdev2 && |
| 3494 | rdev->bdev->bd_contains == |
| 3495 | rdev2->bdev->bd_contains) { |
| 3496 | printk(KERN_WARNING |
| 3497 | "%s: WARNING: %s appears to be" |
| 3498 | " on the same physical disk as" |
| 3499 | " %s.\n", |
| 3500 | mdname(mddev), |
| 3501 | bdevname(rdev->bdev,b), |
| 3502 | bdevname(rdev2->bdev,b2)); |
| 3503 | warned = 1; |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | if (warned) |
| 3508 | printk(KERN_WARNING |
| 3509 | "True protection against single-disk" |
| 3510 | " failure might be compromised.\n"); |
| 3511 | } |
| 3512 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3513 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3515 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3516 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3518 | if (start_readonly) |
| 3519 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3520 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3521 | err = mddev->pers->run(mddev); |
| 3522 | if (!err && mddev->pers->sync_request) { |
| 3523 | err = bitmap_create(mddev); |
| 3524 | if (err) { |
| 3525 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3526 | mdname(mddev), err); |
| 3527 | mddev->pers->stop(mddev); |
| 3528 | } |
| 3529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | if (err) { |
| 3531 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3532 | module_put(mddev->pers->owner); |
| 3533 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3534 | bitmap_destroy(mddev); |
| 3535 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3537 | if (mddev->pers->sync_request) { |
| 3538 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3539 | printk(KERN_WARNING |
| 3540 | "md: cannot register extra attributes for %s\n", |
| 3541 | mdname(mddev)); |
| 3542 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3543 | mddev->ro = 0; |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | atomic_set(&mddev->writes_pending,0); |
| 3546 | mddev->safemode = 0; |
| 3547 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3548 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3549 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3551 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3552 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3553 | if (rdev->raid_disk >= 0) { |
| 3554 | char nm[20]; |
| 3555 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3556 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3557 | printk("md: cannot register %s for %s\n", |
| 3558 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | |
| 3561 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3562 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3563 | if (mddev->flags) |
| 3564 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | |
| 3566 | set_capacity(disk, mddev->array_size<<1); |
| 3567 | |
| 3568 | /* If we call blk_queue_make_request here, it will |
| 3569 | * re-initialise max_sectors etc which may have been |
| 3570 | * refined inside -> run. So just set the bits we need to set. |
| 3571 | * Most initialisation happended when we called |
| 3572 | * blk_queue_make_request(..., md_fail_request) |
| 3573 | * earlier. |
| 3574 | */ |
| 3575 | mddev->queue->queuedata = mddev; |
| 3576 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3577 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3578 | /* If there is a partially-recovered drive we need to |
| 3579 | * start recovery here. If we leave it to md_check_recovery, |
| 3580 | * it will remove the drives and not do the right thing |
| 3581 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3582 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3583 | struct list_head *rtmp; |
| 3584 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3585 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3586 | if (rdev->raid_disk >= 0 && |
| 3587 | !test_bit(In_sync, &rdev->flags) && |
| 3588 | !test_bit(Faulty, &rdev->flags)) |
| 3589 | /* complete an interrupted recovery */ |
| 3590 | spares++; |
| 3591 | if (spares && mddev->pers->sync_request) { |
| 3592 | mddev->recovery = 0; |
| 3593 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3594 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3595 | mddev, |
| 3596 | "%s_resync"); |
| 3597 | if (!mddev->sync_thread) { |
| 3598 | printk(KERN_ERR "%s: could not start resync" |
| 3599 | " thread...\n", |
| 3600 | mdname(mddev)); |
| 3601 | /* leave the spares where they are, it shouldn't hurt */ |
| 3602 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3603 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3604 | } |
| 3605 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3606 | md_wakeup_thread(mddev->thread); |
| 3607 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3608 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3609 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3610 | md_new_event(mddev); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3611 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | return 0; |
| 3613 | } |
| 3614 | |
| 3615 | static int restart_array(mddev_t *mddev) |
| 3616 | { |
| 3617 | struct gendisk *disk = mddev->gendisk; |
| 3618 | int err; |
| 3619 | |
| 3620 | /* |
| 3621 | * Complain if it has no devices |
| 3622 | */ |
| 3623 | err = -ENXIO; |
| 3624 | if (list_empty(&mddev->disks)) |
| 3625 | goto out; |
| 3626 | |
| 3627 | if (mddev->pers) { |
| 3628 | err = -EBUSY; |
| 3629 | if (!mddev->ro) |
| 3630 | goto out; |
| 3631 | |
| 3632 | mddev->safemode = 0; |
| 3633 | mddev->ro = 0; |
| 3634 | set_disk_ro(disk, 0); |
| 3635 | |
| 3636 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3637 | mdname(mddev)); |
| 3638 | /* |
| 3639 | * Kick recovery or resync if necessary |
| 3640 | */ |
| 3641 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3642 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3643 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3645 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | |
| 3648 | out: |
| 3649 | return err; |
| 3650 | } |
| 3651 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3652 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3653 | * to the file ourselves */ |
| 3654 | static int deny_bitmap_write_access(struct file * file) |
| 3655 | { |
| 3656 | struct inode *inode = file->f_mapping->host; |
| 3657 | |
| 3658 | spin_lock(&inode->i_lock); |
| 3659 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3660 | spin_unlock(&inode->i_lock); |
| 3661 | return -ETXTBSY; |
| 3662 | } |
| 3663 | atomic_set(&inode->i_writecount, -1); |
| 3664 | spin_unlock(&inode->i_lock); |
| 3665 | |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
| 3669 | static void restore_bitmap_write_access(struct file *file) |
| 3670 | { |
| 3671 | struct inode *inode = file->f_mapping->host; |
| 3672 | |
| 3673 | spin_lock(&inode->i_lock); |
| 3674 | atomic_set(&inode->i_writecount, 1); |
| 3675 | spin_unlock(&inode->i_lock); |
| 3676 | } |
| 3677 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3678 | /* mode: |
| 3679 | * 0 - completely stop and dis-assemble array |
| 3680 | * 1 - switch to readonly |
| 3681 | * 2 - stop but do not disassemble array |
| 3682 | */ |
| 3683 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | { |
| 3685 | int err = 0; |
| 3686 | struct gendisk *disk = mddev->gendisk; |
| 3687 | |
| 3688 | if (mddev->pers) { |
| 3689 | if (atomic_read(&mddev->active)>2) { |
| 3690 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3691 | return -EBUSY; |
| 3692 | } |
| 3693 | |
| 3694 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3695 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3697 | md_unregister_thread(mddev->sync_thread); |
| 3698 | mddev->sync_thread = NULL; |
| 3699 | } |
| 3700 | |
| 3701 | del_timer_sync(&mddev->safemode_timer); |
| 3702 | |
| 3703 | invalidate_partition(disk, 0); |
| 3704 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3705 | switch(mode) { |
| 3706 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3708 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | goto out; |
| 3710 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3711 | break; |
| 3712 | case 0: /* disassemble */ |
| 3713 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3714 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3715 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | if (mddev->ro) |
| 3717 | set_disk_ro(disk, 0); |
| 3718 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3719 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3720 | mddev->queue->merge_bvec_fn = NULL; |
| 3721 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3722 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3723 | if (mddev->pers->sync_request) |
| 3724 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | module_put(mddev->pers->owner); |
| 3727 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3728 | /* tell userspace to handle 'inactive' */ |
| 3729 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3730 | |
| 3731 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3732 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | if (mddev->ro) |
| 3735 | mddev->ro = 0; |
| 3736 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3737 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | /* mark array as shutdown cleanly */ |
| 3739 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3740 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3742 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3744 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | /* |
| 3748 | * Free resources if final stop |
| 3749 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3750 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3751 | mdk_rdev_t *rdev; |
| 3752 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3755 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3756 | bitmap_destroy(mddev); |
| 3757 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3758 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3759 | fput(mddev->bitmap_file); |
| 3760 | mddev->bitmap_file = NULL; |
| 3761 | } |
| 3762 | mddev->bitmap_offset = 0; |
| 3763 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3764 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3765 | if (rdev->raid_disk >= 0) { |
| 3766 | char nm[20]; |
| 3767 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3768 | sysfs_remove_link(&mddev->kobj, nm); |
| 3769 | } |
| 3770 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3771 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3772 | flush_scheduled_work(); |
| 3773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | export_array(mddev); |
| 3775 | |
| 3776 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3777 | mddev->size = 0; |
| 3778 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3779 | mddev->recovery_cp = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3780 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3781 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3782 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3783 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3784 | mddev->level = LEVEL_NONE; |
| 3785 | mddev->clevel[0] = 0; |
| 3786 | mddev->flags = 0; |
| 3787 | mddev->ro = 0; |
| 3788 | mddev->metadata_type[0] = 0; |
| 3789 | mddev->chunk_size = 0; |
| 3790 | mddev->ctime = mddev->utime = 0; |
| 3791 | mddev->layout = 0; |
| 3792 | mddev->max_disks = 0; |
| 3793 | mddev->events = 0; |
| 3794 | mddev->delta_disks = 0; |
| 3795 | mddev->new_level = LEVEL_NONE; |
| 3796 | mddev->new_layout = 0; |
| 3797 | mddev->new_chunk = 0; |
| 3798 | mddev->curr_resync = 0; |
| 3799 | mddev->resync_mismatches = 0; |
| 3800 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3801 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3802 | mddev->recovery = 0; |
| 3803 | mddev->in_sync = 0; |
| 3804 | mddev->changed = 0; |
| 3805 | mddev->degraded = 0; |
| 3806 | mddev->barriers_work = 0; |
| 3807 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3808 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3809 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3811 | mdname(mddev)); |
| 3812 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3813 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | out: |
| 3815 | return err; |
| 3816 | } |
| 3817 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3818 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | static void autorun_array(mddev_t *mddev) |
| 3820 | { |
| 3821 | mdk_rdev_t *rdev; |
| 3822 | struct list_head *tmp; |
| 3823 | int err; |
| 3824 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3825 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | |
| 3828 | printk(KERN_INFO "md: running: "); |
| 3829 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3830 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | char b[BDEVNAME_SIZE]; |
| 3832 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3833 | } |
| 3834 | printk("\n"); |
| 3835 | |
| 3836 | err = do_md_run (mddev); |
| 3837 | if (err) { |
| 3838 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3839 | do_md_stop (mddev, 0); |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | /* |
| 3844 | * lets try to run arrays based on all disks that have arrived |
| 3845 | * until now. (those are in pending_raid_disks) |
| 3846 | * |
| 3847 | * the method: pick the first pending disk, collect all disks with |
| 3848 | * the same UUID, remove all from the pending list and put them into |
| 3849 | * the 'same_array' list. Then order this list based on superblock |
| 3850 | * update time (freshest comes first), kick out 'old' disks and |
| 3851 | * compare superblocks. If everything's fine then run it. |
| 3852 | * |
| 3853 | * If "unit" is allocated, then bump its reference count |
| 3854 | */ |
| 3855 | static void autorun_devices(int part) |
| 3856 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | struct list_head *tmp; |
| 3858 | mdk_rdev_t *rdev0, *rdev; |
| 3859 | mddev_t *mddev; |
| 3860 | char b[BDEVNAME_SIZE]; |
| 3861 | |
| 3862 | printk(KERN_INFO "md: autorun ...\n"); |
| 3863 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3864 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3866 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | rdev0 = list_entry(pending_raid_disks.next, |
| 3868 | mdk_rdev_t, same_set); |
| 3869 | |
| 3870 | printk(KERN_INFO "md: considering %s ...\n", |
| 3871 | bdevname(rdev0->bdev,b)); |
| 3872 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3873 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3875 | printk(KERN_INFO "md: adding %s ...\n", |
| 3876 | bdevname(rdev->bdev,b)); |
| 3877 | list_move(&rdev->same_set, &candidates); |
| 3878 | } |
| 3879 | /* |
| 3880 | * now we have a set of devices, with all of them having |
| 3881 | * mostly sane superblocks. It's time to allocate the |
| 3882 | * mddev. |
| 3883 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3884 | if (part) { |
| 3885 | dev = MKDEV(mdp_major, |
| 3886 | rdev0->preferred_minor << MdpMinorShift); |
| 3887 | unit = MINOR(dev) >> MdpMinorShift; |
| 3888 | } else { |
| 3889 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3890 | unit = MINOR(dev); |
| 3891 | } |
| 3892 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3894 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3895 | break; |
| 3896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | |
| 3898 | md_probe(dev, NULL, NULL); |
| 3899 | mddev = mddev_find(dev); |
| 3900 | if (!mddev) { |
| 3901 | printk(KERN_ERR |
| 3902 | "md: cannot allocate memory for md drive.\n"); |
| 3903 | break; |
| 3904 | } |
| 3905 | if (mddev_lock(mddev)) |
| 3906 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3907 | mdname(mddev)); |
| 3908 | else if (mddev->raid_disks || mddev->major_version |
| 3909 | || !list_empty(&mddev->disks)) { |
| 3910 | printk(KERN_WARNING |
| 3911 | "md: %s already running, cannot run %s\n", |
| 3912 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 3913 | mddev_unlock(mddev); |
| 3914 | } else { |
| 3915 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3916 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3917 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | list_del_init(&rdev->same_set); |
| 3919 | if (bind_rdev_to_array(rdev, mddev)) |
| 3920 | export_rdev(rdev); |
| 3921 | } |
| 3922 | autorun_array(mddev); |
| 3923 | mddev_unlock(mddev); |
| 3924 | } |
| 3925 | /* on success, candidates will be empty, on error |
| 3926 | * it won't... |
| 3927 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3928 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | export_rdev(rdev); |
| 3930 | mddev_put(mddev); |
| 3931 | } |
| 3932 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 3933 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3934 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | static int get_version(void __user * arg) |
| 3937 | { |
| 3938 | mdu_version_t ver; |
| 3939 | |
| 3940 | ver.major = MD_MAJOR_VERSION; |
| 3941 | ver.minor = MD_MINOR_VERSION; |
| 3942 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 3943 | |
| 3944 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 3945 | return -EFAULT; |
| 3946 | |
| 3947 | return 0; |
| 3948 | } |
| 3949 | |
| 3950 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 3951 | { |
| 3952 | mdu_array_info_t info; |
| 3953 | int nr,working,active,failed,spare; |
| 3954 | mdk_rdev_t *rdev; |
| 3955 | struct list_head *tmp; |
| 3956 | |
| 3957 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3958 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3960 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | failed++; |
| 3962 | else { |
| 3963 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3964 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | active++; |
| 3966 | else |
| 3967 | spare++; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | info.major_version = mddev->major_version; |
| 3972 | info.minor_version = mddev->minor_version; |
| 3973 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 3974 | info.ctime = mddev->ctime; |
| 3975 | info.level = mddev->level; |
| 3976 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 3977 | if (info.size != mddev->size) /* overflow */ |
| 3978 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | info.nr_disks = nr; |
| 3980 | info.raid_disks = mddev->raid_disks; |
| 3981 | info.md_minor = mddev->md_minor; |
| 3982 | info.not_persistent= !mddev->persistent; |
| 3983 | |
| 3984 | info.utime = mddev->utime; |
| 3985 | info.state = 0; |
| 3986 | if (mddev->in_sync) |
| 3987 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3988 | if (mddev->bitmap && mddev->bitmap_offset) |
| 3989 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | info.active_disks = active; |
| 3991 | info.working_disks = working; |
| 3992 | info.failed_disks = failed; |
| 3993 | info.spare_disks = spare; |
| 3994 | |
| 3995 | info.layout = mddev->layout; |
| 3996 | info.chunk_size = mddev->chunk_size; |
| 3997 | |
| 3998 | if (copy_to_user(arg, &info, sizeof(info))) |
| 3999 | return -EFAULT; |
| 4000 | |
| 4001 | return 0; |
| 4002 | } |
| 4003 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4004 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4005 | { |
| 4006 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4007 | char *ptr, *buf = NULL; |
| 4008 | int err = -ENOMEM; |
| 4009 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4010 | md_allow_write(mddev); |
| 4011 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4012 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 4013 | if (!file) |
| 4014 | goto out; |
| 4015 | |
| 4016 | /* bitmap disabled, zero the first byte and copy out */ |
| 4017 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4018 | file->pathname[0] = '\0'; |
| 4019 | goto copy_out; |
| 4020 | } |
| 4021 | |
| 4022 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4023 | if (!buf) |
| 4024 | goto out; |
| 4025 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4026 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4027 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4028 | goto out; |
| 4029 | |
| 4030 | strcpy(file->pathname, ptr); |
| 4031 | |
| 4032 | copy_out: |
| 4033 | err = 0; |
| 4034 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4035 | err = -EFAULT; |
| 4036 | out: |
| 4037 | kfree(buf); |
| 4038 | kfree(file); |
| 4039 | return err; |
| 4040 | } |
| 4041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4043 | { |
| 4044 | mdu_disk_info_t info; |
| 4045 | unsigned int nr; |
| 4046 | mdk_rdev_t *rdev; |
| 4047 | |
| 4048 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4049 | return -EFAULT; |
| 4050 | |
| 4051 | nr = info.number; |
| 4052 | |
| 4053 | rdev = find_rdev_nr(mddev, nr); |
| 4054 | if (rdev) { |
| 4055 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4056 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4057 | info.raid_disk = rdev->raid_disk; |
| 4058 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4059 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4061 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4063 | info.state |= (1<<MD_DISK_SYNC); |
| 4064 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4065 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4066 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | } else { |
| 4068 | info.major = info.minor = 0; |
| 4069 | info.raid_disk = -1; |
| 4070 | info.state = (1<<MD_DISK_REMOVED); |
| 4071 | } |
| 4072 | |
| 4073 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4074 | return -EFAULT; |
| 4075 | |
| 4076 | return 0; |
| 4077 | } |
| 4078 | |
| 4079 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4080 | { |
| 4081 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4082 | mdk_rdev_t *rdev; |
| 4083 | dev_t dev = MKDEV(info->major,info->minor); |
| 4084 | |
| 4085 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4086 | return -EOVERFLOW; |
| 4087 | |
| 4088 | if (!mddev->raid_disks) { |
| 4089 | int err; |
| 4090 | /* expecting a device which has a superblock */ |
| 4091 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4092 | if (IS_ERR(rdev)) { |
| 4093 | printk(KERN_WARNING |
| 4094 | "md: md_import_device returned %ld\n", |
| 4095 | PTR_ERR(rdev)); |
| 4096 | return PTR_ERR(rdev); |
| 4097 | } |
| 4098 | if (!list_empty(&mddev->disks)) { |
| 4099 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4100 | mdk_rdev_t, same_set); |
| 4101 | int err = super_types[mddev->major_version] |
| 4102 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4103 | if (err < 0) { |
| 4104 | printk(KERN_WARNING |
| 4105 | "md: %s has different UUID to %s\n", |
| 4106 | bdevname(rdev->bdev,b), |
| 4107 | bdevname(rdev0->bdev,b2)); |
| 4108 | export_rdev(rdev); |
| 4109 | return -EINVAL; |
| 4110 | } |
| 4111 | } |
| 4112 | err = bind_rdev_to_array(rdev, mddev); |
| 4113 | if (err) |
| 4114 | export_rdev(rdev); |
| 4115 | return err; |
| 4116 | } |
| 4117 | |
| 4118 | /* |
| 4119 | * add_new_disk can be used once the array is assembled |
| 4120 | * to add "hot spares". They must already have a superblock |
| 4121 | * written |
| 4122 | */ |
| 4123 | if (mddev->pers) { |
| 4124 | int err; |
| 4125 | if (!mddev->pers->hot_add_disk) { |
| 4126 | printk(KERN_WARNING |
| 4127 | "%s: personality does not support diskops!\n", |
| 4128 | mdname(mddev)); |
| 4129 | return -EINVAL; |
| 4130 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4131 | if (mddev->persistent) |
| 4132 | rdev = md_import_device(dev, mddev->major_version, |
| 4133 | mddev->minor_version); |
| 4134 | else |
| 4135 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | if (IS_ERR(rdev)) { |
| 4137 | printk(KERN_WARNING |
| 4138 | "md: md_import_device returned %ld\n", |
| 4139 | PTR_ERR(rdev)); |
| 4140 | return PTR_ERR(rdev); |
| 4141 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4142 | /* set save_raid_disk if appropriate */ |
| 4143 | if (!mddev->persistent) { |
| 4144 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4145 | info->raid_disk < mddev->raid_disks) |
| 4146 | rdev->raid_disk = info->raid_disk; |
| 4147 | else |
| 4148 | rdev->raid_disk = -1; |
| 4149 | } else |
| 4150 | super_types[mddev->major_version]. |
| 4151 | validate_super(mddev, rdev); |
| 4152 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4153 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4154 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4155 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4156 | set_bit(WriteMostly, &rdev->flags); |
| 4157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | rdev->raid_disk = -1; |
| 4159 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4160 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4161 | /* If there is hot_add_disk but no hot_remove_disk |
| 4162 | * then added disks for geometry changes, |
| 4163 | * and should be added immediately. |
| 4164 | */ |
| 4165 | super_types[mddev->major_version]. |
| 4166 | validate_super(mddev, rdev); |
| 4167 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4168 | if (err) |
| 4169 | unbind_rdev_from_array(rdev); |
| 4170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | if (err) |
| 4172 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4173 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4174 | md_update_sb(mddev, 1); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4175 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4176 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | return err; |
| 4178 | } |
| 4179 | |
| 4180 | /* otherwise, add_new_disk is only allowed |
| 4181 | * for major_version==0 superblocks |
| 4182 | */ |
| 4183 | if (mddev->major_version != 0) { |
| 4184 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4185 | mdname(mddev)); |
| 4186 | return -EINVAL; |
| 4187 | } |
| 4188 | |
| 4189 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4190 | int err; |
| 4191 | rdev = md_import_device (dev, -1, 0); |
| 4192 | if (IS_ERR(rdev)) { |
| 4193 | printk(KERN_WARNING |
| 4194 | "md: error, md_import_device() returned %ld\n", |
| 4195 | PTR_ERR(rdev)); |
| 4196 | return PTR_ERR(rdev); |
| 4197 | } |
| 4198 | rdev->desc_nr = info->number; |
| 4199 | if (info->raid_disk < mddev->raid_disks) |
| 4200 | rdev->raid_disk = info->raid_disk; |
| 4201 | else |
| 4202 | rdev->raid_disk = -1; |
| 4203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4205 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4206 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4208 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4209 | set_bit(WriteMostly, &rdev->flags); |
| 4210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | if (!mddev->persistent) { |
| 4212 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4213 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4214 | } else |
| 4215 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4216 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4217 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4218 | err = bind_rdev_to_array(rdev, mddev); |
| 4219 | if (err) { |
| 4220 | export_rdev(rdev); |
| 4221 | return err; |
| 4222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | return 0; |
| 4226 | } |
| 4227 | |
| 4228 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4229 | { |
| 4230 | char b[BDEVNAME_SIZE]; |
| 4231 | mdk_rdev_t *rdev; |
| 4232 | |
| 4233 | if (!mddev->pers) |
| 4234 | return -ENODEV; |
| 4235 | |
| 4236 | rdev = find_rdev(mddev, dev); |
| 4237 | if (!rdev) |
| 4238 | return -ENXIO; |
| 4239 | |
| 4240 | if (rdev->raid_disk >= 0) |
| 4241 | goto busy; |
| 4242 | |
| 4243 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4244 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4245 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | |
| 4247 | return 0; |
| 4248 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4249 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4251 | return -EBUSY; |
| 4252 | } |
| 4253 | |
| 4254 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4255 | { |
| 4256 | char b[BDEVNAME_SIZE]; |
| 4257 | int err; |
| 4258 | unsigned int size; |
| 4259 | mdk_rdev_t *rdev; |
| 4260 | |
| 4261 | if (!mddev->pers) |
| 4262 | return -ENODEV; |
| 4263 | |
| 4264 | if (mddev->major_version != 0) { |
| 4265 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4266 | " version-0 superblocks.\n", |
| 4267 | mdname(mddev)); |
| 4268 | return -EINVAL; |
| 4269 | } |
| 4270 | if (!mddev->pers->hot_add_disk) { |
| 4271 | printk(KERN_WARNING |
| 4272 | "%s: personality does not support diskops!\n", |
| 4273 | mdname(mddev)); |
| 4274 | return -EINVAL; |
| 4275 | } |
| 4276 | |
| 4277 | rdev = md_import_device (dev, -1, 0); |
| 4278 | if (IS_ERR(rdev)) { |
| 4279 | printk(KERN_WARNING |
| 4280 | "md: error, md_import_device() returned %ld\n", |
| 4281 | PTR_ERR(rdev)); |
| 4282 | return -EINVAL; |
| 4283 | } |
| 4284 | |
| 4285 | if (mddev->persistent) |
| 4286 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4287 | else |
| 4288 | rdev->sb_offset = |
| 4289 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4290 | |
| 4291 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4292 | rdev->size = size; |
| 4293 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4294 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | printk(KERN_WARNING |
| 4296 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4297 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4298 | err = -EINVAL; |
| 4299 | goto abort_export; |
| 4300 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4301 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4303 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4304 | err = bind_rdev_to_array(rdev, mddev); |
| 4305 | if (err) |
| 4306 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
| 4308 | /* |
| 4309 | * The rest should better be atomic, we can have disk failures |
| 4310 | * noticed in interrupt contexts ... |
| 4311 | */ |
| 4312 | |
| 4313 | if (rdev->desc_nr == mddev->max_disks) { |
| 4314 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4315 | mdname(mddev)); |
| 4316 | err = -EBUSY; |
| 4317 | goto abort_unbind_export; |
| 4318 | } |
| 4319 | |
| 4320 | rdev->raid_disk = -1; |
| 4321 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4322 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
| 4324 | /* |
| 4325 | * Kick recovery, maybe this spare has to be added to the |
| 4326 | * array immediately. |
| 4327 | */ |
| 4328 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4329 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4330 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | return 0; |
| 4332 | |
| 4333 | abort_unbind_export: |
| 4334 | unbind_rdev_from_array(rdev); |
| 4335 | |
| 4336 | abort_export: |
| 4337 | export_rdev(rdev); |
| 4338 | return err; |
| 4339 | } |
| 4340 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4341 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4342 | { |
| 4343 | int err; |
| 4344 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4345 | if (mddev->pers) { |
| 4346 | if (!mddev->pers->quiesce) |
| 4347 | return -EBUSY; |
| 4348 | if (mddev->recovery || mddev->sync_thread) |
| 4349 | return -EBUSY; |
| 4350 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4353 | |
| 4354 | if (fd >= 0) { |
| 4355 | if (mddev->bitmap) |
| 4356 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4357 | mddev->bitmap_file = fget(fd); |
| 4358 | |
| 4359 | if (mddev->bitmap_file == NULL) { |
| 4360 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4361 | mdname(mddev)); |
| 4362 | return -EBADF; |
| 4363 | } |
| 4364 | |
| 4365 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4366 | if (err) { |
| 4367 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4368 | mdname(mddev)); |
| 4369 | fput(mddev->bitmap_file); |
| 4370 | mddev->bitmap_file = NULL; |
| 4371 | return err; |
| 4372 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4373 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4374 | } else if (mddev->bitmap == NULL) |
| 4375 | return -ENOENT; /* cannot remove what isn't there */ |
| 4376 | err = 0; |
| 4377 | if (mddev->pers) { |
| 4378 | mddev->pers->quiesce(mddev, 1); |
| 4379 | if (fd >= 0) |
| 4380 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4381 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4382 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4383 | fd = -1; /* make sure to put the file */ |
| 4384 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4385 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4386 | } |
| 4387 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4388 | if (mddev->bitmap_file) { |
| 4389 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4390 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4391 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4392 | mddev->bitmap_file = NULL; |
| 4393 | } |
| 4394 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4395 | return err; |
| 4396 | } |
| 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | /* |
| 4399 | * set_array_info is used two different ways |
| 4400 | * The original usage is when creating a new array. |
| 4401 | * In this usage, raid_disks is > 0 and it together with |
| 4402 | * level, size, not_persistent,layout,chunksize determine the |
| 4403 | * shape of the array. |
| 4404 | * This will always create an array with a type-0.90.0 superblock. |
| 4405 | * The newer usage is when assembling an array. |
| 4406 | * In this case raid_disks will be 0, and the major_version field is |
| 4407 | * use to determine which style super-blocks are to be found on the devices. |
| 4408 | * The minor and patch _version numbers are also kept incase the |
| 4409 | * super_block handler wishes to interpret them. |
| 4410 | */ |
| 4411 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4412 | { |
| 4413 | |
| 4414 | if (info->raid_disks == 0) { |
| 4415 | /* just setting version number for superblock loading */ |
| 4416 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4417 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | super_types[info->major_version].name == NULL) { |
| 4419 | /* maybe try to auto-load a module? */ |
| 4420 | printk(KERN_INFO |
| 4421 | "md: superblock version %d not known\n", |
| 4422 | info->major_version); |
| 4423 | return -EINVAL; |
| 4424 | } |
| 4425 | mddev->major_version = info->major_version; |
| 4426 | mddev->minor_version = info->minor_version; |
| 4427 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4428 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | return 0; |
| 4430 | } |
| 4431 | mddev->major_version = MD_MAJOR_VERSION; |
| 4432 | mddev->minor_version = MD_MINOR_VERSION; |
| 4433 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4434 | mddev->ctime = get_seconds(); |
| 4435 | |
| 4436 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4437 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | mddev->size = info->size; |
| 4439 | mddev->raid_disks = info->raid_disks; |
| 4440 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4441 | * openned |
| 4442 | */ |
| 4443 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4444 | mddev->recovery_cp = MaxSector; |
| 4445 | else |
| 4446 | mddev->recovery_cp = 0; |
| 4447 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4448 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
| 4450 | mddev->layout = info->layout; |
| 4451 | mddev->chunk_size = info->chunk_size; |
| 4452 | |
| 4453 | mddev->max_disks = MD_SB_DISKS; |
| 4454 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4455 | if (mddev->persistent) |
| 4456 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4457 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4459 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4460 | mddev->bitmap_offset = 0; |
| 4461 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4462 | mddev->reshape_position = MaxSector; |
| 4463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | /* |
| 4465 | * Generate a 128 bit UUID |
| 4466 | */ |
| 4467 | get_random_bytes(mddev->uuid, 16); |
| 4468 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4469 | mddev->new_level = mddev->level; |
| 4470 | mddev->new_chunk = mddev->chunk_size; |
| 4471 | mddev->new_layout = mddev->layout; |
| 4472 | mddev->delta_disks = 0; |
| 4473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | return 0; |
| 4475 | } |
| 4476 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4477 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4478 | { |
| 4479 | mdk_rdev_t * rdev; |
| 4480 | int rv; |
| 4481 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4482 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4483 | |
| 4484 | if (mddev->pers->resize == NULL) |
| 4485 | return -EINVAL; |
| 4486 | /* The "size" is the amount of each device that is used. |
| 4487 | * This can only make sense for arrays with redundancy. |
| 4488 | * linear and raid0 always use whatever space is available |
| 4489 | * We can only consider changing the size if no resync |
| 4490 | * or reconstruction is happening, and if the new size |
| 4491 | * is acceptable. It must fit before the sb_offset or, |
| 4492 | * if that is <data_offset, it must fit before the |
| 4493 | * size of each device. |
| 4494 | * If size is zero, we find the largest size that fits. |
| 4495 | */ |
| 4496 | if (mddev->sync_thread) |
| 4497 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4498 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4499 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4500 | avail = rdev->size * 2; |
| 4501 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4502 | if (fit && (size == 0 || size > avail/2)) |
| 4503 | size = avail/2; |
| 4504 | if (avail < ((sector_t)size << 1)) |
| 4505 | return -ENOSPC; |
| 4506 | } |
| 4507 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4508 | if (!rv) { |
| 4509 | struct block_device *bdev; |
| 4510 | |
| 4511 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4512 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4513 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4514 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4515 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4516 | bdput(bdev); |
| 4517 | } |
| 4518 | } |
| 4519 | return rv; |
| 4520 | } |
| 4521 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4522 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4523 | { |
| 4524 | int rv; |
| 4525 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4526 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4527 | return -EINVAL; |
| 4528 | if (raid_disks <= 0 || |
| 4529 | raid_disks >= mddev->max_disks) |
| 4530 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4531 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4532 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4533 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4534 | |
| 4535 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4536 | return rv; |
| 4537 | } |
| 4538 | |
| 4539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | /* |
| 4541 | * update_array_info is used to change the configuration of an |
| 4542 | * on-line array. |
| 4543 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4544 | * fields in the info are checked against the array. |
| 4545 | * Any differences that cannot be handled will cause an error. |
| 4546 | * Normally, only one change can be managed at a time. |
| 4547 | */ |
| 4548 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4549 | { |
| 4550 | int rv = 0; |
| 4551 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4552 | int state = 0; |
| 4553 | |
| 4554 | /* calculate expected state,ignoring low bits */ |
| 4555 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4556 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | |
| 4558 | if (mddev->major_version != info->major_version || |
| 4559 | mddev->minor_version != info->minor_version || |
| 4560 | /* mddev->patch_version != info->patch_version || */ |
| 4561 | mddev->ctime != info->ctime || |
| 4562 | mddev->level != info->level || |
| 4563 | /* mddev->layout != info->layout || */ |
| 4564 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4565 | mddev->chunk_size != info->chunk_size || |
| 4566 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4567 | ((state^info->state) & 0xfffffe00) |
| 4568 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | return -EINVAL; |
| 4570 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4571 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4573 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4574 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | if (cnt == 0) return 0; |
| 4576 | if (cnt > 1) return -EINVAL; |
| 4577 | |
| 4578 | if (mddev->layout != info->layout) { |
| 4579 | /* Change layout |
| 4580 | * we don't need to do anything at the md level, the |
| 4581 | * personality will take care of it all. |
| 4582 | */ |
| 4583 | if (mddev->pers->reconfig == NULL) |
| 4584 | return -EINVAL; |
| 4585 | else |
| 4586 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4587 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4588 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4589 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4591 | if (mddev->raid_disks != info->raid_disks) |
| 4592 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4594 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4595 | if (mddev->pers->quiesce == NULL) |
| 4596 | return -EINVAL; |
| 4597 | if (mddev->recovery || mddev->sync_thread) |
| 4598 | return -EBUSY; |
| 4599 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4600 | /* add the bitmap */ |
| 4601 | if (mddev->bitmap) |
| 4602 | return -EEXIST; |
| 4603 | if (mddev->default_bitmap_offset == 0) |
| 4604 | return -EINVAL; |
| 4605 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4606 | mddev->pers->quiesce(mddev, 1); |
| 4607 | rv = bitmap_create(mddev); |
| 4608 | if (rv) |
| 4609 | bitmap_destroy(mddev); |
| 4610 | mddev->pers->quiesce(mddev, 0); |
| 4611 | } else { |
| 4612 | /* remove the bitmap */ |
| 4613 | if (!mddev->bitmap) |
| 4614 | return -ENOENT; |
| 4615 | if (mddev->bitmap->file) |
| 4616 | return -EINVAL; |
| 4617 | mddev->pers->quiesce(mddev, 1); |
| 4618 | bitmap_destroy(mddev); |
| 4619 | mddev->pers->quiesce(mddev, 0); |
| 4620 | mddev->bitmap_offset = 0; |
| 4621 | } |
| 4622 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4623 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | return rv; |
| 4625 | } |
| 4626 | |
| 4627 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4628 | { |
| 4629 | mdk_rdev_t *rdev; |
| 4630 | |
| 4631 | if (mddev->pers == NULL) |
| 4632 | return -ENODEV; |
| 4633 | |
| 4634 | rdev = find_rdev(mddev, dev); |
| 4635 | if (!rdev) |
| 4636 | return -ENODEV; |
| 4637 | |
| 4638 | md_error(mddev, rdev); |
| 4639 | return 0; |
| 4640 | } |
| 4641 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4642 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4643 | { |
| 4644 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4645 | |
| 4646 | geo->heads = 2; |
| 4647 | geo->sectors = 4; |
| 4648 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4649 | return 0; |
| 4650 | } |
| 4651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4653 | unsigned int cmd, unsigned long arg) |
| 4654 | { |
| 4655 | int err = 0; |
| 4656 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | mddev_t *mddev = NULL; |
| 4658 | |
| 4659 | if (!capable(CAP_SYS_ADMIN)) |
| 4660 | return -EACCES; |
| 4661 | |
| 4662 | /* |
| 4663 | * Commands dealing with the RAID driver but not any |
| 4664 | * particular array: |
| 4665 | */ |
| 4666 | switch (cmd) |
| 4667 | { |
| 4668 | case RAID_VERSION: |
| 4669 | err = get_version(argp); |
| 4670 | goto done; |
| 4671 | |
| 4672 | case PRINT_RAID_DEBUG: |
| 4673 | err = 0; |
| 4674 | md_print_devices(); |
| 4675 | goto done; |
| 4676 | |
| 4677 | #ifndef MODULE |
| 4678 | case RAID_AUTORUN: |
| 4679 | err = 0; |
| 4680 | autostart_arrays(arg); |
| 4681 | goto done; |
| 4682 | #endif |
| 4683 | default:; |
| 4684 | } |
| 4685 | |
| 4686 | /* |
| 4687 | * Commands creating/starting a new array: |
| 4688 | */ |
| 4689 | |
| 4690 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4691 | |
| 4692 | if (!mddev) { |
| 4693 | BUG(); |
| 4694 | goto abort; |
| 4695 | } |
| 4696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | err = mddev_lock(mddev); |
| 4698 | if (err) { |
| 4699 | printk(KERN_INFO |
| 4700 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4701 | err, cmd); |
| 4702 | goto abort; |
| 4703 | } |
| 4704 | |
| 4705 | switch (cmd) |
| 4706 | { |
| 4707 | case SET_ARRAY_INFO: |
| 4708 | { |
| 4709 | mdu_array_info_t info; |
| 4710 | if (!arg) |
| 4711 | memset(&info, 0, sizeof(info)); |
| 4712 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4713 | err = -EFAULT; |
| 4714 | goto abort_unlock; |
| 4715 | } |
| 4716 | if (mddev->pers) { |
| 4717 | err = update_array_info(mddev, &info); |
| 4718 | if (err) { |
| 4719 | printk(KERN_WARNING "md: couldn't update" |
| 4720 | " array info. %d\n", err); |
| 4721 | goto abort_unlock; |
| 4722 | } |
| 4723 | goto done_unlock; |
| 4724 | } |
| 4725 | if (!list_empty(&mddev->disks)) { |
| 4726 | printk(KERN_WARNING |
| 4727 | "md: array %s already has disks!\n", |
| 4728 | mdname(mddev)); |
| 4729 | err = -EBUSY; |
| 4730 | goto abort_unlock; |
| 4731 | } |
| 4732 | if (mddev->raid_disks) { |
| 4733 | printk(KERN_WARNING |
| 4734 | "md: array %s already initialised!\n", |
| 4735 | mdname(mddev)); |
| 4736 | err = -EBUSY; |
| 4737 | goto abort_unlock; |
| 4738 | } |
| 4739 | err = set_array_info(mddev, &info); |
| 4740 | if (err) { |
| 4741 | printk(KERN_WARNING "md: couldn't set" |
| 4742 | " array info. %d\n", err); |
| 4743 | goto abort_unlock; |
| 4744 | } |
| 4745 | } |
| 4746 | goto done_unlock; |
| 4747 | |
| 4748 | default:; |
| 4749 | } |
| 4750 | |
| 4751 | /* |
| 4752 | * Commands querying/configuring an existing array: |
| 4753 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4754 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4755 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4756 | if ((!mddev->raid_disks && !mddev->external) |
| 4757 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4758 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4759 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | err = -ENODEV; |
| 4761 | goto abort_unlock; |
| 4762 | } |
| 4763 | |
| 4764 | /* |
| 4765 | * Commands even a read-only array can execute: |
| 4766 | */ |
| 4767 | switch (cmd) |
| 4768 | { |
| 4769 | case GET_ARRAY_INFO: |
| 4770 | err = get_array_info(mddev, argp); |
| 4771 | goto done_unlock; |
| 4772 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4773 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4774 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4775 | goto done_unlock; |
| 4776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | case GET_DISK_INFO: |
| 4778 | err = get_disk_info(mddev, argp); |
| 4779 | goto done_unlock; |
| 4780 | |
| 4781 | case RESTART_ARRAY_RW: |
| 4782 | err = restart_array(mddev); |
| 4783 | goto done_unlock; |
| 4784 | |
| 4785 | case STOP_ARRAY: |
| 4786 | err = do_md_stop (mddev, 0); |
| 4787 | goto done_unlock; |
| 4788 | |
| 4789 | case STOP_ARRAY_RO: |
| 4790 | err = do_md_stop (mddev, 1); |
| 4791 | goto done_unlock; |
| 4792 | |
| 4793 | /* |
| 4794 | * We have a problem here : there is no easy way to give a CHS |
| 4795 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4796 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4797 | * dosfs just mad... ;-) |
| 4798 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | /* |
| 4802 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4803 | * superblock, so we do not allow them on read-only arrays. |
| 4804 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4805 | * here and hit the 'default' below, so only disallow |
| 4806 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4808 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4809 | mddev->ro && mddev->pers) { |
| 4810 | if (mddev->ro == 2) { |
| 4811 | mddev->ro = 0; |
| 4812 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4813 | md_wakeup_thread(mddev->thread); |
| 4814 | |
| 4815 | } else { |
| 4816 | err = -EROFS; |
| 4817 | goto abort_unlock; |
| 4818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | switch (cmd) |
| 4822 | { |
| 4823 | case ADD_NEW_DISK: |
| 4824 | { |
| 4825 | mdu_disk_info_t info; |
| 4826 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4827 | err = -EFAULT; |
| 4828 | else |
| 4829 | err = add_new_disk(mddev, &info); |
| 4830 | goto done_unlock; |
| 4831 | } |
| 4832 | |
| 4833 | case HOT_REMOVE_DISK: |
| 4834 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4835 | goto done_unlock; |
| 4836 | |
| 4837 | case HOT_ADD_DISK: |
| 4838 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4839 | goto done_unlock; |
| 4840 | |
| 4841 | case SET_DISK_FAULTY: |
| 4842 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4843 | goto done_unlock; |
| 4844 | |
| 4845 | case RUN_ARRAY: |
| 4846 | err = do_md_run (mddev); |
| 4847 | goto done_unlock; |
| 4848 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4849 | case SET_BITMAP_FILE: |
| 4850 | err = set_bitmap_file(mddev, (int)arg); |
| 4851 | goto done_unlock; |
| 4852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | err = -EINVAL; |
| 4855 | goto abort_unlock; |
| 4856 | } |
| 4857 | |
| 4858 | done_unlock: |
| 4859 | abort_unlock: |
| 4860 | mddev_unlock(mddev); |
| 4861 | |
| 4862 | return err; |
| 4863 | done: |
| 4864 | if (err) |
| 4865 | MD_BUG(); |
| 4866 | abort: |
| 4867 | return err; |
| 4868 | } |
| 4869 | |
| 4870 | static int md_open(struct inode *inode, struct file *file) |
| 4871 | { |
| 4872 | /* |
| 4873 | * Succeed if we can lock the mddev, which confirms that |
| 4874 | * it isn't being stopped right now. |
| 4875 | */ |
| 4876 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4877 | int err; |
| 4878 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 4879 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | goto out; |
| 4881 | |
| 4882 | err = 0; |
| 4883 | mddev_get(mddev); |
| 4884 | mddev_unlock(mddev); |
| 4885 | |
| 4886 | check_disk_change(inode->i_bdev); |
| 4887 | out: |
| 4888 | return err; |
| 4889 | } |
| 4890 | |
| 4891 | static int md_release(struct inode *inode, struct file * file) |
| 4892 | { |
| 4893 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4894 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4895 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | mddev_put(mddev); |
| 4897 | |
| 4898 | return 0; |
| 4899 | } |
| 4900 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4901 | static int md_media_changed(struct gendisk *disk) |
| 4902 | { |
| 4903 | mddev_t *mddev = disk->private_data; |
| 4904 | |
| 4905 | return mddev->changed; |
| 4906 | } |
| 4907 | |
| 4908 | static int md_revalidate(struct gendisk *disk) |
| 4909 | { |
| 4910 | mddev_t *mddev = disk->private_data; |
| 4911 | |
| 4912 | mddev->changed = 0; |
| 4913 | return 0; |
| 4914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | static struct block_device_operations md_fops = |
| 4916 | { |
| 4917 | .owner = THIS_MODULE, |
| 4918 | .open = md_open, |
| 4919 | .release = md_release, |
| 4920 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4921 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4922 | .media_changed = md_media_changed, |
| 4923 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | }; |
| 4925 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4926 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | { |
| 4928 | mdk_thread_t *thread = arg; |
| 4929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | /* |
| 4931 | * md_thread is a 'system-thread', it's priority should be very |
| 4932 | * high. We avoid resource deadlocks individually in each |
| 4933 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 4934 | * the very same RT priority as kswapd, thus we will never get |
| 4935 | * into a priority inversion deadlock. |
| 4936 | * |
| 4937 | * we definitely have to have equal or higher priority than |
| 4938 | * bdflush, otherwise bdflush will deadlock if there are too |
| 4939 | * many dirty RAID5 blocks. |
| 4940 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4942 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4943 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 4945 | /* We need to wait INTERRUPTIBLE so that |
| 4946 | * we don't add to the load-average. |
| 4947 | * That means we need to be sure no signals are |
| 4948 | * pending |
| 4949 | */ |
| 4950 | if (signal_pending(current)) |
| 4951 | flush_signals(current); |
| 4952 | |
| 4953 | wait_event_interruptible_timeout |
| 4954 | (thread->wqueue, |
| 4955 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 4956 | || kthread_should_stop(), |
| 4957 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | |
| 4959 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 4960 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 4961 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | return 0; |
| 4965 | } |
| 4966 | |
| 4967 | void md_wakeup_thread(mdk_thread_t *thread) |
| 4968 | { |
| 4969 | if (thread) { |
| 4970 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 4971 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 4972 | wake_up(&thread->wqueue); |
| 4973 | } |
| 4974 | } |
| 4975 | |
| 4976 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 4977 | const char *name) |
| 4978 | { |
| 4979 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4981 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | if (!thread) |
| 4983 | return NULL; |
| 4984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | init_waitqueue_head(&thread->wqueue); |
| 4986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | thread->run = run; |
| 4988 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4989 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4990 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4991 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | kfree(thread); |
| 4993 | return NULL; |
| 4994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | return thread; |
| 4996 | } |
| 4997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | void md_unregister_thread(mdk_thread_t *thread) |
| 4999 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5000 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5001 | |
| 5002 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | kfree(thread); |
| 5004 | } |
| 5005 | |
| 5006 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5007 | { |
| 5008 | if (!mddev) { |
| 5009 | MD_BUG(); |
| 5010 | return; |
| 5011 | } |
| 5012 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5013 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5015 | |
| 5016 | if (mddev->external) |
| 5017 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5018 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5020 | mdname(mddev), |
| 5021 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5022 | __builtin_return_address(0),__builtin_return_address(1), |
| 5023 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5024 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5025 | if (!mddev->pers) |
| 5026 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | if (!mddev->pers->error_handler) |
| 5028 | return; |
| 5029 | mddev->pers->error_handler(mddev,rdev); |
| 5030 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5031 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5032 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5033 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | /* seq_file implementation /proc/mdstat */ |
| 5037 | |
| 5038 | static void status_unused(struct seq_file *seq) |
| 5039 | { |
| 5040 | int i = 0; |
| 5041 | mdk_rdev_t *rdev; |
| 5042 | struct list_head *tmp; |
| 5043 | |
| 5044 | seq_printf(seq, "unused devices: "); |
| 5045 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5046 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | char b[BDEVNAME_SIZE]; |
| 5048 | i++; |
| 5049 | seq_printf(seq, "%s ", |
| 5050 | bdevname(rdev->bdev,b)); |
| 5051 | } |
| 5052 | if (!i) |
| 5053 | seq_printf(seq, "<none>"); |
| 5054 | |
| 5055 | seq_printf(seq, "\n"); |
| 5056 | } |
| 5057 | |
| 5058 | |
| 5059 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5060 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5061 | sector_t max_blocks, resync, res; |
| 5062 | unsigned long dt, db, rt; |
| 5063 | int scale; |
| 5064 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | |
| 5066 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5067 | |
| 5068 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5069 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5070 | else |
| 5071 | max_blocks = mddev->size; |
| 5072 | |
| 5073 | /* |
| 5074 | * Should not happen. |
| 5075 | */ |
| 5076 | if (!max_blocks) { |
| 5077 | MD_BUG(); |
| 5078 | return; |
| 5079 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5080 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5081 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5082 | * u32, as those are the requirements for sector_div. |
| 5083 | * Thus 'scale' must be at least 10 |
| 5084 | */ |
| 5085 | scale = 10; |
| 5086 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5087 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5088 | scale++; |
| 5089 | } |
| 5090 | res = (resync>>scale)*1000; |
| 5091 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5092 | |
| 5093 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5095 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | seq_printf(seq, "["); |
| 5097 | for (i = 0; i < x; i++) |
| 5098 | seq_printf(seq, "="); |
| 5099 | seq_printf(seq, ">"); |
| 5100 | for (i = 0; i < y; i++) |
| 5101 | seq_printf(seq, "."); |
| 5102 | seq_printf(seq, "] "); |
| 5103 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5104 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5105 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5106 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5107 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5108 | "check" : |
| 5109 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5110 | "resync" : "recovery"))), |
| 5111 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5112 | (unsigned long long) resync, |
| 5113 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | |
| 5115 | /* |
| 5116 | * We do not want to overflow, so the order of operands and |
| 5117 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5118 | * safe against division by zero. We only estimate anyway. |
| 5119 | * |
| 5120 | * dt: time from mark until now |
| 5121 | * db: blocks written from mark until now |
| 5122 | * rt: remaining time |
| 5123 | */ |
| 5124 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5125 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5126 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5127 | - mddev->resync_mark_cnt; |
| 5128 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | |
| 5130 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5131 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5132 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5136 | { |
| 5137 | struct list_head *tmp; |
| 5138 | loff_t l = *pos; |
| 5139 | mddev_t *mddev; |
| 5140 | |
| 5141 | if (l >= 0x10000) |
| 5142 | return NULL; |
| 5143 | if (!l--) |
| 5144 | /* header */ |
| 5145 | return (void*)1; |
| 5146 | |
| 5147 | spin_lock(&all_mddevs_lock); |
| 5148 | list_for_each(tmp,&all_mddevs) |
| 5149 | if (!l--) { |
| 5150 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5151 | mddev_get(mddev); |
| 5152 | spin_unlock(&all_mddevs_lock); |
| 5153 | return mddev; |
| 5154 | } |
| 5155 | spin_unlock(&all_mddevs_lock); |
| 5156 | if (!l--) |
| 5157 | return (void*)2;/* tail */ |
| 5158 | return NULL; |
| 5159 | } |
| 5160 | |
| 5161 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5162 | { |
| 5163 | struct list_head *tmp; |
| 5164 | mddev_t *next_mddev, *mddev = v; |
| 5165 | |
| 5166 | ++*pos; |
| 5167 | if (v == (void*)2) |
| 5168 | return NULL; |
| 5169 | |
| 5170 | spin_lock(&all_mddevs_lock); |
| 5171 | if (v == (void*)1) |
| 5172 | tmp = all_mddevs.next; |
| 5173 | else |
| 5174 | tmp = mddev->all_mddevs.next; |
| 5175 | if (tmp != &all_mddevs) |
| 5176 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5177 | else { |
| 5178 | next_mddev = (void*)2; |
| 5179 | *pos = 0x10000; |
| 5180 | } |
| 5181 | spin_unlock(&all_mddevs_lock); |
| 5182 | |
| 5183 | if (v != (void*)1) |
| 5184 | mddev_put(mddev); |
| 5185 | return next_mddev; |
| 5186 | |
| 5187 | } |
| 5188 | |
| 5189 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5190 | { |
| 5191 | mddev_t *mddev = v; |
| 5192 | |
| 5193 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5194 | mddev_put(mddev); |
| 5195 | } |
| 5196 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5197 | struct mdstat_info { |
| 5198 | int event; |
| 5199 | }; |
| 5200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5202 | { |
| 5203 | mddev_t *mddev = v; |
| 5204 | sector_t size; |
| 5205 | struct list_head *tmp2; |
| 5206 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5207 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5208 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | |
| 5210 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5211 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | seq_printf(seq, "Personalities : "); |
| 5213 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5214 | list_for_each_entry(pers, &pers_list, list) |
| 5215 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | |
| 5217 | spin_unlock(&pers_lock); |
| 5218 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5219 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | return 0; |
| 5221 | } |
| 5222 | if (v == (void*)2) { |
| 5223 | status_unused(seq); |
| 5224 | return 0; |
| 5225 | } |
| 5226 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5227 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5231 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5232 | mddev->pers ? "" : "in"); |
| 5233 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5234 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5236 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5237 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | seq_printf(seq, " %s", mddev->pers->name); |
| 5239 | } |
| 5240 | |
| 5241 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5242 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | char b[BDEVNAME_SIZE]; |
| 5244 | seq_printf(seq, " %s[%d]", |
| 5245 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5246 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5247 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5248 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | seq_printf(seq, "(F)"); |
| 5250 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5251 | } else if (rdev->raid_disk < 0) |
| 5252 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | size += rdev->size; |
| 5254 | } |
| 5255 | |
| 5256 | if (!list_empty(&mddev->disks)) { |
| 5257 | if (mddev->pers) |
| 5258 | seq_printf(seq, "\n %llu blocks", |
| 5259 | (unsigned long long)mddev->array_size); |
| 5260 | else |
| 5261 | seq_printf(seq, "\n %llu blocks", |
| 5262 | (unsigned long long)size); |
| 5263 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5264 | if (mddev->persistent) { |
| 5265 | if (mddev->major_version != 0 || |
| 5266 | mddev->minor_version != 90) { |
| 5267 | seq_printf(seq," super %d.%d", |
| 5268 | mddev->major_version, |
| 5269 | mddev->minor_version); |
| 5270 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5271 | } else if (mddev->external) |
| 5272 | seq_printf(seq, " super external:%s", |
| 5273 | mddev->metadata_type); |
| 5274 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5275 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | |
| 5277 | if (mddev->pers) { |
| 5278 | mddev->pers->status (seq, mddev); |
| 5279 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5280 | if (mddev->pers->sync_request) { |
| 5281 | if (mddev->curr_resync > 2) { |
| 5282 | status_resync (seq, mddev); |
| 5283 | seq_printf(seq, "\n "); |
| 5284 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5285 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5286 | else if (mddev->recovery_cp < MaxSector) |
| 5287 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5288 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5289 | } else |
| 5290 | seq_printf(seq, "\n "); |
| 5291 | |
| 5292 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5293 | unsigned long chunk_kb; |
| 5294 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5295 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5296 | chunk_kb = bitmap->chunksize >> 10; |
| 5297 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5298 | "%lu%s chunk", |
| 5299 | bitmap->pages - bitmap->missing_pages, |
| 5300 | bitmap->pages, |
| 5301 | (bitmap->pages - bitmap->missing_pages) |
| 5302 | << (PAGE_SHIFT - 10), |
| 5303 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5304 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5305 | if (bitmap->file) { |
| 5306 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5307 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5308 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5309 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5310 | seq_printf(seq, "\n"); |
| 5311 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | seq_printf(seq, "\n"); |
| 5315 | } |
| 5316 | mddev_unlock(mddev); |
| 5317 | |
| 5318 | return 0; |
| 5319 | } |
| 5320 | |
| 5321 | static struct seq_operations md_seq_ops = { |
| 5322 | .start = md_seq_start, |
| 5323 | .next = md_seq_next, |
| 5324 | .stop = md_seq_stop, |
| 5325 | .show = md_seq_show, |
| 5326 | }; |
| 5327 | |
| 5328 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5329 | { |
| 5330 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5331 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5332 | if (mi == NULL) |
| 5333 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | |
| 5335 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5336 | if (error) |
| 5337 | kfree(mi); |
| 5338 | else { |
| 5339 | struct seq_file *p = file->private_data; |
| 5340 | p->private = mi; |
| 5341 | mi->event = atomic_read(&md_event_count); |
| 5342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | return error; |
| 5344 | } |
| 5345 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5346 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5347 | { |
| 5348 | struct seq_file *m = filp->private_data; |
| 5349 | struct mdstat_info *mi = m->private; |
| 5350 | int mask; |
| 5351 | |
| 5352 | poll_wait(filp, &md_event_waiters, wait); |
| 5353 | |
| 5354 | /* always allow read */ |
| 5355 | mask = POLLIN | POLLRDNORM; |
| 5356 | |
| 5357 | if (mi->event != atomic_read(&md_event_count)) |
| 5358 | mask |= POLLERR | POLLPRI; |
| 5359 | return mask; |
| 5360 | } |
| 5361 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5362 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5363 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | .open = md_seq_open, |
| 5365 | .read = seq_read, |
| 5366 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5367 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5368 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | }; |
| 5370 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5371 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5374 | list_add_tail(&p->list, &pers_list); |
| 5375 | 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] | 5376 | spin_unlock(&pers_lock); |
| 5377 | return 0; |
| 5378 | } |
| 5379 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5380 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5382 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5384 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | spin_unlock(&pers_lock); |
| 5386 | return 0; |
| 5387 | } |
| 5388 | |
| 5389 | static int is_mddev_idle(mddev_t *mddev) |
| 5390 | { |
| 5391 | mdk_rdev_t * rdev; |
| 5392 | struct list_head *tmp; |
| 5393 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5394 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | |
| 5396 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5397 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5399 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5400 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5402 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5403 | * as sync_io is counted when a request starts, and |
| 5404 | * disk_stats is counted when it completes. |
| 5405 | * So resync activity will cause curr_events to be smaller than |
| 5406 | * when there was no such activity. |
| 5407 | * non-sync IO will cause disk_stat to increase without |
| 5408 | * increasing sync_io so curr_events will (eventually) |
| 5409 | * be larger than it was before. Once it becomes |
| 5410 | * substantially larger, the test below will cause |
| 5411 | * the array to appear non-idle, and resync will slow |
| 5412 | * down. |
| 5413 | * If there is a lot of outstanding resync activity when |
| 5414 | * we set last_event to curr_events, then all that activity |
| 5415 | * completing might cause the array to appear non-idle |
| 5416 | * and resync will be slowed down even though there might |
| 5417 | * not have been non-resync activity. This will only |
| 5418 | * happen once though. 'last_events' will soon reflect |
| 5419 | * the state where there is little or no outstanding |
| 5420 | * resync requests, and further resync activity will |
| 5421 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5422 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5424 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | rdev->last_events = curr_events; |
| 5426 | idle = 0; |
| 5427 | } |
| 5428 | } |
| 5429 | return idle; |
| 5430 | } |
| 5431 | |
| 5432 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5433 | { |
| 5434 | /* another "blocks" (512byte) blocks have been synced */ |
| 5435 | atomic_sub(blocks, &mddev->recovery_active); |
| 5436 | wake_up(&mddev->recovery_wait); |
| 5437 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5438 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | md_wakeup_thread(mddev->thread); |
| 5440 | // stop recovery, signal do_sync .... |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5445 | /* md_write_start(mddev, bi) |
| 5446 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5447 | * in superblock) before writing, schedule a superblock update |
| 5448 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5449 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5450 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5452 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5453 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5454 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5455 | BUG_ON(mddev->ro == 1); |
| 5456 | if (mddev->ro == 2) { |
| 5457 | /* need to switch to read/write */ |
| 5458 | mddev->ro = 0; |
| 5459 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5460 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5461 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5462 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5463 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5464 | if (mddev->safemode == 1) |
| 5465 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5466 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5467 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5468 | if (mddev->in_sync) { |
| 5469 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5470 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5471 | md_wakeup_thread(mddev->thread); |
| 5472 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5473 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5474 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5475 | } |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5476 | wait_event(mddev->sb_wait, |
| 5477 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5478 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | void md_write_end(mddev_t *mddev) |
| 5482 | { |
| 5483 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5484 | if (mddev->safemode == 2) |
| 5485 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5486 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5488 | } |
| 5489 | } |
| 5490 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5491 | /* md_allow_write(mddev) |
| 5492 | * Calling this ensures that the array is marked 'active' so that writes |
| 5493 | * may proceed without blocking. It is important to call this before |
| 5494 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5495 | * Must be called with mddev_lock held. |
| 5496 | */ |
| 5497 | void md_allow_write(mddev_t *mddev) |
| 5498 | { |
| 5499 | if (!mddev->pers) |
| 5500 | return; |
| 5501 | if (mddev->ro) |
| 5502 | return; |
| 5503 | |
| 5504 | spin_lock_irq(&mddev->write_lock); |
| 5505 | if (mddev->in_sync) { |
| 5506 | mddev->in_sync = 0; |
| 5507 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5508 | if (mddev->safemode_delay && |
| 5509 | mddev->safemode == 0) |
| 5510 | mddev->safemode = 1; |
| 5511 | spin_unlock_irq(&mddev->write_lock); |
| 5512 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5513 | |
| 5514 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 5515 | /* wait for the dirty state to be recorded in the metadata */ |
| 5516 | wait_event(mddev->sb_wait, |
| 5517 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5518 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5519 | } else |
| 5520 | spin_unlock_irq(&mddev->write_lock); |
| 5521 | } |
| 5522 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | #define SYNC_MARKS 10 |
| 5525 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5526 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | { |
| 5528 | mddev_t *mddev2; |
| 5529 | unsigned int currspeed = 0, |
| 5530 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5531 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | unsigned long mark[SYNC_MARKS]; |
| 5533 | sector_t mark_cnt[SYNC_MARKS]; |
| 5534 | int last_mark,m; |
| 5535 | struct list_head *tmp; |
| 5536 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5537 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5538 | struct list_head *rtmp; |
| 5539 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5540 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | |
| 5542 | /* just incase thread restarts... */ |
| 5543 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5544 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5545 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5546 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5548 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5549 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5550 | desc = "data-check"; |
| 5551 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5552 | desc = "requested-resync"; |
| 5553 | else |
| 5554 | desc = "resync"; |
| 5555 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5556 | desc = "reshape"; |
| 5557 | else |
| 5558 | desc = "recovery"; |
| 5559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | /* we overload curr_resync somewhat here. |
| 5561 | * 0 == not engaged in resync at all |
| 5562 | * 2 == checking that there is no conflict with another sync |
| 5563 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5564 | * commense |
| 5565 | * other == active in resync - this many blocks |
| 5566 | * |
| 5567 | * Before starting a resync we must have set curr_resync to |
| 5568 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5569 | * less than ours. When we find one that is the same or higher |
| 5570 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5571 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5572 | * This will mean we have to start checking from the beginning again. |
| 5573 | * |
| 5574 | */ |
| 5575 | |
| 5576 | do { |
| 5577 | mddev->curr_resync = 2; |
| 5578 | |
| 5579 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5580 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5581 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | goto skip; |
| 5583 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5584 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | if (mddev2 == mddev) |
| 5586 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5587 | if (!mddev->parallel_resync |
| 5588 | && mddev2->curr_resync |
| 5589 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | DEFINE_WAIT(wq); |
| 5591 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5592 | /* arbitrarily yield */ |
| 5593 | mddev->curr_resync = 1; |
| 5594 | wake_up(&resync_wait); |
| 5595 | } |
| 5596 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5597 | /* no need to wait here, we can wait the next |
| 5598 | * time 'round when curr_resync == 2 |
| 5599 | */ |
| 5600 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5601 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5602 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5603 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5604 | printk(KERN_INFO "md: delaying %s of %s" |
| 5605 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5607 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | mddev_put(mddev2); |
| 5609 | schedule(); |
| 5610 | finish_wait(&resync_wait, &wq); |
| 5611 | goto try_again; |
| 5612 | } |
| 5613 | finish_wait(&resync_wait, &wq); |
| 5614 | } |
| 5615 | } |
| 5616 | } while (mddev->curr_resync < 2); |
| 5617 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5618 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5619 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5621 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | */ |
| 5623 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5624 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5625 | /* we don't use the checkpoint if there's a bitmap */ |
| 5626 | if (!mddev->bitmap && |
| 5627 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5628 | j = mddev->recovery_cp; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5629 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5630 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5631 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | /* recovery follows the physical size of devices */ |
| 5633 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5634 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5635 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5636 | if (rdev->raid_disk >= 0 && |
| 5637 | !test_bit(Faulty, &rdev->flags) && |
| 5638 | !test_bit(In_sync, &rdev->flags) && |
| 5639 | rdev->recovery_offset < j) |
| 5640 | j = rdev->recovery_offset; |
| 5641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5643 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5644 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5645 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5646 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5647 | "(but not more than %d KB/sec) for %s.\n", |
| 5648 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | |
| 5650 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5651 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5652 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5654 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5655 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | } |
| 5657 | last_mark = 0; |
| 5658 | mddev->resync_mark = mark[last_mark]; |
| 5659 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5660 | |
| 5661 | /* |
| 5662 | * Tune reconstruction: |
| 5663 | */ |
| 5664 | window = 32*(PAGE_SIZE/512); |
| 5665 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5666 | window/2,(unsigned long long) max_sectors/2); |
| 5667 | |
| 5668 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | last_check = 0; |
| 5670 | |
| 5671 | if (j>2) { |
| 5672 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5673 | "md: resuming %s of %s from checkpoint.\n", |
| 5674 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | mddev->curr_resync = j; |
| 5676 | } |
| 5677 | |
| 5678 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5679 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5681 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5682 | if (j >= mddev->resync_max) { |
| 5683 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5684 | wait_event(mddev->recovery_wait, |
| 5685 | mddev->resync_max > j |
| 5686 | || kthread_should_stop()); |
| 5687 | } |
| 5688 | if (kthread_should_stop()) |
| 5689 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5690 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5691 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5692 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5693 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | goto out; |
| 5695 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5696 | |
| 5697 | if (!skipped) { /* actual IO requested */ |
| 5698 | io_sectors += sectors; |
| 5699 | atomic_add(sectors, &mddev->recovery_active); |
| 5700 | } |
| 5701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | j += sectors; |
| 5703 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5704 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5705 | if (last_check == 0) |
| 5706 | /* this is the earliers that rebuilt will be |
| 5707 | * visible in /proc/mdstat |
| 5708 | */ |
| 5709 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5710 | |
| 5711 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | continue; |
| 5713 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5714 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5716 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | break; |
| 5718 | |
| 5719 | repeat: |
| 5720 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5721 | /* step marks */ |
| 5722 | int next = (last_mark+1) % SYNC_MARKS; |
| 5723 | |
| 5724 | mddev->resync_mark = mark[next]; |
| 5725 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5726 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5727 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | last_mark = next; |
| 5729 | } |
| 5730 | |
| 5731 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5732 | if (kthread_should_stop()) |
| 5733 | goto interrupted; |
| 5734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
| 5736 | /* |
| 5737 | * this loop exits only if either when we are slower than |
| 5738 | * the 'hard' speed limit, or the system was IO-idle for |
| 5739 | * a jiffy. |
| 5740 | * the system might be non-idle CPU-wise, but we only care |
| 5741 | * about not overloading the IO subsystem. (things like an |
| 5742 | * e2fsck being done on the RAID array should execute fast) |
| 5743 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5744 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | cond_resched(); |
| 5746 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5747 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5748 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5750 | if (currspeed > speed_min(mddev)) { |
| 5751 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5753 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | goto repeat; |
| 5755 | } |
| 5756 | } |
| 5757 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5758 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | /* |
| 5760 | * this also signals 'finished resyncing' to md_stop |
| 5761 | */ |
| 5762 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5763 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | |
| 5765 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5766 | |
| 5767 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5768 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5770 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5771 | mddev->curr_resync > 2) { |
| 5772 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5773 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5774 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5775 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5776 | "md: checkpointing %s of %s.\n", |
| 5777 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5778 | mddev->recovery_cp = mddev->curr_resync; |
| 5779 | } |
| 5780 | } else |
| 5781 | mddev->recovery_cp = MaxSector; |
| 5782 | } else { |
| 5783 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5784 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5785 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5786 | if (rdev->raid_disk >= 0 && |
| 5787 | !test_bit(Faulty, &rdev->flags) && |
| 5788 | !test_bit(In_sync, &rdev->flags) && |
| 5789 | rdev->recovery_offset < mddev->curr_resync) |
| 5790 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5793 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | skip: |
| 5796 | mddev->curr_resync = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5797 | mddev->resync_max = MaxSector; |
| 5798 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | wake_up(&resync_wait); |
| 5800 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5801 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5802 | return; |
| 5803 | |
| 5804 | interrupted: |
| 5805 | /* |
| 5806 | * got a signal, exit. |
| 5807 | */ |
| 5808 | printk(KERN_INFO |
| 5809 | "md: md_do_sync() got signal ... exiting\n"); |
| 5810 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5811 | goto out; |
| 5812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5814 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | |
| 5816 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5817 | static int remove_and_add_spares(mddev_t *mddev) |
| 5818 | { |
| 5819 | mdk_rdev_t *rdev; |
| 5820 | struct list_head *rtmp; |
| 5821 | int spares = 0; |
| 5822 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5823 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5824 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5825 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5826 | (test_bit(Faulty, &rdev->flags) || |
| 5827 | ! test_bit(In_sync, &rdev->flags)) && |
| 5828 | atomic_read(&rdev->nr_pending)==0) { |
| 5829 | if (mddev->pers->hot_remove_disk( |
| 5830 | mddev, rdev->raid_disk)==0) { |
| 5831 | char nm[20]; |
| 5832 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5833 | sysfs_remove_link(&mddev->kobj, nm); |
| 5834 | rdev->raid_disk = -1; |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5839 | rdev_for_each(rdev, rtmp, mddev) { |
| 5840 | if (rdev->raid_disk >= 0 && |
| 5841 | !test_bit(In_sync, &rdev->flags)) |
| 5842 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5843 | if (rdev->raid_disk < 0 |
| 5844 | && !test_bit(Faulty, &rdev->flags)) { |
| 5845 | rdev->recovery_offset = 0; |
| 5846 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 5847 | char nm[20]; |
| 5848 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5849 | if (sysfs_create_link(&mddev->kobj, |
| 5850 | &rdev->kobj, nm)) |
| 5851 | printk(KERN_WARNING |
| 5852 | "md: cannot register " |
| 5853 | "%s for %s\n", |
| 5854 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5855 | spares++; |
| 5856 | md_new_event(mddev); |
| 5857 | } else |
| 5858 | break; |
| 5859 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5860 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5861 | } |
| 5862 | return spares; |
| 5863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | /* |
| 5865 | * This routine is regularly called by all per-raid-array threads to |
| 5866 | * deal with generic issues like resync and super-block update. |
| 5867 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5868 | * need this as they never do any recovery or update the superblock. |
| 5869 | * |
| 5870 | * It does not do any resync itself, but rather "forks" off other threads |
| 5871 | * to do that as needed. |
| 5872 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5873 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5874 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | * and wakeups up this thread which will reap the thread and finish up. |
| 5876 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5877 | * |
| 5878 | * The overall approach is: |
| 5879 | * 1/ if the superblock needs updating, update it. |
| 5880 | * 2/ If a recovery thread is running, don't do anything else. |
| 5881 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5882 | * 4/ If there are any faulty devices, remove them. |
| 5883 | * 5/ If array is degraded, try to add spares devices |
| 5884 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5885 | */ |
| 5886 | void md_check_recovery(mddev_t *mddev) |
| 5887 | { |
| 5888 | mdk_rdev_t *rdev; |
| 5889 | struct list_head *rtmp; |
| 5890 | |
| 5891 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5892 | if (mddev->bitmap) |
| 5893 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | |
| 5895 | if (mddev->ro) |
| 5896 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5897 | |
| 5898 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5899 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5900 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 5901 | mdname(mddev)); |
| 5902 | mddev->safemode = 2; |
| 5903 | } |
| 5904 | flush_signals(current); |
| 5905 | } |
| 5906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5908 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5910 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5911 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5912 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 5913 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | )) |
| 5915 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5916 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5917 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5918 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5919 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5920 | if (!mddev->external) { |
| 5921 | spin_lock_irq(&mddev->write_lock); |
| 5922 | if (mddev->safemode && |
| 5923 | !atomic_read(&mddev->writes_pending) && |
| 5924 | !mddev->in_sync && |
| 5925 | mddev->recovery_cp == MaxSector) { |
| 5926 | mddev->in_sync = 1; |
| 5927 | if (mddev->persistent) |
| 5928 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5929 | } |
| 5930 | if (mddev->safemode == 1) |
| 5931 | mddev->safemode = 0; |
| 5932 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5933 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5934 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5935 | if (mddev->flags) |
| 5936 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5937 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 5940 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 5941 | /* resync/recovery still happening */ |
| 5942 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5943 | goto unlock; |
| 5944 | } |
| 5945 | if (mddev->sync_thread) { |
| 5946 | /* resync has finished, collect result */ |
| 5947 | md_unregister_thread(mddev->sync_thread); |
| 5948 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5949 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | /* success...*/ |
| 5951 | /* activate any spares */ |
| 5952 | mddev->pers->spare_active(mddev); |
| 5953 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5954 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5955 | |
| 5956 | /* if array is no-longer degraded, then any saved_raid_disk |
| 5957 | * information must be scrapped |
| 5958 | */ |
| 5959 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5960 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5961 | rdev->saved_raid_disk = -1; |
| 5962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | mddev->recovery = 0; |
| 5964 | /* flag recovery needed just to double check */ |
| 5965 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5966 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | goto unlock; |
| 5968 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5969 | /* Clear some bits that don't mean anything, but |
| 5970 | * might be left set |
| 5971 | */ |
| 5972 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5973 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5974 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5976 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 5977 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | /* no recovery is running. |
| 5979 | * remove any failed drives, then |
| 5980 | * add spares if possible. |
| 5981 | * Spare are also removed and re-added, to allow |
| 5982 | * the personality to fail the re-add. |
| 5983 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5985 | if (mddev->reshape_position != MaxSector) { |
| 5986 | if (mddev->pers->check_reshape(mddev) != 0) |
| 5987 | /* Cannot proceed */ |
| 5988 | goto unlock; |
| 5989 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5990 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5991 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5992 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5993 | } else if (mddev->recovery_cp < MaxSector) { |
| 5994 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5995 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5996 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | if (mddev->pers->sync_request) { |
| 6000 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6001 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6002 | /* We are adding a device or devices to an array |
| 6003 | * which has the bitmap stored on all devices. |
| 6004 | * So make sure all bitmap pages get written |
| 6005 | */ |
| 6006 | bitmap_write_all(mddev->bitmap); |
| 6007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6009 | mddev, |
| 6010 | "%s_resync"); |
| 6011 | if (!mddev->sync_thread) { |
| 6012 | printk(KERN_ERR "%s: could not start resync" |
| 6013 | " thread...\n", |
| 6014 | mdname(mddev)); |
| 6015 | /* leave the spares where they are, it shouldn't hurt */ |
| 6016 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6017 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6019 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | } |
| 6021 | unlock: |
| 6022 | mddev_unlock(mddev); |
| 6023 | } |
| 6024 | } |
| 6025 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6026 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6027 | { |
| 6028 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6029 | wait_event_timeout(rdev->blocked_wait, |
| 6030 | !test_bit(Blocked, &rdev->flags), |
| 6031 | msecs_to_jiffies(5000)); |
| 6032 | rdev_dec_pending(rdev, mddev); |
| 6033 | } |
| 6034 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6035 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6036 | static int md_notify_reboot(struct notifier_block *this, |
| 6037 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | { |
| 6039 | struct list_head *tmp; |
| 6040 | mddev_t *mddev; |
| 6041 | |
| 6042 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6043 | |
| 6044 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6045 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6046 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6047 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6049 | mddev_unlock(mddev); |
| 6050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | /* |
| 6052 | * certain more exotic SCSI devices are known to be |
| 6053 | * volatile wrt too early system reboots. While the |
| 6054 | * right place to handle this issue is the given |
| 6055 | * driver, we do want to have a safe RAID driver ... |
| 6056 | */ |
| 6057 | mdelay(1000*1); |
| 6058 | } |
| 6059 | return NOTIFY_DONE; |
| 6060 | } |
| 6061 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6062 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | .notifier_call = md_notify_reboot, |
| 6064 | .next = NULL, |
| 6065 | .priority = INT_MAX, /* before any real devices */ |
| 6066 | }; |
| 6067 | |
| 6068 | static void md_geninit(void) |
| 6069 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6071 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6072 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6075 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | if (register_blkdev(MAJOR_NR, "md")) |
| 6078 | return -1; |
| 6079 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6080 | unregister_blkdev(MAJOR_NR, "md"); |
| 6081 | return -1; |
| 6082 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6083 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6084 | md_probe, NULL, NULL); |
| 6085 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | md_probe, NULL, NULL); |
| 6087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6089 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | |
| 6091 | md_geninit(); |
| 6092 | return (0); |
| 6093 | } |
| 6094 | |
| 6095 | |
| 6096 | #ifndef MODULE |
| 6097 | |
| 6098 | /* |
| 6099 | * Searches all registered partitions for autorun RAID arrays |
| 6100 | * at boot time. |
| 6101 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6102 | |
| 6103 | static LIST_HEAD(all_detected_devices); |
| 6104 | struct detected_devices_node { |
| 6105 | struct list_head list; |
| 6106 | dev_t dev; |
| 6107 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | |
| 6109 | void md_autodetect_dev(dev_t dev) |
| 6110 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6111 | struct detected_devices_node *node_detected_dev; |
| 6112 | |
| 6113 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6114 | if (node_detected_dev) { |
| 6115 | node_detected_dev->dev = dev; |
| 6116 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6117 | } else { |
| 6118 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6119 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | |
| 6124 | static void autostart_arrays(int part) |
| 6125 | { |
| 6126 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6127 | struct detected_devices_node *node_detected_dev; |
| 6128 | dev_t dev; |
| 6129 | int i_scanned, i_passed; |
| 6130 | |
| 6131 | i_scanned = 0; |
| 6132 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | |
| 6134 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6135 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6136 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6137 | i_scanned++; |
| 6138 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6139 | struct detected_devices_node, list); |
| 6140 | list_del(&node_detected_dev->list); |
| 6141 | dev = node_detected_dev->dev; |
| 6142 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6143 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | if (IS_ERR(rdev)) |
| 6145 | continue; |
| 6146 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6147 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | MD_BUG(); |
| 6149 | continue; |
| 6150 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6151 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6153 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6155 | |
| 6156 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6157 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | |
| 6159 | autorun_devices(part); |
| 6160 | } |
| 6161 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6162 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | |
| 6164 | static __exit void md_exit(void) |
| 6165 | { |
| 6166 | mddev_t *mddev; |
| 6167 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6168 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6169 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6170 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | |
| 6172 | unregister_blkdev(MAJOR_NR,"md"); |
| 6173 | unregister_blkdev(mdp_major, "mdp"); |
| 6174 | unregister_reboot_notifier(&md_notifier); |
| 6175 | unregister_sysctl_table(raid_table_header); |
| 6176 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6177 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | struct gendisk *disk = mddev->gendisk; |
| 6179 | if (!disk) |
| 6180 | continue; |
| 6181 | export_array(mddev); |
| 6182 | del_gendisk(disk); |
| 6183 | put_disk(disk); |
| 6184 | mddev->gendisk = NULL; |
| 6185 | mddev_put(mddev); |
| 6186 | } |
| 6187 | } |
| 6188 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6189 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | module_exit(md_exit) |
| 6191 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6192 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6193 | { |
| 6194 | return sprintf(buffer, "%d", start_readonly); |
| 6195 | } |
| 6196 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6197 | { |
| 6198 | char *e; |
| 6199 | int num = simple_strtoul(val, &e, 10); |
| 6200 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6201 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6202 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6203 | } |
| 6204 | return -EINVAL; |
| 6205 | } |
| 6206 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6207 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6208 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6209 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | EXPORT_SYMBOL(register_md_personality); |
| 6212 | EXPORT_SYMBOL(unregister_md_personality); |
| 6213 | EXPORT_SYMBOL(md_error); |
| 6214 | EXPORT_SYMBOL(md_done_sync); |
| 6215 | EXPORT_SYMBOL(md_write_start); |
| 6216 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | EXPORT_SYMBOL(md_register_thread); |
| 6218 | EXPORT_SYMBOL(md_unregister_thread); |
| 6219 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | EXPORT_SYMBOL(md_check_recovery); |
| 6221 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6222 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6223 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |