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 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 350 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 351 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 353 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 354 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 357 | static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 359 | sector_t num_sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (chunk_size) |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 362 | num_sectors &= ~((sector_t)chunk_size/512 - 1); |
| 363 | return num_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 367 | { |
| 368 | if (rdev->sb_page) |
| 369 | MD_BUG(); |
| 370 | |
| 371 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 372 | if (!rdev->sb_page) { |
| 373 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 374 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 381 | { |
| 382 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 383 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | rdev->sb_loaded = 0; |
| 385 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 386 | rdev->sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | rdev->size = 0; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 392 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 393 | { |
| 394 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 395 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 396 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 397 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 398 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 399 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 400 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 401 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 402 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 403 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 404 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 405 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 406 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 407 | } |
| 408 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 409 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 410 | { |
| 411 | struct bio *bio2 = bio->bi_private; |
| 412 | mdk_rdev_t *rdev = bio2->bi_private; |
| 413 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 414 | |
| 415 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 416 | error == -EOPNOTSUPP) { |
| 417 | unsigned long flags; |
| 418 | /* barriers don't appear to be supported :-( */ |
| 419 | set_bit(BarriersNotsupp, &rdev->flags); |
| 420 | mddev->barriers_work = 0; |
| 421 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 422 | bio2->bi_next = mddev->biolist; |
| 423 | mddev->biolist = bio2; |
| 424 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 425 | wake_up(&mddev->sb_wait); |
| 426 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 427 | } else { |
| 428 | bio_put(bio2); |
| 429 | bio->bi_private = rdev; |
| 430 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 431 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 432 | } |
| 433 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 434 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 435 | sector_t sector, int size, struct page *page) |
| 436 | { |
| 437 | /* write first size bytes of page to sector of rdev |
| 438 | * Increment mddev->pending_writes before returning |
| 439 | * and decrement it on completion, waking up sb_wait |
| 440 | * if zero is reached. |
| 441 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 442 | * |
| 443 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 444 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 445 | */ |
| 446 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 447 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 448 | |
| 449 | bio->bi_bdev = rdev->bdev; |
| 450 | bio->bi_sector = sector; |
| 451 | bio_add_page(bio, page, size, 0); |
| 452 | bio->bi_private = rdev; |
| 453 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 454 | bio->bi_rw = rw; |
| 455 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 456 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 457 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 458 | struct bio *rbio; |
| 459 | rw |= (1<<BIO_RW_BARRIER); |
| 460 | rbio = bio_clone(bio, GFP_NOIO); |
| 461 | rbio->bi_private = bio; |
| 462 | rbio->bi_end_io = super_written_barrier; |
| 463 | submit_bio(rw, rbio); |
| 464 | } else |
| 465 | submit_bio(rw, bio); |
| 466 | } |
| 467 | |
| 468 | void md_super_wait(mddev_t *mddev) |
| 469 | { |
| 470 | /* wait for all superblock writes that were scheduled to complete. |
| 471 | * if any had to be retried (due to BARRIER problems), retry them |
| 472 | */ |
| 473 | DEFINE_WAIT(wq); |
| 474 | for(;;) { |
| 475 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 476 | if (atomic_read(&mddev->pending_writes)==0) |
| 477 | break; |
| 478 | while (mddev->biolist) { |
| 479 | struct bio *bio; |
| 480 | spin_lock_irq(&mddev->write_lock); |
| 481 | bio = mddev->biolist; |
| 482 | mddev->biolist = bio->bi_next ; |
| 483 | bio->bi_next = NULL; |
| 484 | spin_unlock_irq(&mddev->write_lock); |
| 485 | submit_bio(bio->bi_rw, bio); |
| 486 | } |
| 487 | schedule(); |
| 488 | } |
| 489 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 490 | } |
| 491 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 492 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 497 | 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] | 498 | struct page *page, int rw) |
| 499 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 500 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | struct completion event; |
| 502 | int ret; |
| 503 | |
| 504 | rw |= (1 << BIO_RW_SYNC); |
| 505 | |
| 506 | bio->bi_bdev = bdev; |
| 507 | bio->bi_sector = sector; |
| 508 | bio_add_page(bio, page, size, 0); |
| 509 | init_completion(&event); |
| 510 | bio->bi_private = &event; |
| 511 | bio->bi_end_io = bi_complete; |
| 512 | submit_bio(rw, bio); |
| 513 | wait_for_completion(&event); |
| 514 | |
| 515 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 516 | bio_put(bio); |
| 517 | return ret; |
| 518 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 519 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 521 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
| 523 | char b[BDEVNAME_SIZE]; |
| 524 | if (!rdev->sb_page) { |
| 525 | MD_BUG(); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | if (rdev->sb_loaded) |
| 529 | return 0; |
| 530 | |
| 531 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 532 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | goto fail; |
| 534 | rdev->sb_loaded = 1; |
| 535 | return 0; |
| 536 | |
| 537 | fail: |
| 538 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 539 | bdevname(rdev->bdev,b)); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 544 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 545 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 546 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 547 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 548 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 552 | { |
| 553 | int ret; |
| 554 | mdp_super_t *tmp1, *tmp2; |
| 555 | |
| 556 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 557 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 558 | |
| 559 | if (!tmp1 || !tmp2) { |
| 560 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 561 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto abort; |
| 563 | } |
| 564 | |
| 565 | *tmp1 = *sb1; |
| 566 | *tmp2 = *sb2; |
| 567 | |
| 568 | /* |
| 569 | * nr_disks is not constant |
| 570 | */ |
| 571 | tmp1->nr_disks = 0; |
| 572 | tmp2->nr_disks = 0; |
| 573 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 574 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 576 | kfree(tmp1); |
| 577 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | return ret; |
| 579 | } |
| 580 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 581 | |
| 582 | static u32 md_csum_fold(u32 csum) |
| 583 | { |
| 584 | csum = (csum & 0xffff) + (csum >> 16); |
| 585 | return (csum & 0xffff) + (csum >> 16); |
| 586 | } |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 589 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 590 | u64 newcsum = 0; |
| 591 | u32 *sb32 = (u32*)sb; |
| 592 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | unsigned int disk_csum, csum; |
| 594 | |
| 595 | disk_csum = sb->sb_csum; |
| 596 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 597 | |
| 598 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 599 | newcsum += sb32[i]; |
| 600 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 601 | |
| 602 | |
| 603 | #ifdef CONFIG_ALPHA |
| 604 | /* This used to use csum_partial, which was wrong for several |
| 605 | * reasons including that different results are returned on |
| 606 | * different architectures. It isn't critical that we get exactly |
| 607 | * the same return value as before (we always csum_fold before |
| 608 | * testing, and that removes any differences). However as we |
| 609 | * know that csum_partial always returned a 16bit value on |
| 610 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 611 | */ |
| 612 | sb->sb_csum = md_csum_fold(disk_csum); |
| 613 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 615 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return csum; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /* |
| 621 | * Handle superblock details. |
| 622 | * We want to be able to handle multiple superblock formats |
| 623 | * so we have a common interface to them all, and an array of |
| 624 | * different handlers. |
| 625 | * We rely on user-space to write the initial superblock, and support |
| 626 | * reading and updating of superblocks. |
| 627 | * Interface methods are: |
| 628 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 629 | * loads and validates a superblock on dev. |
| 630 | * if refdev != NULL, compare superblocks on both devices |
| 631 | * Return: |
| 632 | * 0 - dev has a superblock that is compatible with refdev |
| 633 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 634 | * so dev should be used as the refdev in future |
| 635 | * -EINVAL superblock incompatible or invalid |
| 636 | * -othererror e.g. -EIO |
| 637 | * |
| 638 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 639 | * Verify that dev is acceptable into mddev. |
| 640 | * The first time, mddev->raid_disks will be 0, and data from |
| 641 | * dev should be merged in. Subsequent calls check that dev |
| 642 | * is new enough. Return 0 or -EINVAL |
| 643 | * |
| 644 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 645 | * Update the superblock for rdev with data in mddev |
| 646 | * This does not write to disc. |
| 647 | * |
| 648 | */ |
| 649 | |
| 650 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 651 | char *name; |
| 652 | struct module *owner; |
| 653 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 654 | int minor_version); |
| 655 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 656 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 657 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 658 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | /* |
| 662 | * load_super for 0.90.0 |
| 663 | */ |
| 664 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 665 | { |
| 666 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 667 | mdp_super_t *sb; |
| 668 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
| 670 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 671 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | * it's at the end of the disk. |
| 673 | * |
| 674 | * It also happens to be a multiple of 4Kb. |
| 675 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 676 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 678 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (ret) return ret; |
| 680 | |
| 681 | ret = -EINVAL; |
| 682 | |
| 683 | bdevname(rdev->bdev, b); |
| 684 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 685 | |
| 686 | if (sb->md_magic != MD_SB_MAGIC) { |
| 687 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 688 | b); |
| 689 | goto abort; |
| 690 | } |
| 691 | |
| 692 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 693 | sb->minor_version < 90 || |
| 694 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 696 | sb->major_version, sb->minor_version, |
| 697 | b); |
| 698 | goto abort; |
| 699 | } |
| 700 | |
| 701 | if (sb->raid_disks <= 0) |
| 702 | goto abort; |
| 703 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 704 | 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] | 705 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 706 | b); |
| 707 | goto abort; |
| 708 | } |
| 709 | |
| 710 | rdev->preferred_minor = sb->md_minor; |
| 711 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 712 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 714 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 715 | if (sb->level != 1 && sb->level != 4 |
| 716 | && sb->level != 5 && sb->level != 6 |
| 717 | && sb->level != 10) { |
| 718 | /* FIXME use a better test */ |
| 719 | printk(KERN_WARNING |
| 720 | "md: bitmaps not supported for this level.\n"); |
| 721 | goto abort; |
| 722 | } |
| 723 | } |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (sb->level == LEVEL_MULTIPATH) |
| 726 | rdev->desc_nr = -1; |
| 727 | else |
| 728 | rdev->desc_nr = sb->this_disk.number; |
| 729 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 730 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 732 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | __u64 ev1, ev2; |
| 734 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 735 | if (!uuid_equal(refsb, sb)) { |
| 736 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 737 | b, bdevname(refdev->bdev,b2)); |
| 738 | goto abort; |
| 739 | } |
| 740 | if (!sb_equal(refsb, sb)) { |
| 741 | printk(KERN_WARNING "md: %s has same UUID" |
| 742 | " but different superblock to %s\n", |
| 743 | b, bdevname(refdev->bdev, b2)); |
| 744 | goto abort; |
| 745 | } |
| 746 | ev1 = md_event(sb); |
| 747 | ev2 = md_event(refsb); |
| 748 | if (ev1 > ev2) |
| 749 | ret = 1; |
| 750 | else |
| 751 | ret = 0; |
| 752 | } |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 753 | rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 755 | if (rdev->size < sb->size && sb->level > 1) |
| 756 | /* "this cannot possibly happen" ... */ |
| 757 | ret = -EINVAL; |
| 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | abort: |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * validate_super for 0.90.0 |
| 765 | */ |
| 766 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 767 | { |
| 768 | mdp_disk_t *desc; |
| 769 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 770 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 772 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 773 | clear_bit(Faulty, &rdev->flags); |
| 774 | clear_bit(In_sync, &rdev->flags); |
| 775 | clear_bit(WriteMostly, &rdev->flags); |
| 776 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (mddev->raid_disks == 0) { |
| 779 | mddev->major_version = 0; |
| 780 | mddev->minor_version = sb->minor_version; |
| 781 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 782 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | mddev->chunk_size = sb->chunk_size; |
| 784 | mddev->ctime = sb->ctime; |
| 785 | mddev->utime = sb->utime; |
| 786 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 787 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | mddev->layout = sb->layout; |
| 789 | mddev->raid_disks = sb->raid_disks; |
| 790 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 791 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 792 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 793 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 795 | if (mddev->minor_version >= 91) { |
| 796 | mddev->reshape_position = sb->reshape_position; |
| 797 | mddev->delta_disks = sb->delta_disks; |
| 798 | mddev->new_level = sb->new_level; |
| 799 | mddev->new_layout = sb->new_layout; |
| 800 | mddev->new_chunk = sb->new_chunk; |
| 801 | } else { |
| 802 | mddev->reshape_position = MaxSector; |
| 803 | mddev->delta_disks = 0; |
| 804 | mddev->new_level = mddev->level; |
| 805 | mddev->new_layout = mddev->layout; |
| 806 | mddev->new_chunk = mddev->chunk_size; |
| 807 | } |
| 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 810 | mddev->recovery_cp = MaxSector; |
| 811 | else { |
| 812 | if (sb->events_hi == sb->cp_events_hi && |
| 813 | sb->events_lo == sb->cp_events_lo) { |
| 814 | mddev->recovery_cp = sb->recovery_cp; |
| 815 | } else |
| 816 | mddev->recovery_cp = 0; |
| 817 | } |
| 818 | |
| 819 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 820 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 821 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 822 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 823 | |
| 824 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 825 | |
| 826 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 827 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 828 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 829 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 830 | } else if (mddev->pers == NULL) { |
| 831 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | ++ev1; |
| 833 | if (ev1 < mddev->events) |
| 834 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 835 | } else if (mddev->bitmap) { |
| 836 | /* if adding to array with a bitmap, then we can accept an |
| 837 | * older device ... but not too old. |
| 838 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 839 | if (ev1 < mddev->bitmap->events_cleared) |
| 840 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 841 | } else { |
| 842 | if (ev1 < mddev->events) |
| 843 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 844 | return 0; |
| 845 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | desc = sb->disks + rdev->desc_nr; |
| 849 | |
| 850 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 851 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 852 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 853 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 854 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | rdev->raid_disk = desc->raid_disk; |
| 856 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 857 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 858 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 859 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 860 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * sync_super for 0.90.0 |
| 866 | */ |
| 867 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 868 | { |
| 869 | mdp_super_t *sb; |
| 870 | struct list_head *tmp; |
| 871 | mdk_rdev_t *rdev2; |
| 872 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
| 875 | /* make rdev->sb match mddev data.. |
| 876 | * |
| 877 | * 1/ zero out disks |
| 878 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 879 | * 3/ any empty disks < next_spare become removed |
| 880 | * |
| 881 | * disks[0] gets initialised to REMOVED because |
| 882 | * we cannot be sure from other fields if it has |
| 883 | * been initialised or not. |
| 884 | */ |
| 885 | int i; |
| 886 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 887 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 888 | rdev->sb_size = MD_SB_BYTES; |
| 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 891 | |
| 892 | memset(sb, 0, sizeof(*sb)); |
| 893 | |
| 894 | sb->md_magic = MD_SB_MAGIC; |
| 895 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | sb->patch_version = mddev->patch_version; |
| 897 | sb->gvalid_words = 0; /* ignored */ |
| 898 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 899 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 900 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 901 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 902 | |
| 903 | sb->ctime = mddev->ctime; |
| 904 | sb->level = mddev->level; |
| 905 | sb->size = mddev->size; |
| 906 | sb->raid_disks = mddev->raid_disks; |
| 907 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 908 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | sb->utime = mddev->utime; |
| 910 | sb->state = 0; |
| 911 | sb->events_hi = (mddev->events>>32); |
| 912 | sb->events_lo = (u32)mddev->events; |
| 913 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 914 | if (mddev->reshape_position == MaxSector) |
| 915 | sb->minor_version = 90; |
| 916 | else { |
| 917 | sb->minor_version = 91; |
| 918 | sb->reshape_position = mddev->reshape_position; |
| 919 | sb->new_level = mddev->new_level; |
| 920 | sb->delta_disks = mddev->delta_disks; |
| 921 | sb->new_layout = mddev->new_layout; |
| 922 | sb->new_chunk = mddev->new_chunk; |
| 923 | } |
| 924 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (mddev->in_sync) |
| 926 | { |
| 927 | sb->recovery_cp = mddev->recovery_cp; |
| 928 | sb->cp_events_hi = (mddev->events>>32); |
| 929 | sb->cp_events_lo = (u32)mddev->events; |
| 930 | if (mddev->recovery_cp == MaxSector) |
| 931 | sb->state = (1<< MD_SB_CLEAN); |
| 932 | } else |
| 933 | sb->recovery_cp = 0; |
| 934 | |
| 935 | sb->layout = mddev->layout; |
| 936 | sb->chunk_size = mddev->chunk_size; |
| 937 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 938 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 939 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 942 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 944 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 945 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 946 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 947 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 949 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 950 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | d = &sb->disks[rdev2->desc_nr]; |
| 952 | nr_disks++; |
| 953 | d->number = rdev2->desc_nr; |
| 954 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 955 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 956 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 957 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | d->raid_disk = rdev2->raid_disk; |
| 959 | else |
| 960 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 961 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 963 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | d->state = (1<<MD_DISK_ACTIVE); |
| 965 | d->state |= (1<<MD_DISK_SYNC); |
| 966 | active++; |
| 967 | working++; |
| 968 | } else { |
| 969 | d->state = 0; |
| 970 | spare++; |
| 971 | working++; |
| 972 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 973 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 974 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 977 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 978 | mdp_disk_t *d = &sb->disks[i]; |
| 979 | if (d->state == 0 && d->number == 0) { |
| 980 | d->number = i; |
| 981 | d->raid_disk = i; |
| 982 | d->state = (1<<MD_DISK_REMOVED); |
| 983 | d->state |= (1<<MD_DISK_FAULTY); |
| 984 | failed++; |
| 985 | } |
| 986 | } |
| 987 | sb->nr_disks = nr_disks; |
| 988 | sb->active_disks = active; |
| 989 | sb->working_disks = working; |
| 990 | sb->failed_disks = failed; |
| 991 | sb->spare_disks = spare; |
| 992 | |
| 993 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 994 | sb->sb_csum = calc_sb_csum(sb); |
| 995 | } |
| 996 | |
| 997 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 998 | * rdev_size_change for 0.90.0 |
| 999 | */ |
| 1000 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1001 | super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1002 | { |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1003 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1004 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1005 | if (rdev->mddev->bitmap_offset) |
| 1006 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1007 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1008 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1009 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1010 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1011 | rdev->sb_page); |
| 1012 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1013 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | * version 1 superblock |
| 1019 | */ |
| 1020 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1021 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1023 | __le32 disk_csum; |
| 1024 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | unsigned long long newcsum; |
| 1026 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1027 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | int i; |
| 1029 | |
| 1030 | disk_csum = sb->sb_csum; |
| 1031 | sb->sb_csum = 0; |
| 1032 | newcsum = 0; |
| 1033 | for (i=0; size>=4; size -= 4 ) |
| 1034 | newcsum += le32_to_cpu(*isuper++); |
| 1035 | |
| 1036 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1037 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1040 | sb->sb_csum = disk_csum; |
| 1041 | return cpu_to_le32(csum); |
| 1042 | } |
| 1043 | |
| 1044 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1045 | { |
| 1046 | struct mdp_superblock_1 *sb; |
| 1047 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1048 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1050 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
| 1052 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1053 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | * It is always aligned to a 4K boundary and |
| 1055 | * depeding on minor_version, it can be: |
| 1056 | * 0: At least 8K, but less than 12K, from end of device |
| 1057 | * 1: At start of device |
| 1058 | * 2: 4K from start of device. |
| 1059 | */ |
| 1060 | switch(minor_version) { |
| 1061 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1062 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1063 | sb_start -= 8*2; |
| 1064 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | break; |
| 1066 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1067 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | break; |
| 1069 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1070 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | break; |
| 1072 | default: |
| 1073 | return -EINVAL; |
| 1074 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1075 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1077 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1078 | * and it is safe to read 4k, so we do that |
| 1079 | */ |
| 1080 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (ret) return ret; |
| 1082 | |
| 1083 | |
| 1084 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1085 | |
| 1086 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1087 | sb->major_version != cpu_to_le32(1) || |
| 1088 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1089 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1090 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | return -EINVAL; |
| 1092 | |
| 1093 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1094 | printk("md: invalid superblock checksum on %s\n", |
| 1095 | bdevname(rdev->bdev,b)); |
| 1096 | return -EINVAL; |
| 1097 | } |
| 1098 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1099 | printk("md: data_size too small on %s\n", |
| 1100 | bdevname(rdev->bdev,b)); |
| 1101 | return -EINVAL; |
| 1102 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1103 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1104 | if (sb->level != cpu_to_le32(1) && |
| 1105 | sb->level != cpu_to_le32(4) && |
| 1106 | sb->level != cpu_to_le32(5) && |
| 1107 | sb->level != cpu_to_le32(6) && |
| 1108 | sb->level != cpu_to_le32(10)) { |
| 1109 | printk(KERN_WARNING |
| 1110 | "md: bitmaps not supported for this level.\n"); |
| 1111 | return -EINVAL; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | rdev->preferred_minor = 0xffff; |
| 1116 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1117 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1119 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1120 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1121 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1122 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1123 | |
| 1124 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1125 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1126 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1127 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1128 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1129 | rdev->desc_nr = -1; |
| 1130 | else |
| 1131 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1132 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1133 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1134 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1135 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | __u64 ev1, ev2; |
| 1137 | struct mdp_superblock_1 *refsb = |
| 1138 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1139 | |
| 1140 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1141 | sb->level != refsb->level || |
| 1142 | sb->layout != refsb->layout || |
| 1143 | sb->chunksize != refsb->chunksize) { |
| 1144 | printk(KERN_WARNING "md: %s has strangely different" |
| 1145 | " superblock to %s\n", |
| 1146 | bdevname(rdev->bdev,b), |
| 1147 | bdevname(refdev->bdev,b2)); |
| 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | ev1 = le64_to_cpu(sb->events); |
| 1151 | ev2 = le64_to_cpu(refsb->events); |
| 1152 | |
| 1153 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1154 | ret = 1; |
| 1155 | else |
| 1156 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1158 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1160 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1161 | rdev->size = rdev->sb_start / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1163 | return -EINVAL; |
| 1164 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1165 | if (le32_to_cpu(sb->chunksize)) |
| 1166 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1167 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1168 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1169 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1170 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1174 | { |
| 1175 | 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] | 1176 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1178 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1179 | clear_bit(Faulty, &rdev->flags); |
| 1180 | clear_bit(In_sync, &rdev->flags); |
| 1181 | clear_bit(WriteMostly, &rdev->flags); |
| 1182 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | if (mddev->raid_disks == 0) { |
| 1185 | mddev->major_version = 1; |
| 1186 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1187 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1189 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1190 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1191 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1192 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | mddev->layout = le32_to_cpu(sb->layout); |
| 1194 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1195 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1196 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1197 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1198 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1201 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1202 | |
| 1203 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1204 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1205 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1206 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1207 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1208 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1209 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1210 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1211 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1212 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1213 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1214 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1215 | } else { |
| 1216 | mddev->reshape_position = MaxSector; |
| 1217 | mddev->delta_disks = 0; |
| 1218 | mddev->new_level = mddev->level; |
| 1219 | mddev->new_layout = mddev->layout; |
| 1220 | mddev->new_chunk = mddev->chunk_size; |
| 1221 | } |
| 1222 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1223 | } else if (mddev->pers == NULL) { |
| 1224 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | ++ev1; |
| 1226 | if (ev1 < mddev->events) |
| 1227 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1228 | } else if (mddev->bitmap) { |
| 1229 | /* If adding to array with a bitmap, then we can accept an |
| 1230 | * older device, but not too old. |
| 1231 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1232 | if (ev1 < mddev->bitmap->events_cleared) |
| 1233 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1234 | } else { |
| 1235 | if (ev1 < mddev->events) |
| 1236 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1237 | return 0; |
| 1238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1240 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1242 | switch(role) { |
| 1243 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | break; |
| 1245 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1246 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | break; |
| 1248 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1249 | if ((le32_to_cpu(sb->feature_map) & |
| 1250 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1251 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1252 | else |
| 1253 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | rdev->raid_disk = role; |
| 1255 | break; |
| 1256 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1257 | if (sb->devflags & WriteMostly1) |
| 1258 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1259 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1260 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1266 | { |
| 1267 | struct mdp_superblock_1 *sb; |
| 1268 | struct list_head *tmp; |
| 1269 | mdk_rdev_t *rdev2; |
| 1270 | int max_dev, i; |
| 1271 | /* make rdev->sb match mddev and rdev data. */ |
| 1272 | |
| 1273 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1274 | |
| 1275 | sb->feature_map = 0; |
| 1276 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1277 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1279 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1280 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1281 | |
| 1282 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1283 | sb->events = cpu_to_le64(mddev->events); |
| 1284 | if (mddev->in_sync) |
| 1285 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1286 | else |
| 1287 | sb->resync_offset = cpu_to_le64(0); |
| 1288 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1289 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1290 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1291 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1292 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1293 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1294 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1295 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1296 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1297 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1298 | |
| 1299 | if (rdev->raid_disk >= 0 && |
| 1300 | !test_bit(In_sync, &rdev->flags) && |
| 1301 | rdev->recovery_offset > 0) { |
| 1302 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1303 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1304 | } |
| 1305 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1306 | if (mddev->reshape_position != MaxSector) { |
| 1307 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1308 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1309 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1310 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1311 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1312 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1313 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | max_dev = 0; |
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 | if (rdev2->desc_nr+1 > max_dev) |
| 1318 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1319 | |
| 1320 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1321 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | for (i=0; i<max_dev;i++) |
| 1323 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1324 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1325 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1327 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1329 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1331 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1332 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | else |
| 1334 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1335 | } |
| 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1338 | } |
| 1339 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1340 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1341 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1342 | { |
| 1343 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1344 | sector_t max_sectors; |
| 1345 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1346 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1347 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1348 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1349 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1350 | max_sectors -= rdev->data_offset; |
| 1351 | if (!num_sectors || num_sectors > max_sectors) |
| 1352 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1353 | } else if (rdev->mddev->bitmap_offset) { |
| 1354 | /* minor version 0 with bitmap we can't move */ |
| 1355 | return 0; |
| 1356 | } else { |
| 1357 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1358 | sector_t sb_start; |
| 1359 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1360 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1361 | max_sectors = rdev->size * 2 + sb_start - rdev->sb_start; |
| 1362 | if (!num_sectors || num_sectors > max_sectors) |
| 1363 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | } |
| 1366 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1367 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1368 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1369 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1370 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1371 | rdev->sb_page); |
| 1372 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1373 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1376 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | [0] = { |
| 1378 | .name = "0.90.0", |
| 1379 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1380 | .load_super = super_90_load, |
| 1381 | .validate_super = super_90_validate, |
| 1382 | .sync_super = super_90_sync, |
| 1383 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | }, |
| 1385 | [1] = { |
| 1386 | .name = "md-1", |
| 1387 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1388 | .load_super = super_1_load, |
| 1389 | .validate_super = super_1_validate, |
| 1390 | .sync_super = super_1_sync, |
| 1391 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | }, |
| 1393 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1396 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1397 | struct list_head *tmp, *tmp2; |
| 1398 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1400 | rdev_for_each(rdev, tmp, mddev1) |
| 1401 | rdev_for_each(rdev2, tmp2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1402 | if (rdev->bdev->bd_contains == |
| 1403 | rdev2->bdev->bd_contains) |
| 1404 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | static LIST_HEAD(pending_raid_disks); |
| 1410 | |
| 1411 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1412 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1413 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1414 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1415 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1416 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | if (rdev->mddev) { |
| 1419 | MD_BUG(); |
| 1420 | return -EINVAL; |
| 1421 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1422 | |
| 1423 | /* prevent duplicates */ |
| 1424 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1425 | return -EEXIST; |
| 1426 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1427 | /* make sure rdev->size exceeds mddev->size */ |
| 1428 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1429 | if (mddev->pers) { |
| 1430 | /* Cannot change size, so fail |
| 1431 | * If mddev->level <= 0, then we don't care |
| 1432 | * about aligning sizes (e.g. linear) |
| 1433 | */ |
| 1434 | if (mddev->level > 0) |
| 1435 | return -ENOSPC; |
| 1436 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1437 | mddev->size = rdev->size; |
| 1438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | /* Verify rdev->desc_nr is unique. |
| 1441 | * If it is -1, assign a free number, else |
| 1442 | * check number is not in use |
| 1443 | */ |
| 1444 | if (rdev->desc_nr < 0) { |
| 1445 | int choice = 0; |
| 1446 | if (mddev->pers) choice = mddev->raid_disks; |
| 1447 | while (find_rdev_nr(mddev, choice)) |
| 1448 | choice++; |
| 1449 | rdev->desc_nr = choice; |
| 1450 | } else { |
| 1451 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1452 | return -EBUSY; |
| 1453 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1454 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1455 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1456 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1459 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1460 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1461 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1462 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1463 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1464 | if (rdev->bdev->bd_part) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1465 | ko = &rdev->bdev->bd_part->dev.kobj; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1466 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1467 | ko = &rdev->bdev->bd_disk->dev.kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1468 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1469 | kobject_del(&rdev->kobj); |
| 1470 | goto fail; |
| 1471 | } |
| 1472 | list_add(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1473 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1475 | |
| 1476 | fail: |
| 1477 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1478 | b, mdname(mddev)); |
| 1479 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1482 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1483 | { |
| 1484 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1485 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1486 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1490 | { |
| 1491 | char b[BDEVNAME_SIZE]; |
| 1492 | if (!rdev->mddev) { |
| 1493 | MD_BUG(); |
| 1494 | return; |
| 1495 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1496 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | list_del_init(&rdev->same_set); |
| 1498 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1499 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1500 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1501 | |
| 1502 | /* We need to delay this, otherwise we can deadlock when |
| 1503 | * writing to 'remove' to "dev/state" |
| 1504 | */ |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1505 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1506 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1507 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * prevent the device from being mounted, repartitioned or |
| 1512 | * otherwise reused by a RAID array (or any other kernel |
| 1513 | * subsystem), by bd_claiming the device. |
| 1514 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1515 | 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] | 1516 | { |
| 1517 | int err = 0; |
| 1518 | struct block_device *bdev; |
| 1519 | char b[BDEVNAME_SIZE]; |
| 1520 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1521 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | if (IS_ERR(bdev)) { |
| 1523 | printk(KERN_ERR "md: could not open %s.\n", |
| 1524 | __bdevname(dev, b)); |
| 1525 | return PTR_ERR(bdev); |
| 1526 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1527 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | if (err) { |
| 1529 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1530 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1531 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | return err; |
| 1533 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1534 | if (!shared) |
| 1535 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | rdev->bdev = bdev; |
| 1537 | return err; |
| 1538 | } |
| 1539 | |
| 1540 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1541 | { |
| 1542 | struct block_device *bdev = rdev->bdev; |
| 1543 | rdev->bdev = NULL; |
| 1544 | if (!bdev) |
| 1545 | MD_BUG(); |
| 1546 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1547 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void md_autodetect_dev(dev_t dev); |
| 1551 | |
| 1552 | static void export_rdev(mdk_rdev_t * rdev) |
| 1553 | { |
| 1554 | char b[BDEVNAME_SIZE]; |
| 1555 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1556 | bdevname(rdev->bdev,b)); |
| 1557 | if (rdev->mddev) |
| 1558 | MD_BUG(); |
| 1559 | free_disk_sb(rdev); |
| 1560 | list_del_init(&rdev->same_set); |
| 1561 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1562 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1563 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | #endif |
| 1565 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1566 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1570 | { |
| 1571 | unbind_rdev_from_array(rdev); |
| 1572 | export_rdev(rdev); |
| 1573 | } |
| 1574 | |
| 1575 | static void export_array(mddev_t *mddev) |
| 1576 | { |
| 1577 | struct list_head *tmp; |
| 1578 | mdk_rdev_t *rdev; |
| 1579 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1580 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (!rdev->mddev) { |
| 1582 | MD_BUG(); |
| 1583 | continue; |
| 1584 | } |
| 1585 | kick_rdev_from_array(rdev); |
| 1586 | } |
| 1587 | if (!list_empty(&mddev->disks)) |
| 1588 | MD_BUG(); |
| 1589 | mddev->raid_disks = 0; |
| 1590 | mddev->major_version = 0; |
| 1591 | } |
| 1592 | |
| 1593 | static void print_desc(mdp_disk_t *desc) |
| 1594 | { |
| 1595 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1596 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1597 | } |
| 1598 | |
| 1599 | static void print_sb(mdp_super_t *sb) |
| 1600 | { |
| 1601 | int i; |
| 1602 | |
| 1603 | printk(KERN_INFO |
| 1604 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1605 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1606 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1607 | sb->ctime); |
| 1608 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1609 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1610 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1611 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1612 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1613 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1614 | sb->failed_disks, sb->spare_disks, |
| 1615 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1616 | |
| 1617 | printk(KERN_INFO); |
| 1618 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1619 | mdp_disk_t *desc; |
| 1620 | |
| 1621 | desc = sb->disks + i; |
| 1622 | if (desc->number || desc->major || desc->minor || |
| 1623 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1624 | printk(" D %2d: ", i); |
| 1625 | print_desc(desc); |
| 1626 | } |
| 1627 | } |
| 1628 | printk(KERN_INFO "md: THIS: "); |
| 1629 | print_desc(&sb->this_disk); |
| 1630 | |
| 1631 | } |
| 1632 | |
| 1633 | static void print_rdev(mdk_rdev_t *rdev) |
| 1634 | { |
| 1635 | char b[BDEVNAME_SIZE]; |
| 1636 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1637 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1638 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1639 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | if (rdev->sb_loaded) { |
| 1641 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1642 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1643 | } else |
| 1644 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1645 | } |
| 1646 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1647 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | { |
| 1649 | struct list_head *tmp, *tmp2; |
| 1650 | mdk_rdev_t *rdev; |
| 1651 | mddev_t *mddev; |
| 1652 | char b[BDEVNAME_SIZE]; |
| 1653 | |
| 1654 | printk("\n"); |
| 1655 | printk("md: **********************************\n"); |
| 1656 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1657 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1658 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1660 | if (mddev->bitmap) |
| 1661 | bitmap_print_sb(mddev->bitmap); |
| 1662 | else |
| 1663 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1664 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1666 | printk("\n"); |
| 1667 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1668 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | print_rdev(rdev); |
| 1670 | } |
| 1671 | printk("md: **********************************\n"); |
| 1672 | printk("\n"); |
| 1673 | } |
| 1674 | |
| 1675 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1676 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1678 | /* Update each superblock (in-memory image), but |
| 1679 | * if we are allowed to, skip spares which already |
| 1680 | * have the right event counter, or have one earlier |
| 1681 | * (which would mean they aren't being marked as dirty |
| 1682 | * with the rest of the array) |
| 1683 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | mdk_rdev_t *rdev; |
| 1685 | struct list_head *tmp; |
| 1686 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1687 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1688 | if (rdev->sb_events == mddev->events || |
| 1689 | (nospares && |
| 1690 | rdev->raid_disk < 0 && |
| 1691 | (rdev->sb_events&1)==0 && |
| 1692 | rdev->sb_events+1 == mddev->events)) { |
| 1693 | /* Don't update this superblock */ |
| 1694 | rdev->sb_loaded = 2; |
| 1695 | } else { |
| 1696 | super_types[mddev->major_version]. |
| 1697 | sync_super(mddev, rdev); |
| 1698 | rdev->sb_loaded = 1; |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1703 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | struct list_head *tmp; |
| 1706 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1707 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1708 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1710 | if (mddev->external) |
| 1711 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1713 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1714 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1715 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1716 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1717 | force_change = 1; |
| 1718 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1719 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1720 | * though if events isn't the right even/odd, we will have to do |
| 1721 | * spares after all |
| 1722 | */ |
| 1723 | nospares = 1; |
| 1724 | if (force_change) |
| 1725 | nospares = 0; |
| 1726 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1727 | /* If the array is degraded, then skipping spares is both |
| 1728 | * dangerous and fairly pointless. |
| 1729 | * Dangerous because a device that was removed from the array |
| 1730 | * might have a event_count that still looks up-to-date, |
| 1731 | * so it can be re-added without a resync. |
| 1732 | * Pointless because if there are any spares to skip, |
| 1733 | * then a recovery will happen and soon that array won't |
| 1734 | * be degraded any more and the spare can go back to sleep then. |
| 1735 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1736 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1737 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1738 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1740 | |
| 1741 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1742 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1743 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1744 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1745 | && (mddev->events & 1) |
| 1746 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1747 | mddev->events--; |
| 1748 | else { |
| 1749 | /* otherwise we have to go forward and ... */ |
| 1750 | mddev->events ++; |
| 1751 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1752 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1753 | if ((mddev->events&1)==0) { |
| 1754 | mddev->events++; |
| 1755 | nospares = 0; |
| 1756 | } |
| 1757 | } else { |
| 1758 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1759 | if ((mddev->events&1)) { |
| 1760 | mddev->events++; |
| 1761 | nospares = 0; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (!mddev->events) { |
| 1767 | /* |
| 1768 | * oops, this 64-bit counter should never wrap. |
| 1769 | * Either we are in around ~1 trillion A.C., assuming |
| 1770 | * 1 reboot per second, or we have a bug: |
| 1771 | */ |
| 1772 | MD_BUG(); |
| 1773 | mddev->events --; |
| 1774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /* |
| 1777 | * do not write anything to disk if using |
| 1778 | * nonpersistent superblocks |
| 1779 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1780 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1781 | if (!mddev->external) |
| 1782 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1783 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1784 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1785 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1787 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1788 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1789 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | |
| 1791 | dprintk(KERN_INFO |
| 1792 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1793 | mdname(mddev),mddev->in_sync); |
| 1794 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1795 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1796 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | char b[BDEVNAME_SIZE]; |
| 1798 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1799 | if (rdev->sb_loaded != 1) |
| 1800 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1801 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | dprintk("(skipping faulty "); |
| 1803 | |
| 1804 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1805 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1806 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1807 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1808 | rdev->sb_page); |
| 1809 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1810 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1811 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1812 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } else |
| 1815 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1816 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | /* only need to write one superblock... */ |
| 1818 | break; |
| 1819 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1820 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1821 | /* 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] | 1822 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1823 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1824 | if (mddev->in_sync != sync_req || |
| 1825 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1826 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1827 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1828 | goto repeat; |
| 1829 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1830 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1831 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1832 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 1836 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1837 | * We want to accept with case. For this we use cmd_match. |
| 1838 | */ |
| 1839 | static int cmd_match(const char *cmd, const char *str) |
| 1840 | { |
| 1841 | /* See if cmd, written into a sysfs file, matches |
| 1842 | * str. They must either be the same, or cmd can |
| 1843 | * have a trailing newline |
| 1844 | */ |
| 1845 | while (*cmd && *str && *cmd == *str) { |
| 1846 | cmd++; |
| 1847 | str++; |
| 1848 | } |
| 1849 | if (*cmd == '\n') |
| 1850 | cmd++; |
| 1851 | if (*str || *cmd) |
| 1852 | return 0; |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1856 | struct rdev_sysfs_entry { |
| 1857 | struct attribute attr; |
| 1858 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1859 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1860 | }; |
| 1861 | |
| 1862 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1863 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1864 | { |
| 1865 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1866 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1867 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1868 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1869 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1870 | sep = ","; |
| 1871 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1872 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1873 | len += sprintf(page+len, "%sin_sync",sep); |
| 1874 | sep = ","; |
| 1875 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1876 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1877 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1878 | sep = ","; |
| 1879 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1880 | if (test_bit(Blocked, &rdev->flags)) { |
| 1881 | len += sprintf(page+len, "%sblocked", sep); |
| 1882 | sep = ","; |
| 1883 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1884 | if (!test_bit(Faulty, &rdev->flags) && |
| 1885 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1886 | len += sprintf(page+len, "%sspare", sep); |
| 1887 | sep = ","; |
| 1888 | } |
| 1889 | return len+sprintf(page+len, "\n"); |
| 1890 | } |
| 1891 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1892 | static ssize_t |
| 1893 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1894 | { |
| 1895 | /* can write |
| 1896 | * faulty - simulates and error |
| 1897 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1898 | * writemostly - sets write_mostly |
| 1899 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1900 | * blocked - sets the Blocked flag |
| 1901 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1902 | */ |
| 1903 | int err = -EINVAL; |
| 1904 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1905 | md_error(rdev->mddev, rdev); |
| 1906 | err = 0; |
| 1907 | } else if (cmd_match(buf, "remove")) { |
| 1908 | if (rdev->raid_disk >= 0) |
| 1909 | err = -EBUSY; |
| 1910 | else { |
| 1911 | mddev_t *mddev = rdev->mddev; |
| 1912 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1913 | if (mddev->pers) |
| 1914 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1915 | md_new_event(mddev); |
| 1916 | err = 0; |
| 1917 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1918 | } else if (cmd_match(buf, "writemostly")) { |
| 1919 | set_bit(WriteMostly, &rdev->flags); |
| 1920 | err = 0; |
| 1921 | } else if (cmd_match(buf, "-writemostly")) { |
| 1922 | clear_bit(WriteMostly, &rdev->flags); |
| 1923 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1924 | } else if (cmd_match(buf, "blocked")) { |
| 1925 | set_bit(Blocked, &rdev->flags); |
| 1926 | err = 0; |
| 1927 | } else if (cmd_match(buf, "-blocked")) { |
| 1928 | clear_bit(Blocked, &rdev->flags); |
| 1929 | wake_up(&rdev->blocked_wait); |
| 1930 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1931 | md_wakeup_thread(rdev->mddev->thread); |
| 1932 | |
| 1933 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1934 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 1935 | if (!err) |
| 1936 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1937 | return err ? err : len; |
| 1938 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1939 | static struct rdev_sysfs_entry rdev_state = |
| 1940 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1941 | |
| 1942 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1943 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1944 | { |
| 1945 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1946 | } |
| 1947 | |
| 1948 | static ssize_t |
| 1949 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1950 | { |
| 1951 | char *e; |
| 1952 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1953 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1954 | atomic_set(&rdev->corrected_errors, n); |
| 1955 | return len; |
| 1956 | } |
| 1957 | return -EINVAL; |
| 1958 | } |
| 1959 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1960 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1961 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1962 | static ssize_t |
| 1963 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1964 | { |
| 1965 | if (rdev->raid_disk < 0) |
| 1966 | return sprintf(page, "none\n"); |
| 1967 | else |
| 1968 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1969 | } |
| 1970 | |
| 1971 | static ssize_t |
| 1972 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1973 | { |
| 1974 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1975 | int err; |
| 1976 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1977 | int slot = simple_strtoul(buf, &e, 10); |
| 1978 | if (strncmp(buf, "none", 4)==0) |
| 1979 | slot = -1; |
| 1980 | else if (e==buf || (*e && *e!= '\n')) |
| 1981 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1982 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1983 | /* Setting 'slot' on an active array requires also |
| 1984 | * updating the 'rd%d' link, and communicating |
| 1985 | * with the personality with ->hot_*_disk. |
| 1986 | * For now we only support removing |
| 1987 | * failed/spare devices. This normally happens automatically, |
| 1988 | * but not when the metadata is externally managed. |
| 1989 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1990 | if (rdev->raid_disk == -1) |
| 1991 | return -EEXIST; |
| 1992 | /* personality does all needed checks */ |
| 1993 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1994 | return -EINVAL; |
| 1995 | err = rdev->mddev->pers-> |
| 1996 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 1997 | if (err) |
| 1998 | return err; |
| 1999 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2000 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2001 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2002 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2003 | } else if (rdev->mddev->pers) { |
| 2004 | mdk_rdev_t *rdev2; |
| 2005 | struct list_head *tmp; |
| 2006 | /* Activating a spare .. or possibly reactivating |
| 2007 | * if we every get bitmaps working here. |
| 2008 | */ |
| 2009 | |
| 2010 | if (rdev->raid_disk != -1) |
| 2011 | return -EBUSY; |
| 2012 | |
| 2013 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2014 | return -EINVAL; |
| 2015 | |
| 2016 | rdev_for_each(rdev2, tmp, rdev->mddev) |
| 2017 | if (rdev2->raid_disk == slot) |
| 2018 | return -EEXIST; |
| 2019 | |
| 2020 | rdev->raid_disk = slot; |
| 2021 | if (test_bit(In_sync, &rdev->flags)) |
| 2022 | rdev->saved_raid_disk = slot; |
| 2023 | else |
| 2024 | rdev->saved_raid_disk = -1; |
| 2025 | err = rdev->mddev->pers-> |
| 2026 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2027 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2028 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2029 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2030 | } else |
| 2031 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2032 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2033 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2034 | printk(KERN_WARNING |
| 2035 | "md: cannot register " |
| 2036 | "%s for %s\n", |
| 2037 | nm, mdname(rdev->mddev)); |
| 2038 | |
| 2039 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2040 | } else { |
| 2041 | if (slot >= rdev->mddev->raid_disks) |
| 2042 | return -ENOSPC; |
| 2043 | rdev->raid_disk = slot; |
| 2044 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2045 | clear_bit(Faulty, &rdev->flags); |
| 2046 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2047 | set_bit(In_sync, &rdev->flags); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2048 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2049 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2050 | return len; |
| 2051 | } |
| 2052 | |
| 2053 | |
| 2054 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2055 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2056 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2057 | static ssize_t |
| 2058 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2059 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2060 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | static ssize_t |
| 2064 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2065 | { |
| 2066 | char *e; |
| 2067 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2068 | if (e==buf || (*e && *e != '\n')) |
| 2069 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2070 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2071 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2072 | if (rdev->size && rdev->mddev->external) |
| 2073 | /* Must set offset before size, so overlap checks |
| 2074 | * can be sane */ |
| 2075 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2076 | rdev->data_offset = offset; |
| 2077 | return len; |
| 2078 | } |
| 2079 | |
| 2080 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2081 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2082 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2083 | static ssize_t |
| 2084 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2085 | { |
| 2086 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2087 | } |
| 2088 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2089 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2090 | { |
| 2091 | /* check if two start/length pairs overlap */ |
| 2092 | if (s1+l1 <= s2) |
| 2093 | return 0; |
| 2094 | if (s2+l2 <= s1) |
| 2095 | return 0; |
| 2096 | return 1; |
| 2097 | } |
| 2098 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2099 | static ssize_t |
| 2100 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2101 | { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2102 | unsigned long long size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2103 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2104 | mddev_t *my_mddev = rdev->mddev; |
| 2105 | |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2106 | if (strict_strtoull(buf, 10, &size) < 0) |
| 2107 | return -EINVAL; |
| 2108 | if (size < my_mddev->size) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2109 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2110 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2111 | if (my_mddev->persistent) { |
| 2112 | size = super_types[my_mddev->major_version]. |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2113 | rdev_size_change(rdev, size * 2); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2114 | if (!size) |
| 2115 | return -EBUSY; |
| 2116 | } else if (!size) { |
| 2117 | size = (rdev->bdev->bd_inode->i_size >> 10); |
| 2118 | size -= rdev->data_offset/2; |
| 2119 | } |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2120 | if (size < my_mddev->size) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2121 | return -EINVAL; /* component must fit device */ |
| 2122 | } |
| 2123 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2124 | rdev->size = size; |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2125 | if (size > oldsize && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2126 | /* need to check that all other rdevs with the same ->bdev |
| 2127 | * do not overlap. We need to unlock the mddev to avoid |
| 2128 | * a deadlock. We have already changed rdev->size, and if |
| 2129 | * we have to change it back, we will have the lock again. |
| 2130 | */ |
| 2131 | mddev_t *mddev; |
| 2132 | int overlap = 0; |
| 2133 | struct list_head *tmp, *tmp2; |
| 2134 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2135 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2136 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2137 | mdk_rdev_t *rdev2; |
| 2138 | |
| 2139 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2140 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2141 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2142 | (rdev->bdev == rdev2->bdev && |
| 2143 | rdev != rdev2 && |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2144 | overlaps(rdev->data_offset, rdev->size * 2, |
| 2145 | rdev2->data_offset, |
| 2146 | rdev2->size * 2))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2147 | overlap = 1; |
| 2148 | break; |
| 2149 | } |
| 2150 | mddev_unlock(mddev); |
| 2151 | if (overlap) { |
| 2152 | mddev_put(mddev); |
| 2153 | break; |
| 2154 | } |
| 2155 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2156 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2157 | if (overlap) { |
| 2158 | /* Someone else could have slipped in a size |
| 2159 | * change here, but doing so is just silly. |
| 2160 | * We put oldsize back because we *know* it is |
| 2161 | * safe, and trust userspace not to race with |
| 2162 | * itself |
| 2163 | */ |
| 2164 | rdev->size = oldsize; |
| 2165 | return -EBUSY; |
| 2166 | } |
| 2167 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2168 | return len; |
| 2169 | } |
| 2170 | |
| 2171 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2172 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2173 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2174 | static struct attribute *rdev_default_attrs[] = { |
| 2175 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2176 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2177 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2178 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2179 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2180 | NULL, |
| 2181 | }; |
| 2182 | static ssize_t |
| 2183 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2184 | { |
| 2185 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2186 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2187 | mddev_t *mddev = rdev->mddev; |
| 2188 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2189 | |
| 2190 | if (!entry->show) |
| 2191 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2192 | |
| 2193 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2194 | if (!rv) { |
| 2195 | if (rdev->mddev == NULL) |
| 2196 | rv = -EBUSY; |
| 2197 | else |
| 2198 | rv = entry->show(rdev, page); |
| 2199 | mddev_unlock(mddev); |
| 2200 | } |
| 2201 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | static ssize_t |
| 2205 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2206 | const char *page, size_t length) |
| 2207 | { |
| 2208 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2209 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2210 | ssize_t rv; |
| 2211 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2212 | |
| 2213 | if (!entry->store) |
| 2214 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2215 | if (!capable(CAP_SYS_ADMIN)) |
| 2216 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2217 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2218 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2219 | if (rdev->mddev == NULL) |
| 2220 | rv = -EBUSY; |
| 2221 | else |
| 2222 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2223 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2224 | } |
| 2225 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | static void rdev_free(struct kobject *ko) |
| 2229 | { |
| 2230 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2231 | kfree(rdev); |
| 2232 | } |
| 2233 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2234 | .show = rdev_attr_show, |
| 2235 | .store = rdev_attr_store, |
| 2236 | }; |
| 2237 | static struct kobj_type rdev_ktype = { |
| 2238 | .release = rdev_free, |
| 2239 | .sysfs_ops = &rdev_sysfs_ops, |
| 2240 | .default_attrs = rdev_default_attrs, |
| 2241 | }; |
| 2242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | /* |
| 2244 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2245 | * |
| 2246 | * mark the device faulty if: |
| 2247 | * |
| 2248 | * - the device is nonexistent (zero size) |
| 2249 | * - the device has no valid superblock |
| 2250 | * |
| 2251 | * a faulty rdev _never_ has rdev->sb set. |
| 2252 | */ |
| 2253 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2254 | { |
| 2255 | char b[BDEVNAME_SIZE]; |
| 2256 | int err; |
| 2257 | mdk_rdev_t *rdev; |
| 2258 | sector_t size; |
| 2259 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2260 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | if (!rdev) { |
| 2262 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2263 | return ERR_PTR(-ENOMEM); |
| 2264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | |
| 2266 | if ((err = alloc_disk_sb(rdev))) |
| 2267 | goto abort_free; |
| 2268 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2269 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | if (err) |
| 2271 | goto abort_free; |
| 2272 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2273 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2276 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2277 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2278 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2280 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2282 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2283 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
| 2285 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2286 | if (!size) { |
| 2287 | printk(KERN_WARNING |
| 2288 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2289 | bdevname(rdev->bdev,b)); |
| 2290 | err = -EINVAL; |
| 2291 | goto abort_free; |
| 2292 | } |
| 2293 | |
| 2294 | if (super_format >= 0) { |
| 2295 | err = super_types[super_format]. |
| 2296 | load_super(rdev, NULL, super_minor); |
| 2297 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2298 | printk(KERN_WARNING |
| 2299 | "md: %s does not have a valid v%d.%d " |
| 2300 | "superblock, not importing!\n", |
| 2301 | bdevname(rdev->bdev,b), |
| 2302 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | goto abort_free; |
| 2304 | } |
| 2305 | if (err < 0) { |
| 2306 | printk(KERN_WARNING |
| 2307 | "md: could not read %s's sb, not importing!\n", |
| 2308 | bdevname(rdev->bdev,b)); |
| 2309 | goto abort_free; |
| 2310 | } |
| 2311 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2314 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | return rdev; |
| 2317 | |
| 2318 | abort_free: |
| 2319 | if (rdev->sb_page) { |
| 2320 | if (rdev->bdev) |
| 2321 | unlock_rdev(rdev); |
| 2322 | free_disk_sb(rdev); |
| 2323 | } |
| 2324 | kfree(rdev); |
| 2325 | return ERR_PTR(err); |
| 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * Check a full RAID array for plausibility |
| 2330 | */ |
| 2331 | |
| 2332 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2333 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | { |
| 2335 | int i; |
| 2336 | struct list_head *tmp; |
| 2337 | mdk_rdev_t *rdev, *freshest; |
| 2338 | char b[BDEVNAME_SIZE]; |
| 2339 | |
| 2340 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2341 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | switch (super_types[mddev->major_version]. |
| 2343 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2344 | case 1: |
| 2345 | freshest = rdev; |
| 2346 | break; |
| 2347 | case 0: |
| 2348 | break; |
| 2349 | default: |
| 2350 | printk( KERN_ERR \ |
| 2351 | "md: fatal superblock inconsistency in %s" |
| 2352 | " -- removing from array\n", |
| 2353 | bdevname(rdev->bdev,b)); |
| 2354 | kick_rdev_from_array(rdev); |
| 2355 | } |
| 2356 | |
| 2357 | |
| 2358 | super_types[mddev->major_version]. |
| 2359 | validate_super(mddev, freshest); |
| 2360 | |
| 2361 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2362 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | if (rdev != freshest) |
| 2364 | if (super_types[mddev->major_version]. |
| 2365 | validate_super(mddev, rdev)) { |
| 2366 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2367 | " from array!\n", |
| 2368 | bdevname(rdev->bdev,b)); |
| 2369 | kick_rdev_from_array(rdev); |
| 2370 | continue; |
| 2371 | } |
| 2372 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2373 | rdev->desc_nr = i++; |
| 2374 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2375 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2376 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2377 | rdev->raid_disk = -1; |
| 2378 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | if (mddev->recovery_cp != MaxSector && |
| 2385 | mddev->level >= 1) |
| 2386 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2387 | " -- starting background reconstruction\n", |
| 2388 | mdname(mddev)); |
| 2389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2392 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2393 | safe_delay_show(mddev_t *mddev, char *page) |
| 2394 | { |
| 2395 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2396 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2397 | } |
| 2398 | static ssize_t |
| 2399 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2400 | { |
| 2401 | int scale=1; |
| 2402 | int dot=0; |
| 2403 | int i; |
| 2404 | unsigned long msec; |
| 2405 | char buf[30]; |
| 2406 | char *e; |
| 2407 | /* remove a period, and count digits after it */ |
| 2408 | if (len >= sizeof(buf)) |
| 2409 | return -EINVAL; |
| 2410 | strlcpy(buf, cbuf, len); |
| 2411 | buf[len] = 0; |
| 2412 | for (i=0; i<len; i++) { |
| 2413 | if (dot) { |
| 2414 | if (isdigit(buf[i])) { |
| 2415 | buf[i-1] = buf[i]; |
| 2416 | scale *= 10; |
| 2417 | } |
| 2418 | buf[i] = 0; |
| 2419 | } else if (buf[i] == '.') { |
| 2420 | dot=1; |
| 2421 | buf[i] = 0; |
| 2422 | } |
| 2423 | } |
| 2424 | msec = simple_strtoul(buf, &e, 10); |
| 2425 | if (e == buf || (*e && *e != '\n')) |
| 2426 | return -EINVAL; |
| 2427 | msec = (msec * 1000) / scale; |
| 2428 | if (msec == 0) |
| 2429 | mddev->safemode_delay = 0; |
| 2430 | else { |
| 2431 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2432 | if (mddev->safemode_delay == 0) |
| 2433 | mddev->safemode_delay = 1; |
| 2434 | } |
| 2435 | return len; |
| 2436 | } |
| 2437 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2438 | __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] | 2439 | |
| 2440 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2441 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2442 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2443 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2444 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2445 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2446 | else if (mddev->clevel[0]) |
| 2447 | return sprintf(page, "%s\n", mddev->clevel); |
| 2448 | else if (mddev->level != LEVEL_NONE) |
| 2449 | return sprintf(page, "%d\n", mddev->level); |
| 2450 | else |
| 2451 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2454 | static ssize_t |
| 2455 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2456 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2457 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2458 | if (mddev->pers) |
| 2459 | return -EBUSY; |
| 2460 | if (len == 0) |
| 2461 | return 0; |
| 2462 | if (len >= sizeof(mddev->clevel)) |
| 2463 | return -ENOSPC; |
| 2464 | strncpy(mddev->clevel, buf, len); |
| 2465 | if (mddev->clevel[len-1] == '\n') |
| 2466 | len--; |
| 2467 | mddev->clevel[len] = 0; |
| 2468 | mddev->level = LEVEL_NONE; |
| 2469 | return rv; |
| 2470 | } |
| 2471 | |
| 2472 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2473 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2474 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2475 | |
| 2476 | static ssize_t |
| 2477 | layout_show(mddev_t *mddev, char *page) |
| 2478 | { |
| 2479 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2480 | if (mddev->reshape_position != MaxSector && |
| 2481 | mddev->layout != mddev->new_layout) |
| 2482 | return sprintf(page, "%d (%d)\n", |
| 2483 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2484 | return sprintf(page, "%d\n", mddev->layout); |
| 2485 | } |
| 2486 | |
| 2487 | static ssize_t |
| 2488 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2489 | { |
| 2490 | char *e; |
| 2491 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2492 | |
| 2493 | if (!*buf || (*e && *e != '\n')) |
| 2494 | return -EINVAL; |
| 2495 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2496 | if (mddev->pers) |
| 2497 | return -EBUSY; |
| 2498 | if (mddev->reshape_position != MaxSector) |
| 2499 | mddev->new_layout = n; |
| 2500 | else |
| 2501 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2502 | return len; |
| 2503 | } |
| 2504 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2505 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2506 | |
| 2507 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2508 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2509 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2510 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2511 | if (mddev->raid_disks == 0) |
| 2512 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2513 | if (mddev->reshape_position != MaxSector && |
| 2514 | mddev->delta_disks != 0) |
| 2515 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2516 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2517 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2518 | } |
| 2519 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2520 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2521 | |
| 2522 | static ssize_t |
| 2523 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2524 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2525 | char *e; |
| 2526 | int rv = 0; |
| 2527 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2528 | |
| 2529 | if (!*buf || (*e && *e != '\n')) |
| 2530 | return -EINVAL; |
| 2531 | |
| 2532 | if (mddev->pers) |
| 2533 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2534 | else if (mddev->reshape_position != MaxSector) { |
| 2535 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2536 | mddev->delta_disks = n - olddisks; |
| 2537 | mddev->raid_disks = n; |
| 2538 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2539 | mddev->raid_disks = n; |
| 2540 | return rv ? rv : len; |
| 2541 | } |
| 2542 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2543 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2544 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2545 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2546 | chunk_size_show(mddev_t *mddev, char *page) |
| 2547 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2548 | if (mddev->reshape_position != MaxSector && |
| 2549 | mddev->chunk_size != mddev->new_chunk) |
| 2550 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2551 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2552 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2553 | } |
| 2554 | |
| 2555 | static ssize_t |
| 2556 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2557 | { |
| 2558 | /* can only set chunk_size if array is not yet active */ |
| 2559 | char *e; |
| 2560 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2561 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2562 | if (!*buf || (*e && *e != '\n')) |
| 2563 | return -EINVAL; |
| 2564 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2565 | if (mddev->pers) |
| 2566 | return -EBUSY; |
| 2567 | else if (mddev->reshape_position != MaxSector) |
| 2568 | mddev->new_chunk = n; |
| 2569 | else |
| 2570 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2571 | return len; |
| 2572 | } |
| 2573 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2574 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2575 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2576 | static ssize_t |
| 2577 | resync_start_show(mddev_t *mddev, char *page) |
| 2578 | { |
| 2579 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2580 | } |
| 2581 | |
| 2582 | static ssize_t |
| 2583 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2584 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2585 | char *e; |
| 2586 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2587 | |
| 2588 | if (mddev->pers) |
| 2589 | return -EBUSY; |
| 2590 | if (!*buf || (*e && *e != '\n')) |
| 2591 | return -EINVAL; |
| 2592 | |
| 2593 | mddev->recovery_cp = n; |
| 2594 | return len; |
| 2595 | } |
| 2596 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2597 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2598 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2599 | /* |
| 2600 | * The array state can be: |
| 2601 | * |
| 2602 | * clear |
| 2603 | * No devices, no size, no level |
| 2604 | * Equivalent to STOP_ARRAY ioctl |
| 2605 | * inactive |
| 2606 | * May have some settings, but array is not active |
| 2607 | * all IO results in error |
| 2608 | * When written, doesn't tear down array, but just stops it |
| 2609 | * suspended (not supported yet) |
| 2610 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2611 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2612 | * readonly |
| 2613 | * no resync can happen. no superblocks get written. |
| 2614 | * write requests fail |
| 2615 | * read-auto |
| 2616 | * like readonly, but behaves like 'clean' on a write request. |
| 2617 | * |
| 2618 | * clean - no pending writes, but otherwise active. |
| 2619 | * When written to inactive array, starts without resync |
| 2620 | * If a write request arrives then |
| 2621 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2622 | * if not known, block and switch to write-pending |
| 2623 | * If written to an active array that has pending writes, then fails. |
| 2624 | * active |
| 2625 | * fully active: IO and resync can be happening. |
| 2626 | * When written to inactive array, starts with resync |
| 2627 | * |
| 2628 | * write-pending |
| 2629 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2630 | * |
| 2631 | * active-idle |
| 2632 | * like active, but no writes have been seen for a while (100msec). |
| 2633 | * |
| 2634 | */ |
| 2635 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2636 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2637 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2638 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2639 | "write-pending", "active-idle", NULL }; |
| 2640 | |
| 2641 | static int match_word(const char *word, char **list) |
| 2642 | { |
| 2643 | int n; |
| 2644 | for (n=0; list[n]; n++) |
| 2645 | if (cmd_match(word, list[n])) |
| 2646 | break; |
| 2647 | return n; |
| 2648 | } |
| 2649 | |
| 2650 | static ssize_t |
| 2651 | array_state_show(mddev_t *mddev, char *page) |
| 2652 | { |
| 2653 | enum array_state st = inactive; |
| 2654 | |
| 2655 | if (mddev->pers) |
| 2656 | switch(mddev->ro) { |
| 2657 | case 1: |
| 2658 | st = readonly; |
| 2659 | break; |
| 2660 | case 2: |
| 2661 | st = read_auto; |
| 2662 | break; |
| 2663 | case 0: |
| 2664 | if (mddev->in_sync) |
| 2665 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2666 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2667 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2668 | else if (mddev->safemode) |
| 2669 | st = active_idle; |
| 2670 | else |
| 2671 | st = active; |
| 2672 | } |
| 2673 | else { |
| 2674 | if (list_empty(&mddev->disks) && |
| 2675 | mddev->raid_disks == 0 && |
| 2676 | mddev->size == 0) |
| 2677 | st = clear; |
| 2678 | else |
| 2679 | st = inactive; |
| 2680 | } |
| 2681 | return sprintf(page, "%s\n", array_states[st]); |
| 2682 | } |
| 2683 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2684 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2685 | static int do_md_run(mddev_t * mddev); |
| 2686 | static int restart_array(mddev_t *mddev); |
| 2687 | |
| 2688 | static ssize_t |
| 2689 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2690 | { |
| 2691 | int err = -EINVAL; |
| 2692 | enum array_state st = match_word(buf, array_states); |
| 2693 | switch(st) { |
| 2694 | case bad_word: |
| 2695 | break; |
| 2696 | case clear: |
| 2697 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2698 | if (atomic_read(&mddev->active) > 1) |
| 2699 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2700 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2701 | break; |
| 2702 | case inactive: |
| 2703 | /* stopping an active array */ |
| 2704 | if (mddev->pers) { |
| 2705 | if (atomic_read(&mddev->active) > 1) |
| 2706 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2707 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2708 | } else |
| 2709 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2710 | break; |
| 2711 | case suspended: |
| 2712 | break; /* not supported yet */ |
| 2713 | case readonly: |
| 2714 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2715 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2716 | else { |
| 2717 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2718 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2719 | err = do_md_run(mddev); |
| 2720 | } |
| 2721 | break; |
| 2722 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2723 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2724 | if (mddev->ro != 1) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2725 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2726 | else |
| 2727 | err = restart_array(mddev); |
| 2728 | if (err == 0) { |
| 2729 | mddev->ro = 2; |
| 2730 | set_disk_ro(mddev->gendisk, 0); |
| 2731 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2732 | } else { |
| 2733 | mddev->ro = 2; |
| 2734 | err = do_md_run(mddev); |
| 2735 | } |
| 2736 | break; |
| 2737 | case clean: |
| 2738 | if (mddev->pers) { |
| 2739 | restart_array(mddev); |
| 2740 | spin_lock_irq(&mddev->write_lock); |
| 2741 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2742 | if (mddev->in_sync == 0) { |
| 2743 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2744 | if (mddev->safemode == 1) |
| 2745 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2746 | if (mddev->persistent) |
| 2747 | set_bit(MD_CHANGE_CLEAN, |
| 2748 | &mddev->flags); |
| 2749 | } |
| 2750 | err = 0; |
| 2751 | } else |
| 2752 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2753 | spin_unlock_irq(&mddev->write_lock); |
| 2754 | } else { |
| 2755 | mddev->ro = 0; |
| 2756 | mddev->recovery_cp = MaxSector; |
| 2757 | err = do_md_run(mddev); |
| 2758 | } |
| 2759 | break; |
| 2760 | case active: |
| 2761 | if (mddev->pers) { |
| 2762 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2763 | if (mddev->external) |
| 2764 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2765 | wake_up(&mddev->sb_wait); |
| 2766 | err = 0; |
| 2767 | } else { |
| 2768 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2769 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2770 | err = do_md_run(mddev); |
| 2771 | } |
| 2772 | break; |
| 2773 | case write_pending: |
| 2774 | case active_idle: |
| 2775 | /* these cannot be set */ |
| 2776 | break; |
| 2777 | } |
| 2778 | if (err) |
| 2779 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2780 | else { |
| 2781 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2782 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2783 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2784 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2785 | static struct md_sysfs_entry md_array_state = |
| 2786 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2787 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2788 | static ssize_t |
| 2789 | null_show(mddev_t *mddev, char *page) |
| 2790 | { |
| 2791 | return -EINVAL; |
| 2792 | } |
| 2793 | |
| 2794 | static ssize_t |
| 2795 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2796 | { |
| 2797 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2798 | /* The new device is added to the array. |
| 2799 | * If the array has a persistent superblock, we read the |
| 2800 | * superblock to initialise info and check validity. |
| 2801 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2802 | * which mainly checks size. |
| 2803 | */ |
| 2804 | char *e; |
| 2805 | int major = simple_strtoul(buf, &e, 10); |
| 2806 | int minor; |
| 2807 | dev_t dev; |
| 2808 | mdk_rdev_t *rdev; |
| 2809 | int err; |
| 2810 | |
| 2811 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2812 | return -EINVAL; |
| 2813 | minor = simple_strtoul(e+1, &e, 10); |
| 2814 | if (*e && *e != '\n') |
| 2815 | return -EINVAL; |
| 2816 | dev = MKDEV(major, minor); |
| 2817 | if (major != MAJOR(dev) || |
| 2818 | minor != MINOR(dev)) |
| 2819 | return -EOVERFLOW; |
| 2820 | |
| 2821 | |
| 2822 | if (mddev->persistent) { |
| 2823 | rdev = md_import_device(dev, mddev->major_version, |
| 2824 | mddev->minor_version); |
| 2825 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2826 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2827 | mdk_rdev_t, same_set); |
| 2828 | err = super_types[mddev->major_version] |
| 2829 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2830 | if (err < 0) |
| 2831 | goto out; |
| 2832 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2833 | } else if (mddev->external) |
| 2834 | rdev = md_import_device(dev, -2, -1); |
| 2835 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2836 | rdev = md_import_device(dev, -1, -1); |
| 2837 | |
| 2838 | if (IS_ERR(rdev)) |
| 2839 | return PTR_ERR(rdev); |
| 2840 | err = bind_rdev_to_array(rdev, mddev); |
| 2841 | out: |
| 2842 | if (err) |
| 2843 | export_rdev(rdev); |
| 2844 | return err ? err : len; |
| 2845 | } |
| 2846 | |
| 2847 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2848 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2849 | |
| 2850 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2851 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2852 | { |
| 2853 | char *end; |
| 2854 | unsigned long chunk, end_chunk; |
| 2855 | |
| 2856 | if (!mddev->bitmap) |
| 2857 | goto out; |
| 2858 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2859 | while (*buf) { |
| 2860 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2861 | if (buf == end) break; |
| 2862 | if (*end == '-') { /* range */ |
| 2863 | buf = end + 1; |
| 2864 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2865 | if (buf == end) break; |
| 2866 | } |
| 2867 | if (*end && !isspace(*end)) break; |
| 2868 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2869 | buf = end; |
| 2870 | while (isspace(*buf)) buf++; |
| 2871 | } |
| 2872 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2873 | out: |
| 2874 | return len; |
| 2875 | } |
| 2876 | |
| 2877 | static struct md_sysfs_entry md_bitmap = |
| 2878 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2879 | |
| 2880 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2881 | size_show(mddev_t *mddev, char *page) |
| 2882 | { |
| 2883 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2884 | } |
| 2885 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2886 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2887 | |
| 2888 | static ssize_t |
| 2889 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2890 | { |
| 2891 | /* If array is inactive, we can reduce the component size, but |
| 2892 | * not increase it (except from 0). |
| 2893 | * If array is active, we can try an on-line resize |
| 2894 | */ |
| 2895 | char *e; |
| 2896 | int err = 0; |
| 2897 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2898 | if (!*buf || *buf == '\n' || |
| 2899 | (*e && *e != '\n')) |
| 2900 | return -EINVAL; |
| 2901 | |
| 2902 | if (mddev->pers) { |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2903 | err = update_size(mddev, size * 2); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2904 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2905 | } else { |
| 2906 | if (mddev->size == 0 || |
| 2907 | mddev->size > size) |
| 2908 | mddev->size = size; |
| 2909 | else |
| 2910 | err = -ENOSPC; |
| 2911 | } |
| 2912 | return err ? err : len; |
| 2913 | } |
| 2914 | |
| 2915 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2916 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2917 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2918 | |
| 2919 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2920 | * This is one of |
| 2921 | * 'none' for arrays with no metadata (good luck...) |
| 2922 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2923 | * or N.M for internally known formats |
| 2924 | */ |
| 2925 | static ssize_t |
| 2926 | metadata_show(mddev_t *mddev, char *page) |
| 2927 | { |
| 2928 | if (mddev->persistent) |
| 2929 | return sprintf(page, "%d.%d\n", |
| 2930 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2931 | else if (mddev->external) |
| 2932 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2933 | else |
| 2934 | return sprintf(page, "none\n"); |
| 2935 | } |
| 2936 | |
| 2937 | static ssize_t |
| 2938 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2939 | { |
| 2940 | int major, minor; |
| 2941 | char *e; |
| 2942 | if (!list_empty(&mddev->disks)) |
| 2943 | return -EBUSY; |
| 2944 | |
| 2945 | if (cmd_match(buf, "none")) { |
| 2946 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2947 | mddev->external = 0; |
| 2948 | mddev->major_version = 0; |
| 2949 | mddev->minor_version = 90; |
| 2950 | return len; |
| 2951 | } |
| 2952 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2953 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2954 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2955 | namelen = sizeof(mddev->metadata_type)-1; |
| 2956 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2957 | mddev->metadata_type[namelen] = 0; |
| 2958 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2959 | mddev->metadata_type[--namelen] = 0; |
| 2960 | mddev->persistent = 0; |
| 2961 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2962 | mddev->major_version = 0; |
| 2963 | mddev->minor_version = 90; |
| 2964 | return len; |
| 2965 | } |
| 2966 | major = simple_strtoul(buf, &e, 10); |
| 2967 | if (e==buf || *e != '.') |
| 2968 | return -EINVAL; |
| 2969 | buf = e+1; |
| 2970 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2971 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2972 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2973 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2974 | return -ENOENT; |
| 2975 | mddev->major_version = major; |
| 2976 | mddev->minor_version = minor; |
| 2977 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2978 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2979 | return len; |
| 2980 | } |
| 2981 | |
| 2982 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2983 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2984 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2985 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2986 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2987 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2988 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2989 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2990 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2991 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2992 | type = "reshape"; |
| 2993 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2994 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2995 | type = "resync"; |
| 2996 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2997 | type = "check"; |
| 2998 | else |
| 2999 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3000 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3001 | type = "recover"; |
| 3002 | } |
| 3003 | return sprintf(page, "%s\n", type); |
| 3004 | } |
| 3005 | |
| 3006 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3007 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3008 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3009 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3010 | return -EINVAL; |
| 3011 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3012 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3013 | if (mddev->sync_thread) { |
| 3014 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3015 | md_unregister_thread(mddev->sync_thread); |
| 3016 | mddev->sync_thread = NULL; |
| 3017 | mddev->recovery = 0; |
| 3018 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3019 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3020 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3021 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3022 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3023 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3024 | else if (cmd_match(page, "recover")) { |
| 3025 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3026 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3027 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3028 | int err; |
| 3029 | if (mddev->pers->start_reshape == NULL) |
| 3030 | return -EINVAL; |
| 3031 | err = mddev->pers->start_reshape(mddev); |
| 3032 | if (err) |
| 3033 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3034 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3035 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3036 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3037 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3038 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3039 | return -EINVAL; |
| 3040 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3041 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3042 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3043 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3044 | md_wakeup_thread(mddev->thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3045 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3046 | return len; |
| 3047 | } |
| 3048 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3049 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3050 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3051 | { |
| 3052 | return sprintf(page, "%llu\n", |
| 3053 | (unsigned long long) mddev->resync_mismatches); |
| 3054 | } |
| 3055 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3056 | static struct md_sysfs_entry md_scan_mode = |
| 3057 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3058 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3059 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3060 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3061 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3062 | static ssize_t |
| 3063 | sync_min_show(mddev_t *mddev, char *page) |
| 3064 | { |
| 3065 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3066 | mddev->sync_speed_min ? "local": "system"); |
| 3067 | } |
| 3068 | |
| 3069 | static ssize_t |
| 3070 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3071 | { |
| 3072 | int min; |
| 3073 | char *e; |
| 3074 | if (strncmp(buf, "system", 6)==0) { |
| 3075 | mddev->sync_speed_min = 0; |
| 3076 | return len; |
| 3077 | } |
| 3078 | min = simple_strtoul(buf, &e, 10); |
| 3079 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3080 | return -EINVAL; |
| 3081 | mddev->sync_speed_min = min; |
| 3082 | return len; |
| 3083 | } |
| 3084 | |
| 3085 | static struct md_sysfs_entry md_sync_min = |
| 3086 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3087 | |
| 3088 | static ssize_t |
| 3089 | sync_max_show(mddev_t *mddev, char *page) |
| 3090 | { |
| 3091 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3092 | mddev->sync_speed_max ? "local": "system"); |
| 3093 | } |
| 3094 | |
| 3095 | static ssize_t |
| 3096 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3097 | { |
| 3098 | int max; |
| 3099 | char *e; |
| 3100 | if (strncmp(buf, "system", 6)==0) { |
| 3101 | mddev->sync_speed_max = 0; |
| 3102 | return len; |
| 3103 | } |
| 3104 | max = simple_strtoul(buf, &e, 10); |
| 3105 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3106 | return -EINVAL; |
| 3107 | mddev->sync_speed_max = max; |
| 3108 | return len; |
| 3109 | } |
| 3110 | |
| 3111 | static struct md_sysfs_entry md_sync_max = |
| 3112 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3113 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3114 | static ssize_t |
| 3115 | degraded_show(mddev_t *mddev, char *page) |
| 3116 | { |
| 3117 | return sprintf(page, "%d\n", mddev->degraded); |
| 3118 | } |
| 3119 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3120 | |
| 3121 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3122 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3123 | { |
| 3124 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3125 | } |
| 3126 | |
| 3127 | static ssize_t |
| 3128 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3129 | { |
| 3130 | long n; |
| 3131 | |
| 3132 | if (strict_strtol(buf, 10, &n)) |
| 3133 | return -EINVAL; |
| 3134 | |
| 3135 | if (n != 0 && n != 1) |
| 3136 | return -EINVAL; |
| 3137 | |
| 3138 | mddev->parallel_resync = n; |
| 3139 | |
| 3140 | if (mddev->sync_thread) |
| 3141 | wake_up(&resync_wait); |
| 3142 | |
| 3143 | return len; |
| 3144 | } |
| 3145 | |
| 3146 | /* force parallel resync, even with shared block devices */ |
| 3147 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3148 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3149 | sync_force_parallel_show, sync_force_parallel_store); |
| 3150 | |
| 3151 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3152 | sync_speed_show(mddev_t *mddev, char *page) |
| 3153 | { |
| 3154 | unsigned long resync, dt, db; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3155 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3156 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3157 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3158 | db = resync - mddev->resync_mark_cnt; |
| 3159 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3162 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3163 | |
| 3164 | static ssize_t |
| 3165 | sync_completed_show(mddev_t *mddev, char *page) |
| 3166 | { |
| 3167 | unsigned long max_blocks, resync; |
| 3168 | |
| 3169 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3170 | max_blocks = mddev->resync_max_sectors; |
| 3171 | else |
| 3172 | max_blocks = mddev->size << 1; |
| 3173 | |
| 3174 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3175 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3176 | } |
| 3177 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3178 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3179 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3180 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3181 | min_sync_show(mddev_t *mddev, char *page) |
| 3182 | { |
| 3183 | return sprintf(page, "%llu\n", |
| 3184 | (unsigned long long)mddev->resync_min); |
| 3185 | } |
| 3186 | static ssize_t |
| 3187 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3188 | { |
| 3189 | unsigned long long min; |
| 3190 | if (strict_strtoull(buf, 10, &min)) |
| 3191 | return -EINVAL; |
| 3192 | if (min > mddev->resync_max) |
| 3193 | return -EINVAL; |
| 3194 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3195 | return -EBUSY; |
| 3196 | |
| 3197 | /* Must be a multiple of chunk_size */ |
| 3198 | if (mddev->chunk_size) { |
| 3199 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3200 | return -EINVAL; |
| 3201 | } |
| 3202 | mddev->resync_min = min; |
| 3203 | |
| 3204 | return len; |
| 3205 | } |
| 3206 | |
| 3207 | static struct md_sysfs_entry md_min_sync = |
| 3208 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3209 | |
| 3210 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3211 | max_sync_show(mddev_t *mddev, char *page) |
| 3212 | { |
| 3213 | if (mddev->resync_max == MaxSector) |
| 3214 | return sprintf(page, "max\n"); |
| 3215 | else |
| 3216 | return sprintf(page, "%llu\n", |
| 3217 | (unsigned long long)mddev->resync_max); |
| 3218 | } |
| 3219 | static ssize_t |
| 3220 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3221 | { |
| 3222 | if (strncmp(buf, "max", 3) == 0) |
| 3223 | mddev->resync_max = MaxSector; |
| 3224 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3225 | unsigned long long max; |
| 3226 | if (strict_strtoull(buf, 10, &max)) |
| 3227 | return -EINVAL; |
| 3228 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3229 | return -EINVAL; |
| 3230 | if (max < mddev->resync_max && |
| 3231 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3232 | return -EBUSY; |
| 3233 | |
| 3234 | /* Must be a multiple of chunk_size */ |
| 3235 | if (mddev->chunk_size) { |
| 3236 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3237 | return -EINVAL; |
| 3238 | } |
| 3239 | mddev->resync_max = max; |
| 3240 | } |
| 3241 | wake_up(&mddev->recovery_wait); |
| 3242 | return len; |
| 3243 | } |
| 3244 | |
| 3245 | static struct md_sysfs_entry md_max_sync = |
| 3246 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3247 | |
| 3248 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3249 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3250 | { |
| 3251 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3252 | } |
| 3253 | |
| 3254 | static ssize_t |
| 3255 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3256 | { |
| 3257 | char *e; |
| 3258 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3259 | |
| 3260 | if (mddev->pers->quiesce == NULL) |
| 3261 | return -EINVAL; |
| 3262 | if (buf == e || (*e && *e != '\n')) |
| 3263 | return -EINVAL; |
| 3264 | if (new >= mddev->suspend_hi || |
| 3265 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3266 | mddev->suspend_lo = new; |
| 3267 | mddev->pers->quiesce(mddev, 2); |
| 3268 | return len; |
| 3269 | } else |
| 3270 | return -EINVAL; |
| 3271 | } |
| 3272 | static struct md_sysfs_entry md_suspend_lo = |
| 3273 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3274 | |
| 3275 | |
| 3276 | static ssize_t |
| 3277 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3278 | { |
| 3279 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3280 | } |
| 3281 | |
| 3282 | static ssize_t |
| 3283 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3284 | { |
| 3285 | char *e; |
| 3286 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3287 | |
| 3288 | if (mddev->pers->quiesce == NULL) |
| 3289 | return -EINVAL; |
| 3290 | if (buf == e || (*e && *e != '\n')) |
| 3291 | return -EINVAL; |
| 3292 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3293 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3294 | mddev->suspend_hi = new; |
| 3295 | mddev->pers->quiesce(mddev, 1); |
| 3296 | mddev->pers->quiesce(mddev, 0); |
| 3297 | return len; |
| 3298 | } else |
| 3299 | return -EINVAL; |
| 3300 | } |
| 3301 | static struct md_sysfs_entry md_suspend_hi = |
| 3302 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3303 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3304 | static ssize_t |
| 3305 | reshape_position_show(mddev_t *mddev, char *page) |
| 3306 | { |
| 3307 | if (mddev->reshape_position != MaxSector) |
| 3308 | return sprintf(page, "%llu\n", |
| 3309 | (unsigned long long)mddev->reshape_position); |
| 3310 | strcpy(page, "none\n"); |
| 3311 | return 5; |
| 3312 | } |
| 3313 | |
| 3314 | static ssize_t |
| 3315 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3316 | { |
| 3317 | char *e; |
| 3318 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3319 | if (mddev->pers) |
| 3320 | return -EBUSY; |
| 3321 | if (buf == e || (*e && *e != '\n')) |
| 3322 | return -EINVAL; |
| 3323 | mddev->reshape_position = new; |
| 3324 | mddev->delta_disks = 0; |
| 3325 | mddev->new_level = mddev->level; |
| 3326 | mddev->new_layout = mddev->layout; |
| 3327 | mddev->new_chunk = mddev->chunk_size; |
| 3328 | return len; |
| 3329 | } |
| 3330 | |
| 3331 | static struct md_sysfs_entry md_reshape_position = |
| 3332 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3333 | reshape_position_store); |
| 3334 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3335 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3336 | static struct attribute *md_default_attrs[] = { |
| 3337 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3338 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3339 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3340 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3341 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3342 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3343 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3344 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3345 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3346 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3347 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3348 | NULL, |
| 3349 | }; |
| 3350 | |
| 3351 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3352 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3353 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3354 | &md_sync_min.attr, |
| 3355 | &md_sync_max.attr, |
| 3356 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3357 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3358 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3359 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3360 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3361 | &md_suspend_lo.attr, |
| 3362 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3363 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3364 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3365 | NULL, |
| 3366 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3367 | static struct attribute_group md_redundancy_group = { |
| 3368 | .name = NULL, |
| 3369 | .attrs = md_redundancy_attrs, |
| 3370 | }; |
| 3371 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3372 | |
| 3373 | static ssize_t |
| 3374 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3375 | { |
| 3376 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3377 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3378 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3379 | |
| 3380 | if (!entry->show) |
| 3381 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3382 | rv = mddev_lock(mddev); |
| 3383 | if (!rv) { |
| 3384 | rv = entry->show(mddev, page); |
| 3385 | mddev_unlock(mddev); |
| 3386 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3387 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | static ssize_t |
| 3391 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3392 | const char *page, size_t length) |
| 3393 | { |
| 3394 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3395 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3396 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3397 | |
| 3398 | if (!entry->store) |
| 3399 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3400 | if (!capable(CAP_SYS_ADMIN)) |
| 3401 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3402 | rv = mddev_lock(mddev); |
| 3403 | if (!rv) { |
| 3404 | rv = entry->store(mddev, page, length); |
| 3405 | mddev_unlock(mddev); |
| 3406 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3407 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | static void md_free(struct kobject *ko) |
| 3411 | { |
| 3412 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3413 | kfree(mddev); |
| 3414 | } |
| 3415 | |
| 3416 | static struct sysfs_ops md_sysfs_ops = { |
| 3417 | .show = md_attr_show, |
| 3418 | .store = md_attr_store, |
| 3419 | }; |
| 3420 | static struct kobj_type md_ktype = { |
| 3421 | .release = md_free, |
| 3422 | .sysfs_ops = &md_sysfs_ops, |
| 3423 | .default_attrs = md_default_attrs, |
| 3424 | }; |
| 3425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | int mdp_major = 0; |
| 3427 | |
| 3428 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3429 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3430 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | mddev_t *mddev = mddev_find(dev); |
| 3432 | struct gendisk *disk; |
| 3433 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3434 | int shift = partitioned ? MdpMinorShift : 0; |
| 3435 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3436 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | |
| 3438 | if (!mddev) |
| 3439 | return NULL; |
| 3440 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3441 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3443 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | mddev_put(mddev); |
| 3445 | return NULL; |
| 3446 | } |
| 3447 | disk = alloc_disk(1 << shift); |
| 3448 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3449 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | mddev_put(mddev); |
| 3451 | return NULL; |
| 3452 | } |
| 3453 | disk->major = MAJOR(dev); |
| 3454 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3455 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3457 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | disk->fops = &md_fops; |
| 3460 | disk->private_data = mddev; |
| 3461 | disk->queue = mddev->queue; |
| 3462 | add_disk(disk); |
| 3463 | mddev->gendisk = disk; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3464 | 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] | 3465 | "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3466 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3467 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3468 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3469 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3470 | else |
| 3471 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | return NULL; |
| 3473 | } |
| 3474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | static void md_safemode_timeout(unsigned long data) |
| 3476 | { |
| 3477 | mddev_t *mddev = (mddev_t *) data; |
| 3478 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3479 | if (!atomic_read(&mddev->writes_pending)) { |
| 3480 | mddev->safemode = 1; |
| 3481 | if (mddev->external) |
| 3482 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | md_wakeup_thread(mddev->thread); |
| 3485 | } |
| 3486 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3487 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | |
| 3489 | static int do_md_run(mddev_t * mddev) |
| 3490 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3491 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | int chunk_size; |
| 3493 | struct list_head *tmp; |
| 3494 | mdk_rdev_t *rdev; |
| 3495 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3496 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | char b[BDEVNAME_SIZE]; |
| 3498 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3499 | if (list_empty(&mddev->disks)) |
| 3500 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | |
| 3503 | if (mddev->pers) |
| 3504 | return -EBUSY; |
| 3505 | |
| 3506 | /* |
| 3507 | * Analyze all RAID superblock(s) |
| 3508 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3509 | if (!mddev->raid_disks) { |
| 3510 | if (!mddev->persistent) |
| 3511 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3512 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | |
| 3515 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3517 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3519 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3520 | chunk_size, MAX_CHUNK_SIZE); |
| 3521 | return -EINVAL; |
| 3522 | } |
| 3523 | /* |
| 3524 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3525 | */ |
| 3526 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3527 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | return -EINVAL; |
| 3529 | } |
| 3530 | if (chunk_size < PAGE_SIZE) { |
| 3531 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3532 | chunk_size, PAGE_SIZE); |
| 3533 | return -EINVAL; |
| 3534 | } |
| 3535 | |
| 3536 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3537 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3538 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | continue; |
| 3540 | if (rdev->size < chunk_size / 1024) { |
| 3541 | printk(KERN_WARNING |
| 3542 | "md: Dev %s smaller than chunk_size:" |
| 3543 | " %lluk < %dk\n", |
| 3544 | bdevname(rdev->bdev,b), |
| 3545 | (unsigned long long)rdev->size, |
| 3546 | chunk_size / 1024); |
| 3547 | return -EINVAL; |
| 3548 | } |
| 3549 | } |
| 3550 | } |
| 3551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3553 | if (mddev->level != LEVEL_NONE) |
| 3554 | request_module("md-level-%d", mddev->level); |
| 3555 | else if (mddev->clevel[0]) |
| 3556 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | #endif |
| 3558 | |
| 3559 | /* |
| 3560 | * Drop all container device buffers, from now on |
| 3561 | * the only valid external interface is through the md |
| 3562 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3564 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3565 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | continue; |
| 3567 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3568 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3569 | |
| 3570 | /* perform some consistency tests on the device. |
| 3571 | * We don't want the data to overlap the metadata, |
| 3572 | * Internal Bitmap issues has handled elsewhere. |
| 3573 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3574 | if (rdev->data_offset < rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3575 | if (mddev->size && |
| 3576 | rdev->data_offset + mddev->size*2 |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3577 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3578 | printk("md: %s: data overlaps metadata\n", |
| 3579 | mdname(mddev)); |
| 3580 | return -EINVAL; |
| 3581 | } |
| 3582 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3583 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3584 | > rdev->data_offset) { |
| 3585 | printk("md: %s: metadata overlaps data\n", |
| 3586 | mdname(mddev)); |
| 3587 | return -EINVAL; |
| 3588 | } |
| 3589 | } |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 3590 | sysfs_notify(&rdev->kobj, NULL, "state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | md_probe(mddev->unit, NULL, NULL); |
| 3594 | disk = mddev->gendisk; |
| 3595 | if (!disk) |
| 3596 | return -ENOMEM; |
| 3597 | |
| 3598 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3599 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3600 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3602 | if (mddev->level != LEVEL_NONE) |
| 3603 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3604 | mddev->level); |
| 3605 | else |
| 3606 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3607 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | return -EINVAL; |
| 3609 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3610 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3612 | mddev->level = pers->level; |
| 3613 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3615 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3616 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3617 | /* This personality cannot handle reshaping... */ |
| 3618 | mddev->pers = NULL; |
| 3619 | module_put(pers->owner); |
| 3620 | return -EINVAL; |
| 3621 | } |
| 3622 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3623 | if (pers->sync_request) { |
| 3624 | /* Warn if this is a potentially silly |
| 3625 | * configuration. |
| 3626 | */ |
| 3627 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3628 | mdk_rdev_t *rdev2; |
| 3629 | struct list_head *tmp2; |
| 3630 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3631 | rdev_for_each(rdev, tmp, mddev) { |
| 3632 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3633 | if (rdev < rdev2 && |
| 3634 | rdev->bdev->bd_contains == |
| 3635 | rdev2->bdev->bd_contains) { |
| 3636 | printk(KERN_WARNING |
| 3637 | "%s: WARNING: %s appears to be" |
| 3638 | " on the same physical disk as" |
| 3639 | " %s.\n", |
| 3640 | mdname(mddev), |
| 3641 | bdevname(rdev->bdev,b), |
| 3642 | bdevname(rdev2->bdev,b2)); |
| 3643 | warned = 1; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | if (warned) |
| 3648 | printk(KERN_WARNING |
| 3649 | "True protection against single-disk" |
| 3650 | " failure might be compromised.\n"); |
| 3651 | } |
| 3652 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3653 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3655 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3656 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3658 | if (start_readonly) |
| 3659 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3660 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3661 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 3662 | if (err) |
| 3663 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3664 | else if (mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3665 | err = bitmap_create(mddev); |
| 3666 | if (err) { |
| 3667 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3668 | mdname(mddev), err); |
| 3669 | mddev->pers->stop(mddev); |
| 3670 | } |
| 3671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | module_put(mddev->pers->owner); |
| 3674 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3675 | bitmap_destroy(mddev); |
| 3676 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3678 | if (mddev->pers->sync_request) { |
| 3679 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3680 | printk(KERN_WARNING |
| 3681 | "md: cannot register extra attributes for %s\n", |
| 3682 | mdname(mddev)); |
| 3683 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3684 | mddev->ro = 0; |
| 3685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | atomic_set(&mddev->writes_pending,0); |
| 3687 | mddev->safemode = 0; |
| 3688 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3689 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3690 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3692 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3693 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3694 | if (rdev->raid_disk >= 0) { |
| 3695 | char nm[20]; |
| 3696 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3697 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3698 | printk("md: cannot register %s for %s\n", |
| 3699 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | |
| 3702 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3703 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3704 | if (mddev->flags) |
| 3705 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame^] | 3707 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
| 3709 | /* If we call blk_queue_make_request here, it will |
| 3710 | * re-initialise max_sectors etc which may have been |
| 3711 | * refined inside -> run. So just set the bits we need to set. |
| 3712 | * Most initialisation happended when we called |
| 3713 | * blk_queue_make_request(..., md_fail_request) |
| 3714 | * earlier. |
| 3715 | */ |
| 3716 | mddev->queue->queuedata = mddev; |
| 3717 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3718 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3719 | /* If there is a partially-recovered drive we need to |
| 3720 | * start recovery here. If we leave it to md_check_recovery, |
| 3721 | * it will remove the drives and not do the right thing |
| 3722 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3723 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3724 | struct list_head *rtmp; |
| 3725 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3726 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3727 | if (rdev->raid_disk >= 0 && |
| 3728 | !test_bit(In_sync, &rdev->flags) && |
| 3729 | !test_bit(Faulty, &rdev->flags)) |
| 3730 | /* complete an interrupted recovery */ |
| 3731 | spares++; |
| 3732 | if (spares && mddev->pers->sync_request) { |
| 3733 | mddev->recovery = 0; |
| 3734 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3735 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3736 | mddev, |
| 3737 | "%s_resync"); |
| 3738 | if (!mddev->sync_thread) { |
| 3739 | printk(KERN_ERR "%s: could not start resync" |
| 3740 | " thread...\n", |
| 3741 | mdname(mddev)); |
| 3742 | /* leave the spares where they are, it shouldn't hurt */ |
| 3743 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3744 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3745 | } |
| 3746 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3747 | md_wakeup_thread(mddev->thread); |
| 3748 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3749 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3750 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3751 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3752 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3753 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3754 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3755 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | return 0; |
| 3757 | } |
| 3758 | |
| 3759 | static int restart_array(mddev_t *mddev) |
| 3760 | { |
| 3761 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3763 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3765 | return -ENXIO; |
| 3766 | if (!mddev->pers) |
| 3767 | return -EINVAL; |
| 3768 | if (!mddev->ro) |
| 3769 | return -EBUSY; |
| 3770 | mddev->safemode = 0; |
| 3771 | mddev->ro = 0; |
| 3772 | set_disk_ro(disk, 0); |
| 3773 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3774 | mdname(mddev)); |
| 3775 | /* Kick recovery or resync if necessary */ |
| 3776 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3777 | md_wakeup_thread(mddev->thread); |
| 3778 | md_wakeup_thread(mddev->sync_thread); |
| 3779 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 3780 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3783 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3784 | * to the file ourselves */ |
| 3785 | static int deny_bitmap_write_access(struct file * file) |
| 3786 | { |
| 3787 | struct inode *inode = file->f_mapping->host; |
| 3788 | |
| 3789 | spin_lock(&inode->i_lock); |
| 3790 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3791 | spin_unlock(&inode->i_lock); |
| 3792 | return -ETXTBSY; |
| 3793 | } |
| 3794 | atomic_set(&inode->i_writecount, -1); |
| 3795 | spin_unlock(&inode->i_lock); |
| 3796 | |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
| 3800 | static void restore_bitmap_write_access(struct file *file) |
| 3801 | { |
| 3802 | struct inode *inode = file->f_mapping->host; |
| 3803 | |
| 3804 | spin_lock(&inode->i_lock); |
| 3805 | atomic_set(&inode->i_writecount, 1); |
| 3806 | spin_unlock(&inode->i_lock); |
| 3807 | } |
| 3808 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3809 | /* mode: |
| 3810 | * 0 - completely stop and dis-assemble array |
| 3811 | * 1 - switch to readonly |
| 3812 | * 2 - stop but do not disassemble array |
| 3813 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3814 | static int do_md_stop(mddev_t * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | { |
| 3816 | int err = 0; |
| 3817 | struct gendisk *disk = mddev->gendisk; |
| 3818 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3819 | if (atomic_read(&mddev->active) > 1 + is_open) { |
| 3820 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3821 | return -EBUSY; |
| 3822 | } |
| 3823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | |
| 3826 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3827 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3829 | md_unregister_thread(mddev->sync_thread); |
| 3830 | mddev->sync_thread = NULL; |
| 3831 | } |
| 3832 | |
| 3833 | del_timer_sync(&mddev->safemode_timer); |
| 3834 | |
| 3835 | invalidate_partition(disk, 0); |
| 3836 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3837 | switch(mode) { |
| 3838 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3840 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | goto out; |
| 3842 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3843 | break; |
| 3844 | case 0: /* disassemble */ |
| 3845 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3846 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3847 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | if (mddev->ro) |
| 3849 | set_disk_ro(disk, 0); |
| 3850 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3851 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3852 | mddev->queue->merge_bvec_fn = NULL; |
| 3853 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3854 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3855 | if (mddev->pers->sync_request) |
| 3856 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | module_put(mddev->pers->owner); |
| 3859 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3860 | /* tell userspace to handle 'inactive' */ |
| 3861 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3862 | |
| 3863 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3864 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | if (mddev->ro) |
| 3867 | mddev->ro = 0; |
| 3868 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3869 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | /* mark array as shutdown cleanly */ |
| 3871 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3872 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3874 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3876 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | /* |
| 3880 | * Free resources if final stop |
| 3881 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3882 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3883 | mdk_rdev_t *rdev; |
| 3884 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3887 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3888 | bitmap_destroy(mddev); |
| 3889 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3890 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3891 | fput(mddev->bitmap_file); |
| 3892 | mddev->bitmap_file = NULL; |
| 3893 | } |
| 3894 | mddev->bitmap_offset = 0; |
| 3895 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3896 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3897 | if (rdev->raid_disk >= 0) { |
| 3898 | char nm[20]; |
| 3899 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3900 | sysfs_remove_link(&mddev->kobj, nm); |
| 3901 | } |
| 3902 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3903 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3904 | flush_scheduled_work(); |
| 3905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | export_array(mddev); |
| 3907 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame^] | 3908 | mddev->array_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3909 | mddev->size = 0; |
| 3910 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3911 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3912 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3913 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3914 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3915 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3916 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3917 | mddev->level = LEVEL_NONE; |
| 3918 | mddev->clevel[0] = 0; |
| 3919 | mddev->flags = 0; |
| 3920 | mddev->ro = 0; |
| 3921 | mddev->metadata_type[0] = 0; |
| 3922 | mddev->chunk_size = 0; |
| 3923 | mddev->ctime = mddev->utime = 0; |
| 3924 | mddev->layout = 0; |
| 3925 | mddev->max_disks = 0; |
| 3926 | mddev->events = 0; |
| 3927 | mddev->delta_disks = 0; |
| 3928 | mddev->new_level = LEVEL_NONE; |
| 3929 | mddev->new_layout = 0; |
| 3930 | mddev->new_chunk = 0; |
| 3931 | mddev->curr_resync = 0; |
| 3932 | mddev->resync_mismatches = 0; |
| 3933 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3934 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3935 | mddev->recovery = 0; |
| 3936 | mddev->in_sync = 0; |
| 3937 | mddev->changed = 0; |
| 3938 | mddev->degraded = 0; |
| 3939 | mddev->barriers_work = 0; |
| 3940 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3941 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3942 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3944 | mdname(mddev)); |
| 3945 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3946 | md_new_event(mddev); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3947 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | out: |
| 3949 | return err; |
| 3950 | } |
| 3951 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3952 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | static void autorun_array(mddev_t *mddev) |
| 3954 | { |
| 3955 | mdk_rdev_t *rdev; |
| 3956 | struct list_head *tmp; |
| 3957 | int err; |
| 3958 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3959 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | |
| 3962 | printk(KERN_INFO "md: running: "); |
| 3963 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3964 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | char b[BDEVNAME_SIZE]; |
| 3966 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3967 | } |
| 3968 | printk("\n"); |
| 3969 | |
| 3970 | err = do_md_run (mddev); |
| 3971 | if (err) { |
| 3972 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3973 | do_md_stop (mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | /* |
| 3978 | * lets try to run arrays based on all disks that have arrived |
| 3979 | * until now. (those are in pending_raid_disks) |
| 3980 | * |
| 3981 | * the method: pick the first pending disk, collect all disks with |
| 3982 | * the same UUID, remove all from the pending list and put them into |
| 3983 | * the 'same_array' list. Then order this list based on superblock |
| 3984 | * update time (freshest comes first), kick out 'old' disks and |
| 3985 | * compare superblocks. If everything's fine then run it. |
| 3986 | * |
| 3987 | * If "unit" is allocated, then bump its reference count |
| 3988 | */ |
| 3989 | static void autorun_devices(int part) |
| 3990 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | struct list_head *tmp; |
| 3992 | mdk_rdev_t *rdev0, *rdev; |
| 3993 | mddev_t *mddev; |
| 3994 | char b[BDEVNAME_SIZE]; |
| 3995 | |
| 3996 | printk(KERN_INFO "md: autorun ...\n"); |
| 3997 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3998 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4000 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | rdev0 = list_entry(pending_raid_disks.next, |
| 4002 | mdk_rdev_t, same_set); |
| 4003 | |
| 4004 | printk(KERN_INFO "md: considering %s ...\n", |
| 4005 | bdevname(rdev0->bdev,b)); |
| 4006 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4007 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4009 | printk(KERN_INFO "md: adding %s ...\n", |
| 4010 | bdevname(rdev->bdev,b)); |
| 4011 | list_move(&rdev->same_set, &candidates); |
| 4012 | } |
| 4013 | /* |
| 4014 | * now we have a set of devices, with all of them having |
| 4015 | * mostly sane superblocks. It's time to allocate the |
| 4016 | * mddev. |
| 4017 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4018 | if (part) { |
| 4019 | dev = MKDEV(mdp_major, |
| 4020 | rdev0->preferred_minor << MdpMinorShift); |
| 4021 | unit = MINOR(dev) >> MdpMinorShift; |
| 4022 | } else { |
| 4023 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4024 | unit = MINOR(dev); |
| 4025 | } |
| 4026 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4028 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4029 | break; |
| 4030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | |
| 4032 | md_probe(dev, NULL, NULL); |
| 4033 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4034 | if (!mddev || !mddev->gendisk) { |
| 4035 | if (mddev) |
| 4036 | mddev_put(mddev); |
| 4037 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | "md: cannot allocate memory for md drive.\n"); |
| 4039 | break; |
| 4040 | } |
| 4041 | if (mddev_lock(mddev)) |
| 4042 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4043 | mdname(mddev)); |
| 4044 | else if (mddev->raid_disks || mddev->major_version |
| 4045 | || !list_empty(&mddev->disks)) { |
| 4046 | printk(KERN_WARNING |
| 4047 | "md: %s already running, cannot run %s\n", |
| 4048 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4049 | mddev_unlock(mddev); |
| 4050 | } else { |
| 4051 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4052 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4053 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | list_del_init(&rdev->same_set); |
| 4055 | if (bind_rdev_to_array(rdev, mddev)) |
| 4056 | export_rdev(rdev); |
| 4057 | } |
| 4058 | autorun_array(mddev); |
| 4059 | mddev_unlock(mddev); |
| 4060 | } |
| 4061 | /* on success, candidates will be empty, on error |
| 4062 | * it won't... |
| 4063 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4064 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | export_rdev(rdev); |
| 4066 | mddev_put(mddev); |
| 4067 | } |
| 4068 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4069 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4070 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | static int get_version(void __user * arg) |
| 4073 | { |
| 4074 | mdu_version_t ver; |
| 4075 | |
| 4076 | ver.major = MD_MAJOR_VERSION; |
| 4077 | ver.minor = MD_MINOR_VERSION; |
| 4078 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4079 | |
| 4080 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4081 | return -EFAULT; |
| 4082 | |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
| 4086 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4087 | { |
| 4088 | mdu_array_info_t info; |
| 4089 | int nr,working,active,failed,spare; |
| 4090 | mdk_rdev_t *rdev; |
| 4091 | struct list_head *tmp; |
| 4092 | |
| 4093 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4094 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4096 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | failed++; |
| 4098 | else { |
| 4099 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4100 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | active++; |
| 4102 | else |
| 4103 | spare++; |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | info.major_version = mddev->major_version; |
| 4108 | info.minor_version = mddev->minor_version; |
| 4109 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4110 | info.ctime = mddev->ctime; |
| 4111 | info.level = mddev->level; |
| 4112 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4113 | if (info.size != mddev->size) /* overflow */ |
| 4114 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | info.nr_disks = nr; |
| 4116 | info.raid_disks = mddev->raid_disks; |
| 4117 | info.md_minor = mddev->md_minor; |
| 4118 | info.not_persistent= !mddev->persistent; |
| 4119 | |
| 4120 | info.utime = mddev->utime; |
| 4121 | info.state = 0; |
| 4122 | if (mddev->in_sync) |
| 4123 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4124 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4125 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | info.active_disks = active; |
| 4127 | info.working_disks = working; |
| 4128 | info.failed_disks = failed; |
| 4129 | info.spare_disks = spare; |
| 4130 | |
| 4131 | info.layout = mddev->layout; |
| 4132 | info.chunk_size = mddev->chunk_size; |
| 4133 | |
| 4134 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4135 | return -EFAULT; |
| 4136 | |
| 4137 | return 0; |
| 4138 | } |
| 4139 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4140 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4141 | { |
| 4142 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4143 | char *ptr, *buf = NULL; |
| 4144 | int err = -ENOMEM; |
| 4145 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4146 | if (md_allow_write(mddev)) |
| 4147 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4148 | else |
| 4149 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4150 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4151 | if (!file) |
| 4152 | goto out; |
| 4153 | |
| 4154 | /* bitmap disabled, zero the first byte and copy out */ |
| 4155 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4156 | file->pathname[0] = '\0'; |
| 4157 | goto copy_out; |
| 4158 | } |
| 4159 | |
| 4160 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4161 | if (!buf) |
| 4162 | goto out; |
| 4163 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4164 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4165 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4166 | goto out; |
| 4167 | |
| 4168 | strcpy(file->pathname, ptr); |
| 4169 | |
| 4170 | copy_out: |
| 4171 | err = 0; |
| 4172 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4173 | err = -EFAULT; |
| 4174 | out: |
| 4175 | kfree(buf); |
| 4176 | kfree(file); |
| 4177 | return err; |
| 4178 | } |
| 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4181 | { |
| 4182 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | mdk_rdev_t *rdev; |
| 4184 | |
| 4185 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4186 | return -EFAULT; |
| 4187 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4188 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | if (rdev) { |
| 4190 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4191 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4192 | info.raid_disk = rdev->raid_disk; |
| 4193 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4194 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4196 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4198 | info.state |= (1<<MD_DISK_SYNC); |
| 4199 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4200 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4201 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | } else { |
| 4203 | info.major = info.minor = 0; |
| 4204 | info.raid_disk = -1; |
| 4205 | info.state = (1<<MD_DISK_REMOVED); |
| 4206 | } |
| 4207 | |
| 4208 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4209 | return -EFAULT; |
| 4210 | |
| 4211 | return 0; |
| 4212 | } |
| 4213 | |
| 4214 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4215 | { |
| 4216 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4217 | mdk_rdev_t *rdev; |
| 4218 | dev_t dev = MKDEV(info->major,info->minor); |
| 4219 | |
| 4220 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4221 | return -EOVERFLOW; |
| 4222 | |
| 4223 | if (!mddev->raid_disks) { |
| 4224 | int err; |
| 4225 | /* expecting a device which has a superblock */ |
| 4226 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 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 | } |
| 4233 | if (!list_empty(&mddev->disks)) { |
| 4234 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4235 | mdk_rdev_t, same_set); |
| 4236 | int err = super_types[mddev->major_version] |
| 4237 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4238 | if (err < 0) { |
| 4239 | printk(KERN_WARNING |
| 4240 | "md: %s has different UUID to %s\n", |
| 4241 | bdevname(rdev->bdev,b), |
| 4242 | bdevname(rdev0->bdev,b2)); |
| 4243 | export_rdev(rdev); |
| 4244 | return -EINVAL; |
| 4245 | } |
| 4246 | } |
| 4247 | err = bind_rdev_to_array(rdev, mddev); |
| 4248 | if (err) |
| 4249 | export_rdev(rdev); |
| 4250 | return err; |
| 4251 | } |
| 4252 | |
| 4253 | /* |
| 4254 | * add_new_disk can be used once the array is assembled |
| 4255 | * to add "hot spares". They must already have a superblock |
| 4256 | * written |
| 4257 | */ |
| 4258 | if (mddev->pers) { |
| 4259 | int err; |
| 4260 | if (!mddev->pers->hot_add_disk) { |
| 4261 | printk(KERN_WARNING |
| 4262 | "%s: personality does not support diskops!\n", |
| 4263 | mdname(mddev)); |
| 4264 | return -EINVAL; |
| 4265 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4266 | if (mddev->persistent) |
| 4267 | rdev = md_import_device(dev, mddev->major_version, |
| 4268 | mddev->minor_version); |
| 4269 | else |
| 4270 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | if (IS_ERR(rdev)) { |
| 4272 | printk(KERN_WARNING |
| 4273 | "md: md_import_device returned %ld\n", |
| 4274 | PTR_ERR(rdev)); |
| 4275 | return PTR_ERR(rdev); |
| 4276 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4277 | /* set save_raid_disk if appropriate */ |
| 4278 | if (!mddev->persistent) { |
| 4279 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4280 | info->raid_disk < mddev->raid_disks) |
| 4281 | rdev->raid_disk = info->raid_disk; |
| 4282 | else |
| 4283 | rdev->raid_disk = -1; |
| 4284 | } else |
| 4285 | super_types[mddev->major_version]. |
| 4286 | validate_super(mddev, rdev); |
| 4287 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4288 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4289 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4290 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4291 | set_bit(WriteMostly, &rdev->flags); |
| 4292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | rdev->raid_disk = -1; |
| 4294 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4295 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4296 | /* If there is hot_add_disk but no hot_remove_disk |
| 4297 | * then added disks for geometry changes, |
| 4298 | * and should be added immediately. |
| 4299 | */ |
| 4300 | super_types[mddev->major_version]. |
| 4301 | validate_super(mddev, rdev); |
| 4302 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4303 | if (err) |
| 4304 | unbind_rdev_from_array(rdev); |
| 4305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | if (err) |
| 4307 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4308 | else |
| 4309 | sysfs_notify(&rdev->kobj, NULL, "state"); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4310 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4311 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4312 | if (mddev->degraded) |
| 4313 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4314 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4315 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | return err; |
| 4317 | } |
| 4318 | |
| 4319 | /* otherwise, add_new_disk is only allowed |
| 4320 | * for major_version==0 superblocks |
| 4321 | */ |
| 4322 | if (mddev->major_version != 0) { |
| 4323 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4324 | mdname(mddev)); |
| 4325 | return -EINVAL; |
| 4326 | } |
| 4327 | |
| 4328 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4329 | int err; |
| 4330 | rdev = md_import_device (dev, -1, 0); |
| 4331 | if (IS_ERR(rdev)) { |
| 4332 | printk(KERN_WARNING |
| 4333 | "md: error, md_import_device() returned %ld\n", |
| 4334 | PTR_ERR(rdev)); |
| 4335 | return PTR_ERR(rdev); |
| 4336 | } |
| 4337 | rdev->desc_nr = info->number; |
| 4338 | if (info->raid_disk < mddev->raid_disks) |
| 4339 | rdev->raid_disk = info->raid_disk; |
| 4340 | else |
| 4341 | rdev->raid_disk = -1; |
| 4342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4344 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4345 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4347 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4348 | set_bit(WriteMostly, &rdev->flags); |
| 4349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | if (!mddev->persistent) { |
| 4351 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4352 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4354 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4355 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4357 | err = bind_rdev_to_array(rdev, mddev); |
| 4358 | if (err) { |
| 4359 | export_rdev(rdev); |
| 4360 | return err; |
| 4361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | return 0; |
| 4365 | } |
| 4366 | |
| 4367 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4368 | { |
| 4369 | char b[BDEVNAME_SIZE]; |
| 4370 | mdk_rdev_t *rdev; |
| 4371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | rdev = find_rdev(mddev, dev); |
| 4373 | if (!rdev) |
| 4374 | return -ENXIO; |
| 4375 | |
| 4376 | if (rdev->raid_disk >= 0) |
| 4377 | goto busy; |
| 4378 | |
| 4379 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4380 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4381 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | |
| 4383 | return 0; |
| 4384 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4385 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4387 | return -EBUSY; |
| 4388 | } |
| 4389 | |
| 4390 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4391 | { |
| 4392 | char b[BDEVNAME_SIZE]; |
| 4393 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | mdk_rdev_t *rdev; |
| 4395 | |
| 4396 | if (!mddev->pers) |
| 4397 | return -ENODEV; |
| 4398 | |
| 4399 | if (mddev->major_version != 0) { |
| 4400 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4401 | " version-0 superblocks.\n", |
| 4402 | mdname(mddev)); |
| 4403 | return -EINVAL; |
| 4404 | } |
| 4405 | if (!mddev->pers->hot_add_disk) { |
| 4406 | printk(KERN_WARNING |
| 4407 | "%s: personality does not support diskops!\n", |
| 4408 | mdname(mddev)); |
| 4409 | return -EINVAL; |
| 4410 | } |
| 4411 | |
| 4412 | rdev = md_import_device (dev, -1, 0); |
| 4413 | if (IS_ERR(rdev)) { |
| 4414 | printk(KERN_WARNING |
| 4415 | "md: error, md_import_device() returned %ld\n", |
| 4416 | PTR_ERR(rdev)); |
| 4417 | return -EINVAL; |
| 4418 | } |
| 4419 | |
| 4420 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4421 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4423 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4425 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4427 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | printk(KERN_WARNING |
| 4429 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4430 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4431 | err = -EINVAL; |
| 4432 | goto abort_export; |
| 4433 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4434 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4436 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4437 | err = bind_rdev_to_array(rdev, mddev); |
| 4438 | if (err) |
| 4439 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | |
| 4441 | /* |
| 4442 | * The rest should better be atomic, we can have disk failures |
| 4443 | * noticed in interrupt contexts ... |
| 4444 | */ |
| 4445 | |
| 4446 | if (rdev->desc_nr == mddev->max_disks) { |
| 4447 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4448 | mdname(mddev)); |
| 4449 | err = -EBUSY; |
| 4450 | goto abort_unbind_export; |
| 4451 | } |
| 4452 | |
| 4453 | rdev->raid_disk = -1; |
| 4454 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4455 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
| 4457 | /* |
| 4458 | * Kick recovery, maybe this spare has to be added to the |
| 4459 | * array immediately. |
| 4460 | */ |
| 4461 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4462 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4463 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | return 0; |
| 4465 | |
| 4466 | abort_unbind_export: |
| 4467 | unbind_rdev_from_array(rdev); |
| 4468 | |
| 4469 | abort_export: |
| 4470 | export_rdev(rdev); |
| 4471 | return err; |
| 4472 | } |
| 4473 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4474 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4475 | { |
| 4476 | int err; |
| 4477 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4478 | if (mddev->pers) { |
| 4479 | if (!mddev->pers->quiesce) |
| 4480 | return -EBUSY; |
| 4481 | if (mddev->recovery || mddev->sync_thread) |
| 4482 | return -EBUSY; |
| 4483 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4486 | |
| 4487 | if (fd >= 0) { |
| 4488 | if (mddev->bitmap) |
| 4489 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4490 | mddev->bitmap_file = fget(fd); |
| 4491 | |
| 4492 | if (mddev->bitmap_file == NULL) { |
| 4493 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4494 | mdname(mddev)); |
| 4495 | return -EBADF; |
| 4496 | } |
| 4497 | |
| 4498 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4499 | if (err) { |
| 4500 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4501 | mdname(mddev)); |
| 4502 | fput(mddev->bitmap_file); |
| 4503 | mddev->bitmap_file = NULL; |
| 4504 | return err; |
| 4505 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4506 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4507 | } else if (mddev->bitmap == NULL) |
| 4508 | return -ENOENT; /* cannot remove what isn't there */ |
| 4509 | err = 0; |
| 4510 | if (mddev->pers) { |
| 4511 | mddev->pers->quiesce(mddev, 1); |
| 4512 | if (fd >= 0) |
| 4513 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4514 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4515 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4516 | fd = -1; /* make sure to put the file */ |
| 4517 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4518 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4519 | } |
| 4520 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4521 | if (mddev->bitmap_file) { |
| 4522 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4523 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4524 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4525 | mddev->bitmap_file = NULL; |
| 4526 | } |
| 4527 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4528 | return err; |
| 4529 | } |
| 4530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | /* |
| 4532 | * set_array_info is used two different ways |
| 4533 | * The original usage is when creating a new array. |
| 4534 | * In this usage, raid_disks is > 0 and it together with |
| 4535 | * level, size, not_persistent,layout,chunksize determine the |
| 4536 | * shape of the array. |
| 4537 | * This will always create an array with a type-0.90.0 superblock. |
| 4538 | * The newer usage is when assembling an array. |
| 4539 | * In this case raid_disks will be 0, and the major_version field is |
| 4540 | * use to determine which style super-blocks are to be found on the devices. |
| 4541 | * The minor and patch _version numbers are also kept incase the |
| 4542 | * super_block handler wishes to interpret them. |
| 4543 | */ |
| 4544 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4545 | { |
| 4546 | |
| 4547 | if (info->raid_disks == 0) { |
| 4548 | /* just setting version number for superblock loading */ |
| 4549 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4550 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | super_types[info->major_version].name == NULL) { |
| 4552 | /* maybe try to auto-load a module? */ |
| 4553 | printk(KERN_INFO |
| 4554 | "md: superblock version %d not known\n", |
| 4555 | info->major_version); |
| 4556 | return -EINVAL; |
| 4557 | } |
| 4558 | mddev->major_version = info->major_version; |
| 4559 | mddev->minor_version = info->minor_version; |
| 4560 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4561 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | return 0; |
| 4563 | } |
| 4564 | mddev->major_version = MD_MAJOR_VERSION; |
| 4565 | mddev->minor_version = MD_MINOR_VERSION; |
| 4566 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4567 | mddev->ctime = get_seconds(); |
| 4568 | |
| 4569 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4570 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | mddev->size = info->size; |
| 4572 | mddev->raid_disks = info->raid_disks; |
| 4573 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4574 | * openned |
| 4575 | */ |
| 4576 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4577 | mddev->recovery_cp = MaxSector; |
| 4578 | else |
| 4579 | mddev->recovery_cp = 0; |
| 4580 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4581 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | |
| 4583 | mddev->layout = info->layout; |
| 4584 | mddev->chunk_size = info->chunk_size; |
| 4585 | |
| 4586 | mddev->max_disks = MD_SB_DISKS; |
| 4587 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4588 | if (mddev->persistent) |
| 4589 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4590 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4592 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4593 | mddev->bitmap_offset = 0; |
| 4594 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4595 | mddev->reshape_position = MaxSector; |
| 4596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | /* |
| 4598 | * Generate a 128 bit UUID |
| 4599 | */ |
| 4600 | get_random_bytes(mddev->uuid, 16); |
| 4601 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4602 | mddev->new_level = mddev->level; |
| 4603 | mddev->new_chunk = mddev->chunk_size; |
| 4604 | mddev->new_layout = mddev->layout; |
| 4605 | mddev->delta_disks = 0; |
| 4606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | return 0; |
| 4608 | } |
| 4609 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4610 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4611 | { |
| 4612 | mdk_rdev_t * rdev; |
| 4613 | int rv; |
| 4614 | struct list_head *tmp; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4615 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4616 | |
| 4617 | if (mddev->pers->resize == NULL) |
| 4618 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4619 | /* The "num_sectors" is the number of sectors of each device that |
| 4620 | * is used. This can only make sense for arrays with redundancy. |
| 4621 | * linear and raid0 always use whatever space is available. We can only |
| 4622 | * consider changing this number if no resync or reconstruction is |
| 4623 | * happening, and if the new size is acceptable. It must fit before the |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4624 | * sb_start or, if that is <data_offset, it must fit before the size |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4625 | * of each device. If num_sectors is zero, we find the largest size |
| 4626 | * that fits. |
| 4627 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4628 | */ |
| 4629 | if (mddev->sync_thread) |
| 4630 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4631 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4632 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4633 | avail = rdev->size * 2; |
| 4634 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4635 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 4636 | num_sectors = avail; |
| 4637 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4638 | return -ENOSPC; |
| 4639 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4640 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4641 | if (!rv) { |
| 4642 | struct block_device *bdev; |
| 4643 | |
| 4644 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4645 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4646 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame^] | 4647 | i_size_write(bdev->bd_inode, |
| 4648 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4649 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4650 | bdput(bdev); |
| 4651 | } |
| 4652 | } |
| 4653 | return rv; |
| 4654 | } |
| 4655 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4656 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4657 | { |
| 4658 | int rv; |
| 4659 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4660 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4661 | return -EINVAL; |
| 4662 | if (raid_disks <= 0 || |
| 4663 | raid_disks >= mddev->max_disks) |
| 4664 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4665 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4666 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4667 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4668 | |
| 4669 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4670 | return rv; |
| 4671 | } |
| 4672 | |
| 4673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | /* |
| 4675 | * update_array_info is used to change the configuration of an |
| 4676 | * on-line array. |
| 4677 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4678 | * fields in the info are checked against the array. |
| 4679 | * Any differences that cannot be handled will cause an error. |
| 4680 | * Normally, only one change can be managed at a time. |
| 4681 | */ |
| 4682 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4683 | { |
| 4684 | int rv = 0; |
| 4685 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4686 | int state = 0; |
| 4687 | |
| 4688 | /* calculate expected state,ignoring low bits */ |
| 4689 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4690 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
| 4692 | if (mddev->major_version != info->major_version || |
| 4693 | mddev->minor_version != info->minor_version || |
| 4694 | /* mddev->patch_version != info->patch_version || */ |
| 4695 | mddev->ctime != info->ctime || |
| 4696 | mddev->level != info->level || |
| 4697 | /* mddev->layout != info->layout || */ |
| 4698 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4699 | mddev->chunk_size != info->chunk_size || |
| 4700 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4701 | ((state^info->state) & 0xfffffe00) |
| 4702 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | return -EINVAL; |
| 4704 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4705 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4707 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4708 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | if (cnt == 0) return 0; |
| 4710 | if (cnt > 1) return -EINVAL; |
| 4711 | |
| 4712 | if (mddev->layout != info->layout) { |
| 4713 | /* Change layout |
| 4714 | * we don't need to do anything at the md level, the |
| 4715 | * personality will take care of it all. |
| 4716 | */ |
| 4717 | if (mddev->pers->reconfig == NULL) |
| 4718 | return -EINVAL; |
| 4719 | else |
| 4720 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4721 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4722 | if (info->size >= 0 && mddev->size != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4723 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4725 | if (mddev->raid_disks != info->raid_disks) |
| 4726 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4728 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4729 | if (mddev->pers->quiesce == NULL) |
| 4730 | return -EINVAL; |
| 4731 | if (mddev->recovery || mddev->sync_thread) |
| 4732 | return -EBUSY; |
| 4733 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4734 | /* add the bitmap */ |
| 4735 | if (mddev->bitmap) |
| 4736 | return -EEXIST; |
| 4737 | if (mddev->default_bitmap_offset == 0) |
| 4738 | return -EINVAL; |
| 4739 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4740 | mddev->pers->quiesce(mddev, 1); |
| 4741 | rv = bitmap_create(mddev); |
| 4742 | if (rv) |
| 4743 | bitmap_destroy(mddev); |
| 4744 | mddev->pers->quiesce(mddev, 0); |
| 4745 | } else { |
| 4746 | /* remove the bitmap */ |
| 4747 | if (!mddev->bitmap) |
| 4748 | return -ENOENT; |
| 4749 | if (mddev->bitmap->file) |
| 4750 | return -EINVAL; |
| 4751 | mddev->pers->quiesce(mddev, 1); |
| 4752 | bitmap_destroy(mddev); |
| 4753 | mddev->pers->quiesce(mddev, 0); |
| 4754 | mddev->bitmap_offset = 0; |
| 4755 | } |
| 4756 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4757 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | return rv; |
| 4759 | } |
| 4760 | |
| 4761 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4762 | { |
| 4763 | mdk_rdev_t *rdev; |
| 4764 | |
| 4765 | if (mddev->pers == NULL) |
| 4766 | return -ENODEV; |
| 4767 | |
| 4768 | rdev = find_rdev(mddev, dev); |
| 4769 | if (!rdev) |
| 4770 | return -ENODEV; |
| 4771 | |
| 4772 | md_error(mddev, rdev); |
| 4773 | return 0; |
| 4774 | } |
| 4775 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 4776 | /* |
| 4777 | * We have a problem here : there is no easy way to give a CHS |
| 4778 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4779 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4780 | * dosfs just mad... ;-) |
| 4781 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4782 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4783 | { |
| 4784 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4785 | |
| 4786 | geo->heads = 2; |
| 4787 | geo->sectors = 4; |
| 4788 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4789 | return 0; |
| 4790 | } |
| 4791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4793 | unsigned int cmd, unsigned long arg) |
| 4794 | { |
| 4795 | int err = 0; |
| 4796 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | mddev_t *mddev = NULL; |
| 4798 | |
| 4799 | if (!capable(CAP_SYS_ADMIN)) |
| 4800 | return -EACCES; |
| 4801 | |
| 4802 | /* |
| 4803 | * Commands dealing with the RAID driver but not any |
| 4804 | * particular array: |
| 4805 | */ |
| 4806 | switch (cmd) |
| 4807 | { |
| 4808 | case RAID_VERSION: |
| 4809 | err = get_version(argp); |
| 4810 | goto done; |
| 4811 | |
| 4812 | case PRINT_RAID_DEBUG: |
| 4813 | err = 0; |
| 4814 | md_print_devices(); |
| 4815 | goto done; |
| 4816 | |
| 4817 | #ifndef MODULE |
| 4818 | case RAID_AUTORUN: |
| 4819 | err = 0; |
| 4820 | autostart_arrays(arg); |
| 4821 | goto done; |
| 4822 | #endif |
| 4823 | default:; |
| 4824 | } |
| 4825 | |
| 4826 | /* |
| 4827 | * Commands creating/starting a new array: |
| 4828 | */ |
| 4829 | |
| 4830 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4831 | |
| 4832 | if (!mddev) { |
| 4833 | BUG(); |
| 4834 | goto abort; |
| 4835 | } |
| 4836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | err = mddev_lock(mddev); |
| 4838 | if (err) { |
| 4839 | printk(KERN_INFO |
| 4840 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4841 | err, cmd); |
| 4842 | goto abort; |
| 4843 | } |
| 4844 | |
| 4845 | switch (cmd) |
| 4846 | { |
| 4847 | case SET_ARRAY_INFO: |
| 4848 | { |
| 4849 | mdu_array_info_t info; |
| 4850 | if (!arg) |
| 4851 | memset(&info, 0, sizeof(info)); |
| 4852 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4853 | err = -EFAULT; |
| 4854 | goto abort_unlock; |
| 4855 | } |
| 4856 | if (mddev->pers) { |
| 4857 | err = update_array_info(mddev, &info); |
| 4858 | if (err) { |
| 4859 | printk(KERN_WARNING "md: couldn't update" |
| 4860 | " array info. %d\n", err); |
| 4861 | goto abort_unlock; |
| 4862 | } |
| 4863 | goto done_unlock; |
| 4864 | } |
| 4865 | if (!list_empty(&mddev->disks)) { |
| 4866 | printk(KERN_WARNING |
| 4867 | "md: array %s already has disks!\n", |
| 4868 | mdname(mddev)); |
| 4869 | err = -EBUSY; |
| 4870 | goto abort_unlock; |
| 4871 | } |
| 4872 | if (mddev->raid_disks) { |
| 4873 | printk(KERN_WARNING |
| 4874 | "md: array %s already initialised!\n", |
| 4875 | mdname(mddev)); |
| 4876 | err = -EBUSY; |
| 4877 | goto abort_unlock; |
| 4878 | } |
| 4879 | err = set_array_info(mddev, &info); |
| 4880 | if (err) { |
| 4881 | printk(KERN_WARNING "md: couldn't set" |
| 4882 | " array info. %d\n", err); |
| 4883 | goto abort_unlock; |
| 4884 | } |
| 4885 | } |
| 4886 | goto done_unlock; |
| 4887 | |
| 4888 | default:; |
| 4889 | } |
| 4890 | |
| 4891 | /* |
| 4892 | * Commands querying/configuring an existing array: |
| 4893 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4894 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4895 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4896 | if ((!mddev->raid_disks && !mddev->external) |
| 4897 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4898 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4899 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | err = -ENODEV; |
| 4901 | goto abort_unlock; |
| 4902 | } |
| 4903 | |
| 4904 | /* |
| 4905 | * Commands even a read-only array can execute: |
| 4906 | */ |
| 4907 | switch (cmd) |
| 4908 | { |
| 4909 | case GET_ARRAY_INFO: |
| 4910 | err = get_array_info(mddev, argp); |
| 4911 | goto done_unlock; |
| 4912 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4913 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4914 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4915 | goto done_unlock; |
| 4916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | case GET_DISK_INFO: |
| 4918 | err = get_disk_info(mddev, argp); |
| 4919 | goto done_unlock; |
| 4920 | |
| 4921 | case RESTART_ARRAY_RW: |
| 4922 | err = restart_array(mddev); |
| 4923 | goto done_unlock; |
| 4924 | |
| 4925 | case STOP_ARRAY: |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4926 | err = do_md_stop (mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | goto done_unlock; |
| 4928 | |
| 4929 | case STOP_ARRAY_RO: |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4930 | err = do_md_stop (mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | goto done_unlock; |
| 4932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | /* |
| 4936 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4937 | * superblock, so we do not allow them on read-only arrays. |
| 4938 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4939 | * here and hit the 'default' below, so only disallow |
| 4940 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 4942 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4943 | if (mddev->ro == 2) { |
| 4944 | mddev->ro = 0; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4945 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 4946 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4947 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4948 | } else { |
| 4949 | err = -EROFS; |
| 4950 | goto abort_unlock; |
| 4951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | switch (cmd) |
| 4955 | { |
| 4956 | case ADD_NEW_DISK: |
| 4957 | { |
| 4958 | mdu_disk_info_t info; |
| 4959 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4960 | err = -EFAULT; |
| 4961 | else |
| 4962 | err = add_new_disk(mddev, &info); |
| 4963 | goto done_unlock; |
| 4964 | } |
| 4965 | |
| 4966 | case HOT_REMOVE_DISK: |
| 4967 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4968 | goto done_unlock; |
| 4969 | |
| 4970 | case HOT_ADD_DISK: |
| 4971 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4972 | goto done_unlock; |
| 4973 | |
| 4974 | case SET_DISK_FAULTY: |
| 4975 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4976 | goto done_unlock; |
| 4977 | |
| 4978 | case RUN_ARRAY: |
| 4979 | err = do_md_run (mddev); |
| 4980 | goto done_unlock; |
| 4981 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4982 | case SET_BITMAP_FILE: |
| 4983 | err = set_bitmap_file(mddev, (int)arg); |
| 4984 | goto done_unlock; |
| 4985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | err = -EINVAL; |
| 4988 | goto abort_unlock; |
| 4989 | } |
| 4990 | |
| 4991 | done_unlock: |
| 4992 | abort_unlock: |
| 4993 | mddev_unlock(mddev); |
| 4994 | |
| 4995 | return err; |
| 4996 | done: |
| 4997 | if (err) |
| 4998 | MD_BUG(); |
| 4999 | abort: |
| 5000 | return err; |
| 5001 | } |
| 5002 | |
| 5003 | static int md_open(struct inode *inode, struct file *file) |
| 5004 | { |
| 5005 | /* |
| 5006 | * Succeed if we can lock the mddev, which confirms that |
| 5007 | * it isn't being stopped right now. |
| 5008 | */ |
| 5009 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5010 | int err; |
| 5011 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5012 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | goto out; |
| 5014 | |
| 5015 | err = 0; |
| 5016 | mddev_get(mddev); |
| 5017 | mddev_unlock(mddev); |
| 5018 | |
| 5019 | check_disk_change(inode->i_bdev); |
| 5020 | out: |
| 5021 | return err; |
| 5022 | } |
| 5023 | |
| 5024 | static int md_release(struct inode *inode, struct file * file) |
| 5025 | { |
| 5026 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 5027 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5028 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | mddev_put(mddev); |
| 5030 | |
| 5031 | return 0; |
| 5032 | } |
| 5033 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5034 | static int md_media_changed(struct gendisk *disk) |
| 5035 | { |
| 5036 | mddev_t *mddev = disk->private_data; |
| 5037 | |
| 5038 | return mddev->changed; |
| 5039 | } |
| 5040 | |
| 5041 | static int md_revalidate(struct gendisk *disk) |
| 5042 | { |
| 5043 | mddev_t *mddev = disk->private_data; |
| 5044 | |
| 5045 | mddev->changed = 0; |
| 5046 | return 0; |
| 5047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5048 | static struct block_device_operations md_fops = |
| 5049 | { |
| 5050 | .owner = THIS_MODULE, |
| 5051 | .open = md_open, |
| 5052 | .release = md_release, |
| 5053 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5054 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5055 | .media_changed = md_media_changed, |
| 5056 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | }; |
| 5058 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5059 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | { |
| 5061 | mdk_thread_t *thread = arg; |
| 5062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | /* |
| 5064 | * md_thread is a 'system-thread', it's priority should be very |
| 5065 | * high. We avoid resource deadlocks individually in each |
| 5066 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5067 | * the very same RT priority as kswapd, thus we will never get |
| 5068 | * into a priority inversion deadlock. |
| 5069 | * |
| 5070 | * we definitely have to have equal or higher priority than |
| 5071 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5072 | * many dirty RAID5 blocks. |
| 5073 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5075 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5076 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5078 | /* We need to wait INTERRUPTIBLE so that |
| 5079 | * we don't add to the load-average. |
| 5080 | * That means we need to be sure no signals are |
| 5081 | * pending |
| 5082 | */ |
| 5083 | if (signal_pending(current)) |
| 5084 | flush_signals(current); |
| 5085 | |
| 5086 | wait_event_interruptible_timeout |
| 5087 | (thread->wqueue, |
| 5088 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5089 | || kthread_should_stop(), |
| 5090 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | |
| 5092 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5093 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5094 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | return 0; |
| 5098 | } |
| 5099 | |
| 5100 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5101 | { |
| 5102 | if (thread) { |
| 5103 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5104 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5105 | wake_up(&thread->wqueue); |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5110 | const char *name) |
| 5111 | { |
| 5112 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5114 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | if (!thread) |
| 5116 | return NULL; |
| 5117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | init_waitqueue_head(&thread->wqueue); |
| 5119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | thread->run = run; |
| 5121 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5122 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5123 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5124 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | kfree(thread); |
| 5126 | return NULL; |
| 5127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | return thread; |
| 5129 | } |
| 5130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | void md_unregister_thread(mdk_thread_t *thread) |
| 5132 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5133 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5134 | |
| 5135 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | kfree(thread); |
| 5137 | } |
| 5138 | |
| 5139 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5140 | { |
| 5141 | if (!mddev) { |
| 5142 | MD_BUG(); |
| 5143 | return; |
| 5144 | } |
| 5145 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5146 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5148 | |
| 5149 | if (mddev->external) |
| 5150 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5151 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5153 | mdname(mddev), |
| 5154 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5155 | __builtin_return_address(0),__builtin_return_address(1), |
| 5156 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5157 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5158 | if (!mddev->pers) |
| 5159 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | if (!mddev->pers->error_handler) |
| 5161 | return; |
| 5162 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5163 | if (mddev->degraded) |
| 5164 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5165 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5167 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5168 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5169 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | /* seq_file implementation /proc/mdstat */ |
| 5173 | |
| 5174 | static void status_unused(struct seq_file *seq) |
| 5175 | { |
| 5176 | int i = 0; |
| 5177 | mdk_rdev_t *rdev; |
| 5178 | struct list_head *tmp; |
| 5179 | |
| 5180 | seq_printf(seq, "unused devices: "); |
| 5181 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5182 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | char b[BDEVNAME_SIZE]; |
| 5184 | i++; |
| 5185 | seq_printf(seq, "%s ", |
| 5186 | bdevname(rdev->bdev,b)); |
| 5187 | } |
| 5188 | if (!i) |
| 5189 | seq_printf(seq, "<none>"); |
| 5190 | |
| 5191 | seq_printf(seq, "\n"); |
| 5192 | } |
| 5193 | |
| 5194 | |
| 5195 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5196 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5197 | sector_t max_blocks, resync, res; |
| 5198 | unsigned long dt, db, rt; |
| 5199 | int scale; |
| 5200 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | |
| 5202 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5203 | |
| 5204 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5205 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5206 | else |
| 5207 | max_blocks = mddev->size; |
| 5208 | |
| 5209 | /* |
| 5210 | * Should not happen. |
| 5211 | */ |
| 5212 | if (!max_blocks) { |
| 5213 | MD_BUG(); |
| 5214 | return; |
| 5215 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5216 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5217 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5218 | * u32, as those are the requirements for sector_div. |
| 5219 | * Thus 'scale' must be at least 10 |
| 5220 | */ |
| 5221 | scale = 10; |
| 5222 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5223 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5224 | scale++; |
| 5225 | } |
| 5226 | res = (resync>>scale)*1000; |
| 5227 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5228 | |
| 5229 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5231 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | seq_printf(seq, "["); |
| 5233 | for (i = 0; i < x; i++) |
| 5234 | seq_printf(seq, "="); |
| 5235 | seq_printf(seq, ">"); |
| 5236 | for (i = 0; i < y; i++) |
| 5237 | seq_printf(seq, "."); |
| 5238 | seq_printf(seq, "] "); |
| 5239 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5240 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5241 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5242 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5243 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5244 | "check" : |
| 5245 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5246 | "resync" : "recovery"))), |
| 5247 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5248 | (unsigned long long) resync, |
| 5249 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
| 5251 | /* |
| 5252 | * We do not want to overflow, so the order of operands and |
| 5253 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5254 | * safe against division by zero. We only estimate anyway. |
| 5255 | * |
| 5256 | * dt: time from mark until now |
| 5257 | * db: blocks written from mark until now |
| 5258 | * rt: remaining time |
| 5259 | */ |
| 5260 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5261 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5262 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5263 | - mddev->resync_mark_cnt; |
| 5264 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | |
| 5266 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5267 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5268 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5272 | { |
| 5273 | struct list_head *tmp; |
| 5274 | loff_t l = *pos; |
| 5275 | mddev_t *mddev; |
| 5276 | |
| 5277 | if (l >= 0x10000) |
| 5278 | return NULL; |
| 5279 | if (!l--) |
| 5280 | /* header */ |
| 5281 | return (void*)1; |
| 5282 | |
| 5283 | spin_lock(&all_mddevs_lock); |
| 5284 | list_for_each(tmp,&all_mddevs) |
| 5285 | if (!l--) { |
| 5286 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5287 | mddev_get(mddev); |
| 5288 | spin_unlock(&all_mddevs_lock); |
| 5289 | return mddev; |
| 5290 | } |
| 5291 | spin_unlock(&all_mddevs_lock); |
| 5292 | if (!l--) |
| 5293 | return (void*)2;/* tail */ |
| 5294 | return NULL; |
| 5295 | } |
| 5296 | |
| 5297 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5298 | { |
| 5299 | struct list_head *tmp; |
| 5300 | mddev_t *next_mddev, *mddev = v; |
| 5301 | |
| 5302 | ++*pos; |
| 5303 | if (v == (void*)2) |
| 5304 | return NULL; |
| 5305 | |
| 5306 | spin_lock(&all_mddevs_lock); |
| 5307 | if (v == (void*)1) |
| 5308 | tmp = all_mddevs.next; |
| 5309 | else |
| 5310 | tmp = mddev->all_mddevs.next; |
| 5311 | if (tmp != &all_mddevs) |
| 5312 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5313 | else { |
| 5314 | next_mddev = (void*)2; |
| 5315 | *pos = 0x10000; |
| 5316 | } |
| 5317 | spin_unlock(&all_mddevs_lock); |
| 5318 | |
| 5319 | if (v != (void*)1) |
| 5320 | mddev_put(mddev); |
| 5321 | return next_mddev; |
| 5322 | |
| 5323 | } |
| 5324 | |
| 5325 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5326 | { |
| 5327 | mddev_t *mddev = v; |
| 5328 | |
| 5329 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5330 | mddev_put(mddev); |
| 5331 | } |
| 5332 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5333 | struct mdstat_info { |
| 5334 | int event; |
| 5335 | }; |
| 5336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5338 | { |
| 5339 | mddev_t *mddev = v; |
| 5340 | sector_t size; |
| 5341 | struct list_head *tmp2; |
| 5342 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5343 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5344 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | |
| 5346 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5347 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | seq_printf(seq, "Personalities : "); |
| 5349 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5350 | list_for_each_entry(pers, &pers_list, list) |
| 5351 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | |
| 5353 | spin_unlock(&pers_lock); |
| 5354 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5355 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | return 0; |
| 5357 | } |
| 5358 | if (v == (void*)2) { |
| 5359 | status_unused(seq); |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5363 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5367 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5368 | mddev->pers ? "" : "in"); |
| 5369 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5370 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5372 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5373 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | seq_printf(seq, " %s", mddev->pers->name); |
| 5375 | } |
| 5376 | |
| 5377 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5378 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | char b[BDEVNAME_SIZE]; |
| 5380 | seq_printf(seq, " %s[%d]", |
| 5381 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5382 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5383 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5384 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | seq_printf(seq, "(F)"); |
| 5386 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5387 | } else if (rdev->raid_disk < 0) |
| 5388 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | size += rdev->size; |
| 5390 | } |
| 5391 | |
| 5392 | if (!list_empty(&mddev->disks)) { |
| 5393 | if (mddev->pers) |
| 5394 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame^] | 5395 | (unsigned long long) |
| 5396 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | else |
| 5398 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame^] | 5399 | (unsigned long long)size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5401 | if (mddev->persistent) { |
| 5402 | if (mddev->major_version != 0 || |
| 5403 | mddev->minor_version != 90) { |
| 5404 | seq_printf(seq," super %d.%d", |
| 5405 | mddev->major_version, |
| 5406 | mddev->minor_version); |
| 5407 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5408 | } else if (mddev->external) |
| 5409 | seq_printf(seq, " super external:%s", |
| 5410 | mddev->metadata_type); |
| 5411 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5412 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | |
| 5414 | if (mddev->pers) { |
| 5415 | mddev->pers->status (seq, mddev); |
| 5416 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5417 | if (mddev->pers->sync_request) { |
| 5418 | if (mddev->curr_resync > 2) { |
| 5419 | status_resync (seq, mddev); |
| 5420 | seq_printf(seq, "\n "); |
| 5421 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5422 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5423 | else if (mddev->recovery_cp < MaxSector) |
| 5424 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5425 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5426 | } else |
| 5427 | seq_printf(seq, "\n "); |
| 5428 | |
| 5429 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5430 | unsigned long chunk_kb; |
| 5431 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5432 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5433 | chunk_kb = bitmap->chunksize >> 10; |
| 5434 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5435 | "%lu%s chunk", |
| 5436 | bitmap->pages - bitmap->missing_pages, |
| 5437 | bitmap->pages, |
| 5438 | (bitmap->pages - bitmap->missing_pages) |
| 5439 | << (PAGE_SHIFT - 10), |
| 5440 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5441 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5442 | if (bitmap->file) { |
| 5443 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5444 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5445 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5446 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5447 | seq_printf(seq, "\n"); |
| 5448 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | seq_printf(seq, "\n"); |
| 5452 | } |
| 5453 | mddev_unlock(mddev); |
| 5454 | |
| 5455 | return 0; |
| 5456 | } |
| 5457 | |
| 5458 | static struct seq_operations md_seq_ops = { |
| 5459 | .start = md_seq_start, |
| 5460 | .next = md_seq_next, |
| 5461 | .stop = md_seq_stop, |
| 5462 | .show = md_seq_show, |
| 5463 | }; |
| 5464 | |
| 5465 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5466 | { |
| 5467 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5468 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5469 | if (mi == NULL) |
| 5470 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | |
| 5472 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5473 | if (error) |
| 5474 | kfree(mi); |
| 5475 | else { |
| 5476 | struct seq_file *p = file->private_data; |
| 5477 | p->private = mi; |
| 5478 | mi->event = atomic_read(&md_event_count); |
| 5479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | return error; |
| 5481 | } |
| 5482 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5483 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5484 | { |
| 5485 | struct seq_file *m = filp->private_data; |
| 5486 | struct mdstat_info *mi = m->private; |
| 5487 | int mask; |
| 5488 | |
| 5489 | poll_wait(filp, &md_event_waiters, wait); |
| 5490 | |
| 5491 | /* always allow read */ |
| 5492 | mask = POLLIN | POLLRDNORM; |
| 5493 | |
| 5494 | if (mi->event != atomic_read(&md_event_count)) |
| 5495 | mask |= POLLERR | POLLPRI; |
| 5496 | return mask; |
| 5497 | } |
| 5498 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5499 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5500 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | .open = md_seq_open, |
| 5502 | .read = seq_read, |
| 5503 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5504 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5505 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | }; |
| 5507 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5508 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5511 | list_add_tail(&p->list, &pers_list); |
| 5512 | 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] | 5513 | spin_unlock(&pers_lock); |
| 5514 | return 0; |
| 5515 | } |
| 5516 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5517 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5519 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5521 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | spin_unlock(&pers_lock); |
| 5523 | return 0; |
| 5524 | } |
| 5525 | |
| 5526 | static int is_mddev_idle(mddev_t *mddev) |
| 5527 | { |
| 5528 | mdk_rdev_t * rdev; |
| 5529 | struct list_head *tmp; |
| 5530 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5531 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | |
| 5533 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5534 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5536 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5537 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5539 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5540 | * as sync_io is counted when a request starts, and |
| 5541 | * disk_stats is counted when it completes. |
| 5542 | * So resync activity will cause curr_events to be smaller than |
| 5543 | * when there was no such activity. |
| 5544 | * non-sync IO will cause disk_stat to increase without |
| 5545 | * increasing sync_io so curr_events will (eventually) |
| 5546 | * be larger than it was before. Once it becomes |
| 5547 | * substantially larger, the test below will cause |
| 5548 | * the array to appear non-idle, and resync will slow |
| 5549 | * down. |
| 5550 | * If there is a lot of outstanding resync activity when |
| 5551 | * we set last_event to curr_events, then all that activity |
| 5552 | * completing might cause the array to appear non-idle |
| 5553 | * and resync will be slowed down even though there might |
| 5554 | * not have been non-resync activity. This will only |
| 5555 | * happen once though. 'last_events' will soon reflect |
| 5556 | * the state where there is little or no outstanding |
| 5557 | * resync requests, and further resync activity will |
| 5558 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5559 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5561 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | rdev->last_events = curr_events; |
| 5563 | idle = 0; |
| 5564 | } |
| 5565 | } |
| 5566 | return idle; |
| 5567 | } |
| 5568 | |
| 5569 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5570 | { |
| 5571 | /* another "blocks" (512byte) blocks have been synced */ |
| 5572 | atomic_sub(blocks, &mddev->recovery_active); |
| 5573 | wake_up(&mddev->recovery_wait); |
| 5574 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5575 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | md_wakeup_thread(mddev->thread); |
| 5577 | // stop recovery, signal do_sync .... |
| 5578 | } |
| 5579 | } |
| 5580 | |
| 5581 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5582 | /* md_write_start(mddev, bi) |
| 5583 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5584 | * in superblock) before writing, schedule a superblock update |
| 5585 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5586 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5587 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5589 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5590 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5591 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5592 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5593 | BUG_ON(mddev->ro == 1); |
| 5594 | if (mddev->ro == 2) { |
| 5595 | /* need to switch to read/write */ |
| 5596 | mddev->ro = 0; |
| 5597 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5598 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5599 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5600 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5601 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5602 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5603 | if (mddev->safemode == 1) |
| 5604 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5605 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5606 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5607 | if (mddev->in_sync) { |
| 5608 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5609 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5610 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5611 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5612 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5613 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5614 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5615 | if (did_change) |
| 5616 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5617 | wait_event(mddev->sb_wait, |
| 5618 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5619 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | void md_write_end(mddev_t *mddev) |
| 5623 | { |
| 5624 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5625 | if (mddev->safemode == 2) |
| 5626 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5627 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5629 | } |
| 5630 | } |
| 5631 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5632 | /* md_allow_write(mddev) |
| 5633 | * Calling this ensures that the array is marked 'active' so that writes |
| 5634 | * may proceed without blocking. It is important to call this before |
| 5635 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5636 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5637 | * |
| 5638 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5639 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5640 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5641 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5642 | { |
| 5643 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5644 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5645 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5646 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5647 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5648 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5649 | |
| 5650 | spin_lock_irq(&mddev->write_lock); |
| 5651 | if (mddev->in_sync) { |
| 5652 | mddev->in_sync = 0; |
| 5653 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5654 | if (mddev->safemode_delay && |
| 5655 | mddev->safemode == 0) |
| 5656 | mddev->safemode = 1; |
| 5657 | spin_unlock_irq(&mddev->write_lock); |
| 5658 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5659 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5660 | } else |
| 5661 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5662 | |
| 5663 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5664 | return -EAGAIN; |
| 5665 | else |
| 5666 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5667 | } |
| 5668 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | #define SYNC_MARKS 10 |
| 5671 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5672 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | { |
| 5674 | mddev_t *mddev2; |
| 5675 | unsigned int currspeed = 0, |
| 5676 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5677 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | unsigned long mark[SYNC_MARKS]; |
| 5679 | sector_t mark_cnt[SYNC_MARKS]; |
| 5680 | int last_mark,m; |
| 5681 | struct list_head *tmp; |
| 5682 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5683 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5684 | struct list_head *rtmp; |
| 5685 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5686 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | |
| 5688 | /* just incase thread restarts... */ |
| 5689 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5690 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5691 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5692 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5694 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5695 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5696 | desc = "data-check"; |
| 5697 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5698 | desc = "requested-resync"; |
| 5699 | else |
| 5700 | desc = "resync"; |
| 5701 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5702 | desc = "reshape"; |
| 5703 | else |
| 5704 | desc = "recovery"; |
| 5705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | /* we overload curr_resync somewhat here. |
| 5707 | * 0 == not engaged in resync at all |
| 5708 | * 2 == checking that there is no conflict with another sync |
| 5709 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5710 | * commense |
| 5711 | * other == active in resync - this many blocks |
| 5712 | * |
| 5713 | * Before starting a resync we must have set curr_resync to |
| 5714 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5715 | * less than ours. When we find one that is the same or higher |
| 5716 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5717 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5718 | * This will mean we have to start checking from the beginning again. |
| 5719 | * |
| 5720 | */ |
| 5721 | |
| 5722 | do { |
| 5723 | mddev->curr_resync = 2; |
| 5724 | |
| 5725 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5726 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5727 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | goto skip; |
| 5729 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5730 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | if (mddev2 == mddev) |
| 5732 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5733 | if (!mddev->parallel_resync |
| 5734 | && mddev2->curr_resync |
| 5735 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | DEFINE_WAIT(wq); |
| 5737 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5738 | /* arbitrarily yield */ |
| 5739 | mddev->curr_resync = 1; |
| 5740 | wake_up(&resync_wait); |
| 5741 | } |
| 5742 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5743 | /* no need to wait here, we can wait the next |
| 5744 | * time 'round when curr_resync == 2 |
| 5745 | */ |
| 5746 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5747 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5748 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5749 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5750 | printk(KERN_INFO "md: delaying %s of %s" |
| 5751 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5753 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | mddev_put(mddev2); |
| 5755 | schedule(); |
| 5756 | finish_wait(&resync_wait, &wq); |
| 5757 | goto try_again; |
| 5758 | } |
| 5759 | finish_wait(&resync_wait, &wq); |
| 5760 | } |
| 5761 | } |
| 5762 | } while (mddev->curr_resync < 2); |
| 5763 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5764 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5765 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5767 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | */ |
| 5769 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5770 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5771 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5772 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5773 | j = mddev->resync_min; |
| 5774 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5775 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5776 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5777 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5778 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5779 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | /* recovery follows the physical size of devices */ |
| 5781 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5782 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5783 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5784 | if (rdev->raid_disk >= 0 && |
| 5785 | !test_bit(Faulty, &rdev->flags) && |
| 5786 | !test_bit(In_sync, &rdev->flags) && |
| 5787 | rdev->recovery_offset < j) |
| 5788 | j = rdev->recovery_offset; |
| 5789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5791 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5792 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5793 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5794 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5795 | "(but not more than %d KB/sec) for %s.\n", |
| 5796 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
| 5798 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5799 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5800 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5802 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5803 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | } |
| 5805 | last_mark = 0; |
| 5806 | mddev->resync_mark = mark[last_mark]; |
| 5807 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5808 | |
| 5809 | /* |
| 5810 | * Tune reconstruction: |
| 5811 | */ |
| 5812 | window = 32*(PAGE_SIZE/512); |
| 5813 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5814 | window/2,(unsigned long long) max_sectors/2); |
| 5815 | |
| 5816 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | last_check = 0; |
| 5818 | |
| 5819 | if (j>2) { |
| 5820 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5821 | "md: resuming %s of %s from checkpoint.\n", |
| 5822 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | mddev->curr_resync = j; |
| 5824 | } |
| 5825 | |
| 5826 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5827 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5829 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5830 | if (j >= mddev->resync_max) { |
| 5831 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5832 | wait_event(mddev->recovery_wait, |
| 5833 | mddev->resync_max > j |
| 5834 | || kthread_should_stop()); |
| 5835 | } |
| 5836 | if (kthread_should_stop()) |
| 5837 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5838 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5839 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5840 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5841 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | goto out; |
| 5843 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5844 | |
| 5845 | if (!skipped) { /* actual IO requested */ |
| 5846 | io_sectors += sectors; |
| 5847 | atomic_add(sectors, &mddev->recovery_active); |
| 5848 | } |
| 5849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | j += sectors; |
| 5851 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5852 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5853 | if (last_check == 0) |
| 5854 | /* this is the earliers that rebuilt will be |
| 5855 | * visible in /proc/mdstat |
| 5856 | */ |
| 5857 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5858 | |
| 5859 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | continue; |
| 5861 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5862 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5864 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | break; |
| 5866 | |
| 5867 | repeat: |
| 5868 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5869 | /* step marks */ |
| 5870 | int next = (last_mark+1) % SYNC_MARKS; |
| 5871 | |
| 5872 | mddev->resync_mark = mark[next]; |
| 5873 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5874 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5875 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | last_mark = next; |
| 5877 | } |
| 5878 | |
| 5879 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5880 | if (kthread_should_stop()) |
| 5881 | goto interrupted; |
| 5882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | |
| 5884 | /* |
| 5885 | * this loop exits only if either when we are slower than |
| 5886 | * the 'hard' speed limit, or the system was IO-idle for |
| 5887 | * a jiffy. |
| 5888 | * the system might be non-idle CPU-wise, but we only care |
| 5889 | * about not overloading the IO subsystem. (things like an |
| 5890 | * e2fsck being done on the RAID array should execute fast) |
| 5891 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5892 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | cond_resched(); |
| 5894 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5895 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5896 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5898 | if (currspeed > speed_min(mddev)) { |
| 5899 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5901 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | goto repeat; |
| 5903 | } |
| 5904 | } |
| 5905 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5906 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | /* |
| 5908 | * this also signals 'finished resyncing' to md_stop |
| 5909 | */ |
| 5910 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5911 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | |
| 5913 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5914 | |
| 5915 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5916 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5918 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5919 | mddev->curr_resync > 2) { |
| 5920 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5921 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5922 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5923 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5924 | "md: checkpointing %s of %s.\n", |
| 5925 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5926 | mddev->recovery_cp = mddev->curr_resync; |
| 5927 | } |
| 5928 | } else |
| 5929 | mddev->recovery_cp = MaxSector; |
| 5930 | } else { |
| 5931 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5932 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5933 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5934 | if (rdev->raid_disk >= 0 && |
| 5935 | !test_bit(Faulty, &rdev->flags) && |
| 5936 | !test_bit(In_sync, &rdev->flags) && |
| 5937 | rdev->recovery_offset < mddev->curr_resync) |
| 5938 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5941 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | skip: |
| 5944 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5945 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5946 | mddev->resync_max = MaxSector; |
| 5947 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | wake_up(&resync_wait); |
| 5949 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5950 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5951 | return; |
| 5952 | |
| 5953 | interrupted: |
| 5954 | /* |
| 5955 | * got a signal, exit. |
| 5956 | */ |
| 5957 | printk(KERN_INFO |
| 5958 | "md: md_do_sync() got signal ... exiting\n"); |
| 5959 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5960 | goto out; |
| 5961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5963 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | |
| 5965 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5966 | static int remove_and_add_spares(mddev_t *mddev) |
| 5967 | { |
| 5968 | mdk_rdev_t *rdev; |
| 5969 | struct list_head *rtmp; |
| 5970 | int spares = 0; |
| 5971 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5972 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5973 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5974 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5975 | (test_bit(Faulty, &rdev->flags) || |
| 5976 | ! test_bit(In_sync, &rdev->flags)) && |
| 5977 | atomic_read(&rdev->nr_pending)==0) { |
| 5978 | if (mddev->pers->hot_remove_disk( |
| 5979 | mddev, rdev->raid_disk)==0) { |
| 5980 | char nm[20]; |
| 5981 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5982 | sysfs_remove_link(&mddev->kobj, nm); |
| 5983 | rdev->raid_disk = -1; |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5988 | rdev_for_each(rdev, rtmp, mddev) { |
| 5989 | if (rdev->raid_disk >= 0 && |
| 5990 | !test_bit(In_sync, &rdev->flags)) |
| 5991 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5992 | if (rdev->raid_disk < 0 |
| 5993 | && !test_bit(Faulty, &rdev->flags)) { |
| 5994 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5995 | if (mddev->pers-> |
| 5996 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5997 | char nm[20]; |
| 5998 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5999 | if (sysfs_create_link(&mddev->kobj, |
| 6000 | &rdev->kobj, nm)) |
| 6001 | printk(KERN_WARNING |
| 6002 | "md: cannot register " |
| 6003 | "%s for %s\n", |
| 6004 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6005 | spares++; |
| 6006 | md_new_event(mddev); |
| 6007 | } else |
| 6008 | break; |
| 6009 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6010 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6011 | } |
| 6012 | return spares; |
| 6013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | /* |
| 6015 | * This routine is regularly called by all per-raid-array threads to |
| 6016 | * deal with generic issues like resync and super-block update. |
| 6017 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6018 | * need this as they never do any recovery or update the superblock. |
| 6019 | * |
| 6020 | * It does not do any resync itself, but rather "forks" off other threads |
| 6021 | * to do that as needed. |
| 6022 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6023 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6024 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | * and wakeups up this thread which will reap the thread and finish up. |
| 6026 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6027 | * |
| 6028 | * The overall approach is: |
| 6029 | * 1/ if the superblock needs updating, update it. |
| 6030 | * 2/ If a recovery thread is running, don't do anything else. |
| 6031 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6032 | * 4/ If there are any faulty devices, remove them. |
| 6033 | * 5/ If array is degraded, try to add spares devices |
| 6034 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6035 | */ |
| 6036 | void md_check_recovery(mddev_t *mddev) |
| 6037 | { |
| 6038 | mdk_rdev_t *rdev; |
| 6039 | struct list_head *rtmp; |
| 6040 | |
| 6041 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6042 | if (mddev->bitmap) |
| 6043 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | |
| 6045 | if (mddev->ro) |
| 6046 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6047 | |
| 6048 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6049 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6050 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6051 | mdname(mddev)); |
| 6052 | mddev->safemode = 2; |
| 6053 | } |
| 6054 | flush_signals(current); |
| 6055 | } |
| 6056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6058 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6060 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6061 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6062 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6063 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | )) |
| 6065 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6066 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6067 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6068 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6069 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6070 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6071 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6072 | spin_lock_irq(&mddev->write_lock); |
| 6073 | if (mddev->safemode && |
| 6074 | !atomic_read(&mddev->writes_pending) && |
| 6075 | !mddev->in_sync && |
| 6076 | mddev->recovery_cp == MaxSector) { |
| 6077 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6078 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6079 | if (mddev->persistent) |
| 6080 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6081 | } |
| 6082 | if (mddev->safemode == 1) |
| 6083 | mddev->safemode = 0; |
| 6084 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6085 | if (did_change) |
| 6086 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6087 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6088 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6089 | if (mddev->flags) |
| 6090 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6091 | |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6092 | rdev_for_each(rdev, rtmp, mddev) |
| 6093 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
| 6094 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6095 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6098 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6099 | /* resync/recovery still happening */ |
| 6100 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6101 | goto unlock; |
| 6102 | } |
| 6103 | if (mddev->sync_thread) { |
| 6104 | /* resync has finished, collect result */ |
| 6105 | md_unregister_thread(mddev->sync_thread); |
| 6106 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6107 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | /* success...*/ |
| 6109 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6110 | if (mddev->pers->spare_active(mddev)) |
| 6111 | sysfs_notify(&mddev->kobj, NULL, |
| 6112 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6114 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6115 | |
| 6116 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6117 | * information must be scrapped |
| 6118 | */ |
| 6119 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6120 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6121 | rdev->saved_raid_disk = -1; |
| 6122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | mddev->recovery = 0; |
| 6124 | /* flag recovery needed just to double check */ |
| 6125 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6126 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6127 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | goto unlock; |
| 6129 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6130 | /* Set RUNNING before clearing NEEDED to avoid |
| 6131 | * any transients in the value of "sync_action". |
| 6132 | */ |
| 6133 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6134 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6135 | /* Clear some bits that don't mean anything, but |
| 6136 | * might be left set |
| 6137 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6138 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6139 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6141 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6142 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | /* no recovery is running. |
| 6144 | * remove any failed drives, then |
| 6145 | * add spares if possible. |
| 6146 | * Spare are also removed and re-added, to allow |
| 6147 | * the personality to fail the re-add. |
| 6148 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6150 | if (mddev->reshape_position != MaxSector) { |
| 6151 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6152 | /* Cannot proceed */ |
| 6153 | goto unlock; |
| 6154 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6155 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6156 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6157 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6158 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6159 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6160 | } else if (mddev->recovery_cp < MaxSector) { |
| 6161 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6162 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6163 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6164 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6168 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6169 | /* We are adding a device or devices to an array |
| 6170 | * which has the bitmap stored on all devices. |
| 6171 | * So make sure all bitmap pages get written |
| 6172 | */ |
| 6173 | bitmap_write_all(mddev->bitmap); |
| 6174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6176 | mddev, |
| 6177 | "%s_resync"); |
| 6178 | if (!mddev->sync_thread) { |
| 6179 | printk(KERN_ERR "%s: could not start resync" |
| 6180 | " thread...\n", |
| 6181 | mdname(mddev)); |
| 6182 | /* leave the spares where they are, it shouldn't hurt */ |
| 6183 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6184 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6186 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6187 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | } |
| 6189 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6190 | if (!mddev->sync_thread) { |
| 6191 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6192 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6193 | &mddev->recovery)) |
| 6194 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
| 6195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | mddev_unlock(mddev); |
| 6197 | } |
| 6198 | } |
| 6199 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6200 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6201 | { |
| 6202 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6203 | wait_event_timeout(rdev->blocked_wait, |
| 6204 | !test_bit(Blocked, &rdev->flags), |
| 6205 | msecs_to_jiffies(5000)); |
| 6206 | rdev_dec_pending(rdev, mddev); |
| 6207 | } |
| 6208 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6209 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6210 | static int md_notify_reboot(struct notifier_block *this, |
| 6211 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | { |
| 6213 | struct list_head *tmp; |
| 6214 | mddev_t *mddev; |
| 6215 | |
| 6216 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6217 | |
| 6218 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6219 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6220 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6221 | if (mddev_trylock(mddev)) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6222 | do_md_stop (mddev, 1, 0); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6223 | mddev_unlock(mddev); |
| 6224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | /* |
| 6226 | * certain more exotic SCSI devices are known to be |
| 6227 | * volatile wrt too early system reboots. While the |
| 6228 | * right place to handle this issue is the given |
| 6229 | * driver, we do want to have a safe RAID driver ... |
| 6230 | */ |
| 6231 | mdelay(1000*1); |
| 6232 | } |
| 6233 | return NOTIFY_DONE; |
| 6234 | } |
| 6235 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6236 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | .notifier_call = md_notify_reboot, |
| 6238 | .next = NULL, |
| 6239 | .priority = INT_MAX, /* before any real devices */ |
| 6240 | }; |
| 6241 | |
| 6242 | static void md_geninit(void) |
| 6243 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6245 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6246 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | } |
| 6248 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6249 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | if (register_blkdev(MAJOR_NR, "md")) |
| 6252 | return -1; |
| 6253 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6254 | unregister_blkdev(MAJOR_NR, "md"); |
| 6255 | return -1; |
| 6256 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6257 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6258 | md_probe, NULL, NULL); |
| 6259 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | md_probe, NULL, NULL); |
| 6261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6263 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
| 6265 | md_geninit(); |
| 6266 | return (0); |
| 6267 | } |
| 6268 | |
| 6269 | |
| 6270 | #ifndef MODULE |
| 6271 | |
| 6272 | /* |
| 6273 | * Searches all registered partitions for autorun RAID arrays |
| 6274 | * at boot time. |
| 6275 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6276 | |
| 6277 | static LIST_HEAD(all_detected_devices); |
| 6278 | struct detected_devices_node { |
| 6279 | struct list_head list; |
| 6280 | dev_t dev; |
| 6281 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | |
| 6283 | void md_autodetect_dev(dev_t dev) |
| 6284 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6285 | struct detected_devices_node *node_detected_dev; |
| 6286 | |
| 6287 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6288 | if (node_detected_dev) { |
| 6289 | node_detected_dev->dev = dev; |
| 6290 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6291 | } else { |
| 6292 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6293 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | |
| 6298 | static void autostart_arrays(int part) |
| 6299 | { |
| 6300 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6301 | struct detected_devices_node *node_detected_dev; |
| 6302 | dev_t dev; |
| 6303 | int i_scanned, i_passed; |
| 6304 | |
| 6305 | i_scanned = 0; |
| 6306 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | |
| 6308 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6309 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6310 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6311 | i_scanned++; |
| 6312 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6313 | struct detected_devices_node, list); |
| 6314 | list_del(&node_detected_dev->list); |
| 6315 | dev = node_detected_dev->dev; |
| 6316 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6317 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | if (IS_ERR(rdev)) |
| 6319 | continue; |
| 6320 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6321 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | MD_BUG(); |
| 6323 | continue; |
| 6324 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6325 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6327 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6329 | |
| 6330 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6331 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | |
| 6333 | autorun_devices(part); |
| 6334 | } |
| 6335 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6336 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | |
| 6338 | static __exit void md_exit(void) |
| 6339 | { |
| 6340 | mddev_t *mddev; |
| 6341 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6342 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6343 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6344 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | |
| 6346 | unregister_blkdev(MAJOR_NR,"md"); |
| 6347 | unregister_blkdev(mdp_major, "mdp"); |
| 6348 | unregister_reboot_notifier(&md_notifier); |
| 6349 | unregister_sysctl_table(raid_table_header); |
| 6350 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6351 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | struct gendisk *disk = mddev->gendisk; |
| 6353 | if (!disk) |
| 6354 | continue; |
| 6355 | export_array(mddev); |
| 6356 | del_gendisk(disk); |
| 6357 | put_disk(disk); |
| 6358 | mddev->gendisk = NULL; |
| 6359 | mddev_put(mddev); |
| 6360 | } |
| 6361 | } |
| 6362 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6363 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | module_exit(md_exit) |
| 6365 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6366 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6367 | { |
| 6368 | return sprintf(buffer, "%d", start_readonly); |
| 6369 | } |
| 6370 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6371 | { |
| 6372 | char *e; |
| 6373 | int num = simple_strtoul(val, &e, 10); |
| 6374 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6375 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6376 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6377 | } |
| 6378 | return -EINVAL; |
| 6379 | } |
| 6380 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6381 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6382 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6383 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | EXPORT_SYMBOL(register_md_personality); |
| 6386 | EXPORT_SYMBOL(unregister_md_personality); |
| 6387 | EXPORT_SYMBOL(md_error); |
| 6388 | EXPORT_SYMBOL(md_done_sync); |
| 6389 | EXPORT_SYMBOL(md_write_start); |
| 6390 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | EXPORT_SYMBOL(md_register_thread); |
| 6392 | EXPORT_SYMBOL(md_unregister_thread); |
| 6393 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | EXPORT_SYMBOL(md_check_recovery); |
| 6395 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6396 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6397 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |