Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 37 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/linkage.h> |
| 39 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 43 | #include <linux/poll.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 44 | #include <linux/mutex.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 45 | #include <linux/ctype.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 46 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <linux/init.h> |
| 49 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 50 | #include <linux/file.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_KMOD |
| 53 | #include <linux/kmod.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <asm/unaligned.h> |
| 57 | |
| 58 | #define MAJOR_NR MD_MAJOR |
| 59 | #define MD_DRIVER |
| 60 | |
| 61 | /* 63 partitions with the alternate major number (mdp) */ |
| 62 | #define MdpMinorShift 6 |
| 63 | |
| 64 | #define DEBUG 0 |
| 65 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 66 | |
| 67 | |
| 68 | #ifndef MODULE |
| 69 | static void autostart_arrays (int part); |
| 70 | #endif |
| 71 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 72 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(pers_lock); |
| 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | static void md_print_devices(void); |
| 76 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 77 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 78 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 79 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
| 112 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 113 | .procname = "speed_limit_min", |
| 114 | .data = &sysctl_speed_limit_min, |
| 115 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 116 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .proc_handler = &proc_dointvec, |
| 118 | }, |
| 119 | { |
| 120 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .proc_handler = &proc_dointvec, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
| 132 | .ctl_name = DEV_RAID, |
| 133 | .procname = "raid", |
| 134 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 135 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .child = raid_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static ctl_table raid_root_table[] = { |
| 142 | { |
| 143 | .ctl_name = CTL_DEV, |
| 144 | .procname = "dev", |
| 145 | .maxlen = 0, |
| 146 | .mode = 0555, |
| 147 | .child = raid_dir_table, |
| 148 | }, |
| 149 | { .ctl_name = 0 } |
| 150 | }; |
| 151 | |
| 152 | static struct block_device_operations md_fops; |
| 153 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 154 | static int start_readonly; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | * We have a system wide 'event count' that is incremented |
| 158 | * on any 'interesting' event, and readers of /proc/mdstat |
| 159 | * can use 'poll' or 'select' to find out when the event |
| 160 | * count increases. |
| 161 | * |
| 162 | * Events are: |
| 163 | * start array, stop array, error, add device, remove device, |
| 164 | * start build, activate spare |
| 165 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 166 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 168 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
| 172 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 174 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 175 | /* Alternate version that can be called from interrupts |
| 176 | * when calling sysfs_notify isn't needed. |
| 177 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 178 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 179 | { |
| 180 | atomic_inc(&md_event_count); |
| 181 | wake_up(&md_event_waiters); |
| 182 | } |
| 183 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 184 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * Enables to iterate over all existing md arrays |
| 186 | * all_mddevs_lock protects this list. |
| 187 | */ |
| 188 | static LIST_HEAD(all_mddevs); |
| 189 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | * iterates through all used mddevs in the system. |
| 194 | * We take care to grab the all_mddevs_lock whenever navigating |
| 195 | * the list, and to always hold a refcount when unlocked. |
| 196 | * Any code which breaks out of this loop while own |
| 197 | * a reference to the current mddev and must mddev_put it. |
| 198 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 199 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | \ |
| 201 | for (({ spin_lock(&all_mddevs_lock); \ |
| 202 | tmp = all_mddevs.next; \ |
| 203 | mddev = NULL;}); \ |
| 204 | ({ if (tmp != &all_mddevs) \ |
| 205 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 206 | spin_unlock(&all_mddevs_lock); \ |
| 207 | if (mddev) mddev_put(mddev); \ |
| 208 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 209 | tmp != &all_mddevs;}); \ |
| 210 | ({ spin_lock(&all_mddevs_lock); \ |
| 211 | tmp = tmp->next;}) \ |
| 212 | ) |
| 213 | |
| 214 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 215 | static int md_fail_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 217 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 222 | { |
| 223 | atomic_inc(&mddev->active); |
| 224 | return mddev; |
| 225 | } |
| 226 | |
| 227 | static void mddev_put(mddev_t *mddev) |
| 228 | { |
| 229 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 230 | return; |
| 231 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 232 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 233 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 234 | blk_cleanup_queue(mddev->queue); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 235 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 236 | } else |
| 237 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static mddev_t * mddev_find(dev_t unit) |
| 241 | { |
| 242 | mddev_t *mddev, *new = NULL; |
| 243 | |
| 244 | retry: |
| 245 | spin_lock(&all_mddevs_lock); |
| 246 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 247 | if (mddev->unit == unit) { |
| 248 | mddev_get(mddev); |
| 249 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 250 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return mddev; |
| 252 | } |
| 253 | |
| 254 | if (new) { |
| 255 | list_add(&new->all_mddevs, &all_mddevs); |
| 256 | spin_unlock(&all_mddevs_lock); |
| 257 | return new; |
| 258 | } |
| 259 | spin_unlock(&all_mddevs_lock); |
| 260 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 261 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (!new) |
| 263 | return NULL; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | new->unit = unit; |
| 266 | if (MAJOR(unit) == MD_MAJOR) |
| 267 | new->md_minor = MINOR(unit); |
| 268 | else |
| 269 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 270 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 271 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | INIT_LIST_HEAD(&new->disks); |
| 273 | INIT_LIST_HEAD(&new->all_mddevs); |
| 274 | init_timer(&new->safemode_timer); |
| 275 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 276 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 277 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 278 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 279 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 280 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 281 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 282 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 285 | if (!new->queue) { |
| 286 | kfree(new); |
| 287 | return NULL; |
| 288 | } |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 289 | /* Can be unlocked because the queue is new: no concurrency */ |
| 290 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | blk_queue_make_request(new->queue, md_fail_request); |
| 293 | |
| 294 | goto retry; |
| 295 | } |
| 296 | |
| 297 | static inline int mddev_lock(mddev_t * mddev) |
| 298 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 299 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | static inline int mddev_trylock(mddev_t * mddev) |
| 303 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 304 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static inline void mddev_unlock(mddev_t * mddev) |
| 308 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 309 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 311 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 314 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | mdk_rdev_t * rdev; |
| 317 | struct list_head *tmp; |
| 318 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 319 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (rdev->desc_nr == nr) |
| 321 | return rdev; |
| 322 | } |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 327 | { |
| 328 | struct list_head *tmp; |
| 329 | mdk_rdev_t *rdev; |
| 330 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 331 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (rdev->bdev->bd_dev == dev) |
| 333 | return rdev; |
| 334 | } |
| 335 | return NULL; |
| 336 | } |
| 337 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 338 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 339 | { |
| 340 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 341 | list_for_each_entry(pers, &pers_list, list) { |
| 342 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 343 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 344 | if (strcmp(pers->name, clevel)==0) |
| 345 | return pers; |
| 346 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 347 | return NULL; |
| 348 | } |
| 349 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 350 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
| 352 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 353 | return MD_NEW_SIZE_BLOCKS(size); |
| 354 | } |
| 355 | |
| 356 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 357 | { |
| 358 | sector_t size; |
| 359 | |
| 360 | size = rdev->sb_offset; |
| 361 | |
| 362 | if (chunk_size) |
| 363 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 364 | return size; |
| 365 | } |
| 366 | |
| 367 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 368 | { |
| 369 | if (rdev->sb_page) |
| 370 | MD_BUG(); |
| 371 | |
| 372 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 373 | if (!rdev->sb_page) { |
| 374 | printk(KERN_ALERT "md: out of memory.\n"); |
| 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 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 1889 | if (!err) |
| 1890 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1891 | return err ? err : len; |
| 1892 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1893 | static struct rdev_sysfs_entry rdev_state = |
| 1894 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1895 | |
| 1896 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1897 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1898 | { |
| 1899 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1900 | } |
| 1901 | |
| 1902 | static ssize_t |
| 1903 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1904 | { |
| 1905 | char *e; |
| 1906 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1907 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1908 | atomic_set(&rdev->corrected_errors, n); |
| 1909 | return len; |
| 1910 | } |
| 1911 | return -EINVAL; |
| 1912 | } |
| 1913 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1914 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1915 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1916 | static ssize_t |
| 1917 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1918 | { |
| 1919 | if (rdev->raid_disk < 0) |
| 1920 | return sprintf(page, "none\n"); |
| 1921 | else |
| 1922 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1923 | } |
| 1924 | |
| 1925 | static ssize_t |
| 1926 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1927 | { |
| 1928 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1929 | int err; |
| 1930 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1931 | int slot = simple_strtoul(buf, &e, 10); |
| 1932 | if (strncmp(buf, "none", 4)==0) |
| 1933 | slot = -1; |
| 1934 | else if (e==buf || (*e && *e!= '\n')) |
| 1935 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1936 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1937 | /* Setting 'slot' on an active array requires also |
| 1938 | * updating the 'rd%d' link, and communicating |
| 1939 | * with the personality with ->hot_*_disk. |
| 1940 | * For now we only support removing |
| 1941 | * failed/spare devices. This normally happens automatically, |
| 1942 | * but not when the metadata is externally managed. |
| 1943 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 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); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1957 | } else if (rdev->mddev->pers) { |
| 1958 | mdk_rdev_t *rdev2; |
| 1959 | struct list_head *tmp; |
| 1960 | /* Activating a spare .. or possibly reactivating |
| 1961 | * if we every get bitmaps working here. |
| 1962 | */ |
| 1963 | |
| 1964 | if (rdev->raid_disk != -1) |
| 1965 | return -EBUSY; |
| 1966 | |
| 1967 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1968 | return -EINVAL; |
| 1969 | |
| 1970 | rdev_for_each(rdev2, tmp, rdev->mddev) |
| 1971 | if (rdev2->raid_disk == slot) |
| 1972 | return -EEXIST; |
| 1973 | |
| 1974 | rdev->raid_disk = slot; |
| 1975 | if (test_bit(In_sync, &rdev->flags)) |
| 1976 | rdev->saved_raid_disk = slot; |
| 1977 | else |
| 1978 | rdev->saved_raid_disk = -1; |
| 1979 | err = rdev->mddev->pers-> |
| 1980 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1981 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1982 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1983 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 1984 | } else |
| 1985 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1986 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1987 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 1988 | printk(KERN_WARNING |
| 1989 | "md: cannot register " |
| 1990 | "%s for %s\n", |
| 1991 | nm, mdname(rdev->mddev)); |
| 1992 | |
| 1993 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1994 | } else { |
| 1995 | if (slot >= rdev->mddev->raid_disks) |
| 1996 | return -ENOSPC; |
| 1997 | rdev->raid_disk = slot; |
| 1998 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1999 | clear_bit(Faulty, &rdev->flags); |
| 2000 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2001 | set_bit(In_sync, &rdev->flags); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 2002 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2003 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2004 | return len; |
| 2005 | } |
| 2006 | |
| 2007 | |
| 2008 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2009 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2010 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2011 | static ssize_t |
| 2012 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2013 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2014 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | static ssize_t |
| 2018 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2019 | { |
| 2020 | char *e; |
| 2021 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2022 | if (e==buf || (*e && *e != '\n')) |
| 2023 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2024 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2025 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2026 | if (rdev->size && rdev->mddev->external) |
| 2027 | /* Must set offset before size, so overlap checks |
| 2028 | * can be sane */ |
| 2029 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2030 | rdev->data_offset = offset; |
| 2031 | return len; |
| 2032 | } |
| 2033 | |
| 2034 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2035 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2036 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2037 | static ssize_t |
| 2038 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2039 | { |
| 2040 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2041 | } |
| 2042 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2043 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2044 | { |
| 2045 | /* check if two start/length pairs overlap */ |
| 2046 | if (s1+l1 <= s2) |
| 2047 | return 0; |
| 2048 | if (s2+l2 <= s1) |
| 2049 | return 0; |
| 2050 | return 1; |
| 2051 | } |
| 2052 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2053 | static ssize_t |
| 2054 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2055 | { |
| 2056 | char *e; |
| 2057 | unsigned long long size = simple_strtoull(buf, &e, 10); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2058 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2059 | mddev_t *my_mddev = rdev->mddev; |
| 2060 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2061 | if (e==buf || (*e && *e != '\n')) |
| 2062 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2063 | if (my_mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2064 | return -EBUSY; |
| 2065 | rdev->size = size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2066 | if (size > oldsize && rdev->mddev->external) { |
| 2067 | /* need to check that all other rdevs with the same ->bdev |
| 2068 | * do not overlap. We need to unlock the mddev to avoid |
| 2069 | * a deadlock. We have already changed rdev->size, and if |
| 2070 | * we have to change it back, we will have the lock again. |
| 2071 | */ |
| 2072 | mddev_t *mddev; |
| 2073 | int overlap = 0; |
| 2074 | struct list_head *tmp, *tmp2; |
| 2075 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2076 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2077 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2078 | mdk_rdev_t *rdev2; |
| 2079 | |
| 2080 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2081 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2082 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2083 | (rdev->bdev == rdev2->bdev && |
| 2084 | rdev != rdev2 && |
| 2085 | overlaps(rdev->data_offset, rdev->size, |
| 2086 | rdev2->data_offset, rdev2->size))) { |
| 2087 | overlap = 1; |
| 2088 | break; |
| 2089 | } |
| 2090 | mddev_unlock(mddev); |
| 2091 | if (overlap) { |
| 2092 | mddev_put(mddev); |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2096 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2097 | if (overlap) { |
| 2098 | /* Someone else could have slipped in a size |
| 2099 | * change here, but doing so is just silly. |
| 2100 | * We put oldsize back because we *know* it is |
| 2101 | * safe, and trust userspace not to race with |
| 2102 | * itself |
| 2103 | */ |
| 2104 | rdev->size = oldsize; |
| 2105 | return -EBUSY; |
| 2106 | } |
| 2107 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2108 | if (size < my_mddev->size || my_mddev->size == 0) |
| 2109 | my_mddev->size = size; |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2110 | return len; |
| 2111 | } |
| 2112 | |
| 2113 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2114 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2115 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2116 | static struct attribute *rdev_default_attrs[] = { |
| 2117 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2118 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2119 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2120 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2121 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2122 | NULL, |
| 2123 | }; |
| 2124 | static ssize_t |
| 2125 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2126 | { |
| 2127 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2128 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2129 | mddev_t *mddev = rdev->mddev; |
| 2130 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2131 | |
| 2132 | if (!entry->show) |
| 2133 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2134 | |
| 2135 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2136 | if (!rv) { |
| 2137 | if (rdev->mddev == NULL) |
| 2138 | rv = -EBUSY; |
| 2139 | else |
| 2140 | rv = entry->show(rdev, page); |
| 2141 | mddev_unlock(mddev); |
| 2142 | } |
| 2143 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | static ssize_t |
| 2147 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2148 | const char *page, size_t length) |
| 2149 | { |
| 2150 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2151 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2152 | ssize_t rv; |
| 2153 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2154 | |
| 2155 | if (!entry->store) |
| 2156 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2157 | if (!capable(CAP_SYS_ADMIN)) |
| 2158 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2159 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2160 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2161 | if (rdev->mddev == NULL) |
| 2162 | rv = -EBUSY; |
| 2163 | else |
| 2164 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2165 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2166 | } |
| 2167 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | static void rdev_free(struct kobject *ko) |
| 2171 | { |
| 2172 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2173 | kfree(rdev); |
| 2174 | } |
| 2175 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2176 | .show = rdev_attr_show, |
| 2177 | .store = rdev_attr_store, |
| 2178 | }; |
| 2179 | static struct kobj_type rdev_ktype = { |
| 2180 | .release = rdev_free, |
| 2181 | .sysfs_ops = &rdev_sysfs_ops, |
| 2182 | .default_attrs = rdev_default_attrs, |
| 2183 | }; |
| 2184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | /* |
| 2186 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2187 | * |
| 2188 | * mark the device faulty if: |
| 2189 | * |
| 2190 | * - the device is nonexistent (zero size) |
| 2191 | * - the device has no valid superblock |
| 2192 | * |
| 2193 | * a faulty rdev _never_ has rdev->sb set. |
| 2194 | */ |
| 2195 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2196 | { |
| 2197 | char b[BDEVNAME_SIZE]; |
| 2198 | int err; |
| 2199 | mdk_rdev_t *rdev; |
| 2200 | sector_t size; |
| 2201 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2202 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | if (!rdev) { |
| 2204 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2205 | return ERR_PTR(-ENOMEM); |
| 2206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | |
| 2208 | if ((err = alloc_disk_sb(rdev))) |
| 2209 | goto abort_free; |
| 2210 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2211 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | if (err) |
| 2213 | goto abort_free; |
| 2214 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2215 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2218 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2219 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2220 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2222 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2224 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2225 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | |
| 2227 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2228 | if (!size) { |
| 2229 | printk(KERN_WARNING |
| 2230 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2231 | bdevname(rdev->bdev,b)); |
| 2232 | err = -EINVAL; |
| 2233 | goto abort_free; |
| 2234 | } |
| 2235 | |
| 2236 | if (super_format >= 0) { |
| 2237 | err = super_types[super_format]. |
| 2238 | load_super(rdev, NULL, super_minor); |
| 2239 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2240 | printk(KERN_WARNING |
| 2241 | "md: %s does not have a valid v%d.%d " |
| 2242 | "superblock, not importing!\n", |
| 2243 | bdevname(rdev->bdev,b), |
| 2244 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | goto abort_free; |
| 2246 | } |
| 2247 | if (err < 0) { |
| 2248 | printk(KERN_WARNING |
| 2249 | "md: could not read %s's sb, not importing!\n", |
| 2250 | bdevname(rdev->bdev,b)); |
| 2251 | goto abort_free; |
| 2252 | } |
| 2253 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2256 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | |
| 2258 | return rdev; |
| 2259 | |
| 2260 | abort_free: |
| 2261 | if (rdev->sb_page) { |
| 2262 | if (rdev->bdev) |
| 2263 | unlock_rdev(rdev); |
| 2264 | free_disk_sb(rdev); |
| 2265 | } |
| 2266 | kfree(rdev); |
| 2267 | return ERR_PTR(err); |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * Check a full RAID array for plausibility |
| 2272 | */ |
| 2273 | |
| 2274 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2275 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | { |
| 2277 | int i; |
| 2278 | struct list_head *tmp; |
| 2279 | mdk_rdev_t *rdev, *freshest; |
| 2280 | char b[BDEVNAME_SIZE]; |
| 2281 | |
| 2282 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2283 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | switch (super_types[mddev->major_version]. |
| 2285 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2286 | case 1: |
| 2287 | freshest = rdev; |
| 2288 | break; |
| 2289 | case 0: |
| 2290 | break; |
| 2291 | default: |
| 2292 | printk( KERN_ERR \ |
| 2293 | "md: fatal superblock inconsistency in %s" |
| 2294 | " -- removing from array\n", |
| 2295 | bdevname(rdev->bdev,b)); |
| 2296 | kick_rdev_from_array(rdev); |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | super_types[mddev->major_version]. |
| 2301 | validate_super(mddev, freshest); |
| 2302 | |
| 2303 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2304 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | if (rdev != freshest) |
| 2306 | if (super_types[mddev->major_version]. |
| 2307 | validate_super(mddev, rdev)) { |
| 2308 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2309 | " from array!\n", |
| 2310 | bdevname(rdev->bdev,b)); |
| 2311 | kick_rdev_from_array(rdev); |
| 2312 | continue; |
| 2313 | } |
| 2314 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2315 | rdev->desc_nr = i++; |
| 2316 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2317 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2318 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2319 | rdev->raid_disk = -1; |
| 2320 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | |
| 2325 | |
| 2326 | if (mddev->recovery_cp != MaxSector && |
| 2327 | mddev->level >= 1) |
| 2328 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2329 | " -- starting background reconstruction\n", |
| 2330 | mdname(mddev)); |
| 2331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2334 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2335 | safe_delay_show(mddev_t *mddev, char *page) |
| 2336 | { |
| 2337 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2338 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2339 | } |
| 2340 | static ssize_t |
| 2341 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2342 | { |
| 2343 | int scale=1; |
| 2344 | int dot=0; |
| 2345 | int i; |
| 2346 | unsigned long msec; |
| 2347 | char buf[30]; |
| 2348 | char *e; |
| 2349 | /* remove a period, and count digits after it */ |
| 2350 | if (len >= sizeof(buf)) |
| 2351 | return -EINVAL; |
| 2352 | strlcpy(buf, cbuf, len); |
| 2353 | buf[len] = 0; |
| 2354 | for (i=0; i<len; i++) { |
| 2355 | if (dot) { |
| 2356 | if (isdigit(buf[i])) { |
| 2357 | buf[i-1] = buf[i]; |
| 2358 | scale *= 10; |
| 2359 | } |
| 2360 | buf[i] = 0; |
| 2361 | } else if (buf[i] == '.') { |
| 2362 | dot=1; |
| 2363 | buf[i] = 0; |
| 2364 | } |
| 2365 | } |
| 2366 | msec = simple_strtoul(buf, &e, 10); |
| 2367 | if (e == buf || (*e && *e != '\n')) |
| 2368 | return -EINVAL; |
| 2369 | msec = (msec * 1000) / scale; |
| 2370 | if (msec == 0) |
| 2371 | mddev->safemode_delay = 0; |
| 2372 | else { |
| 2373 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2374 | if (mddev->safemode_delay == 0) |
| 2375 | mddev->safemode_delay = 1; |
| 2376 | } |
| 2377 | return len; |
| 2378 | } |
| 2379 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2380 | __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] | 2381 | |
| 2382 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2383 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2384 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2385 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2386 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2387 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2388 | else if (mddev->clevel[0]) |
| 2389 | return sprintf(page, "%s\n", mddev->clevel); |
| 2390 | else if (mddev->level != LEVEL_NONE) |
| 2391 | return sprintf(page, "%d\n", mddev->level); |
| 2392 | else |
| 2393 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2396 | static ssize_t |
| 2397 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2398 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2399 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2400 | if (mddev->pers) |
| 2401 | return -EBUSY; |
| 2402 | if (len == 0) |
| 2403 | return 0; |
| 2404 | if (len >= sizeof(mddev->clevel)) |
| 2405 | return -ENOSPC; |
| 2406 | strncpy(mddev->clevel, buf, len); |
| 2407 | if (mddev->clevel[len-1] == '\n') |
| 2408 | len--; |
| 2409 | mddev->clevel[len] = 0; |
| 2410 | mddev->level = LEVEL_NONE; |
| 2411 | return rv; |
| 2412 | } |
| 2413 | |
| 2414 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2415 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2416 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2417 | |
| 2418 | static ssize_t |
| 2419 | layout_show(mddev_t *mddev, char *page) |
| 2420 | { |
| 2421 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2422 | if (mddev->reshape_position != MaxSector && |
| 2423 | mddev->layout != mddev->new_layout) |
| 2424 | return sprintf(page, "%d (%d)\n", |
| 2425 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2426 | return sprintf(page, "%d\n", mddev->layout); |
| 2427 | } |
| 2428 | |
| 2429 | static ssize_t |
| 2430 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2431 | { |
| 2432 | char *e; |
| 2433 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2434 | |
| 2435 | if (!*buf || (*e && *e != '\n')) |
| 2436 | return -EINVAL; |
| 2437 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2438 | if (mddev->pers) |
| 2439 | return -EBUSY; |
| 2440 | if (mddev->reshape_position != MaxSector) |
| 2441 | mddev->new_layout = n; |
| 2442 | else |
| 2443 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2444 | return len; |
| 2445 | } |
| 2446 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2447 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2448 | |
| 2449 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2450 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2451 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2452 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2453 | if (mddev->raid_disks == 0) |
| 2454 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2455 | if (mddev->reshape_position != MaxSector && |
| 2456 | mddev->delta_disks != 0) |
| 2457 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2458 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2459 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2460 | } |
| 2461 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2462 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2463 | |
| 2464 | static ssize_t |
| 2465 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2466 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2467 | char *e; |
| 2468 | int rv = 0; |
| 2469 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2470 | |
| 2471 | if (!*buf || (*e && *e != '\n')) |
| 2472 | return -EINVAL; |
| 2473 | |
| 2474 | if (mddev->pers) |
| 2475 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2476 | else if (mddev->reshape_position != MaxSector) { |
| 2477 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2478 | mddev->delta_disks = n - olddisks; |
| 2479 | mddev->raid_disks = n; |
| 2480 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2481 | mddev->raid_disks = n; |
| 2482 | return rv ? rv : len; |
| 2483 | } |
| 2484 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2485 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2486 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2487 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2488 | chunk_size_show(mddev_t *mddev, char *page) |
| 2489 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2490 | if (mddev->reshape_position != MaxSector && |
| 2491 | mddev->chunk_size != mddev->new_chunk) |
| 2492 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2493 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2494 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2495 | } |
| 2496 | |
| 2497 | static ssize_t |
| 2498 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2499 | { |
| 2500 | /* can only set chunk_size if array is not yet active */ |
| 2501 | char *e; |
| 2502 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2503 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2504 | if (!*buf || (*e && *e != '\n')) |
| 2505 | return -EINVAL; |
| 2506 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2507 | if (mddev->pers) |
| 2508 | return -EBUSY; |
| 2509 | else if (mddev->reshape_position != MaxSector) |
| 2510 | mddev->new_chunk = n; |
| 2511 | else |
| 2512 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2513 | return len; |
| 2514 | } |
| 2515 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2516 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2517 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2518 | static ssize_t |
| 2519 | resync_start_show(mddev_t *mddev, char *page) |
| 2520 | { |
| 2521 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2522 | } |
| 2523 | |
| 2524 | static ssize_t |
| 2525 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2526 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2527 | char *e; |
| 2528 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2529 | |
| 2530 | if (mddev->pers) |
| 2531 | return -EBUSY; |
| 2532 | if (!*buf || (*e && *e != '\n')) |
| 2533 | return -EINVAL; |
| 2534 | |
| 2535 | mddev->recovery_cp = n; |
| 2536 | return len; |
| 2537 | } |
| 2538 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2539 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2540 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2541 | /* |
| 2542 | * The array state can be: |
| 2543 | * |
| 2544 | * clear |
| 2545 | * No devices, no size, no level |
| 2546 | * Equivalent to STOP_ARRAY ioctl |
| 2547 | * inactive |
| 2548 | * May have some settings, but array is not active |
| 2549 | * all IO results in error |
| 2550 | * When written, doesn't tear down array, but just stops it |
| 2551 | * suspended (not supported yet) |
| 2552 | * All IO requests will block. The array can be reconfigured. |
| 2553 | * Writing this, if accepted, will block until array is quiessent |
| 2554 | * readonly |
| 2555 | * no resync can happen. no superblocks get written. |
| 2556 | * write requests fail |
| 2557 | * read-auto |
| 2558 | * like readonly, but behaves like 'clean' on a write request. |
| 2559 | * |
| 2560 | * clean - no pending writes, but otherwise active. |
| 2561 | * When written to inactive array, starts without resync |
| 2562 | * If a write request arrives then |
| 2563 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2564 | * if not known, block and switch to write-pending |
| 2565 | * If written to an active array that has pending writes, then fails. |
| 2566 | * active |
| 2567 | * fully active: IO and resync can be happening. |
| 2568 | * When written to inactive array, starts with resync |
| 2569 | * |
| 2570 | * write-pending |
| 2571 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2572 | * |
| 2573 | * active-idle |
| 2574 | * like active, but no writes have been seen for a while (100msec). |
| 2575 | * |
| 2576 | */ |
| 2577 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2578 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2579 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2580 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2581 | "write-pending", "active-idle", NULL }; |
| 2582 | |
| 2583 | static int match_word(const char *word, char **list) |
| 2584 | { |
| 2585 | int n; |
| 2586 | for (n=0; list[n]; n++) |
| 2587 | if (cmd_match(word, list[n])) |
| 2588 | break; |
| 2589 | return n; |
| 2590 | } |
| 2591 | |
| 2592 | static ssize_t |
| 2593 | array_state_show(mddev_t *mddev, char *page) |
| 2594 | { |
| 2595 | enum array_state st = inactive; |
| 2596 | |
| 2597 | if (mddev->pers) |
| 2598 | switch(mddev->ro) { |
| 2599 | case 1: |
| 2600 | st = readonly; |
| 2601 | break; |
| 2602 | case 2: |
| 2603 | st = read_auto; |
| 2604 | break; |
| 2605 | case 0: |
| 2606 | if (mddev->in_sync) |
| 2607 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2608 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2609 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2610 | else if (mddev->safemode) |
| 2611 | st = active_idle; |
| 2612 | else |
| 2613 | st = active; |
| 2614 | } |
| 2615 | else { |
| 2616 | if (list_empty(&mddev->disks) && |
| 2617 | mddev->raid_disks == 0 && |
| 2618 | mddev->size == 0) |
| 2619 | st = clear; |
| 2620 | else |
| 2621 | st = inactive; |
| 2622 | } |
| 2623 | return sprintf(page, "%s\n", array_states[st]); |
| 2624 | } |
| 2625 | |
| 2626 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2627 | static int do_md_run(mddev_t * mddev); |
| 2628 | static int restart_array(mddev_t *mddev); |
| 2629 | |
| 2630 | static ssize_t |
| 2631 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2632 | { |
| 2633 | int err = -EINVAL; |
| 2634 | enum array_state st = match_word(buf, array_states); |
| 2635 | switch(st) { |
| 2636 | case bad_word: |
| 2637 | break; |
| 2638 | case clear: |
| 2639 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2640 | if (atomic_read(&mddev->active) > 1) |
| 2641 | return -EBUSY; |
| 2642 | err = do_md_stop(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2643 | break; |
| 2644 | case inactive: |
| 2645 | /* stopping an active array */ |
| 2646 | if (mddev->pers) { |
| 2647 | if (atomic_read(&mddev->active) > 1) |
| 2648 | return -EBUSY; |
| 2649 | err = do_md_stop(mddev, 2); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2650 | } else |
| 2651 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2652 | break; |
| 2653 | case suspended: |
| 2654 | break; /* not supported yet */ |
| 2655 | case readonly: |
| 2656 | if (mddev->pers) |
| 2657 | err = do_md_stop(mddev, 1); |
| 2658 | else { |
| 2659 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2660 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2661 | err = do_md_run(mddev); |
| 2662 | } |
| 2663 | break; |
| 2664 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2665 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2666 | if (mddev->ro != 1) |
| 2667 | err = do_md_stop(mddev, 1); |
| 2668 | else |
| 2669 | err = restart_array(mddev); |
| 2670 | if (err == 0) { |
| 2671 | mddev->ro = 2; |
| 2672 | set_disk_ro(mddev->gendisk, 0); |
| 2673 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2674 | } else { |
| 2675 | mddev->ro = 2; |
| 2676 | err = do_md_run(mddev); |
| 2677 | } |
| 2678 | break; |
| 2679 | case clean: |
| 2680 | if (mddev->pers) { |
| 2681 | restart_array(mddev); |
| 2682 | spin_lock_irq(&mddev->write_lock); |
| 2683 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2684 | if (mddev->in_sync == 0) { |
| 2685 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2686 | if (mddev->safemode == 1) |
| 2687 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2688 | if (mddev->persistent) |
| 2689 | set_bit(MD_CHANGE_CLEAN, |
| 2690 | &mddev->flags); |
| 2691 | } |
| 2692 | err = 0; |
| 2693 | } else |
| 2694 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2695 | spin_unlock_irq(&mddev->write_lock); |
| 2696 | } else { |
| 2697 | mddev->ro = 0; |
| 2698 | mddev->recovery_cp = MaxSector; |
| 2699 | err = do_md_run(mddev); |
| 2700 | } |
| 2701 | break; |
| 2702 | case active: |
| 2703 | if (mddev->pers) { |
| 2704 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2705 | if (mddev->external) |
| 2706 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2707 | wake_up(&mddev->sb_wait); |
| 2708 | err = 0; |
| 2709 | } else { |
| 2710 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2711 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2712 | err = do_md_run(mddev); |
| 2713 | } |
| 2714 | break; |
| 2715 | case write_pending: |
| 2716 | case active_idle: |
| 2717 | /* these cannot be set */ |
| 2718 | break; |
| 2719 | } |
| 2720 | if (err) |
| 2721 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2722 | else { |
| 2723 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2724 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2725 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2726 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2727 | static struct md_sysfs_entry md_array_state = |
| 2728 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2729 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2730 | static ssize_t |
| 2731 | null_show(mddev_t *mddev, char *page) |
| 2732 | { |
| 2733 | return -EINVAL; |
| 2734 | } |
| 2735 | |
| 2736 | static ssize_t |
| 2737 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2738 | { |
| 2739 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2740 | /* The new device is added to the array. |
| 2741 | * If the array has a persistent superblock, we read the |
| 2742 | * superblock to initialise info and check validity. |
| 2743 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2744 | * which mainly checks size. |
| 2745 | */ |
| 2746 | char *e; |
| 2747 | int major = simple_strtoul(buf, &e, 10); |
| 2748 | int minor; |
| 2749 | dev_t dev; |
| 2750 | mdk_rdev_t *rdev; |
| 2751 | int err; |
| 2752 | |
| 2753 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2754 | return -EINVAL; |
| 2755 | minor = simple_strtoul(e+1, &e, 10); |
| 2756 | if (*e && *e != '\n') |
| 2757 | return -EINVAL; |
| 2758 | dev = MKDEV(major, minor); |
| 2759 | if (major != MAJOR(dev) || |
| 2760 | minor != MINOR(dev)) |
| 2761 | return -EOVERFLOW; |
| 2762 | |
| 2763 | |
| 2764 | if (mddev->persistent) { |
| 2765 | rdev = md_import_device(dev, mddev->major_version, |
| 2766 | mddev->minor_version); |
| 2767 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2768 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2769 | mdk_rdev_t, same_set); |
| 2770 | err = super_types[mddev->major_version] |
| 2771 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2772 | if (err < 0) |
| 2773 | goto out; |
| 2774 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2775 | } else if (mddev->external) |
| 2776 | rdev = md_import_device(dev, -2, -1); |
| 2777 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2778 | rdev = md_import_device(dev, -1, -1); |
| 2779 | |
| 2780 | if (IS_ERR(rdev)) |
| 2781 | return PTR_ERR(rdev); |
| 2782 | err = bind_rdev_to_array(rdev, mddev); |
| 2783 | out: |
| 2784 | if (err) |
| 2785 | export_rdev(rdev); |
| 2786 | return err ? err : len; |
| 2787 | } |
| 2788 | |
| 2789 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2790 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2791 | |
| 2792 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2793 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2794 | { |
| 2795 | char *end; |
| 2796 | unsigned long chunk, end_chunk; |
| 2797 | |
| 2798 | if (!mddev->bitmap) |
| 2799 | goto out; |
| 2800 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2801 | while (*buf) { |
| 2802 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2803 | if (buf == end) break; |
| 2804 | if (*end == '-') { /* range */ |
| 2805 | buf = end + 1; |
| 2806 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2807 | if (buf == end) break; |
| 2808 | } |
| 2809 | if (*end && !isspace(*end)) break; |
| 2810 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2811 | buf = end; |
| 2812 | while (isspace(*buf)) buf++; |
| 2813 | } |
| 2814 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2815 | out: |
| 2816 | return len; |
| 2817 | } |
| 2818 | |
| 2819 | static struct md_sysfs_entry md_bitmap = |
| 2820 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2821 | |
| 2822 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2823 | size_show(mddev_t *mddev, char *page) |
| 2824 | { |
| 2825 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2826 | } |
| 2827 | |
| 2828 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2829 | |
| 2830 | static ssize_t |
| 2831 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2832 | { |
| 2833 | /* If array is inactive, we can reduce the component size, but |
| 2834 | * not increase it (except from 0). |
| 2835 | * If array is active, we can try an on-line resize |
| 2836 | */ |
| 2837 | char *e; |
| 2838 | int err = 0; |
| 2839 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2840 | if (!*buf || *buf == '\n' || |
| 2841 | (*e && *e != '\n')) |
| 2842 | return -EINVAL; |
| 2843 | |
| 2844 | if (mddev->pers) { |
| 2845 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2846 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2847 | } else { |
| 2848 | if (mddev->size == 0 || |
| 2849 | mddev->size > size) |
| 2850 | mddev->size = size; |
| 2851 | else |
| 2852 | err = -ENOSPC; |
| 2853 | } |
| 2854 | return err ? err : len; |
| 2855 | } |
| 2856 | |
| 2857 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2858 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2859 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2860 | |
| 2861 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2862 | * This is one of |
| 2863 | * 'none' for arrays with no metadata (good luck...) |
| 2864 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2865 | * or N.M for internally known formats |
| 2866 | */ |
| 2867 | static ssize_t |
| 2868 | metadata_show(mddev_t *mddev, char *page) |
| 2869 | { |
| 2870 | if (mddev->persistent) |
| 2871 | return sprintf(page, "%d.%d\n", |
| 2872 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2873 | else if (mddev->external) |
| 2874 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2875 | else |
| 2876 | return sprintf(page, "none\n"); |
| 2877 | } |
| 2878 | |
| 2879 | static ssize_t |
| 2880 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2881 | { |
| 2882 | int major, minor; |
| 2883 | char *e; |
| 2884 | if (!list_empty(&mddev->disks)) |
| 2885 | return -EBUSY; |
| 2886 | |
| 2887 | if (cmd_match(buf, "none")) { |
| 2888 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2889 | mddev->external = 0; |
| 2890 | mddev->major_version = 0; |
| 2891 | mddev->minor_version = 90; |
| 2892 | return len; |
| 2893 | } |
| 2894 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2895 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2896 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2897 | namelen = sizeof(mddev->metadata_type)-1; |
| 2898 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2899 | mddev->metadata_type[namelen] = 0; |
| 2900 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2901 | mddev->metadata_type[--namelen] = 0; |
| 2902 | mddev->persistent = 0; |
| 2903 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2904 | mddev->major_version = 0; |
| 2905 | mddev->minor_version = 90; |
| 2906 | return len; |
| 2907 | } |
| 2908 | major = simple_strtoul(buf, &e, 10); |
| 2909 | if (e==buf || *e != '.') |
| 2910 | return -EINVAL; |
| 2911 | buf = e+1; |
| 2912 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2913 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2914 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2915 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2916 | return -ENOENT; |
| 2917 | mddev->major_version = major; |
| 2918 | mddev->minor_version = minor; |
| 2919 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2920 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2921 | return len; |
| 2922 | } |
| 2923 | |
| 2924 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2925 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2926 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2927 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2928 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2929 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2930 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2931 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2932 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2933 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2934 | type = "reshape"; |
| 2935 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2936 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2937 | type = "resync"; |
| 2938 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2939 | type = "check"; |
| 2940 | else |
| 2941 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 2942 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2943 | type = "recover"; |
| 2944 | } |
| 2945 | return sprintf(page, "%s\n", type); |
| 2946 | } |
| 2947 | |
| 2948 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2949 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2950 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2951 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2952 | return -EINVAL; |
| 2953 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2954 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2955 | if (mddev->sync_thread) { |
| 2956 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2957 | md_unregister_thread(mddev->sync_thread); |
| 2958 | mddev->sync_thread = NULL; |
| 2959 | mddev->recovery = 0; |
| 2960 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2961 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2962 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2963 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 2964 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2965 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 2966 | else if (cmd_match(page, "recover")) { |
| 2967 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2968 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2969 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2970 | int err; |
| 2971 | if (mddev->pers->start_reshape == NULL) |
| 2972 | return -EINVAL; |
| 2973 | err = mddev->pers->start_reshape(mddev); |
| 2974 | if (err) |
| 2975 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 2976 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2977 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2978 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2979 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2980 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2981 | return -EINVAL; |
| 2982 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2983 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2984 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2985 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2986 | md_wakeup_thread(mddev->thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 2987 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2988 | return len; |
| 2989 | } |
| 2990 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2991 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2992 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2993 | { |
| 2994 | return sprintf(page, "%llu\n", |
| 2995 | (unsigned long long) mddev->resync_mismatches); |
| 2996 | } |
| 2997 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2998 | static struct md_sysfs_entry md_scan_mode = |
| 2999 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3000 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3001 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3002 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3003 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3004 | static ssize_t |
| 3005 | sync_min_show(mddev_t *mddev, char *page) |
| 3006 | { |
| 3007 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3008 | mddev->sync_speed_min ? "local": "system"); |
| 3009 | } |
| 3010 | |
| 3011 | static ssize_t |
| 3012 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3013 | { |
| 3014 | int min; |
| 3015 | char *e; |
| 3016 | if (strncmp(buf, "system", 6)==0) { |
| 3017 | mddev->sync_speed_min = 0; |
| 3018 | return len; |
| 3019 | } |
| 3020 | min = simple_strtoul(buf, &e, 10); |
| 3021 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3022 | return -EINVAL; |
| 3023 | mddev->sync_speed_min = min; |
| 3024 | return len; |
| 3025 | } |
| 3026 | |
| 3027 | static struct md_sysfs_entry md_sync_min = |
| 3028 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3029 | |
| 3030 | static ssize_t |
| 3031 | sync_max_show(mddev_t *mddev, char *page) |
| 3032 | { |
| 3033 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3034 | mddev->sync_speed_max ? "local": "system"); |
| 3035 | } |
| 3036 | |
| 3037 | static ssize_t |
| 3038 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3039 | { |
| 3040 | int max; |
| 3041 | char *e; |
| 3042 | if (strncmp(buf, "system", 6)==0) { |
| 3043 | mddev->sync_speed_max = 0; |
| 3044 | return len; |
| 3045 | } |
| 3046 | max = simple_strtoul(buf, &e, 10); |
| 3047 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3048 | return -EINVAL; |
| 3049 | mddev->sync_speed_max = max; |
| 3050 | return len; |
| 3051 | } |
| 3052 | |
| 3053 | static struct md_sysfs_entry md_sync_max = |
| 3054 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3055 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3056 | static ssize_t |
| 3057 | degraded_show(mddev_t *mddev, char *page) |
| 3058 | { |
| 3059 | return sprintf(page, "%d\n", mddev->degraded); |
| 3060 | } |
| 3061 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3062 | |
| 3063 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3064 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3065 | { |
| 3066 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3067 | } |
| 3068 | |
| 3069 | static ssize_t |
| 3070 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3071 | { |
| 3072 | long n; |
| 3073 | |
| 3074 | if (strict_strtol(buf, 10, &n)) |
| 3075 | return -EINVAL; |
| 3076 | |
| 3077 | if (n != 0 && n != 1) |
| 3078 | return -EINVAL; |
| 3079 | |
| 3080 | mddev->parallel_resync = n; |
| 3081 | |
| 3082 | if (mddev->sync_thread) |
| 3083 | wake_up(&resync_wait); |
| 3084 | |
| 3085 | return len; |
| 3086 | } |
| 3087 | |
| 3088 | /* force parallel resync, even with shared block devices */ |
| 3089 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3090 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3091 | sync_force_parallel_show, sync_force_parallel_store); |
| 3092 | |
| 3093 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3094 | sync_speed_show(mddev_t *mddev, char *page) |
| 3095 | { |
| 3096 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 3097 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3098 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 3099 | if (!dt) dt++; |
| 3100 | db = resync - (mddev->resync_mark_cnt); |
| 3101 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 3102 | } |
| 3103 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3104 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3105 | |
| 3106 | static ssize_t |
| 3107 | sync_completed_show(mddev_t *mddev, char *page) |
| 3108 | { |
| 3109 | unsigned long max_blocks, resync; |
| 3110 | |
| 3111 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3112 | max_blocks = mddev->resync_max_sectors; |
| 3113 | else |
| 3114 | max_blocks = mddev->size << 1; |
| 3115 | |
| 3116 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3117 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3118 | } |
| 3119 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3120 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3121 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3122 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3123 | min_sync_show(mddev_t *mddev, char *page) |
| 3124 | { |
| 3125 | return sprintf(page, "%llu\n", |
| 3126 | (unsigned long long)mddev->resync_min); |
| 3127 | } |
| 3128 | static ssize_t |
| 3129 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3130 | { |
| 3131 | unsigned long long min; |
| 3132 | if (strict_strtoull(buf, 10, &min)) |
| 3133 | return -EINVAL; |
| 3134 | if (min > mddev->resync_max) |
| 3135 | return -EINVAL; |
| 3136 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3137 | return -EBUSY; |
| 3138 | |
| 3139 | /* Must be a multiple of chunk_size */ |
| 3140 | if (mddev->chunk_size) { |
| 3141 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3142 | return -EINVAL; |
| 3143 | } |
| 3144 | mddev->resync_min = min; |
| 3145 | |
| 3146 | return len; |
| 3147 | } |
| 3148 | |
| 3149 | static struct md_sysfs_entry md_min_sync = |
| 3150 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3151 | |
| 3152 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3153 | max_sync_show(mddev_t *mddev, char *page) |
| 3154 | { |
| 3155 | if (mddev->resync_max == MaxSector) |
| 3156 | return sprintf(page, "max\n"); |
| 3157 | else |
| 3158 | return sprintf(page, "%llu\n", |
| 3159 | (unsigned long long)mddev->resync_max); |
| 3160 | } |
| 3161 | static ssize_t |
| 3162 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3163 | { |
| 3164 | if (strncmp(buf, "max", 3) == 0) |
| 3165 | mddev->resync_max = MaxSector; |
| 3166 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3167 | unsigned long long max; |
| 3168 | if (strict_strtoull(buf, 10, &max)) |
| 3169 | return -EINVAL; |
| 3170 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3171 | return -EINVAL; |
| 3172 | if (max < mddev->resync_max && |
| 3173 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3174 | return -EBUSY; |
| 3175 | |
| 3176 | /* Must be a multiple of chunk_size */ |
| 3177 | if (mddev->chunk_size) { |
| 3178 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3179 | return -EINVAL; |
| 3180 | } |
| 3181 | mddev->resync_max = max; |
| 3182 | } |
| 3183 | wake_up(&mddev->recovery_wait); |
| 3184 | return len; |
| 3185 | } |
| 3186 | |
| 3187 | static struct md_sysfs_entry md_max_sync = |
| 3188 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3189 | |
| 3190 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3191 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3192 | { |
| 3193 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3194 | } |
| 3195 | |
| 3196 | static ssize_t |
| 3197 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3198 | { |
| 3199 | char *e; |
| 3200 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3201 | |
| 3202 | if (mddev->pers->quiesce == NULL) |
| 3203 | return -EINVAL; |
| 3204 | if (buf == e || (*e && *e != '\n')) |
| 3205 | return -EINVAL; |
| 3206 | if (new >= mddev->suspend_hi || |
| 3207 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3208 | mddev->suspend_lo = new; |
| 3209 | mddev->pers->quiesce(mddev, 2); |
| 3210 | return len; |
| 3211 | } else |
| 3212 | return -EINVAL; |
| 3213 | } |
| 3214 | static struct md_sysfs_entry md_suspend_lo = |
| 3215 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3216 | |
| 3217 | |
| 3218 | static ssize_t |
| 3219 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3220 | { |
| 3221 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3222 | } |
| 3223 | |
| 3224 | static ssize_t |
| 3225 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3226 | { |
| 3227 | char *e; |
| 3228 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3229 | |
| 3230 | if (mddev->pers->quiesce == NULL) |
| 3231 | return -EINVAL; |
| 3232 | if (buf == e || (*e && *e != '\n')) |
| 3233 | return -EINVAL; |
| 3234 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3235 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3236 | mddev->suspend_hi = new; |
| 3237 | mddev->pers->quiesce(mddev, 1); |
| 3238 | mddev->pers->quiesce(mddev, 0); |
| 3239 | return len; |
| 3240 | } else |
| 3241 | return -EINVAL; |
| 3242 | } |
| 3243 | static struct md_sysfs_entry md_suspend_hi = |
| 3244 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3245 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3246 | static ssize_t |
| 3247 | reshape_position_show(mddev_t *mddev, char *page) |
| 3248 | { |
| 3249 | if (mddev->reshape_position != MaxSector) |
| 3250 | return sprintf(page, "%llu\n", |
| 3251 | (unsigned long long)mddev->reshape_position); |
| 3252 | strcpy(page, "none\n"); |
| 3253 | return 5; |
| 3254 | } |
| 3255 | |
| 3256 | static ssize_t |
| 3257 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3258 | { |
| 3259 | char *e; |
| 3260 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3261 | if (mddev->pers) |
| 3262 | return -EBUSY; |
| 3263 | if (buf == e || (*e && *e != '\n')) |
| 3264 | return -EINVAL; |
| 3265 | mddev->reshape_position = new; |
| 3266 | mddev->delta_disks = 0; |
| 3267 | mddev->new_level = mddev->level; |
| 3268 | mddev->new_layout = mddev->layout; |
| 3269 | mddev->new_chunk = mddev->chunk_size; |
| 3270 | return len; |
| 3271 | } |
| 3272 | |
| 3273 | static struct md_sysfs_entry md_reshape_position = |
| 3274 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3275 | reshape_position_store); |
| 3276 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3277 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3278 | static struct attribute *md_default_attrs[] = { |
| 3279 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3280 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3281 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3282 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3283 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3284 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3285 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3286 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3287 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3288 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3289 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3290 | NULL, |
| 3291 | }; |
| 3292 | |
| 3293 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3294 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3295 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3296 | &md_sync_min.attr, |
| 3297 | &md_sync_max.attr, |
| 3298 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3299 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3300 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3301 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3302 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3303 | &md_suspend_lo.attr, |
| 3304 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3305 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3306 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3307 | NULL, |
| 3308 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3309 | static struct attribute_group md_redundancy_group = { |
| 3310 | .name = NULL, |
| 3311 | .attrs = md_redundancy_attrs, |
| 3312 | }; |
| 3313 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3314 | |
| 3315 | static ssize_t |
| 3316 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3317 | { |
| 3318 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3319 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3320 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3321 | |
| 3322 | if (!entry->show) |
| 3323 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3324 | rv = mddev_lock(mddev); |
| 3325 | if (!rv) { |
| 3326 | rv = entry->show(mddev, page); |
| 3327 | mddev_unlock(mddev); |
| 3328 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3329 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | static ssize_t |
| 3333 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3334 | const char *page, size_t length) |
| 3335 | { |
| 3336 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3337 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3338 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3339 | |
| 3340 | if (!entry->store) |
| 3341 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3342 | if (!capable(CAP_SYS_ADMIN)) |
| 3343 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3344 | rv = mddev_lock(mddev); |
| 3345 | if (!rv) { |
| 3346 | rv = entry->store(mddev, page, length); |
| 3347 | mddev_unlock(mddev); |
| 3348 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3349 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | static void md_free(struct kobject *ko) |
| 3353 | { |
| 3354 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3355 | kfree(mddev); |
| 3356 | } |
| 3357 | |
| 3358 | static struct sysfs_ops md_sysfs_ops = { |
| 3359 | .show = md_attr_show, |
| 3360 | .store = md_attr_store, |
| 3361 | }; |
| 3362 | static struct kobj_type md_ktype = { |
| 3363 | .release = md_free, |
| 3364 | .sysfs_ops = &md_sysfs_ops, |
| 3365 | .default_attrs = md_default_attrs, |
| 3366 | }; |
| 3367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | int mdp_major = 0; |
| 3369 | |
| 3370 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3371 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3372 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | mddev_t *mddev = mddev_find(dev); |
| 3374 | struct gendisk *disk; |
| 3375 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3376 | int shift = partitioned ? MdpMinorShift : 0; |
| 3377 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3378 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | |
| 3380 | if (!mddev) |
| 3381 | return NULL; |
| 3382 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3383 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3385 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | mddev_put(mddev); |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | disk = alloc_disk(1 << shift); |
| 3390 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3391 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | mddev_put(mddev); |
| 3393 | return NULL; |
| 3394 | } |
| 3395 | disk->major = MAJOR(dev); |
| 3396 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3397 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3399 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | disk->fops = &md_fops; |
| 3402 | disk->private_data = mddev; |
| 3403 | disk->queue = mddev->queue; |
| 3404 | add_disk(disk); |
| 3405 | mddev->gendisk = disk; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3406 | 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] | 3407 | "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3408 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3409 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3410 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3411 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3412 | else |
| 3413 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | return NULL; |
| 3415 | } |
| 3416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | static void md_safemode_timeout(unsigned long data) |
| 3418 | { |
| 3419 | mddev_t *mddev = (mddev_t *) data; |
| 3420 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3421 | if (!atomic_read(&mddev->writes_pending)) { |
| 3422 | mddev->safemode = 1; |
| 3423 | if (mddev->external) |
| 3424 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | md_wakeup_thread(mddev->thread); |
| 3427 | } |
| 3428 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3429 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | |
| 3431 | static int do_md_run(mddev_t * mddev) |
| 3432 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3433 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | int chunk_size; |
| 3435 | struct list_head *tmp; |
| 3436 | mdk_rdev_t *rdev; |
| 3437 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3438 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | char b[BDEVNAME_SIZE]; |
| 3440 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3441 | if (list_empty(&mddev->disks)) |
| 3442 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | |
| 3445 | if (mddev->pers) |
| 3446 | return -EBUSY; |
| 3447 | |
| 3448 | /* |
| 3449 | * Analyze all RAID superblock(s) |
| 3450 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3451 | if (!mddev->raid_disks) { |
| 3452 | if (!mddev->persistent) |
| 3453 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3454 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | |
| 3457 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3459 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3461 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3462 | chunk_size, MAX_CHUNK_SIZE); |
| 3463 | return -EINVAL; |
| 3464 | } |
| 3465 | /* |
| 3466 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3467 | */ |
| 3468 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3469 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | return -EINVAL; |
| 3471 | } |
| 3472 | if (chunk_size < PAGE_SIZE) { |
| 3473 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3474 | chunk_size, PAGE_SIZE); |
| 3475 | return -EINVAL; |
| 3476 | } |
| 3477 | |
| 3478 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3479 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3480 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | continue; |
| 3482 | if (rdev->size < chunk_size / 1024) { |
| 3483 | printk(KERN_WARNING |
| 3484 | "md: Dev %s smaller than chunk_size:" |
| 3485 | " %lluk < %dk\n", |
| 3486 | bdevname(rdev->bdev,b), |
| 3487 | (unsigned long long)rdev->size, |
| 3488 | chunk_size / 1024); |
| 3489 | return -EINVAL; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3495 | if (mddev->level != LEVEL_NONE) |
| 3496 | request_module("md-level-%d", mddev->level); |
| 3497 | else if (mddev->clevel[0]) |
| 3498 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | #endif |
| 3500 | |
| 3501 | /* |
| 3502 | * Drop all container device buffers, from now on |
| 3503 | * the only valid external interface is through the md |
| 3504 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3506 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3507 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | continue; |
| 3509 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3510 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3511 | |
| 3512 | /* perform some consistency tests on the device. |
| 3513 | * We don't want the data to overlap the metadata, |
| 3514 | * Internal Bitmap issues has handled elsewhere. |
| 3515 | */ |
| 3516 | if (rdev->data_offset < rdev->sb_offset) { |
| 3517 | if (mddev->size && |
| 3518 | rdev->data_offset + mddev->size*2 |
| 3519 | > rdev->sb_offset*2) { |
| 3520 | printk("md: %s: data overlaps metadata\n", |
| 3521 | mdname(mddev)); |
| 3522 | return -EINVAL; |
| 3523 | } |
| 3524 | } else { |
| 3525 | if (rdev->sb_offset*2 + rdev->sb_size/512 |
| 3526 | > rdev->data_offset) { |
| 3527 | printk("md: %s: metadata overlaps data\n", |
| 3528 | mdname(mddev)); |
| 3529 | return -EINVAL; |
| 3530 | } |
| 3531 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 3532 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | md_probe(mddev->unit, NULL, NULL); |
| 3536 | disk = mddev->gendisk; |
| 3537 | if (!disk) |
| 3538 | return -ENOMEM; |
| 3539 | |
| 3540 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3541 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3542 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3544 | if (mddev->level != LEVEL_NONE) |
| 3545 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3546 | mddev->level); |
| 3547 | else |
| 3548 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3549 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | return -EINVAL; |
| 3551 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3552 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3554 | mddev->level = pers->level; |
| 3555 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3557 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3558 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3559 | /* This personality cannot handle reshaping... */ |
| 3560 | mddev->pers = NULL; |
| 3561 | module_put(pers->owner); |
| 3562 | return -EINVAL; |
| 3563 | } |
| 3564 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3565 | if (pers->sync_request) { |
| 3566 | /* Warn if this is a potentially silly |
| 3567 | * configuration. |
| 3568 | */ |
| 3569 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3570 | mdk_rdev_t *rdev2; |
| 3571 | struct list_head *tmp2; |
| 3572 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3573 | rdev_for_each(rdev, tmp, mddev) { |
| 3574 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3575 | if (rdev < rdev2 && |
| 3576 | rdev->bdev->bd_contains == |
| 3577 | rdev2->bdev->bd_contains) { |
| 3578 | printk(KERN_WARNING |
| 3579 | "%s: WARNING: %s appears to be" |
| 3580 | " on the same physical disk as" |
| 3581 | " %s.\n", |
| 3582 | mdname(mddev), |
| 3583 | bdevname(rdev->bdev,b), |
| 3584 | bdevname(rdev2->bdev,b2)); |
| 3585 | warned = 1; |
| 3586 | } |
| 3587 | } |
| 3588 | } |
| 3589 | if (warned) |
| 3590 | printk(KERN_WARNING |
| 3591 | "True protection against single-disk" |
| 3592 | " failure might be compromised.\n"); |
| 3593 | } |
| 3594 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3595 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3597 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3598 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3600 | if (start_readonly) |
| 3601 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3602 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3603 | err = mddev->pers->run(mddev); |
| 3604 | if (!err && mddev->pers->sync_request) { |
| 3605 | err = bitmap_create(mddev); |
| 3606 | if (err) { |
| 3607 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3608 | mdname(mddev), err); |
| 3609 | mddev->pers->stop(mddev); |
| 3610 | } |
| 3611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | if (err) { |
| 3613 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3614 | module_put(mddev->pers->owner); |
| 3615 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3616 | bitmap_destroy(mddev); |
| 3617 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3619 | if (mddev->pers->sync_request) { |
| 3620 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3621 | printk(KERN_WARNING |
| 3622 | "md: cannot register extra attributes for %s\n", |
| 3623 | mdname(mddev)); |
| 3624 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3625 | mddev->ro = 0; |
| 3626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | atomic_set(&mddev->writes_pending,0); |
| 3628 | mddev->safemode = 0; |
| 3629 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3630 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3631 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3633 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3634 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3635 | if (rdev->raid_disk >= 0) { |
| 3636 | char nm[20]; |
| 3637 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3638 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3639 | printk("md: cannot register %s for %s\n", |
| 3640 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | |
| 3643 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3644 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3645 | if (mddev->flags) |
| 3646 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | |
| 3648 | set_capacity(disk, mddev->array_size<<1); |
| 3649 | |
| 3650 | /* If we call blk_queue_make_request here, it will |
| 3651 | * re-initialise max_sectors etc which may have been |
| 3652 | * refined inside -> run. So just set the bits we need to set. |
| 3653 | * Most initialisation happended when we called |
| 3654 | * blk_queue_make_request(..., md_fail_request) |
| 3655 | * earlier. |
| 3656 | */ |
| 3657 | mddev->queue->queuedata = mddev; |
| 3658 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3659 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3660 | /* If there is a partially-recovered drive we need to |
| 3661 | * start recovery here. If we leave it to md_check_recovery, |
| 3662 | * it will remove the drives and not do the right thing |
| 3663 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3664 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3665 | struct list_head *rtmp; |
| 3666 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3667 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3668 | if (rdev->raid_disk >= 0 && |
| 3669 | !test_bit(In_sync, &rdev->flags) && |
| 3670 | !test_bit(Faulty, &rdev->flags)) |
| 3671 | /* complete an interrupted recovery */ |
| 3672 | spares++; |
| 3673 | if (spares && mddev->pers->sync_request) { |
| 3674 | mddev->recovery = 0; |
| 3675 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3676 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3677 | mddev, |
| 3678 | "%s_resync"); |
| 3679 | if (!mddev->sync_thread) { |
| 3680 | printk(KERN_ERR "%s: could not start resync" |
| 3681 | " thread...\n", |
| 3682 | mdname(mddev)); |
| 3683 | /* leave the spares where they are, it shouldn't hurt */ |
| 3684 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3685 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3686 | } |
| 3687 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3688 | md_wakeup_thread(mddev->thread); |
| 3689 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3690 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3691 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3692 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3693 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3694 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3695 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3696 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
| 3699 | |
| 3700 | static int restart_array(mddev_t *mddev) |
| 3701 | { |
| 3702 | struct gendisk *disk = mddev->gendisk; |
| 3703 | int err; |
| 3704 | |
| 3705 | /* |
| 3706 | * Complain if it has no devices |
| 3707 | */ |
| 3708 | err = -ENXIO; |
| 3709 | if (list_empty(&mddev->disks)) |
| 3710 | goto out; |
| 3711 | |
| 3712 | if (mddev->pers) { |
| 3713 | err = -EBUSY; |
| 3714 | if (!mddev->ro) |
| 3715 | goto out; |
| 3716 | |
| 3717 | mddev->safemode = 0; |
| 3718 | mddev->ro = 0; |
| 3719 | set_disk_ro(disk, 0); |
| 3720 | |
| 3721 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3722 | mdname(mddev)); |
| 3723 | /* |
| 3724 | * Kick recovery or resync if necessary |
| 3725 | */ |
| 3726 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3727 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3728 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | err = 0; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3730 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3731 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3732 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | |
| 3735 | out: |
| 3736 | return err; |
| 3737 | } |
| 3738 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3739 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3740 | * to the file ourselves */ |
| 3741 | static int deny_bitmap_write_access(struct file * file) |
| 3742 | { |
| 3743 | struct inode *inode = file->f_mapping->host; |
| 3744 | |
| 3745 | spin_lock(&inode->i_lock); |
| 3746 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3747 | spin_unlock(&inode->i_lock); |
| 3748 | return -ETXTBSY; |
| 3749 | } |
| 3750 | atomic_set(&inode->i_writecount, -1); |
| 3751 | spin_unlock(&inode->i_lock); |
| 3752 | |
| 3753 | return 0; |
| 3754 | } |
| 3755 | |
| 3756 | static void restore_bitmap_write_access(struct file *file) |
| 3757 | { |
| 3758 | struct inode *inode = file->f_mapping->host; |
| 3759 | |
| 3760 | spin_lock(&inode->i_lock); |
| 3761 | atomic_set(&inode->i_writecount, 1); |
| 3762 | spin_unlock(&inode->i_lock); |
| 3763 | } |
| 3764 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3765 | /* mode: |
| 3766 | * 0 - completely stop and dis-assemble array |
| 3767 | * 1 - switch to readonly |
| 3768 | * 2 - stop but do not disassemble array |
| 3769 | */ |
| 3770 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | { |
| 3772 | int err = 0; |
| 3773 | struct gendisk *disk = mddev->gendisk; |
| 3774 | |
| 3775 | if (mddev->pers) { |
| 3776 | if (atomic_read(&mddev->active)>2) { |
| 3777 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3778 | return -EBUSY; |
| 3779 | } |
| 3780 | |
| 3781 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3782 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3784 | md_unregister_thread(mddev->sync_thread); |
| 3785 | mddev->sync_thread = NULL; |
| 3786 | } |
| 3787 | |
| 3788 | del_timer_sync(&mddev->safemode_timer); |
| 3789 | |
| 3790 | invalidate_partition(disk, 0); |
| 3791 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3792 | switch(mode) { |
| 3793 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3795 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | goto out; |
| 3797 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3798 | break; |
| 3799 | case 0: /* disassemble */ |
| 3800 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3801 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3802 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | if (mddev->ro) |
| 3804 | set_disk_ro(disk, 0); |
| 3805 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3806 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3807 | mddev->queue->merge_bvec_fn = NULL; |
| 3808 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3809 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3810 | if (mddev->pers->sync_request) |
| 3811 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | module_put(mddev->pers->owner); |
| 3814 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3815 | /* tell userspace to handle 'inactive' */ |
| 3816 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3817 | |
| 3818 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3819 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | if (mddev->ro) |
| 3822 | mddev->ro = 0; |
| 3823 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3824 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | /* mark array as shutdown cleanly */ |
| 3826 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3827 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3829 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3831 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | /* |
| 3835 | * Free resources if final stop |
| 3836 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3837 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3838 | mdk_rdev_t *rdev; |
| 3839 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3842 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3843 | bitmap_destroy(mddev); |
| 3844 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3845 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3846 | fput(mddev->bitmap_file); |
| 3847 | mddev->bitmap_file = NULL; |
| 3848 | } |
| 3849 | mddev->bitmap_offset = 0; |
| 3850 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3851 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3852 | if (rdev->raid_disk >= 0) { |
| 3853 | char nm[20]; |
| 3854 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3855 | sysfs_remove_link(&mddev->kobj, nm); |
| 3856 | } |
| 3857 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3858 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3859 | flush_scheduled_work(); |
| 3860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | export_array(mddev); |
| 3862 | |
| 3863 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3864 | mddev->size = 0; |
| 3865 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3866 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3867 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3868 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3869 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3870 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3871 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3872 | mddev->level = LEVEL_NONE; |
| 3873 | mddev->clevel[0] = 0; |
| 3874 | mddev->flags = 0; |
| 3875 | mddev->ro = 0; |
| 3876 | mddev->metadata_type[0] = 0; |
| 3877 | mddev->chunk_size = 0; |
| 3878 | mddev->ctime = mddev->utime = 0; |
| 3879 | mddev->layout = 0; |
| 3880 | mddev->max_disks = 0; |
| 3881 | mddev->events = 0; |
| 3882 | mddev->delta_disks = 0; |
| 3883 | mddev->new_level = LEVEL_NONE; |
| 3884 | mddev->new_layout = 0; |
| 3885 | mddev->new_chunk = 0; |
| 3886 | mddev->curr_resync = 0; |
| 3887 | mddev->resync_mismatches = 0; |
| 3888 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3889 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3890 | mddev->recovery = 0; |
| 3891 | mddev->in_sync = 0; |
| 3892 | mddev->changed = 0; |
| 3893 | mddev->degraded = 0; |
| 3894 | mddev->barriers_work = 0; |
| 3895 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3896 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3897 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3899 | mdname(mddev)); |
| 3900 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3901 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3902 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | out: |
| 3904 | return err; |
| 3905 | } |
| 3906 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3907 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | static void autorun_array(mddev_t *mddev) |
| 3909 | { |
| 3910 | mdk_rdev_t *rdev; |
| 3911 | struct list_head *tmp; |
| 3912 | int err; |
| 3913 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3914 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | |
| 3917 | printk(KERN_INFO "md: running: "); |
| 3918 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3919 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | char b[BDEVNAME_SIZE]; |
| 3921 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3922 | } |
| 3923 | printk("\n"); |
| 3924 | |
| 3925 | err = do_md_run (mddev); |
| 3926 | if (err) { |
| 3927 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3928 | do_md_stop (mddev, 0); |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * lets try to run arrays based on all disks that have arrived |
| 3934 | * until now. (those are in pending_raid_disks) |
| 3935 | * |
| 3936 | * the method: pick the first pending disk, collect all disks with |
| 3937 | * the same UUID, remove all from the pending list and put them into |
| 3938 | * the 'same_array' list. Then order this list based on superblock |
| 3939 | * update time (freshest comes first), kick out 'old' disks and |
| 3940 | * compare superblocks. If everything's fine then run it. |
| 3941 | * |
| 3942 | * If "unit" is allocated, then bump its reference count |
| 3943 | */ |
| 3944 | static void autorun_devices(int part) |
| 3945 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | struct list_head *tmp; |
| 3947 | mdk_rdev_t *rdev0, *rdev; |
| 3948 | mddev_t *mddev; |
| 3949 | char b[BDEVNAME_SIZE]; |
| 3950 | |
| 3951 | printk(KERN_INFO "md: autorun ...\n"); |
| 3952 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3953 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3955 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | rdev0 = list_entry(pending_raid_disks.next, |
| 3957 | mdk_rdev_t, same_set); |
| 3958 | |
| 3959 | printk(KERN_INFO "md: considering %s ...\n", |
| 3960 | bdevname(rdev0->bdev,b)); |
| 3961 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3962 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3964 | printk(KERN_INFO "md: adding %s ...\n", |
| 3965 | bdevname(rdev->bdev,b)); |
| 3966 | list_move(&rdev->same_set, &candidates); |
| 3967 | } |
| 3968 | /* |
| 3969 | * now we have a set of devices, with all of them having |
| 3970 | * mostly sane superblocks. It's time to allocate the |
| 3971 | * mddev. |
| 3972 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3973 | if (part) { |
| 3974 | dev = MKDEV(mdp_major, |
| 3975 | rdev0->preferred_minor << MdpMinorShift); |
| 3976 | unit = MINOR(dev) >> MdpMinorShift; |
| 3977 | } else { |
| 3978 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3979 | unit = MINOR(dev); |
| 3980 | } |
| 3981 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3983 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3984 | break; |
| 3985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | |
| 3987 | md_probe(dev, NULL, NULL); |
| 3988 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 3989 | if (!mddev || !mddev->gendisk) { |
| 3990 | if (mddev) |
| 3991 | mddev_put(mddev); |
| 3992 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | "md: cannot allocate memory for md drive.\n"); |
| 3994 | break; |
| 3995 | } |
| 3996 | if (mddev_lock(mddev)) |
| 3997 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3998 | mdname(mddev)); |
| 3999 | else if (mddev->raid_disks || mddev->major_version |
| 4000 | || !list_empty(&mddev->disks)) { |
| 4001 | printk(KERN_WARNING |
| 4002 | "md: %s already running, cannot run %s\n", |
| 4003 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4004 | mddev_unlock(mddev); |
| 4005 | } else { |
| 4006 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4007 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4008 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | list_del_init(&rdev->same_set); |
| 4010 | if (bind_rdev_to_array(rdev, mddev)) |
| 4011 | export_rdev(rdev); |
| 4012 | } |
| 4013 | autorun_array(mddev); |
| 4014 | mddev_unlock(mddev); |
| 4015 | } |
| 4016 | /* on success, candidates will be empty, on error |
| 4017 | * it won't... |
| 4018 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4019 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | export_rdev(rdev); |
| 4021 | mddev_put(mddev); |
| 4022 | } |
| 4023 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4024 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4025 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | static int get_version(void __user * arg) |
| 4028 | { |
| 4029 | mdu_version_t ver; |
| 4030 | |
| 4031 | ver.major = MD_MAJOR_VERSION; |
| 4032 | ver.minor = MD_MINOR_VERSION; |
| 4033 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4034 | |
| 4035 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4036 | return -EFAULT; |
| 4037 | |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4042 | { |
| 4043 | mdu_array_info_t info; |
| 4044 | int nr,working,active,failed,spare; |
| 4045 | mdk_rdev_t *rdev; |
| 4046 | struct list_head *tmp; |
| 4047 | |
| 4048 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4049 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4051 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | failed++; |
| 4053 | else { |
| 4054 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4055 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | active++; |
| 4057 | else |
| 4058 | spare++; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | info.major_version = mddev->major_version; |
| 4063 | info.minor_version = mddev->minor_version; |
| 4064 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4065 | info.ctime = mddev->ctime; |
| 4066 | info.level = mddev->level; |
| 4067 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4068 | if (info.size != mddev->size) /* overflow */ |
| 4069 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | info.nr_disks = nr; |
| 4071 | info.raid_disks = mddev->raid_disks; |
| 4072 | info.md_minor = mddev->md_minor; |
| 4073 | info.not_persistent= !mddev->persistent; |
| 4074 | |
| 4075 | info.utime = mddev->utime; |
| 4076 | info.state = 0; |
| 4077 | if (mddev->in_sync) |
| 4078 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4079 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4080 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | info.active_disks = active; |
| 4082 | info.working_disks = working; |
| 4083 | info.failed_disks = failed; |
| 4084 | info.spare_disks = spare; |
| 4085 | |
| 4086 | info.layout = mddev->layout; |
| 4087 | info.chunk_size = mddev->chunk_size; |
| 4088 | |
| 4089 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4090 | return -EFAULT; |
| 4091 | |
| 4092 | return 0; |
| 4093 | } |
| 4094 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4095 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4096 | { |
| 4097 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4098 | char *ptr, *buf = NULL; |
| 4099 | int err = -ENOMEM; |
| 4100 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4101 | md_allow_write(mddev); |
| 4102 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4103 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 4104 | if (!file) |
| 4105 | goto out; |
| 4106 | |
| 4107 | /* bitmap disabled, zero the first byte and copy out */ |
| 4108 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4109 | file->pathname[0] = '\0'; |
| 4110 | goto copy_out; |
| 4111 | } |
| 4112 | |
| 4113 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4114 | if (!buf) |
| 4115 | goto out; |
| 4116 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4117 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4118 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4119 | goto out; |
| 4120 | |
| 4121 | strcpy(file->pathname, ptr); |
| 4122 | |
| 4123 | copy_out: |
| 4124 | err = 0; |
| 4125 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4126 | err = -EFAULT; |
| 4127 | out: |
| 4128 | kfree(buf); |
| 4129 | kfree(file); |
| 4130 | return err; |
| 4131 | } |
| 4132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4134 | { |
| 4135 | mdu_disk_info_t info; |
| 4136 | unsigned int nr; |
| 4137 | mdk_rdev_t *rdev; |
| 4138 | |
| 4139 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4140 | return -EFAULT; |
| 4141 | |
| 4142 | nr = info.number; |
| 4143 | |
| 4144 | rdev = find_rdev_nr(mddev, nr); |
| 4145 | if (rdev) { |
| 4146 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4147 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4148 | info.raid_disk = rdev->raid_disk; |
| 4149 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4150 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4152 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4154 | info.state |= (1<<MD_DISK_SYNC); |
| 4155 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4156 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4157 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | } else { |
| 4159 | info.major = info.minor = 0; |
| 4160 | info.raid_disk = -1; |
| 4161 | info.state = (1<<MD_DISK_REMOVED); |
| 4162 | } |
| 4163 | |
| 4164 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4165 | return -EFAULT; |
| 4166 | |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
| 4170 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4171 | { |
| 4172 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4173 | mdk_rdev_t *rdev; |
| 4174 | dev_t dev = MKDEV(info->major,info->minor); |
| 4175 | |
| 4176 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4177 | return -EOVERFLOW; |
| 4178 | |
| 4179 | if (!mddev->raid_disks) { |
| 4180 | int err; |
| 4181 | /* expecting a device which has a superblock */ |
| 4182 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4183 | if (IS_ERR(rdev)) { |
| 4184 | printk(KERN_WARNING |
| 4185 | "md: md_import_device returned %ld\n", |
| 4186 | PTR_ERR(rdev)); |
| 4187 | return PTR_ERR(rdev); |
| 4188 | } |
| 4189 | if (!list_empty(&mddev->disks)) { |
| 4190 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4191 | mdk_rdev_t, same_set); |
| 4192 | int err = super_types[mddev->major_version] |
| 4193 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4194 | if (err < 0) { |
| 4195 | printk(KERN_WARNING |
| 4196 | "md: %s has different UUID to %s\n", |
| 4197 | bdevname(rdev->bdev,b), |
| 4198 | bdevname(rdev0->bdev,b2)); |
| 4199 | export_rdev(rdev); |
| 4200 | return -EINVAL; |
| 4201 | } |
| 4202 | } |
| 4203 | err = bind_rdev_to_array(rdev, mddev); |
| 4204 | if (err) |
| 4205 | export_rdev(rdev); |
| 4206 | return err; |
| 4207 | } |
| 4208 | |
| 4209 | /* |
| 4210 | * add_new_disk can be used once the array is assembled |
| 4211 | * to add "hot spares". They must already have a superblock |
| 4212 | * written |
| 4213 | */ |
| 4214 | if (mddev->pers) { |
| 4215 | int err; |
| 4216 | if (!mddev->pers->hot_add_disk) { |
| 4217 | printk(KERN_WARNING |
| 4218 | "%s: personality does not support diskops!\n", |
| 4219 | mdname(mddev)); |
| 4220 | return -EINVAL; |
| 4221 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4222 | if (mddev->persistent) |
| 4223 | rdev = md_import_device(dev, mddev->major_version, |
| 4224 | mddev->minor_version); |
| 4225 | else |
| 4226 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | if (IS_ERR(rdev)) { |
| 4228 | printk(KERN_WARNING |
| 4229 | "md: md_import_device returned %ld\n", |
| 4230 | PTR_ERR(rdev)); |
| 4231 | return PTR_ERR(rdev); |
| 4232 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4233 | /* set save_raid_disk if appropriate */ |
| 4234 | if (!mddev->persistent) { |
| 4235 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4236 | info->raid_disk < mddev->raid_disks) |
| 4237 | rdev->raid_disk = info->raid_disk; |
| 4238 | else |
| 4239 | rdev->raid_disk = -1; |
| 4240 | } else |
| 4241 | super_types[mddev->major_version]. |
| 4242 | validate_super(mddev, rdev); |
| 4243 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4244 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4245 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4246 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4247 | set_bit(WriteMostly, &rdev->flags); |
| 4248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | rdev->raid_disk = -1; |
| 4250 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4251 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4252 | /* If there is hot_add_disk but no hot_remove_disk |
| 4253 | * then added disks for geometry changes, |
| 4254 | * and should be added immediately. |
| 4255 | */ |
| 4256 | super_types[mddev->major_version]. |
| 4257 | validate_super(mddev, rdev); |
| 4258 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4259 | if (err) |
| 4260 | unbind_rdev_from_array(rdev); |
| 4261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | if (err) |
| 4263 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 4264 | else |
| 4265 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4266 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4267 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4268 | if (mddev->degraded) |
| 4269 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4270 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4271 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | return err; |
| 4273 | } |
| 4274 | |
| 4275 | /* otherwise, add_new_disk is only allowed |
| 4276 | * for major_version==0 superblocks |
| 4277 | */ |
| 4278 | if (mddev->major_version != 0) { |
| 4279 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4280 | mdname(mddev)); |
| 4281 | return -EINVAL; |
| 4282 | } |
| 4283 | |
| 4284 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4285 | int err; |
| 4286 | rdev = md_import_device (dev, -1, 0); |
| 4287 | if (IS_ERR(rdev)) { |
| 4288 | printk(KERN_WARNING |
| 4289 | "md: error, md_import_device() returned %ld\n", |
| 4290 | PTR_ERR(rdev)); |
| 4291 | return PTR_ERR(rdev); |
| 4292 | } |
| 4293 | rdev->desc_nr = info->number; |
| 4294 | if (info->raid_disk < mddev->raid_disks) |
| 4295 | rdev->raid_disk = info->raid_disk; |
| 4296 | else |
| 4297 | rdev->raid_disk = -1; |
| 4298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4300 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4301 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4303 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4304 | set_bit(WriteMostly, &rdev->flags); |
| 4305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | if (!mddev->persistent) { |
| 4307 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4308 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4309 | } else |
| 4310 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4311 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4312 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4313 | err = bind_rdev_to_array(rdev, mddev); |
| 4314 | if (err) { |
| 4315 | export_rdev(rdev); |
| 4316 | return err; |
| 4317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
| 4323 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4324 | { |
| 4325 | char b[BDEVNAME_SIZE]; |
| 4326 | mdk_rdev_t *rdev; |
| 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | rdev = find_rdev(mddev, dev); |
| 4329 | if (!rdev) |
| 4330 | return -ENXIO; |
| 4331 | |
| 4332 | if (rdev->raid_disk >= 0) |
| 4333 | goto busy; |
| 4334 | |
| 4335 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4336 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4337 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | |
| 4339 | return 0; |
| 4340 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4341 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4343 | return -EBUSY; |
| 4344 | } |
| 4345 | |
| 4346 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4347 | { |
| 4348 | char b[BDEVNAME_SIZE]; |
| 4349 | int err; |
| 4350 | unsigned int size; |
| 4351 | mdk_rdev_t *rdev; |
| 4352 | |
| 4353 | if (!mddev->pers) |
| 4354 | return -ENODEV; |
| 4355 | |
| 4356 | if (mddev->major_version != 0) { |
| 4357 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4358 | " version-0 superblocks.\n", |
| 4359 | mdname(mddev)); |
| 4360 | return -EINVAL; |
| 4361 | } |
| 4362 | if (!mddev->pers->hot_add_disk) { |
| 4363 | printk(KERN_WARNING |
| 4364 | "%s: personality does not support diskops!\n", |
| 4365 | mdname(mddev)); |
| 4366 | return -EINVAL; |
| 4367 | } |
| 4368 | |
| 4369 | rdev = md_import_device (dev, -1, 0); |
| 4370 | if (IS_ERR(rdev)) { |
| 4371 | printk(KERN_WARNING |
| 4372 | "md: error, md_import_device() returned %ld\n", |
| 4373 | PTR_ERR(rdev)); |
| 4374 | return -EINVAL; |
| 4375 | } |
| 4376 | |
| 4377 | if (mddev->persistent) |
| 4378 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4379 | else |
| 4380 | rdev->sb_offset = |
| 4381 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4382 | |
| 4383 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4384 | rdev->size = size; |
| 4385 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4386 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | printk(KERN_WARNING |
| 4388 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4389 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4390 | err = -EINVAL; |
| 4391 | goto abort_export; |
| 4392 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4393 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4395 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4396 | err = bind_rdev_to_array(rdev, mddev); |
| 4397 | if (err) |
| 4398 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
| 4400 | /* |
| 4401 | * The rest should better be atomic, we can have disk failures |
| 4402 | * noticed in interrupt contexts ... |
| 4403 | */ |
| 4404 | |
| 4405 | if (rdev->desc_nr == mddev->max_disks) { |
| 4406 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4407 | mdname(mddev)); |
| 4408 | err = -EBUSY; |
| 4409 | goto abort_unbind_export; |
| 4410 | } |
| 4411 | |
| 4412 | rdev->raid_disk = -1; |
| 4413 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4414 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | |
| 4416 | /* |
| 4417 | * Kick recovery, maybe this spare has to be added to the |
| 4418 | * array immediately. |
| 4419 | */ |
| 4420 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4421 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4422 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | return 0; |
| 4424 | |
| 4425 | abort_unbind_export: |
| 4426 | unbind_rdev_from_array(rdev); |
| 4427 | |
| 4428 | abort_export: |
| 4429 | export_rdev(rdev); |
| 4430 | return err; |
| 4431 | } |
| 4432 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4433 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4434 | { |
| 4435 | int err; |
| 4436 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4437 | if (mddev->pers) { |
| 4438 | if (!mddev->pers->quiesce) |
| 4439 | return -EBUSY; |
| 4440 | if (mddev->recovery || mddev->sync_thread) |
| 4441 | return -EBUSY; |
| 4442 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4445 | |
| 4446 | if (fd >= 0) { |
| 4447 | if (mddev->bitmap) |
| 4448 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4449 | mddev->bitmap_file = fget(fd); |
| 4450 | |
| 4451 | if (mddev->bitmap_file == NULL) { |
| 4452 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4453 | mdname(mddev)); |
| 4454 | return -EBADF; |
| 4455 | } |
| 4456 | |
| 4457 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4458 | if (err) { |
| 4459 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4460 | mdname(mddev)); |
| 4461 | fput(mddev->bitmap_file); |
| 4462 | mddev->bitmap_file = NULL; |
| 4463 | return err; |
| 4464 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4465 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4466 | } else if (mddev->bitmap == NULL) |
| 4467 | return -ENOENT; /* cannot remove what isn't there */ |
| 4468 | err = 0; |
| 4469 | if (mddev->pers) { |
| 4470 | mddev->pers->quiesce(mddev, 1); |
| 4471 | if (fd >= 0) |
| 4472 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4473 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4474 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4475 | fd = -1; /* make sure to put the file */ |
| 4476 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4477 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4478 | } |
| 4479 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4480 | if (mddev->bitmap_file) { |
| 4481 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4482 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4483 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4484 | mddev->bitmap_file = NULL; |
| 4485 | } |
| 4486 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4487 | return err; |
| 4488 | } |
| 4489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | /* |
| 4491 | * set_array_info is used two different ways |
| 4492 | * The original usage is when creating a new array. |
| 4493 | * In this usage, raid_disks is > 0 and it together with |
| 4494 | * level, size, not_persistent,layout,chunksize determine the |
| 4495 | * shape of the array. |
| 4496 | * This will always create an array with a type-0.90.0 superblock. |
| 4497 | * The newer usage is when assembling an array. |
| 4498 | * In this case raid_disks will be 0, and the major_version field is |
| 4499 | * use to determine which style super-blocks are to be found on the devices. |
| 4500 | * The minor and patch _version numbers are also kept incase the |
| 4501 | * super_block handler wishes to interpret them. |
| 4502 | */ |
| 4503 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4504 | { |
| 4505 | |
| 4506 | if (info->raid_disks == 0) { |
| 4507 | /* just setting version number for superblock loading */ |
| 4508 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4509 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | super_types[info->major_version].name == NULL) { |
| 4511 | /* maybe try to auto-load a module? */ |
| 4512 | printk(KERN_INFO |
| 4513 | "md: superblock version %d not known\n", |
| 4514 | info->major_version); |
| 4515 | return -EINVAL; |
| 4516 | } |
| 4517 | mddev->major_version = info->major_version; |
| 4518 | mddev->minor_version = info->minor_version; |
| 4519 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4520 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | return 0; |
| 4522 | } |
| 4523 | mddev->major_version = MD_MAJOR_VERSION; |
| 4524 | mddev->minor_version = MD_MINOR_VERSION; |
| 4525 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4526 | mddev->ctime = get_seconds(); |
| 4527 | |
| 4528 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4529 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | mddev->size = info->size; |
| 4531 | mddev->raid_disks = info->raid_disks; |
| 4532 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4533 | * openned |
| 4534 | */ |
| 4535 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4536 | mddev->recovery_cp = MaxSector; |
| 4537 | else |
| 4538 | mddev->recovery_cp = 0; |
| 4539 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4540 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
| 4542 | mddev->layout = info->layout; |
| 4543 | mddev->chunk_size = info->chunk_size; |
| 4544 | |
| 4545 | mddev->max_disks = MD_SB_DISKS; |
| 4546 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4547 | if (mddev->persistent) |
| 4548 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4549 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4551 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4552 | mddev->bitmap_offset = 0; |
| 4553 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4554 | mddev->reshape_position = MaxSector; |
| 4555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | /* |
| 4557 | * Generate a 128 bit UUID |
| 4558 | */ |
| 4559 | get_random_bytes(mddev->uuid, 16); |
| 4560 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4561 | mddev->new_level = mddev->level; |
| 4562 | mddev->new_chunk = mddev->chunk_size; |
| 4563 | mddev->new_layout = mddev->layout; |
| 4564 | mddev->delta_disks = 0; |
| 4565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | return 0; |
| 4567 | } |
| 4568 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4569 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4570 | { |
| 4571 | mdk_rdev_t * rdev; |
| 4572 | int rv; |
| 4573 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4574 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4575 | |
| 4576 | if (mddev->pers->resize == NULL) |
| 4577 | return -EINVAL; |
| 4578 | /* The "size" is the amount of each device that is used. |
| 4579 | * This can only make sense for arrays with redundancy. |
| 4580 | * linear and raid0 always use whatever space is available |
| 4581 | * We can only consider changing the size if no resync |
| 4582 | * or reconstruction is happening, and if the new size |
| 4583 | * is acceptable. It must fit before the sb_offset or, |
| 4584 | * if that is <data_offset, it must fit before the |
| 4585 | * size of each device. |
| 4586 | * If size is zero, we find the largest size that fits. |
| 4587 | */ |
| 4588 | if (mddev->sync_thread) |
| 4589 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4590 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4591 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4592 | avail = rdev->size * 2; |
| 4593 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4594 | if (fit && (size == 0 || size > avail/2)) |
| 4595 | size = avail/2; |
| 4596 | if (avail < ((sector_t)size << 1)) |
| 4597 | return -ENOSPC; |
| 4598 | } |
| 4599 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4600 | if (!rv) { |
| 4601 | struct block_device *bdev; |
| 4602 | |
| 4603 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4604 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4605 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4606 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4607 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4608 | bdput(bdev); |
| 4609 | } |
| 4610 | } |
| 4611 | return rv; |
| 4612 | } |
| 4613 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4614 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4615 | { |
| 4616 | int rv; |
| 4617 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4618 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4619 | return -EINVAL; |
| 4620 | if (raid_disks <= 0 || |
| 4621 | raid_disks >= mddev->max_disks) |
| 4622 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4623 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4624 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4625 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4626 | |
| 4627 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4628 | return rv; |
| 4629 | } |
| 4630 | |
| 4631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | /* |
| 4633 | * update_array_info is used to change the configuration of an |
| 4634 | * on-line array. |
| 4635 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4636 | * fields in the info are checked against the array. |
| 4637 | * Any differences that cannot be handled will cause an error. |
| 4638 | * Normally, only one change can be managed at a time. |
| 4639 | */ |
| 4640 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4641 | { |
| 4642 | int rv = 0; |
| 4643 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4644 | int state = 0; |
| 4645 | |
| 4646 | /* calculate expected state,ignoring low bits */ |
| 4647 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4648 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | |
| 4650 | if (mddev->major_version != info->major_version || |
| 4651 | mddev->minor_version != info->minor_version || |
| 4652 | /* mddev->patch_version != info->patch_version || */ |
| 4653 | mddev->ctime != info->ctime || |
| 4654 | mddev->level != info->level || |
| 4655 | /* mddev->layout != info->layout || */ |
| 4656 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4657 | mddev->chunk_size != info->chunk_size || |
| 4658 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4659 | ((state^info->state) & 0xfffffe00) |
| 4660 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | return -EINVAL; |
| 4662 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4663 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4665 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4666 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | if (cnt == 0) return 0; |
| 4668 | if (cnt > 1) return -EINVAL; |
| 4669 | |
| 4670 | if (mddev->layout != info->layout) { |
| 4671 | /* Change layout |
| 4672 | * we don't need to do anything at the md level, the |
| 4673 | * personality will take care of it all. |
| 4674 | */ |
| 4675 | if (mddev->pers->reconfig == NULL) |
| 4676 | return -EINVAL; |
| 4677 | else |
| 4678 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4679 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4680 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4681 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4683 | if (mddev->raid_disks != info->raid_disks) |
| 4684 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4686 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4687 | if (mddev->pers->quiesce == NULL) |
| 4688 | return -EINVAL; |
| 4689 | if (mddev->recovery || mddev->sync_thread) |
| 4690 | return -EBUSY; |
| 4691 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4692 | /* add the bitmap */ |
| 4693 | if (mddev->bitmap) |
| 4694 | return -EEXIST; |
| 4695 | if (mddev->default_bitmap_offset == 0) |
| 4696 | return -EINVAL; |
| 4697 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4698 | mddev->pers->quiesce(mddev, 1); |
| 4699 | rv = bitmap_create(mddev); |
| 4700 | if (rv) |
| 4701 | bitmap_destroy(mddev); |
| 4702 | mddev->pers->quiesce(mddev, 0); |
| 4703 | } else { |
| 4704 | /* remove the bitmap */ |
| 4705 | if (!mddev->bitmap) |
| 4706 | return -ENOENT; |
| 4707 | if (mddev->bitmap->file) |
| 4708 | return -EINVAL; |
| 4709 | mddev->pers->quiesce(mddev, 1); |
| 4710 | bitmap_destroy(mddev); |
| 4711 | mddev->pers->quiesce(mddev, 0); |
| 4712 | mddev->bitmap_offset = 0; |
| 4713 | } |
| 4714 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4715 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | return rv; |
| 4717 | } |
| 4718 | |
| 4719 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4720 | { |
| 4721 | mdk_rdev_t *rdev; |
| 4722 | |
| 4723 | if (mddev->pers == NULL) |
| 4724 | return -ENODEV; |
| 4725 | |
| 4726 | rdev = find_rdev(mddev, dev); |
| 4727 | if (!rdev) |
| 4728 | return -ENODEV; |
| 4729 | |
| 4730 | md_error(mddev, rdev); |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4734 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4735 | { |
| 4736 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4737 | |
| 4738 | geo->heads = 2; |
| 4739 | geo->sectors = 4; |
| 4740 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4741 | return 0; |
| 4742 | } |
| 4743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4745 | unsigned int cmd, unsigned long arg) |
| 4746 | { |
| 4747 | int err = 0; |
| 4748 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | mddev_t *mddev = NULL; |
| 4750 | |
| 4751 | if (!capable(CAP_SYS_ADMIN)) |
| 4752 | return -EACCES; |
| 4753 | |
| 4754 | /* |
| 4755 | * Commands dealing with the RAID driver but not any |
| 4756 | * particular array: |
| 4757 | */ |
| 4758 | switch (cmd) |
| 4759 | { |
| 4760 | case RAID_VERSION: |
| 4761 | err = get_version(argp); |
| 4762 | goto done; |
| 4763 | |
| 4764 | case PRINT_RAID_DEBUG: |
| 4765 | err = 0; |
| 4766 | md_print_devices(); |
| 4767 | goto done; |
| 4768 | |
| 4769 | #ifndef MODULE |
| 4770 | case RAID_AUTORUN: |
| 4771 | err = 0; |
| 4772 | autostart_arrays(arg); |
| 4773 | goto done; |
| 4774 | #endif |
| 4775 | default:; |
| 4776 | } |
| 4777 | |
| 4778 | /* |
| 4779 | * Commands creating/starting a new array: |
| 4780 | */ |
| 4781 | |
| 4782 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4783 | |
| 4784 | if (!mddev) { |
| 4785 | BUG(); |
| 4786 | goto abort; |
| 4787 | } |
| 4788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | err = mddev_lock(mddev); |
| 4790 | if (err) { |
| 4791 | printk(KERN_INFO |
| 4792 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4793 | err, cmd); |
| 4794 | goto abort; |
| 4795 | } |
| 4796 | |
| 4797 | switch (cmd) |
| 4798 | { |
| 4799 | case SET_ARRAY_INFO: |
| 4800 | { |
| 4801 | mdu_array_info_t info; |
| 4802 | if (!arg) |
| 4803 | memset(&info, 0, sizeof(info)); |
| 4804 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4805 | err = -EFAULT; |
| 4806 | goto abort_unlock; |
| 4807 | } |
| 4808 | if (mddev->pers) { |
| 4809 | err = update_array_info(mddev, &info); |
| 4810 | if (err) { |
| 4811 | printk(KERN_WARNING "md: couldn't update" |
| 4812 | " array info. %d\n", err); |
| 4813 | goto abort_unlock; |
| 4814 | } |
| 4815 | goto done_unlock; |
| 4816 | } |
| 4817 | if (!list_empty(&mddev->disks)) { |
| 4818 | printk(KERN_WARNING |
| 4819 | "md: array %s already has disks!\n", |
| 4820 | mdname(mddev)); |
| 4821 | err = -EBUSY; |
| 4822 | goto abort_unlock; |
| 4823 | } |
| 4824 | if (mddev->raid_disks) { |
| 4825 | printk(KERN_WARNING |
| 4826 | "md: array %s already initialised!\n", |
| 4827 | mdname(mddev)); |
| 4828 | err = -EBUSY; |
| 4829 | goto abort_unlock; |
| 4830 | } |
| 4831 | err = set_array_info(mddev, &info); |
| 4832 | if (err) { |
| 4833 | printk(KERN_WARNING "md: couldn't set" |
| 4834 | " array info. %d\n", err); |
| 4835 | goto abort_unlock; |
| 4836 | } |
| 4837 | } |
| 4838 | goto done_unlock; |
| 4839 | |
| 4840 | default:; |
| 4841 | } |
| 4842 | |
| 4843 | /* |
| 4844 | * Commands querying/configuring an existing array: |
| 4845 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4846 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4847 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4848 | if ((!mddev->raid_disks && !mddev->external) |
| 4849 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4850 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4851 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | err = -ENODEV; |
| 4853 | goto abort_unlock; |
| 4854 | } |
| 4855 | |
| 4856 | /* |
| 4857 | * Commands even a read-only array can execute: |
| 4858 | */ |
| 4859 | switch (cmd) |
| 4860 | { |
| 4861 | case GET_ARRAY_INFO: |
| 4862 | err = get_array_info(mddev, argp); |
| 4863 | goto done_unlock; |
| 4864 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4865 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4866 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4867 | goto done_unlock; |
| 4868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | case GET_DISK_INFO: |
| 4870 | err = get_disk_info(mddev, argp); |
| 4871 | goto done_unlock; |
| 4872 | |
| 4873 | case RESTART_ARRAY_RW: |
| 4874 | err = restart_array(mddev); |
| 4875 | goto done_unlock; |
| 4876 | |
| 4877 | case STOP_ARRAY: |
| 4878 | err = do_md_stop (mddev, 0); |
| 4879 | goto done_unlock; |
| 4880 | |
| 4881 | case STOP_ARRAY_RO: |
| 4882 | err = do_md_stop (mddev, 1); |
| 4883 | goto done_unlock; |
| 4884 | |
| 4885 | /* |
| 4886 | * We have a problem here : there is no easy way to give a CHS |
| 4887 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4888 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4889 | * dosfs just mad... ;-) |
| 4890 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | /* |
| 4894 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4895 | * superblock, so we do not allow them on read-only arrays. |
| 4896 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4897 | * here and hit the 'default' below, so only disallow |
| 4898 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4900 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4901 | mddev->ro && mddev->pers) { |
| 4902 | if (mddev->ro == 2) { |
| 4903 | mddev->ro = 0; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4904 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 4905 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4906 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4907 | |
| 4908 | } else { |
| 4909 | err = -EROFS; |
| 4910 | goto abort_unlock; |
| 4911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | switch (cmd) |
| 4915 | { |
| 4916 | case ADD_NEW_DISK: |
| 4917 | { |
| 4918 | mdu_disk_info_t info; |
| 4919 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4920 | err = -EFAULT; |
| 4921 | else |
| 4922 | err = add_new_disk(mddev, &info); |
| 4923 | goto done_unlock; |
| 4924 | } |
| 4925 | |
| 4926 | case HOT_REMOVE_DISK: |
| 4927 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4928 | goto done_unlock; |
| 4929 | |
| 4930 | case HOT_ADD_DISK: |
| 4931 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4932 | goto done_unlock; |
| 4933 | |
| 4934 | case SET_DISK_FAULTY: |
| 4935 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4936 | goto done_unlock; |
| 4937 | |
| 4938 | case RUN_ARRAY: |
| 4939 | err = do_md_run (mddev); |
| 4940 | goto done_unlock; |
| 4941 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4942 | case SET_BITMAP_FILE: |
| 4943 | err = set_bitmap_file(mddev, (int)arg); |
| 4944 | goto done_unlock; |
| 4945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | err = -EINVAL; |
| 4948 | goto abort_unlock; |
| 4949 | } |
| 4950 | |
| 4951 | done_unlock: |
| 4952 | abort_unlock: |
| 4953 | mddev_unlock(mddev); |
| 4954 | |
| 4955 | return err; |
| 4956 | done: |
| 4957 | if (err) |
| 4958 | MD_BUG(); |
| 4959 | abort: |
| 4960 | return err; |
| 4961 | } |
| 4962 | |
| 4963 | static int md_open(struct inode *inode, struct file *file) |
| 4964 | { |
| 4965 | /* |
| 4966 | * Succeed if we can lock the mddev, which confirms that |
| 4967 | * it isn't being stopped right now. |
| 4968 | */ |
| 4969 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4970 | int err; |
| 4971 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 4972 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | goto out; |
| 4974 | |
| 4975 | err = 0; |
| 4976 | mddev_get(mddev); |
| 4977 | mddev_unlock(mddev); |
| 4978 | |
| 4979 | check_disk_change(inode->i_bdev); |
| 4980 | out: |
| 4981 | return err; |
| 4982 | } |
| 4983 | |
| 4984 | static int md_release(struct inode *inode, struct file * file) |
| 4985 | { |
| 4986 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4987 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4988 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | mddev_put(mddev); |
| 4990 | |
| 4991 | return 0; |
| 4992 | } |
| 4993 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4994 | static int md_media_changed(struct gendisk *disk) |
| 4995 | { |
| 4996 | mddev_t *mddev = disk->private_data; |
| 4997 | |
| 4998 | return mddev->changed; |
| 4999 | } |
| 5000 | |
| 5001 | static int md_revalidate(struct gendisk *disk) |
| 5002 | { |
| 5003 | mddev_t *mddev = disk->private_data; |
| 5004 | |
| 5005 | mddev->changed = 0; |
| 5006 | return 0; |
| 5007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | static struct block_device_operations md_fops = |
| 5009 | { |
| 5010 | .owner = THIS_MODULE, |
| 5011 | .open = md_open, |
| 5012 | .release = md_release, |
| 5013 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5014 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5015 | .media_changed = md_media_changed, |
| 5016 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | }; |
| 5018 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5019 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | { |
| 5021 | mdk_thread_t *thread = arg; |
| 5022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | /* |
| 5024 | * md_thread is a 'system-thread', it's priority should be very |
| 5025 | * high. We avoid resource deadlocks individually in each |
| 5026 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5027 | * the very same RT priority as kswapd, thus we will never get |
| 5028 | * into a priority inversion deadlock. |
| 5029 | * |
| 5030 | * we definitely have to have equal or higher priority than |
| 5031 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5032 | * many dirty RAID5 blocks. |
| 5033 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5035 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5036 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5038 | /* We need to wait INTERRUPTIBLE so that |
| 5039 | * we don't add to the load-average. |
| 5040 | * That means we need to be sure no signals are |
| 5041 | * pending |
| 5042 | */ |
| 5043 | if (signal_pending(current)) |
| 5044 | flush_signals(current); |
| 5045 | |
| 5046 | wait_event_interruptible_timeout |
| 5047 | (thread->wqueue, |
| 5048 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5049 | || kthread_should_stop(), |
| 5050 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | |
| 5052 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5053 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5054 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | return 0; |
| 5058 | } |
| 5059 | |
| 5060 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5061 | { |
| 5062 | if (thread) { |
| 5063 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5064 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5065 | wake_up(&thread->wqueue); |
| 5066 | } |
| 5067 | } |
| 5068 | |
| 5069 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5070 | const char *name) |
| 5071 | { |
| 5072 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5074 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | if (!thread) |
| 5076 | return NULL; |
| 5077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | init_waitqueue_head(&thread->wqueue); |
| 5079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | thread->run = run; |
| 5081 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5082 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5083 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5084 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | kfree(thread); |
| 5086 | return NULL; |
| 5087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | return thread; |
| 5089 | } |
| 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | void md_unregister_thread(mdk_thread_t *thread) |
| 5092 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5093 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5094 | |
| 5095 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | kfree(thread); |
| 5097 | } |
| 5098 | |
| 5099 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5100 | { |
| 5101 | if (!mddev) { |
| 5102 | MD_BUG(); |
| 5103 | return; |
| 5104 | } |
| 5105 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5106 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5108 | |
| 5109 | if (mddev->external) |
| 5110 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5111 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5113 | mdname(mddev), |
| 5114 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5115 | __builtin_return_address(0),__builtin_return_address(1), |
| 5116 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5117 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5118 | if (!mddev->pers) |
| 5119 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | if (!mddev->pers->error_handler) |
| 5121 | return; |
| 5122 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5123 | if (mddev->degraded) |
| 5124 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 5125 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5127 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5128 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5129 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | /* seq_file implementation /proc/mdstat */ |
| 5133 | |
| 5134 | static void status_unused(struct seq_file *seq) |
| 5135 | { |
| 5136 | int i = 0; |
| 5137 | mdk_rdev_t *rdev; |
| 5138 | struct list_head *tmp; |
| 5139 | |
| 5140 | seq_printf(seq, "unused devices: "); |
| 5141 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5142 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | char b[BDEVNAME_SIZE]; |
| 5144 | i++; |
| 5145 | seq_printf(seq, "%s ", |
| 5146 | bdevname(rdev->bdev,b)); |
| 5147 | } |
| 5148 | if (!i) |
| 5149 | seq_printf(seq, "<none>"); |
| 5150 | |
| 5151 | seq_printf(seq, "\n"); |
| 5152 | } |
| 5153 | |
| 5154 | |
| 5155 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5156 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5157 | sector_t max_blocks, resync, res; |
| 5158 | unsigned long dt, db, rt; |
| 5159 | int scale; |
| 5160 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
| 5162 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5163 | |
| 5164 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5165 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5166 | else |
| 5167 | max_blocks = mddev->size; |
| 5168 | |
| 5169 | /* |
| 5170 | * Should not happen. |
| 5171 | */ |
| 5172 | if (!max_blocks) { |
| 5173 | MD_BUG(); |
| 5174 | return; |
| 5175 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5176 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5177 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5178 | * u32, as those are the requirements for sector_div. |
| 5179 | * Thus 'scale' must be at least 10 |
| 5180 | */ |
| 5181 | scale = 10; |
| 5182 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5183 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5184 | scale++; |
| 5185 | } |
| 5186 | res = (resync>>scale)*1000; |
| 5187 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5188 | |
| 5189 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5191 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | seq_printf(seq, "["); |
| 5193 | for (i = 0; i < x; i++) |
| 5194 | seq_printf(seq, "="); |
| 5195 | seq_printf(seq, ">"); |
| 5196 | for (i = 0; i < y; i++) |
| 5197 | seq_printf(seq, "."); |
| 5198 | seq_printf(seq, "] "); |
| 5199 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5200 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5201 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5202 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5203 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5204 | "check" : |
| 5205 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5206 | "resync" : "recovery"))), |
| 5207 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5208 | (unsigned long long) resync, |
| 5209 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | |
| 5211 | /* |
| 5212 | * We do not want to overflow, so the order of operands and |
| 5213 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5214 | * safe against division by zero. We only estimate anyway. |
| 5215 | * |
| 5216 | * dt: time from mark until now |
| 5217 | * db: blocks written from mark until now |
| 5218 | * rt: remaining time |
| 5219 | */ |
| 5220 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5221 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5222 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5223 | - mddev->resync_mark_cnt; |
| 5224 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | |
| 5226 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5227 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5228 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5232 | { |
| 5233 | struct list_head *tmp; |
| 5234 | loff_t l = *pos; |
| 5235 | mddev_t *mddev; |
| 5236 | |
| 5237 | if (l >= 0x10000) |
| 5238 | return NULL; |
| 5239 | if (!l--) |
| 5240 | /* header */ |
| 5241 | return (void*)1; |
| 5242 | |
| 5243 | spin_lock(&all_mddevs_lock); |
| 5244 | list_for_each(tmp,&all_mddevs) |
| 5245 | if (!l--) { |
| 5246 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5247 | mddev_get(mddev); |
| 5248 | spin_unlock(&all_mddevs_lock); |
| 5249 | return mddev; |
| 5250 | } |
| 5251 | spin_unlock(&all_mddevs_lock); |
| 5252 | if (!l--) |
| 5253 | return (void*)2;/* tail */ |
| 5254 | return NULL; |
| 5255 | } |
| 5256 | |
| 5257 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5258 | { |
| 5259 | struct list_head *tmp; |
| 5260 | mddev_t *next_mddev, *mddev = v; |
| 5261 | |
| 5262 | ++*pos; |
| 5263 | if (v == (void*)2) |
| 5264 | return NULL; |
| 5265 | |
| 5266 | spin_lock(&all_mddevs_lock); |
| 5267 | if (v == (void*)1) |
| 5268 | tmp = all_mddevs.next; |
| 5269 | else |
| 5270 | tmp = mddev->all_mddevs.next; |
| 5271 | if (tmp != &all_mddevs) |
| 5272 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5273 | else { |
| 5274 | next_mddev = (void*)2; |
| 5275 | *pos = 0x10000; |
| 5276 | } |
| 5277 | spin_unlock(&all_mddevs_lock); |
| 5278 | |
| 5279 | if (v != (void*)1) |
| 5280 | mddev_put(mddev); |
| 5281 | return next_mddev; |
| 5282 | |
| 5283 | } |
| 5284 | |
| 5285 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5286 | { |
| 5287 | mddev_t *mddev = v; |
| 5288 | |
| 5289 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5290 | mddev_put(mddev); |
| 5291 | } |
| 5292 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5293 | struct mdstat_info { |
| 5294 | int event; |
| 5295 | }; |
| 5296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5298 | { |
| 5299 | mddev_t *mddev = v; |
| 5300 | sector_t size; |
| 5301 | struct list_head *tmp2; |
| 5302 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5303 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5304 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | |
| 5306 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5307 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | seq_printf(seq, "Personalities : "); |
| 5309 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5310 | list_for_each_entry(pers, &pers_list, list) |
| 5311 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | |
| 5313 | spin_unlock(&pers_lock); |
| 5314 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5315 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | return 0; |
| 5317 | } |
| 5318 | if (v == (void*)2) { |
| 5319 | status_unused(seq); |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5323 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5327 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5328 | mddev->pers ? "" : "in"); |
| 5329 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5330 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5332 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5333 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | seq_printf(seq, " %s", mddev->pers->name); |
| 5335 | } |
| 5336 | |
| 5337 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5338 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | char b[BDEVNAME_SIZE]; |
| 5340 | seq_printf(seq, " %s[%d]", |
| 5341 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5342 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5343 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5344 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | seq_printf(seq, "(F)"); |
| 5346 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5347 | } else if (rdev->raid_disk < 0) |
| 5348 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | size += rdev->size; |
| 5350 | } |
| 5351 | |
| 5352 | if (!list_empty(&mddev->disks)) { |
| 5353 | if (mddev->pers) |
| 5354 | seq_printf(seq, "\n %llu blocks", |
| 5355 | (unsigned long long)mddev->array_size); |
| 5356 | else |
| 5357 | seq_printf(seq, "\n %llu blocks", |
| 5358 | (unsigned long long)size); |
| 5359 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5360 | if (mddev->persistent) { |
| 5361 | if (mddev->major_version != 0 || |
| 5362 | mddev->minor_version != 90) { |
| 5363 | seq_printf(seq," super %d.%d", |
| 5364 | mddev->major_version, |
| 5365 | mddev->minor_version); |
| 5366 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5367 | } else if (mddev->external) |
| 5368 | seq_printf(seq, " super external:%s", |
| 5369 | mddev->metadata_type); |
| 5370 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5371 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | |
| 5373 | if (mddev->pers) { |
| 5374 | mddev->pers->status (seq, mddev); |
| 5375 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5376 | if (mddev->pers->sync_request) { |
| 5377 | if (mddev->curr_resync > 2) { |
| 5378 | status_resync (seq, mddev); |
| 5379 | seq_printf(seq, "\n "); |
| 5380 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5381 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5382 | else if (mddev->recovery_cp < MaxSector) |
| 5383 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5384 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5385 | } else |
| 5386 | seq_printf(seq, "\n "); |
| 5387 | |
| 5388 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5389 | unsigned long chunk_kb; |
| 5390 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5391 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5392 | chunk_kb = bitmap->chunksize >> 10; |
| 5393 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5394 | "%lu%s chunk", |
| 5395 | bitmap->pages - bitmap->missing_pages, |
| 5396 | bitmap->pages, |
| 5397 | (bitmap->pages - bitmap->missing_pages) |
| 5398 | << (PAGE_SHIFT - 10), |
| 5399 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5400 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5401 | if (bitmap->file) { |
| 5402 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5403 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5404 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5405 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5406 | seq_printf(seq, "\n"); |
| 5407 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | seq_printf(seq, "\n"); |
| 5411 | } |
| 5412 | mddev_unlock(mddev); |
| 5413 | |
| 5414 | return 0; |
| 5415 | } |
| 5416 | |
| 5417 | static struct seq_operations md_seq_ops = { |
| 5418 | .start = md_seq_start, |
| 5419 | .next = md_seq_next, |
| 5420 | .stop = md_seq_stop, |
| 5421 | .show = md_seq_show, |
| 5422 | }; |
| 5423 | |
| 5424 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5425 | { |
| 5426 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5427 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5428 | if (mi == NULL) |
| 5429 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | |
| 5431 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5432 | if (error) |
| 5433 | kfree(mi); |
| 5434 | else { |
| 5435 | struct seq_file *p = file->private_data; |
| 5436 | p->private = mi; |
| 5437 | mi->event = atomic_read(&md_event_count); |
| 5438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | return error; |
| 5440 | } |
| 5441 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5442 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5443 | { |
| 5444 | struct seq_file *m = filp->private_data; |
| 5445 | struct mdstat_info *mi = m->private; |
| 5446 | int mask; |
| 5447 | |
| 5448 | poll_wait(filp, &md_event_waiters, wait); |
| 5449 | |
| 5450 | /* always allow read */ |
| 5451 | mask = POLLIN | POLLRDNORM; |
| 5452 | |
| 5453 | if (mi->event != atomic_read(&md_event_count)) |
| 5454 | mask |= POLLERR | POLLPRI; |
| 5455 | return mask; |
| 5456 | } |
| 5457 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5458 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5459 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | .open = md_seq_open, |
| 5461 | .read = seq_read, |
| 5462 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5463 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5464 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | }; |
| 5466 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5467 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5470 | list_add_tail(&p->list, &pers_list); |
| 5471 | 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] | 5472 | spin_unlock(&pers_lock); |
| 5473 | return 0; |
| 5474 | } |
| 5475 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5476 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5478 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5480 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | spin_unlock(&pers_lock); |
| 5482 | return 0; |
| 5483 | } |
| 5484 | |
| 5485 | static int is_mddev_idle(mddev_t *mddev) |
| 5486 | { |
| 5487 | mdk_rdev_t * rdev; |
| 5488 | struct list_head *tmp; |
| 5489 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5490 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | |
| 5492 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5493 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5495 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5496 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5498 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5499 | * as sync_io is counted when a request starts, and |
| 5500 | * disk_stats is counted when it completes. |
| 5501 | * So resync activity will cause curr_events to be smaller than |
| 5502 | * when there was no such activity. |
| 5503 | * non-sync IO will cause disk_stat to increase without |
| 5504 | * increasing sync_io so curr_events will (eventually) |
| 5505 | * be larger than it was before. Once it becomes |
| 5506 | * substantially larger, the test below will cause |
| 5507 | * the array to appear non-idle, and resync will slow |
| 5508 | * down. |
| 5509 | * If there is a lot of outstanding resync activity when |
| 5510 | * we set last_event to curr_events, then all that activity |
| 5511 | * completing might cause the array to appear non-idle |
| 5512 | * and resync will be slowed down even though there might |
| 5513 | * not have been non-resync activity. This will only |
| 5514 | * happen once though. 'last_events' will soon reflect |
| 5515 | * the state where there is little or no outstanding |
| 5516 | * resync requests, and further resync activity will |
| 5517 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5518 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5520 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | rdev->last_events = curr_events; |
| 5522 | idle = 0; |
| 5523 | } |
| 5524 | } |
| 5525 | return idle; |
| 5526 | } |
| 5527 | |
| 5528 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5529 | { |
| 5530 | /* another "blocks" (512byte) blocks have been synced */ |
| 5531 | atomic_sub(blocks, &mddev->recovery_active); |
| 5532 | wake_up(&mddev->recovery_wait); |
| 5533 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5534 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | md_wakeup_thread(mddev->thread); |
| 5536 | // stop recovery, signal do_sync .... |
| 5537 | } |
| 5538 | } |
| 5539 | |
| 5540 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5541 | /* md_write_start(mddev, bi) |
| 5542 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5543 | * in superblock) before writing, schedule a superblock update |
| 5544 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5545 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5546 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5548 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5549 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5550 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5551 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5552 | BUG_ON(mddev->ro == 1); |
| 5553 | if (mddev->ro == 2) { |
| 5554 | /* need to switch to read/write */ |
| 5555 | mddev->ro = 0; |
| 5556 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5557 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5558 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5559 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5560 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5561 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5562 | if (mddev->safemode == 1) |
| 5563 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5564 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5565 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5566 | if (mddev->in_sync) { |
| 5567 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5568 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5569 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5570 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5571 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5572 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5573 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5574 | if (did_change) |
| 5575 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5576 | wait_event(mddev->sb_wait, |
| 5577 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5578 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | void md_write_end(mddev_t *mddev) |
| 5582 | { |
| 5583 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5584 | if (mddev->safemode == 2) |
| 5585 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5586 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5588 | } |
| 5589 | } |
| 5590 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5591 | /* md_allow_write(mddev) |
| 5592 | * Calling this ensures that the array is marked 'active' so that writes |
| 5593 | * may proceed without blocking. It is important to call this before |
| 5594 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5595 | * Must be called with mddev_lock held. |
| 5596 | */ |
| 5597 | void md_allow_write(mddev_t *mddev) |
| 5598 | { |
| 5599 | if (!mddev->pers) |
| 5600 | return; |
| 5601 | if (mddev->ro) |
| 5602 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5603 | if (!mddev->pers->sync_request) |
| 5604 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5605 | |
| 5606 | spin_lock_irq(&mddev->write_lock); |
| 5607 | if (mddev->in_sync) { |
| 5608 | mddev->in_sync = 0; |
| 5609 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5610 | if (mddev->safemode_delay && |
| 5611 | mddev->safemode == 0) |
| 5612 | mddev->safemode = 1; |
| 5613 | spin_unlock_irq(&mddev->write_lock); |
| 5614 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5615 | |
| 5616 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 5617 | /* wait for the dirty state to be recorded in the metadata */ |
| 5618 | wait_event(mddev->sb_wait, |
| 5619 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5620 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5621 | } else |
| 5622 | spin_unlock_irq(&mddev->write_lock); |
| 5623 | } |
| 5624 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | #define SYNC_MARKS 10 |
| 5627 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5628 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | { |
| 5630 | mddev_t *mddev2; |
| 5631 | unsigned int currspeed = 0, |
| 5632 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5633 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | unsigned long mark[SYNC_MARKS]; |
| 5635 | sector_t mark_cnt[SYNC_MARKS]; |
| 5636 | int last_mark,m; |
| 5637 | struct list_head *tmp; |
| 5638 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5639 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5640 | struct list_head *rtmp; |
| 5641 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5642 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | |
| 5644 | /* just incase thread restarts... */ |
| 5645 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5646 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5647 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5648 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5650 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5651 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5652 | desc = "data-check"; |
| 5653 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5654 | desc = "requested-resync"; |
| 5655 | else |
| 5656 | desc = "resync"; |
| 5657 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5658 | desc = "reshape"; |
| 5659 | else |
| 5660 | desc = "recovery"; |
| 5661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | /* we overload curr_resync somewhat here. |
| 5663 | * 0 == not engaged in resync at all |
| 5664 | * 2 == checking that there is no conflict with another sync |
| 5665 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5666 | * commense |
| 5667 | * other == active in resync - this many blocks |
| 5668 | * |
| 5669 | * Before starting a resync we must have set curr_resync to |
| 5670 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5671 | * less than ours. When we find one that is the same or higher |
| 5672 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5673 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5674 | * This will mean we have to start checking from the beginning again. |
| 5675 | * |
| 5676 | */ |
| 5677 | |
| 5678 | do { |
| 5679 | mddev->curr_resync = 2; |
| 5680 | |
| 5681 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5682 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5683 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | goto skip; |
| 5685 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5686 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | if (mddev2 == mddev) |
| 5688 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5689 | if (!mddev->parallel_resync |
| 5690 | && mddev2->curr_resync |
| 5691 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | DEFINE_WAIT(wq); |
| 5693 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5694 | /* arbitrarily yield */ |
| 5695 | mddev->curr_resync = 1; |
| 5696 | wake_up(&resync_wait); |
| 5697 | } |
| 5698 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5699 | /* no need to wait here, we can wait the next |
| 5700 | * time 'round when curr_resync == 2 |
| 5701 | */ |
| 5702 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5703 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5704 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5705 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5706 | printk(KERN_INFO "md: delaying %s of %s" |
| 5707 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5709 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | mddev_put(mddev2); |
| 5711 | schedule(); |
| 5712 | finish_wait(&resync_wait, &wq); |
| 5713 | goto try_again; |
| 5714 | } |
| 5715 | finish_wait(&resync_wait, &wq); |
| 5716 | } |
| 5717 | } |
| 5718 | } while (mddev->curr_resync < 2); |
| 5719 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5720 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5721 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5723 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | */ |
| 5725 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5726 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5727 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5728 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5729 | j = mddev->resync_min; |
| 5730 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5731 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5732 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5733 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5734 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5735 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | /* recovery follows the physical size of devices */ |
| 5737 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5738 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5739 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5740 | if (rdev->raid_disk >= 0 && |
| 5741 | !test_bit(Faulty, &rdev->flags) && |
| 5742 | !test_bit(In_sync, &rdev->flags) && |
| 5743 | rdev->recovery_offset < j) |
| 5744 | j = rdev->recovery_offset; |
| 5745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5747 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5748 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5749 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5750 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5751 | "(but not more than %d KB/sec) for %s.\n", |
| 5752 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | |
| 5754 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5755 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5756 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5758 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5759 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | } |
| 5761 | last_mark = 0; |
| 5762 | mddev->resync_mark = mark[last_mark]; |
| 5763 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5764 | |
| 5765 | /* |
| 5766 | * Tune reconstruction: |
| 5767 | */ |
| 5768 | window = 32*(PAGE_SIZE/512); |
| 5769 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5770 | window/2,(unsigned long long) max_sectors/2); |
| 5771 | |
| 5772 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | last_check = 0; |
| 5774 | |
| 5775 | if (j>2) { |
| 5776 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5777 | "md: resuming %s of %s from checkpoint.\n", |
| 5778 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | mddev->curr_resync = j; |
| 5780 | } |
| 5781 | |
| 5782 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5783 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5785 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5786 | if (j >= mddev->resync_max) { |
| 5787 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5788 | wait_event(mddev->recovery_wait, |
| 5789 | mddev->resync_max > j |
| 5790 | || kthread_should_stop()); |
| 5791 | } |
| 5792 | if (kthread_should_stop()) |
| 5793 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5794 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5795 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5796 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5797 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | goto out; |
| 5799 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5800 | |
| 5801 | if (!skipped) { /* actual IO requested */ |
| 5802 | io_sectors += sectors; |
| 5803 | atomic_add(sectors, &mddev->recovery_active); |
| 5804 | } |
| 5805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | j += sectors; |
| 5807 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5808 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5809 | if (last_check == 0) |
| 5810 | /* this is the earliers that rebuilt will be |
| 5811 | * visible in /proc/mdstat |
| 5812 | */ |
| 5813 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5814 | |
| 5815 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | continue; |
| 5817 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5818 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5820 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | break; |
| 5822 | |
| 5823 | repeat: |
| 5824 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5825 | /* step marks */ |
| 5826 | int next = (last_mark+1) % SYNC_MARKS; |
| 5827 | |
| 5828 | mddev->resync_mark = mark[next]; |
| 5829 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5830 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5831 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | last_mark = next; |
| 5833 | } |
| 5834 | |
| 5835 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5836 | if (kthread_should_stop()) |
| 5837 | goto interrupted; |
| 5838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
| 5840 | /* |
| 5841 | * this loop exits only if either when we are slower than |
| 5842 | * the 'hard' speed limit, or the system was IO-idle for |
| 5843 | * a jiffy. |
| 5844 | * the system might be non-idle CPU-wise, but we only care |
| 5845 | * about not overloading the IO subsystem. (things like an |
| 5846 | * e2fsck being done on the RAID array should execute fast) |
| 5847 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5848 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | cond_resched(); |
| 5850 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5851 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5852 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5854 | if (currspeed > speed_min(mddev)) { |
| 5855 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5857 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | goto repeat; |
| 5859 | } |
| 5860 | } |
| 5861 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5862 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | /* |
| 5864 | * this also signals 'finished resyncing' to md_stop |
| 5865 | */ |
| 5866 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5867 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | |
| 5869 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5870 | |
| 5871 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5872 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5874 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5875 | mddev->curr_resync > 2) { |
| 5876 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5877 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5878 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5879 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5880 | "md: checkpointing %s of %s.\n", |
| 5881 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5882 | mddev->recovery_cp = mddev->curr_resync; |
| 5883 | } |
| 5884 | } else |
| 5885 | mddev->recovery_cp = MaxSector; |
| 5886 | } else { |
| 5887 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5888 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5889 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5890 | if (rdev->raid_disk >= 0 && |
| 5891 | !test_bit(Faulty, &rdev->flags) && |
| 5892 | !test_bit(In_sync, &rdev->flags) && |
| 5893 | rdev->recovery_offset < mddev->curr_resync) |
| 5894 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5897 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | skip: |
| 5900 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5901 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5902 | mddev->resync_max = MaxSector; |
| 5903 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | wake_up(&resync_wait); |
| 5905 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5906 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5907 | return; |
| 5908 | |
| 5909 | interrupted: |
| 5910 | /* |
| 5911 | * got a signal, exit. |
| 5912 | */ |
| 5913 | printk(KERN_INFO |
| 5914 | "md: md_do_sync() got signal ... exiting\n"); |
| 5915 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5916 | goto out; |
| 5917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5919 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | |
| 5921 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5922 | static int remove_and_add_spares(mddev_t *mddev) |
| 5923 | { |
| 5924 | mdk_rdev_t *rdev; |
| 5925 | struct list_head *rtmp; |
| 5926 | int spares = 0; |
| 5927 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5928 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5929 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5930 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5931 | (test_bit(Faulty, &rdev->flags) || |
| 5932 | ! test_bit(In_sync, &rdev->flags)) && |
| 5933 | atomic_read(&rdev->nr_pending)==0) { |
| 5934 | if (mddev->pers->hot_remove_disk( |
| 5935 | mddev, rdev->raid_disk)==0) { |
| 5936 | char nm[20]; |
| 5937 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5938 | sysfs_remove_link(&mddev->kobj, nm); |
| 5939 | rdev->raid_disk = -1; |
| 5940 | } |
| 5941 | } |
| 5942 | |
| 5943 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5944 | rdev_for_each(rdev, rtmp, mddev) { |
| 5945 | if (rdev->raid_disk >= 0 && |
| 5946 | !test_bit(In_sync, &rdev->flags)) |
| 5947 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5948 | if (rdev->raid_disk < 0 |
| 5949 | && !test_bit(Faulty, &rdev->flags)) { |
| 5950 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5951 | if (mddev->pers-> |
| 5952 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5953 | char nm[20]; |
| 5954 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5955 | if (sysfs_create_link(&mddev->kobj, |
| 5956 | &rdev->kobj, nm)) |
| 5957 | printk(KERN_WARNING |
| 5958 | "md: cannot register " |
| 5959 | "%s for %s\n", |
| 5960 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5961 | spares++; |
| 5962 | md_new_event(mddev); |
| 5963 | } else |
| 5964 | break; |
| 5965 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5966 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5967 | } |
| 5968 | return spares; |
| 5969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | /* |
| 5971 | * This routine is regularly called by all per-raid-array threads to |
| 5972 | * deal with generic issues like resync and super-block update. |
| 5973 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5974 | * need this as they never do any recovery or update the superblock. |
| 5975 | * |
| 5976 | * It does not do any resync itself, but rather "forks" off other threads |
| 5977 | * to do that as needed. |
| 5978 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5979 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5980 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | * and wakeups up this thread which will reap the thread and finish up. |
| 5982 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5983 | * |
| 5984 | * The overall approach is: |
| 5985 | * 1/ if the superblock needs updating, update it. |
| 5986 | * 2/ If a recovery thread is running, don't do anything else. |
| 5987 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5988 | * 4/ If there are any faulty devices, remove them. |
| 5989 | * 5/ If array is degraded, try to add spares devices |
| 5990 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5991 | */ |
| 5992 | void md_check_recovery(mddev_t *mddev) |
| 5993 | { |
| 5994 | mdk_rdev_t *rdev; |
| 5995 | struct list_head *rtmp; |
| 5996 | |
| 5997 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5998 | if (mddev->bitmap) |
| 5999 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | |
| 6001 | if (mddev->ro) |
| 6002 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6003 | |
| 6004 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6005 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6006 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6007 | mdname(mddev)); |
| 6008 | mddev->safemode = 2; |
| 6009 | } |
| 6010 | flush_signals(current); |
| 6011 | } |
| 6012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6014 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6016 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6017 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6018 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6019 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | )) |
| 6021 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6022 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6023 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6024 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6025 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6026 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6027 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6028 | spin_lock_irq(&mddev->write_lock); |
| 6029 | if (mddev->safemode && |
| 6030 | !atomic_read(&mddev->writes_pending) && |
| 6031 | !mddev->in_sync && |
| 6032 | mddev->recovery_cp == MaxSector) { |
| 6033 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6034 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6035 | if (mddev->persistent) |
| 6036 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6037 | } |
| 6038 | if (mddev->safemode == 1) |
| 6039 | mddev->safemode = 0; |
| 6040 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6041 | if (did_change) |
| 6042 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6043 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6044 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6045 | if (mddev->flags) |
| 6046 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6047 | |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame^] | 6048 | rdev_for_each(rdev, rtmp, mddev) |
| 6049 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
| 6050 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6051 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6054 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6055 | /* resync/recovery still happening */ |
| 6056 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6057 | goto unlock; |
| 6058 | } |
| 6059 | if (mddev->sync_thread) { |
| 6060 | /* resync has finished, collect result */ |
| 6061 | md_unregister_thread(mddev->sync_thread); |
| 6062 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6063 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | /* success...*/ |
| 6065 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6066 | if (mddev->pers->spare_active(mddev)) |
| 6067 | sysfs_notify(&mddev->kobj, NULL, |
| 6068 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6070 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6071 | |
| 6072 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6073 | * information must be scrapped |
| 6074 | */ |
| 6075 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6076 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6077 | rdev->saved_raid_disk = -1; |
| 6078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | mddev->recovery = 0; |
| 6080 | /* flag recovery needed just to double check */ |
| 6081 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6082 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6083 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | goto unlock; |
| 6085 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6086 | /* Set RUNNING before clearing NEEDED to avoid |
| 6087 | * any transients in the value of "sync_action". |
| 6088 | */ |
| 6089 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6090 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6091 | /* Clear some bits that don't mean anything, but |
| 6092 | * might be left set |
| 6093 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6094 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6095 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6097 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6098 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | /* no recovery is running. |
| 6100 | * remove any failed drives, then |
| 6101 | * add spares if possible. |
| 6102 | * Spare are also removed and re-added, to allow |
| 6103 | * the personality to fail the re-add. |
| 6104 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6106 | if (mddev->reshape_position != MaxSector) { |
| 6107 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6108 | /* Cannot proceed */ |
| 6109 | goto unlock; |
| 6110 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6111 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6112 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6113 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6114 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6115 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6116 | } else if (mddev->recovery_cp < MaxSector) { |
| 6117 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6118 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6119 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6120 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6124 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6125 | /* We are adding a device or devices to an array |
| 6126 | * which has the bitmap stored on all devices. |
| 6127 | * So make sure all bitmap pages get written |
| 6128 | */ |
| 6129 | bitmap_write_all(mddev->bitmap); |
| 6130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6132 | mddev, |
| 6133 | "%s_resync"); |
| 6134 | if (!mddev->sync_thread) { |
| 6135 | printk(KERN_ERR "%s: could not start resync" |
| 6136 | " thread...\n", |
| 6137 | mdname(mddev)); |
| 6138 | /* leave the spares where they are, it shouldn't hurt */ |
| 6139 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6140 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6142 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6143 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | } |
| 6145 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6146 | if (!mddev->sync_thread) { |
| 6147 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6148 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6149 | &mddev->recovery)) |
| 6150 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
| 6151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | mddev_unlock(mddev); |
| 6153 | } |
| 6154 | } |
| 6155 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6156 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6157 | { |
| 6158 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6159 | wait_event_timeout(rdev->blocked_wait, |
| 6160 | !test_bit(Blocked, &rdev->flags), |
| 6161 | msecs_to_jiffies(5000)); |
| 6162 | rdev_dec_pending(rdev, mddev); |
| 6163 | } |
| 6164 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6165 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6166 | static int md_notify_reboot(struct notifier_block *this, |
| 6167 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | { |
| 6169 | struct list_head *tmp; |
| 6170 | mddev_t *mddev; |
| 6171 | |
| 6172 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6173 | |
| 6174 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6175 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6176 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6177 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6179 | mddev_unlock(mddev); |
| 6180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | /* |
| 6182 | * certain more exotic SCSI devices are known to be |
| 6183 | * volatile wrt too early system reboots. While the |
| 6184 | * right place to handle this issue is the given |
| 6185 | * driver, we do want to have a safe RAID driver ... |
| 6186 | */ |
| 6187 | mdelay(1000*1); |
| 6188 | } |
| 6189 | return NOTIFY_DONE; |
| 6190 | } |
| 6191 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6192 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | .notifier_call = md_notify_reboot, |
| 6194 | .next = NULL, |
| 6195 | .priority = INT_MAX, /* before any real devices */ |
| 6196 | }; |
| 6197 | |
| 6198 | static void md_geninit(void) |
| 6199 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6201 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6202 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | } |
| 6204 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6205 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | if (register_blkdev(MAJOR_NR, "md")) |
| 6208 | return -1; |
| 6209 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6210 | unregister_blkdev(MAJOR_NR, "md"); |
| 6211 | return -1; |
| 6212 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6213 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6214 | md_probe, NULL, NULL); |
| 6215 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | md_probe, NULL, NULL); |
| 6217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6219 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | |
| 6221 | md_geninit(); |
| 6222 | return (0); |
| 6223 | } |
| 6224 | |
| 6225 | |
| 6226 | #ifndef MODULE |
| 6227 | |
| 6228 | /* |
| 6229 | * Searches all registered partitions for autorun RAID arrays |
| 6230 | * at boot time. |
| 6231 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6232 | |
| 6233 | static LIST_HEAD(all_detected_devices); |
| 6234 | struct detected_devices_node { |
| 6235 | struct list_head list; |
| 6236 | dev_t dev; |
| 6237 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | |
| 6239 | void md_autodetect_dev(dev_t dev) |
| 6240 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6241 | struct detected_devices_node *node_detected_dev; |
| 6242 | |
| 6243 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6244 | if (node_detected_dev) { |
| 6245 | node_detected_dev->dev = dev; |
| 6246 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6247 | } else { |
| 6248 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6249 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | |
| 6254 | static void autostart_arrays(int part) |
| 6255 | { |
| 6256 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6257 | struct detected_devices_node *node_detected_dev; |
| 6258 | dev_t dev; |
| 6259 | int i_scanned, i_passed; |
| 6260 | |
| 6261 | i_scanned = 0; |
| 6262 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | |
| 6264 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6265 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6266 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6267 | i_scanned++; |
| 6268 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6269 | struct detected_devices_node, list); |
| 6270 | list_del(&node_detected_dev->list); |
| 6271 | dev = node_detected_dev->dev; |
| 6272 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6273 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | if (IS_ERR(rdev)) |
| 6275 | continue; |
| 6276 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6277 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | MD_BUG(); |
| 6279 | continue; |
| 6280 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6281 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6283 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6285 | |
| 6286 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6287 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | |
| 6289 | autorun_devices(part); |
| 6290 | } |
| 6291 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6292 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | |
| 6294 | static __exit void md_exit(void) |
| 6295 | { |
| 6296 | mddev_t *mddev; |
| 6297 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6298 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6299 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6300 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | |
| 6302 | unregister_blkdev(MAJOR_NR,"md"); |
| 6303 | unregister_blkdev(mdp_major, "mdp"); |
| 6304 | unregister_reboot_notifier(&md_notifier); |
| 6305 | unregister_sysctl_table(raid_table_header); |
| 6306 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6307 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | struct gendisk *disk = mddev->gendisk; |
| 6309 | if (!disk) |
| 6310 | continue; |
| 6311 | export_array(mddev); |
| 6312 | del_gendisk(disk); |
| 6313 | put_disk(disk); |
| 6314 | mddev->gendisk = NULL; |
| 6315 | mddev_put(mddev); |
| 6316 | } |
| 6317 | } |
| 6318 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6319 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | module_exit(md_exit) |
| 6321 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6322 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6323 | { |
| 6324 | return sprintf(buffer, "%d", start_readonly); |
| 6325 | } |
| 6326 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6327 | { |
| 6328 | char *e; |
| 6329 | int num = simple_strtoul(val, &e, 10); |
| 6330 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6331 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6332 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6333 | } |
| 6334 | return -EINVAL; |
| 6335 | } |
| 6336 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6337 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6338 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6339 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | EXPORT_SYMBOL(register_md_personality); |
| 6342 | EXPORT_SYMBOL(unregister_md_personality); |
| 6343 | EXPORT_SYMBOL(md_error); |
| 6344 | EXPORT_SYMBOL(md_done_sync); |
| 6345 | EXPORT_SYMBOL(md_write_start); |
| 6346 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | EXPORT_SYMBOL(md_register_thread); |
| 6348 | EXPORT_SYMBOL(md_unregister_thread); |
| 6349 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | EXPORT_SYMBOL(md_check_recovery); |
| 6351 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6352 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6353 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |