Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 37 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/linkage.h> |
| 39 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 43 | #include <linux/poll.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 44 | #include <linux/mutex.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 45 | #include <linux/ctype.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 46 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <linux/init.h> |
| 49 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 50 | #include <linux/file.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_KMOD |
| 53 | #include <linux/kmod.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <asm/unaligned.h> |
| 57 | |
| 58 | #define MAJOR_NR MD_MAJOR |
| 59 | #define MD_DRIVER |
| 60 | |
| 61 | /* 63 partitions with the alternate major number (mdp) */ |
| 62 | #define MdpMinorShift 6 |
| 63 | |
| 64 | #define DEBUG 0 |
| 65 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 66 | |
| 67 | |
| 68 | #ifndef MODULE |
| 69 | static void autostart_arrays (int part); |
| 70 | #endif |
| 71 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 72 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(pers_lock); |
| 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | static void md_print_devices(void); |
| 76 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 77 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 78 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 79 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
| 112 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 113 | .procname = "speed_limit_min", |
| 114 | .data = &sysctl_speed_limit_min, |
| 115 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 116 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .proc_handler = &proc_dointvec, |
| 118 | }, |
| 119 | { |
| 120 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .proc_handler = &proc_dointvec, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
| 132 | .ctl_name = DEV_RAID, |
| 133 | .procname = "raid", |
| 134 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 135 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .child = raid_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static ctl_table raid_root_table[] = { |
| 142 | { |
| 143 | .ctl_name = CTL_DEV, |
| 144 | .procname = "dev", |
| 145 | .maxlen = 0, |
| 146 | .mode = 0555, |
| 147 | .child = raid_dir_table, |
| 148 | }, |
| 149 | { .ctl_name = 0 } |
| 150 | }; |
| 151 | |
| 152 | static struct block_device_operations md_fops; |
| 153 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 154 | static int start_readonly; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | * We have a system wide 'event count' that is incremented |
| 158 | * on any 'interesting' event, and readers of /proc/mdstat |
| 159 | * can use 'poll' or 'select' to find out when the event |
| 160 | * count increases. |
| 161 | * |
| 162 | * Events are: |
| 163 | * start array, stop array, error, add device, remove device, |
| 164 | * start build, activate spare |
| 165 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 166 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 168 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 172 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 173 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 174 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 175 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 176 | /* Alternate version that can be called from interrupts |
| 177 | * when calling sysfs_notify isn't needed. |
| 178 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 179 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 180 | { |
| 181 | atomic_inc(&md_event_count); |
| 182 | wake_up(&md_event_waiters); |
| 183 | } |
| 184 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 185 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * Enables to iterate over all existing md arrays |
| 187 | * all_mddevs_lock protects this list. |
| 188 | */ |
| 189 | static LIST_HEAD(all_mddevs); |
| 190 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 191 | |
| 192 | |
| 193 | /* |
| 194 | * iterates through all used mddevs in the system. |
| 195 | * We take care to grab the all_mddevs_lock whenever navigating |
| 196 | * the list, and to always hold a refcount when unlocked. |
| 197 | * Any code which breaks out of this loop while own |
| 198 | * a reference to the current mddev and must mddev_put it. |
| 199 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 200 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | \ |
| 202 | for (({ spin_lock(&all_mddevs_lock); \ |
| 203 | tmp = all_mddevs.next; \ |
| 204 | mddev = NULL;}); \ |
| 205 | ({ if (tmp != &all_mddevs) \ |
| 206 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 207 | spin_unlock(&all_mddevs_lock); \ |
| 208 | if (mddev) mddev_put(mddev); \ |
| 209 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 210 | tmp != &all_mddevs;}); \ |
| 211 | ({ spin_lock(&all_mddevs_lock); \ |
| 212 | tmp = tmp->next;}) \ |
| 213 | ) |
| 214 | |
| 215 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 216 | static int md_fail_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 218 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 223 | { |
| 224 | atomic_inc(&mddev->active); |
| 225 | return mddev; |
| 226 | } |
| 227 | |
| 228 | static void mddev_put(mddev_t *mddev) |
| 229 | { |
| 230 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 231 | return; |
| 232 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 233 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 234 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 235 | blk_cleanup_queue(mddev->queue); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 236 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 237 | } else |
| 238 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static mddev_t * mddev_find(dev_t unit) |
| 242 | { |
| 243 | mddev_t *mddev, *new = NULL; |
| 244 | |
| 245 | retry: |
| 246 | spin_lock(&all_mddevs_lock); |
| 247 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 248 | if (mddev->unit == unit) { |
| 249 | mddev_get(mddev); |
| 250 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 251 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return mddev; |
| 253 | } |
| 254 | |
| 255 | if (new) { |
| 256 | list_add(&new->all_mddevs, &all_mddevs); |
| 257 | spin_unlock(&all_mddevs_lock); |
| 258 | return new; |
| 259 | } |
| 260 | spin_unlock(&all_mddevs_lock); |
| 261 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 262 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!new) |
| 264 | return NULL; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | new->unit = unit; |
| 267 | if (MAJOR(unit) == MD_MAJOR) |
| 268 | new->md_minor = MINOR(unit); |
| 269 | else |
| 270 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 271 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 272 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | INIT_LIST_HEAD(&new->disks); |
| 274 | INIT_LIST_HEAD(&new->all_mddevs); |
| 275 | init_timer(&new->safemode_timer); |
| 276 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 277 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 278 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 279 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 280 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 281 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 282 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 283 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 286 | if (!new->queue) { |
| 287 | kfree(new); |
| 288 | return NULL; |
| 289 | } |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 290 | /* Can be unlocked because the queue is new: no concurrency */ |
| 291 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | blk_queue_make_request(new->queue, md_fail_request); |
| 294 | |
| 295 | goto retry; |
| 296 | } |
| 297 | |
| 298 | static inline int mddev_lock(mddev_t * mddev) |
| 299 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 300 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static inline int mddev_trylock(mddev_t * mddev) |
| 304 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 305 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static inline void mddev_unlock(mddev_t * mddev) |
| 309 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 310 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 312 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 315 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | mdk_rdev_t * rdev; |
| 318 | struct list_head *tmp; |
| 319 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 320 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | if (rdev->desc_nr == nr) |
| 322 | return rdev; |
| 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 328 | { |
| 329 | struct list_head *tmp; |
| 330 | mdk_rdev_t *rdev; |
| 331 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 332 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (rdev->bdev->bd_dev == dev) |
| 334 | return rdev; |
| 335 | } |
| 336 | return NULL; |
| 337 | } |
| 338 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 339 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 340 | { |
| 341 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 342 | list_for_each_entry(pers, &pers_list, list) { |
| 343 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 344 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 345 | if (strcmp(pers->name, clevel)==0) |
| 346 | return pers; |
| 347 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | |
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 | { |
| 353 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 354 | return MD_NEW_SIZE_BLOCKS(size); |
| 355 | } |
| 356 | |
| 357 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 358 | { |
| 359 | sector_t size; |
| 360 | |
| 361 | size = rdev->sb_offset; |
| 362 | |
| 363 | if (chunk_size) |
| 364 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 365 | return size; |
| 366 | } |
| 367 | |
| 368 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 369 | { |
| 370 | if (rdev->sb_page) |
| 371 | MD_BUG(); |
| 372 | |
| 373 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 374 | if (!rdev->sb_page) { |
| 375 | printk(KERN_ALERT "md: out of memory.\n"); |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 383 | { |
| 384 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 385 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | rdev->sb_loaded = 0; |
| 387 | rdev->sb_page = NULL; |
| 388 | rdev->sb_offset = 0; |
| 389 | rdev->size = 0; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 394 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 395 | { |
| 396 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 397 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 398 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 399 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 400 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 401 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 402 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 403 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 404 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 405 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 406 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 407 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 408 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 409 | } |
| 410 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 411 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 412 | { |
| 413 | struct bio *bio2 = bio->bi_private; |
| 414 | mdk_rdev_t *rdev = bio2->bi_private; |
| 415 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 416 | |
| 417 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 418 | error == -EOPNOTSUPP) { |
| 419 | unsigned long flags; |
| 420 | /* barriers don't appear to be supported :-( */ |
| 421 | set_bit(BarriersNotsupp, &rdev->flags); |
| 422 | mddev->barriers_work = 0; |
| 423 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 424 | bio2->bi_next = mddev->biolist; |
| 425 | mddev->biolist = bio2; |
| 426 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 427 | wake_up(&mddev->sb_wait); |
| 428 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 429 | } else { |
| 430 | bio_put(bio2); |
| 431 | bio->bi_private = rdev; |
| 432 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 433 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 434 | } |
| 435 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 436 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 437 | sector_t sector, int size, struct page *page) |
| 438 | { |
| 439 | /* write first size bytes of page to sector of rdev |
| 440 | * Increment mddev->pending_writes before returning |
| 441 | * and decrement it on completion, waking up sb_wait |
| 442 | * if zero is reached. |
| 443 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 444 | * |
| 445 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 446 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 447 | */ |
| 448 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 449 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 450 | |
| 451 | bio->bi_bdev = rdev->bdev; |
| 452 | bio->bi_sector = sector; |
| 453 | bio_add_page(bio, page, size, 0); |
| 454 | bio->bi_private = rdev; |
| 455 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 456 | bio->bi_rw = rw; |
| 457 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 458 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 459 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 460 | struct bio *rbio; |
| 461 | rw |= (1<<BIO_RW_BARRIER); |
| 462 | rbio = bio_clone(bio, GFP_NOIO); |
| 463 | rbio->bi_private = bio; |
| 464 | rbio->bi_end_io = super_written_barrier; |
| 465 | submit_bio(rw, rbio); |
| 466 | } else |
| 467 | submit_bio(rw, bio); |
| 468 | } |
| 469 | |
| 470 | void md_super_wait(mddev_t *mddev) |
| 471 | { |
| 472 | /* wait for all superblock writes that were scheduled to complete. |
| 473 | * if any had to be retried (due to BARRIER problems), retry them |
| 474 | */ |
| 475 | DEFINE_WAIT(wq); |
| 476 | for(;;) { |
| 477 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 478 | if (atomic_read(&mddev->pending_writes)==0) |
| 479 | break; |
| 480 | while (mddev->biolist) { |
| 481 | struct bio *bio; |
| 482 | spin_lock_irq(&mddev->write_lock); |
| 483 | bio = mddev->biolist; |
| 484 | mddev->biolist = bio->bi_next ; |
| 485 | bio->bi_next = NULL; |
| 486 | spin_unlock_irq(&mddev->write_lock); |
| 487 | submit_bio(bio->bi_rw, bio); |
| 488 | } |
| 489 | schedule(); |
| 490 | } |
| 491 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 494 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 499 | 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] | 500 | struct page *page, int rw) |
| 501 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 502 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | struct completion event; |
| 504 | int ret; |
| 505 | |
| 506 | rw |= (1 << BIO_RW_SYNC); |
| 507 | |
| 508 | bio->bi_bdev = bdev; |
| 509 | bio->bi_sector = sector; |
| 510 | bio_add_page(bio, page, size, 0); |
| 511 | init_completion(&event); |
| 512 | bio->bi_private = &event; |
| 513 | bio->bi_end_io = bi_complete; |
| 514 | submit_bio(rw, bio); |
| 515 | wait_for_completion(&event); |
| 516 | |
| 517 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 518 | bio_put(bio); |
| 519 | return ret; |
| 520 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 521 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 523 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | char b[BDEVNAME_SIZE]; |
| 526 | if (!rdev->sb_page) { |
| 527 | MD_BUG(); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | if (rdev->sb_loaded) |
| 531 | return 0; |
| 532 | |
| 533 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 534 | if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | goto fail; |
| 536 | rdev->sb_loaded = 1; |
| 537 | return 0; |
| 538 | |
| 539 | fail: |
| 540 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 541 | bdevname(rdev->bdev,b)); |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
| 545 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 546 | { |
| 547 | if ( (sb1->set_uuid0 == sb2->set_uuid0) && |
| 548 | (sb1->set_uuid1 == sb2->set_uuid1) && |
| 549 | (sb1->set_uuid2 == sb2->set_uuid2) && |
| 550 | (sb1->set_uuid3 == sb2->set_uuid3)) |
| 551 | |
| 552 | return 1; |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 559 | { |
| 560 | int ret; |
| 561 | mdp_super_t *tmp1, *tmp2; |
| 562 | |
| 563 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 564 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 565 | |
| 566 | if (!tmp1 || !tmp2) { |
| 567 | ret = 0; |
| 568 | printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n"); |
| 569 | goto abort; |
| 570 | } |
| 571 | |
| 572 | *tmp1 = *sb1; |
| 573 | *tmp2 = *sb2; |
| 574 | |
| 575 | /* |
| 576 | * nr_disks is not constant |
| 577 | */ |
| 578 | tmp1->nr_disks = 0; |
| 579 | tmp2->nr_disks = 0; |
| 580 | |
| 581 | if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4)) |
| 582 | ret = 0; |
| 583 | else |
| 584 | ret = 1; |
| 585 | |
| 586 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 587 | kfree(tmp1); |
| 588 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return ret; |
| 590 | } |
| 591 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 592 | |
| 593 | static u32 md_csum_fold(u32 csum) |
| 594 | { |
| 595 | csum = (csum & 0xffff) + (csum >> 16); |
| 596 | return (csum & 0xffff) + (csum >> 16); |
| 597 | } |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 600 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 601 | u64 newcsum = 0; |
| 602 | u32 *sb32 = (u32*)sb; |
| 603 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unsigned int disk_csum, csum; |
| 605 | |
| 606 | disk_csum = sb->sb_csum; |
| 607 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 608 | |
| 609 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 610 | newcsum += sb32[i]; |
| 611 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 612 | |
| 613 | |
| 614 | #ifdef CONFIG_ALPHA |
| 615 | /* This used to use csum_partial, which was wrong for several |
| 616 | * reasons including that different results are returned on |
| 617 | * different architectures. It isn't critical that we get exactly |
| 618 | * the same return value as before (we always csum_fold before |
| 619 | * testing, and that removes any differences). However as we |
| 620 | * know that csum_partial always returned a 16bit value on |
| 621 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 622 | */ |
| 623 | sb->sb_csum = md_csum_fold(disk_csum); |
| 624 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 626 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return csum; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /* |
| 632 | * Handle superblock details. |
| 633 | * We want to be able to handle multiple superblock formats |
| 634 | * so we have a common interface to them all, and an array of |
| 635 | * different handlers. |
| 636 | * We rely on user-space to write the initial superblock, and support |
| 637 | * reading and updating of superblocks. |
| 638 | * Interface methods are: |
| 639 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 640 | * loads and validates a superblock on dev. |
| 641 | * if refdev != NULL, compare superblocks on both devices |
| 642 | * Return: |
| 643 | * 0 - dev has a superblock that is compatible with refdev |
| 644 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 645 | * so dev should be used as the refdev in future |
| 646 | * -EINVAL superblock incompatible or invalid |
| 647 | * -othererror e.g. -EIO |
| 648 | * |
| 649 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 650 | * Verify that dev is acceptable into mddev. |
| 651 | * The first time, mddev->raid_disks will be 0, and data from |
| 652 | * dev should be merged in. Subsequent calls check that dev |
| 653 | * is new enough. Return 0 or -EINVAL |
| 654 | * |
| 655 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 656 | * Update the superblock for rdev with data in mddev |
| 657 | * This does not write to disc. |
| 658 | * |
| 659 | */ |
| 660 | |
| 661 | struct super_type { |
| 662 | char *name; |
| 663 | struct module *owner; |
| 664 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version); |
| 665 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 666 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 667 | }; |
| 668 | |
| 669 | /* |
| 670 | * load_super for 0.90.0 |
| 671 | */ |
| 672 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 673 | { |
| 674 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 675 | mdp_super_t *sb; |
| 676 | int ret; |
| 677 | sector_t sb_offset; |
| 678 | |
| 679 | /* |
| 680 | * Calculate the position of the superblock, |
| 681 | * it's at the end of the disk. |
| 682 | * |
| 683 | * It also happens to be a multiple of 4Kb. |
| 684 | */ |
| 685 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 686 | rdev->sb_offset = sb_offset; |
| 687 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 688 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (ret) return ret; |
| 690 | |
| 691 | ret = -EINVAL; |
| 692 | |
| 693 | bdevname(rdev->bdev, b); |
| 694 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 695 | |
| 696 | if (sb->md_magic != MD_SB_MAGIC) { |
| 697 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 698 | b); |
| 699 | goto abort; |
| 700 | } |
| 701 | |
| 702 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 703 | sb->minor_version < 90 || |
| 704 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 706 | sb->major_version, sb->minor_version, |
| 707 | b); |
| 708 | goto abort; |
| 709 | } |
| 710 | |
| 711 | if (sb->raid_disks <= 0) |
| 712 | goto abort; |
| 713 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 714 | 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] | 715 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 716 | b); |
| 717 | goto abort; |
| 718 | } |
| 719 | |
| 720 | rdev->preferred_minor = sb->md_minor; |
| 721 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 722 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 724 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 725 | if (sb->level != 1 && sb->level != 4 |
| 726 | && sb->level != 5 && sb->level != 6 |
| 727 | && sb->level != 10) { |
| 728 | /* FIXME use a better test */ |
| 729 | printk(KERN_WARNING |
| 730 | "md: bitmaps not supported for this level.\n"); |
| 731 | goto abort; |
| 732 | } |
| 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (sb->level == LEVEL_MULTIPATH) |
| 736 | rdev->desc_nr = -1; |
| 737 | else |
| 738 | rdev->desc_nr = sb->this_disk.number; |
| 739 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 740 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 742 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | __u64 ev1, ev2; |
| 744 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 745 | if (!uuid_equal(refsb, sb)) { |
| 746 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 747 | b, bdevname(refdev->bdev,b2)); |
| 748 | goto abort; |
| 749 | } |
| 750 | if (!sb_equal(refsb, sb)) { |
| 751 | printk(KERN_WARNING "md: %s has same UUID" |
| 752 | " but different superblock to %s\n", |
| 753 | b, bdevname(refdev->bdev, b2)); |
| 754 | goto abort; |
| 755 | } |
| 756 | ev1 = md_event(sb); |
| 757 | ev2 = md_event(refsb); |
| 758 | if (ev1 > ev2) |
| 759 | ret = 1; |
| 760 | else |
| 761 | ret = 0; |
| 762 | } |
| 763 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 764 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 765 | if (rdev->size < sb->size && sb->level > 1) |
| 766 | /* "this cannot possibly happen" ... */ |
| 767 | ret = -EINVAL; |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | abort: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * validate_super for 0.90.0 |
| 775 | */ |
| 776 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 777 | { |
| 778 | mdp_disk_t *desc; |
| 779 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 780 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 782 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 783 | clear_bit(Faulty, &rdev->flags); |
| 784 | clear_bit(In_sync, &rdev->flags); |
| 785 | clear_bit(WriteMostly, &rdev->flags); |
| 786 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (mddev->raid_disks == 0) { |
| 789 | mddev->major_version = 0; |
| 790 | mddev->minor_version = sb->minor_version; |
| 791 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 792 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | mddev->chunk_size = sb->chunk_size; |
| 794 | mddev->ctime = sb->ctime; |
| 795 | mddev->utime = sb->utime; |
| 796 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 797 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | mddev->layout = sb->layout; |
| 799 | mddev->raid_disks = sb->raid_disks; |
| 800 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 801 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 802 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 803 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 805 | if (mddev->minor_version >= 91) { |
| 806 | mddev->reshape_position = sb->reshape_position; |
| 807 | mddev->delta_disks = sb->delta_disks; |
| 808 | mddev->new_level = sb->new_level; |
| 809 | mddev->new_layout = sb->new_layout; |
| 810 | mddev->new_chunk = sb->new_chunk; |
| 811 | } else { |
| 812 | mddev->reshape_position = MaxSector; |
| 813 | mddev->delta_disks = 0; |
| 814 | mddev->new_level = mddev->level; |
| 815 | mddev->new_layout = mddev->layout; |
| 816 | mddev->new_chunk = mddev->chunk_size; |
| 817 | } |
| 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 820 | mddev->recovery_cp = MaxSector; |
| 821 | else { |
| 822 | if (sb->events_hi == sb->cp_events_hi && |
| 823 | sb->events_lo == sb->cp_events_lo) { |
| 824 | mddev->recovery_cp = sb->recovery_cp; |
| 825 | } else |
| 826 | mddev->recovery_cp = 0; |
| 827 | } |
| 828 | |
| 829 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 830 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 831 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 832 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 833 | |
| 834 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 835 | |
| 836 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 837 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 838 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 839 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 840 | } else if (mddev->pers == NULL) { |
| 841 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | ++ev1; |
| 843 | if (ev1 < mddev->events) |
| 844 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 845 | } else if (mddev->bitmap) { |
| 846 | /* if adding to array with a bitmap, then we can accept an |
| 847 | * older device ... but not too old. |
| 848 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 849 | if (ev1 < mddev->bitmap->events_cleared) |
| 850 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 851 | } else { |
| 852 | if (ev1 < mddev->events) |
| 853 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 854 | return 0; |
| 855 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | desc = sb->disks + rdev->desc_nr; |
| 859 | |
| 860 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 861 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 862 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 863 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 864 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | rdev->raid_disk = desc->raid_disk; |
| 866 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 867 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 868 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 869 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 870 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * sync_super for 0.90.0 |
| 876 | */ |
| 877 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 878 | { |
| 879 | mdp_super_t *sb; |
| 880 | struct list_head *tmp; |
| 881 | mdk_rdev_t *rdev2; |
| 882 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | /* make rdev->sb match mddev data.. |
| 886 | * |
| 887 | * 1/ zero out disks |
| 888 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 889 | * 3/ any empty disks < next_spare become removed |
| 890 | * |
| 891 | * disks[0] gets initialised to REMOVED because |
| 892 | * we cannot be sure from other fields if it has |
| 893 | * been initialised or not. |
| 894 | */ |
| 895 | int i; |
| 896 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 897 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 898 | rdev->sb_size = MD_SB_BYTES; |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 901 | |
| 902 | memset(sb, 0, sizeof(*sb)); |
| 903 | |
| 904 | sb->md_magic = MD_SB_MAGIC; |
| 905 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | sb->patch_version = mddev->patch_version; |
| 907 | sb->gvalid_words = 0; /* ignored */ |
| 908 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 909 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 910 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 911 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 912 | |
| 913 | sb->ctime = mddev->ctime; |
| 914 | sb->level = mddev->level; |
| 915 | sb->size = mddev->size; |
| 916 | sb->raid_disks = mddev->raid_disks; |
| 917 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 918 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | sb->utime = mddev->utime; |
| 920 | sb->state = 0; |
| 921 | sb->events_hi = (mddev->events>>32); |
| 922 | sb->events_lo = (u32)mddev->events; |
| 923 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 924 | if (mddev->reshape_position == MaxSector) |
| 925 | sb->minor_version = 90; |
| 926 | else { |
| 927 | sb->minor_version = 91; |
| 928 | sb->reshape_position = mddev->reshape_position; |
| 929 | sb->new_level = mddev->new_level; |
| 930 | sb->delta_disks = mddev->delta_disks; |
| 931 | sb->new_layout = mddev->new_layout; |
| 932 | sb->new_chunk = mddev->new_chunk; |
| 933 | } |
| 934 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (mddev->in_sync) |
| 936 | { |
| 937 | sb->recovery_cp = mddev->recovery_cp; |
| 938 | sb->cp_events_hi = (mddev->events>>32); |
| 939 | sb->cp_events_lo = (u32)mddev->events; |
| 940 | if (mddev->recovery_cp == MaxSector) |
| 941 | sb->state = (1<< MD_SB_CLEAN); |
| 942 | } else |
| 943 | sb->recovery_cp = 0; |
| 944 | |
| 945 | sb->layout = mddev->layout; |
| 946 | sb->chunk_size = mddev->chunk_size; |
| 947 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 948 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 949 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 952 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 954 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 955 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 956 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 957 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 959 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 960 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | d = &sb->disks[rdev2->desc_nr]; |
| 962 | nr_disks++; |
| 963 | d->number = rdev2->desc_nr; |
| 964 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 965 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 966 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 967 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | d->raid_disk = rdev2->raid_disk; |
| 969 | else |
| 970 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 971 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 973 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | d->state = (1<<MD_DISK_ACTIVE); |
| 975 | d->state |= (1<<MD_DISK_SYNC); |
| 976 | active++; |
| 977 | working++; |
| 978 | } else { |
| 979 | d->state = 0; |
| 980 | spare++; |
| 981 | working++; |
| 982 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 983 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 984 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 987 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 988 | mdp_disk_t *d = &sb->disks[i]; |
| 989 | if (d->state == 0 && d->number == 0) { |
| 990 | d->number = i; |
| 991 | d->raid_disk = i; |
| 992 | d->state = (1<<MD_DISK_REMOVED); |
| 993 | d->state |= (1<<MD_DISK_FAULTY); |
| 994 | failed++; |
| 995 | } |
| 996 | } |
| 997 | sb->nr_disks = nr_disks; |
| 998 | sb->active_disks = active; |
| 999 | sb->working_disks = working; |
| 1000 | sb->failed_disks = failed; |
| 1001 | sb->spare_disks = spare; |
| 1002 | |
| 1003 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1004 | sb->sb_csum = calc_sb_csum(sb); |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * version 1 superblock |
| 1009 | */ |
| 1010 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1011 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1013 | __le32 disk_csum; |
| 1014 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | unsigned long long newcsum; |
| 1016 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1017 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | int i; |
| 1019 | |
| 1020 | disk_csum = sb->sb_csum; |
| 1021 | sb->sb_csum = 0; |
| 1022 | newcsum = 0; |
| 1023 | for (i=0; size>=4; size -= 4 ) |
| 1024 | newcsum += le32_to_cpu(*isuper++); |
| 1025 | |
| 1026 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1027 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
| 1029 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1030 | sb->sb_csum = disk_csum; |
| 1031 | return cpu_to_le32(csum); |
| 1032 | } |
| 1033 | |
| 1034 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1035 | { |
| 1036 | struct mdp_superblock_1 *sb; |
| 1037 | int ret; |
| 1038 | sector_t sb_offset; |
| 1039 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1040 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | /* |
| 1043 | * Calculate the position of the superblock. |
| 1044 | * It is always aligned to a 4K boundary and |
| 1045 | * depeding on minor_version, it can be: |
| 1046 | * 0: At least 8K, but less than 12K, from end of device |
| 1047 | * 1: At start of device |
| 1048 | * 2: 4K from start of device. |
| 1049 | */ |
| 1050 | switch(minor_version) { |
| 1051 | case 0: |
| 1052 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 1053 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1054 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | /* convert from sectors to K */ |
| 1056 | sb_offset /= 2; |
| 1057 | break; |
| 1058 | case 1: |
| 1059 | sb_offset = 0; |
| 1060 | break; |
| 1061 | case 2: |
| 1062 | sb_offset = 4; |
| 1063 | break; |
| 1064 | default: |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | rdev->sb_offset = sb_offset; |
| 1068 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1069 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1070 | * and it is safe to read 4k, so we do that |
| 1071 | */ |
| 1072 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (ret) return ret; |
| 1074 | |
| 1075 | |
| 1076 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1077 | |
| 1078 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1079 | sb->major_version != cpu_to_le32(1) || |
| 1080 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 1081 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1082 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return -EINVAL; |
| 1084 | |
| 1085 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1086 | printk("md: invalid superblock checksum on %s\n", |
| 1087 | bdevname(rdev->bdev,b)); |
| 1088 | return -EINVAL; |
| 1089 | } |
| 1090 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1091 | printk("md: data_size too small on %s\n", |
| 1092 | bdevname(rdev->bdev,b)); |
| 1093 | return -EINVAL; |
| 1094 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1095 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1096 | if (sb->level != cpu_to_le32(1) && |
| 1097 | sb->level != cpu_to_le32(4) && |
| 1098 | sb->level != cpu_to_le32(5) && |
| 1099 | sb->level != cpu_to_le32(6) && |
| 1100 | sb->level != cpu_to_le32(10)) { |
| 1101 | printk(KERN_WARNING |
| 1102 | "md: bitmaps not supported for this level.\n"); |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | } |
| 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | rdev->preferred_minor = 0xffff; |
| 1108 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1109 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1111 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1112 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1113 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1114 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1115 | |
| 1116 | if (minor_version |
| 1117 | && rdev->data_offset < sb_offset + (rdev->sb_size/512)) |
| 1118 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1119 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1120 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1121 | rdev->desc_nr = -1; |
| 1122 | else |
| 1123 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1124 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1125 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1126 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1127 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | __u64 ev1, ev2; |
| 1129 | struct mdp_superblock_1 *refsb = |
| 1130 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1131 | |
| 1132 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1133 | sb->level != refsb->level || |
| 1134 | sb->layout != refsb->layout || |
| 1135 | sb->chunksize != refsb->chunksize) { |
| 1136 | printk(KERN_WARNING "md: %s has strangely different" |
| 1137 | " superblock to %s\n", |
| 1138 | bdevname(rdev->bdev,b), |
| 1139 | bdevname(refdev->bdev,b2)); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | ev1 = le64_to_cpu(sb->events); |
| 1143 | ev2 = le64_to_cpu(refsb->events); |
| 1144 | |
| 1145 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1146 | ret = 1; |
| 1147 | else |
| 1148 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1150 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1152 | else |
| 1153 | rdev->size = rdev->sb_offset; |
| 1154 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1155 | return -EINVAL; |
| 1156 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1157 | if (le32_to_cpu(sb->chunksize)) |
| 1158 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1159 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1160 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1161 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1162 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1166 | { |
| 1167 | 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] | 1168 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1170 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1171 | clear_bit(Faulty, &rdev->flags); |
| 1172 | clear_bit(In_sync, &rdev->flags); |
| 1173 | clear_bit(WriteMostly, &rdev->flags); |
| 1174 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | if (mddev->raid_disks == 0) { |
| 1177 | mddev->major_version = 1; |
| 1178 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1179 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1181 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1182 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1183 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1184 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | mddev->layout = le32_to_cpu(sb->layout); |
| 1186 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1187 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1188 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1189 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1190 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1193 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1194 | |
| 1195 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1196 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1197 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1198 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1199 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1200 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1201 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1202 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1203 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1204 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1205 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1206 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1207 | } else { |
| 1208 | mddev->reshape_position = MaxSector; |
| 1209 | mddev->delta_disks = 0; |
| 1210 | mddev->new_level = mddev->level; |
| 1211 | mddev->new_layout = mddev->layout; |
| 1212 | mddev->new_chunk = mddev->chunk_size; |
| 1213 | } |
| 1214 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1215 | } else if (mddev->pers == NULL) { |
| 1216 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | ++ev1; |
| 1218 | if (ev1 < mddev->events) |
| 1219 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1220 | } else if (mddev->bitmap) { |
| 1221 | /* If adding to array with a bitmap, then we can accept an |
| 1222 | * older device, but not too old. |
| 1223 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1224 | if (ev1 < mddev->bitmap->events_cleared) |
| 1225 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1226 | } else { |
| 1227 | if (ev1 < mddev->events) |
| 1228 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1229 | return 0; |
| 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1232 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1234 | switch(role) { |
| 1235 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | break; |
| 1237 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1238 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | break; |
| 1240 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1241 | if ((le32_to_cpu(sb->feature_map) & |
| 1242 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1243 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1244 | else |
| 1245 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | rdev->raid_disk = role; |
| 1247 | break; |
| 1248 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1249 | if (sb->devflags & WriteMostly1) |
| 1250 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1251 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1252 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1258 | { |
| 1259 | struct mdp_superblock_1 *sb; |
| 1260 | struct list_head *tmp; |
| 1261 | mdk_rdev_t *rdev2; |
| 1262 | int max_dev, i; |
| 1263 | /* make rdev->sb match mddev and rdev data. */ |
| 1264 | |
| 1265 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1266 | |
| 1267 | sb->feature_map = 0; |
| 1268 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1269 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1271 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1272 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1273 | |
| 1274 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1275 | sb->events = cpu_to_le64(mddev->events); |
| 1276 | if (mddev->in_sync) |
| 1277 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1278 | else |
| 1279 | sb->resync_offset = cpu_to_le64(0); |
| 1280 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1281 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1282 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1283 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1284 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1285 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1286 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1287 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1288 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1289 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1290 | |
| 1291 | if (rdev->raid_disk >= 0 && |
| 1292 | !test_bit(In_sync, &rdev->flags) && |
| 1293 | rdev->recovery_offset > 0) { |
| 1294 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1295 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1296 | } |
| 1297 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1298 | if (mddev->reshape_position != MaxSector) { |
| 1299 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1300 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1301 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1302 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1303 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1304 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1305 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | max_dev = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1308 | rdev_for_each(rdev2, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | if (rdev2->desc_nr+1 > max_dev) |
| 1310 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1311 | |
| 1312 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1313 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | for (i=0; i<max_dev;i++) |
| 1315 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1316 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1317 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1319 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1321 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1323 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1324 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | else |
| 1326 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1327 | } |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1330 | } |
| 1331 | |
| 1332 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1333 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | [0] = { |
| 1335 | .name = "0.90.0", |
| 1336 | .owner = THIS_MODULE, |
| 1337 | .load_super = super_90_load, |
| 1338 | .validate_super = super_90_validate, |
| 1339 | .sync_super = super_90_sync, |
| 1340 | }, |
| 1341 | [1] = { |
| 1342 | .name = "md-1", |
| 1343 | .owner = THIS_MODULE, |
| 1344 | .load_super = super_1_load, |
| 1345 | .validate_super = super_1_validate, |
| 1346 | .sync_super = super_1_sync, |
| 1347 | }, |
| 1348 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
| 1350 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1351 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1352 | struct list_head *tmp, *tmp2; |
| 1353 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1355 | rdev_for_each(rdev, tmp, mddev1) |
| 1356 | rdev_for_each(rdev2, tmp2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1357 | if (rdev->bdev->bd_contains == |
| 1358 | rdev2->bdev->bd_contains) |
| 1359 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | static LIST_HEAD(pending_raid_disks); |
| 1365 | |
| 1366 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1367 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1368 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1369 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1370 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1371 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (rdev->mddev) { |
| 1374 | MD_BUG(); |
| 1375 | return -EINVAL; |
| 1376 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* prevent duplicates */ |
| 1379 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1380 | return -EEXIST; |
| 1381 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1382 | /* make sure rdev->size exceeds mddev->size */ |
| 1383 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1384 | if (mddev->pers) { |
| 1385 | /* Cannot change size, so fail |
| 1386 | * If mddev->level <= 0, then we don't care |
| 1387 | * about aligning sizes (e.g. linear) |
| 1388 | */ |
| 1389 | if (mddev->level > 0) |
| 1390 | return -ENOSPC; |
| 1391 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1392 | mddev->size = rdev->size; |
| 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | /* Verify rdev->desc_nr is unique. |
| 1396 | * If it is -1, assign a free number, else |
| 1397 | * check number is not in use |
| 1398 | */ |
| 1399 | if (rdev->desc_nr < 0) { |
| 1400 | int choice = 0; |
| 1401 | if (mddev->pers) choice = mddev->raid_disks; |
| 1402 | while (find_rdev_nr(mddev, choice)) |
| 1403 | choice++; |
| 1404 | rdev->desc_nr = choice; |
| 1405 | } else { |
| 1406 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1407 | return -EBUSY; |
| 1408 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1409 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1410 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1411 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1414 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1415 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1416 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1417 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1418 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1419 | if (rdev->bdev->bd_part) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1420 | ko = &rdev->bdev->bd_part->dev.kobj; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1421 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1422 | ko = &rdev->bdev->bd_disk->dev.kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1423 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1424 | kobject_del(&rdev->kobj); |
| 1425 | goto fail; |
| 1426 | } |
| 1427 | list_add(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1428 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1430 | |
| 1431 | fail: |
| 1432 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1433 | b, mdname(mddev)); |
| 1434 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1437 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1438 | { |
| 1439 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1440 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1441 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1445 | { |
| 1446 | char b[BDEVNAME_SIZE]; |
| 1447 | if (!rdev->mddev) { |
| 1448 | MD_BUG(); |
| 1449 | return; |
| 1450 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1451 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | list_del_init(&rdev->same_set); |
| 1453 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1454 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1455 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1456 | |
| 1457 | /* We need to delay this, otherwise we can deadlock when |
| 1458 | * writing to 'remove' to "dev/state" |
| 1459 | */ |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1460 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1461 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1462 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * prevent the device from being mounted, repartitioned or |
| 1467 | * otherwise reused by a RAID array (or any other kernel |
| 1468 | * subsystem), by bd_claiming the device. |
| 1469 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1470 | 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] | 1471 | { |
| 1472 | int err = 0; |
| 1473 | struct block_device *bdev; |
| 1474 | char b[BDEVNAME_SIZE]; |
| 1475 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1476 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (IS_ERR(bdev)) { |
| 1478 | printk(KERN_ERR "md: could not open %s.\n", |
| 1479 | __bdevname(dev, b)); |
| 1480 | return PTR_ERR(bdev); |
| 1481 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1482 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (err) { |
| 1484 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1485 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1486 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | return err; |
| 1488 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1489 | if (!shared) |
| 1490 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | rdev->bdev = bdev; |
| 1492 | return err; |
| 1493 | } |
| 1494 | |
| 1495 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1496 | { |
| 1497 | struct block_device *bdev = rdev->bdev; |
| 1498 | rdev->bdev = NULL; |
| 1499 | if (!bdev) |
| 1500 | MD_BUG(); |
| 1501 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1502 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | void md_autodetect_dev(dev_t dev); |
| 1506 | |
| 1507 | static void export_rdev(mdk_rdev_t * rdev) |
| 1508 | { |
| 1509 | char b[BDEVNAME_SIZE]; |
| 1510 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1511 | bdevname(rdev->bdev,b)); |
| 1512 | if (rdev->mddev) |
| 1513 | MD_BUG(); |
| 1514 | free_disk_sb(rdev); |
| 1515 | list_del_init(&rdev->same_set); |
| 1516 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1517 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1518 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | #endif |
| 1520 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1521 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1525 | { |
| 1526 | unbind_rdev_from_array(rdev); |
| 1527 | export_rdev(rdev); |
| 1528 | } |
| 1529 | |
| 1530 | static void export_array(mddev_t *mddev) |
| 1531 | { |
| 1532 | struct list_head *tmp; |
| 1533 | mdk_rdev_t *rdev; |
| 1534 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1535 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (!rdev->mddev) { |
| 1537 | MD_BUG(); |
| 1538 | continue; |
| 1539 | } |
| 1540 | kick_rdev_from_array(rdev); |
| 1541 | } |
| 1542 | if (!list_empty(&mddev->disks)) |
| 1543 | MD_BUG(); |
| 1544 | mddev->raid_disks = 0; |
| 1545 | mddev->major_version = 0; |
| 1546 | } |
| 1547 | |
| 1548 | static void print_desc(mdp_disk_t *desc) |
| 1549 | { |
| 1550 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1551 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1552 | } |
| 1553 | |
| 1554 | static void print_sb(mdp_super_t *sb) |
| 1555 | { |
| 1556 | int i; |
| 1557 | |
| 1558 | printk(KERN_INFO |
| 1559 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1560 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1561 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1562 | sb->ctime); |
| 1563 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1564 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1565 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1566 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1567 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1568 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1569 | sb->failed_disks, sb->spare_disks, |
| 1570 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1571 | |
| 1572 | printk(KERN_INFO); |
| 1573 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1574 | mdp_disk_t *desc; |
| 1575 | |
| 1576 | desc = sb->disks + i; |
| 1577 | if (desc->number || desc->major || desc->minor || |
| 1578 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1579 | printk(" D %2d: ", i); |
| 1580 | print_desc(desc); |
| 1581 | } |
| 1582 | } |
| 1583 | printk(KERN_INFO "md: THIS: "); |
| 1584 | print_desc(&sb->this_disk); |
| 1585 | |
| 1586 | } |
| 1587 | |
| 1588 | static void print_rdev(mdk_rdev_t *rdev) |
| 1589 | { |
| 1590 | char b[BDEVNAME_SIZE]; |
| 1591 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1592 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1593 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1594 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | if (rdev->sb_loaded) { |
| 1596 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1597 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1598 | } else |
| 1599 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1600 | } |
| 1601 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1602 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | { |
| 1604 | struct list_head *tmp, *tmp2; |
| 1605 | mdk_rdev_t *rdev; |
| 1606 | mddev_t *mddev; |
| 1607 | char b[BDEVNAME_SIZE]; |
| 1608 | |
| 1609 | printk("\n"); |
| 1610 | printk("md: **********************************\n"); |
| 1611 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1612 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1613 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1615 | if (mddev->bitmap) |
| 1616 | bitmap_print_sb(mddev->bitmap); |
| 1617 | else |
| 1618 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1619 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1621 | printk("\n"); |
| 1622 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1623 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | print_rdev(rdev); |
| 1625 | } |
| 1626 | printk("md: **********************************\n"); |
| 1627 | printk("\n"); |
| 1628 | } |
| 1629 | |
| 1630 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1631 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1633 | /* Update each superblock (in-memory image), but |
| 1634 | * if we are allowed to, skip spares which already |
| 1635 | * have the right event counter, or have one earlier |
| 1636 | * (which would mean they aren't being marked as dirty |
| 1637 | * with the rest of the array) |
| 1638 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | mdk_rdev_t *rdev; |
| 1640 | struct list_head *tmp; |
| 1641 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1642 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1643 | if (rdev->sb_events == mddev->events || |
| 1644 | (nospares && |
| 1645 | rdev->raid_disk < 0 && |
| 1646 | (rdev->sb_events&1)==0 && |
| 1647 | rdev->sb_events+1 == mddev->events)) { |
| 1648 | /* Don't update this superblock */ |
| 1649 | rdev->sb_loaded = 2; |
| 1650 | } else { |
| 1651 | super_types[mddev->major_version]. |
| 1652 | sync_super(mddev, rdev); |
| 1653 | rdev->sb_loaded = 1; |
| 1654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1658 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | struct list_head *tmp; |
| 1661 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1662 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1663 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1665 | if (mddev->external) |
| 1666 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1668 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1669 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1670 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1671 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1672 | force_change = 1; |
| 1673 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1674 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1675 | * though if events isn't the right even/odd, we will have to do |
| 1676 | * spares after all |
| 1677 | */ |
| 1678 | nospares = 1; |
| 1679 | if (force_change) |
| 1680 | nospares = 0; |
| 1681 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1682 | /* If the array is degraded, then skipping spares is both |
| 1683 | * dangerous and fairly pointless. |
| 1684 | * Dangerous because a device that was removed from the array |
| 1685 | * might have a event_count that still looks up-to-date, |
| 1686 | * so it can be re-added without a resync. |
| 1687 | * Pointless because if there are any spares to skip, |
| 1688 | * then a recovery will happen and soon that array won't |
| 1689 | * be degraded any more and the spare can go back to sleep then. |
| 1690 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1691 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1692 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1693 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1695 | |
| 1696 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1697 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1698 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1699 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1700 | && (mddev->events & 1) |
| 1701 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1702 | mddev->events--; |
| 1703 | else { |
| 1704 | /* otherwise we have to go forward and ... */ |
| 1705 | mddev->events ++; |
| 1706 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1707 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1708 | if ((mddev->events&1)==0) { |
| 1709 | mddev->events++; |
| 1710 | nospares = 0; |
| 1711 | } |
| 1712 | } else { |
| 1713 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1714 | if ((mddev->events&1)) { |
| 1715 | mddev->events++; |
| 1716 | nospares = 0; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
| 1721 | if (!mddev->events) { |
| 1722 | /* |
| 1723 | * oops, this 64-bit counter should never wrap. |
| 1724 | * Either we are in around ~1 trillion A.C., assuming |
| 1725 | * 1 reboot per second, or we have a bug: |
| 1726 | */ |
| 1727 | MD_BUG(); |
| 1728 | mddev->events --; |
| 1729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * do not write anything to disk if using |
| 1733 | * nonpersistent superblocks |
| 1734 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1735 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1736 | if (!mddev->external) |
| 1737 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1738 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1739 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1740 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1742 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1743 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1744 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | dprintk(KERN_INFO |
| 1747 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1748 | mdname(mddev),mddev->in_sync); |
| 1749 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1750 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1751 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | char b[BDEVNAME_SIZE]; |
| 1753 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1754 | if (rdev->sb_loaded != 1) |
| 1755 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1756 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | dprintk("(skipping faulty "); |
| 1758 | |
| 1759 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1760 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1761 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1762 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1763 | rdev->sb_page); |
| 1764 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1765 | bdevname(rdev->bdev,b), |
| 1766 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1767 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } else |
| 1770 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1771 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /* only need to write one superblock... */ |
| 1773 | break; |
| 1774 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1775 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1776 | /* 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] | 1777 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1778 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1779 | if (mddev->in_sync != sync_req || |
| 1780 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1781 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1782 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1783 | goto repeat; |
| 1784 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1785 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1786 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1787 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1791 | /* words written to sysfs files may, or my not, be \n terminated. |
| 1792 | * We want to accept with case. For this we use cmd_match. |
| 1793 | */ |
| 1794 | static int cmd_match(const char *cmd, const char *str) |
| 1795 | { |
| 1796 | /* See if cmd, written into a sysfs file, matches |
| 1797 | * str. They must either be the same, or cmd can |
| 1798 | * have a trailing newline |
| 1799 | */ |
| 1800 | while (*cmd && *str && *cmd == *str) { |
| 1801 | cmd++; |
| 1802 | str++; |
| 1803 | } |
| 1804 | if (*cmd == '\n') |
| 1805 | cmd++; |
| 1806 | if (*str || *cmd) |
| 1807 | return 0; |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1811 | struct rdev_sysfs_entry { |
| 1812 | struct attribute attr; |
| 1813 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1814 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1815 | }; |
| 1816 | |
| 1817 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1818 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1819 | { |
| 1820 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1821 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1822 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1823 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1824 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1825 | sep = ","; |
| 1826 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1827 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1828 | len += sprintf(page+len, "%sin_sync",sep); |
| 1829 | sep = ","; |
| 1830 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1831 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1832 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1833 | sep = ","; |
| 1834 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1835 | if (test_bit(Blocked, &rdev->flags)) { |
| 1836 | len += sprintf(page+len, "%sblocked", sep); |
| 1837 | sep = ","; |
| 1838 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1839 | if (!test_bit(Faulty, &rdev->flags) && |
| 1840 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1841 | len += sprintf(page+len, "%sspare", sep); |
| 1842 | sep = ","; |
| 1843 | } |
| 1844 | return len+sprintf(page+len, "\n"); |
| 1845 | } |
| 1846 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1847 | static ssize_t |
| 1848 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1849 | { |
| 1850 | /* can write |
| 1851 | * faulty - simulates and error |
| 1852 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1853 | * writemostly - sets write_mostly |
| 1854 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1855 | * blocked - sets the Blocked flag |
| 1856 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1857 | */ |
| 1858 | int err = -EINVAL; |
| 1859 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1860 | md_error(rdev->mddev, rdev); |
| 1861 | err = 0; |
| 1862 | } else if (cmd_match(buf, "remove")) { |
| 1863 | if (rdev->raid_disk >= 0) |
| 1864 | err = -EBUSY; |
| 1865 | else { |
| 1866 | mddev_t *mddev = rdev->mddev; |
| 1867 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1868 | if (mddev->pers) |
| 1869 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1870 | md_new_event(mddev); |
| 1871 | err = 0; |
| 1872 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1873 | } else if (cmd_match(buf, "writemostly")) { |
| 1874 | set_bit(WriteMostly, &rdev->flags); |
| 1875 | err = 0; |
| 1876 | } else if (cmd_match(buf, "-writemostly")) { |
| 1877 | clear_bit(WriteMostly, &rdev->flags); |
| 1878 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1879 | } else if (cmd_match(buf, "blocked")) { |
| 1880 | set_bit(Blocked, &rdev->flags); |
| 1881 | err = 0; |
| 1882 | } else if (cmd_match(buf, "-blocked")) { |
| 1883 | clear_bit(Blocked, &rdev->flags); |
| 1884 | wake_up(&rdev->blocked_wait); |
| 1885 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1886 | md_wakeup_thread(rdev->mddev->thread); |
| 1887 | |
| 1888 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1889 | } |
| 1890 | return err ? err : len; |
| 1891 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1892 | static struct rdev_sysfs_entry rdev_state = |
| 1893 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1894 | |
| 1895 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1896 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1897 | { |
| 1898 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1899 | } |
| 1900 | |
| 1901 | static ssize_t |
| 1902 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1903 | { |
| 1904 | char *e; |
| 1905 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1906 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1907 | atomic_set(&rdev->corrected_errors, n); |
| 1908 | return len; |
| 1909 | } |
| 1910 | return -EINVAL; |
| 1911 | } |
| 1912 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1913 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1914 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1915 | static ssize_t |
| 1916 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1917 | { |
| 1918 | if (rdev->raid_disk < 0) |
| 1919 | return sprintf(page, "none\n"); |
| 1920 | else |
| 1921 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1922 | } |
| 1923 | |
| 1924 | static ssize_t |
| 1925 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1926 | { |
| 1927 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1928 | int err; |
| 1929 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1930 | int slot = simple_strtoul(buf, &e, 10); |
| 1931 | if (strncmp(buf, "none", 4)==0) |
| 1932 | slot = -1; |
| 1933 | else if (e==buf || (*e && *e!= '\n')) |
| 1934 | return -EINVAL; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1935 | if (rdev->mddev->pers) { |
| 1936 | /* Setting 'slot' on an active array requires also |
| 1937 | * updating the 'rd%d' link, and communicating |
| 1938 | * with the personality with ->hot_*_disk. |
| 1939 | * For now we only support removing |
| 1940 | * failed/spare devices. This normally happens automatically, |
| 1941 | * but not when the metadata is externally managed. |
| 1942 | */ |
| 1943 | if (slot != -1) |
| 1944 | return -EBUSY; |
| 1945 | if (rdev->raid_disk == -1) |
| 1946 | return -EEXIST; |
| 1947 | /* personality does all needed checks */ |
| 1948 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1949 | return -EINVAL; |
| 1950 | err = rdev->mddev->pers-> |
| 1951 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 1952 | if (err) |
| 1953 | return err; |
| 1954 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1955 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 1956 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1957 | md_wakeup_thread(rdev->mddev->thread); |
| 1958 | } else { |
| 1959 | if (slot >= rdev->mddev->raid_disks) |
| 1960 | return -ENOSPC; |
| 1961 | rdev->raid_disk = slot; |
| 1962 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1963 | clear_bit(Faulty, &rdev->flags); |
| 1964 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1965 | set_bit(In_sync, &rdev->flags); |
| 1966 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1967 | return len; |
| 1968 | } |
| 1969 | |
| 1970 | |
| 1971 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1972 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1973 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1974 | static ssize_t |
| 1975 | offset_show(mdk_rdev_t *rdev, char *page) |
| 1976 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 1977 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | static ssize_t |
| 1981 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1982 | { |
| 1983 | char *e; |
| 1984 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 1985 | if (e==buf || (*e && *e != '\n')) |
| 1986 | return -EINVAL; |
| 1987 | if (rdev->mddev->pers) |
| 1988 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1989 | if (rdev->size && rdev->mddev->external) |
| 1990 | /* Must set offset before size, so overlap checks |
| 1991 | * can be sane */ |
| 1992 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1993 | rdev->data_offset = offset; |
| 1994 | return len; |
| 1995 | } |
| 1996 | |
| 1997 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1998 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1999 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2000 | static ssize_t |
| 2001 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2002 | { |
| 2003 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2004 | } |
| 2005 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2006 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2007 | { |
| 2008 | /* check if two start/length pairs overlap */ |
| 2009 | if (s1+l1 <= s2) |
| 2010 | return 0; |
| 2011 | if (s2+l2 <= s1) |
| 2012 | return 0; |
| 2013 | return 1; |
| 2014 | } |
| 2015 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2016 | static ssize_t |
| 2017 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2018 | { |
| 2019 | char *e; |
| 2020 | unsigned long long size = simple_strtoull(buf, &e, 10); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2021 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2022 | mddev_t *my_mddev = rdev->mddev; |
| 2023 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2024 | if (e==buf || (*e && *e != '\n')) |
| 2025 | return -EINVAL; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2026 | if (my_mddev->pers) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2027 | return -EBUSY; |
| 2028 | rdev->size = size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2029 | if (size > oldsize && rdev->mddev->external) { |
| 2030 | /* need to check that all other rdevs with the same ->bdev |
| 2031 | * do not overlap. We need to unlock the mddev to avoid |
| 2032 | * a deadlock. We have already changed rdev->size, and if |
| 2033 | * we have to change it back, we will have the lock again. |
| 2034 | */ |
| 2035 | mddev_t *mddev; |
| 2036 | int overlap = 0; |
| 2037 | struct list_head *tmp, *tmp2; |
| 2038 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2039 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2040 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2041 | mdk_rdev_t *rdev2; |
| 2042 | |
| 2043 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2044 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2045 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2046 | (rdev->bdev == rdev2->bdev && |
| 2047 | rdev != rdev2 && |
| 2048 | overlaps(rdev->data_offset, rdev->size, |
| 2049 | rdev2->data_offset, rdev2->size))) { |
| 2050 | overlap = 1; |
| 2051 | break; |
| 2052 | } |
| 2053 | mddev_unlock(mddev); |
| 2054 | if (overlap) { |
| 2055 | mddev_put(mddev); |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2059 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2060 | if (overlap) { |
| 2061 | /* Someone else could have slipped in a size |
| 2062 | * change here, but doing so is just silly. |
| 2063 | * We put oldsize back because we *know* it is |
| 2064 | * safe, and trust userspace not to race with |
| 2065 | * itself |
| 2066 | */ |
| 2067 | rdev->size = oldsize; |
| 2068 | return -EBUSY; |
| 2069 | } |
| 2070 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2071 | if (size < my_mddev->size || my_mddev->size == 0) |
| 2072 | my_mddev->size = size; |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2073 | return len; |
| 2074 | } |
| 2075 | |
| 2076 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2077 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2078 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2079 | static struct attribute *rdev_default_attrs[] = { |
| 2080 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2081 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2082 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2083 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2084 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2085 | NULL, |
| 2086 | }; |
| 2087 | static ssize_t |
| 2088 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2089 | { |
| 2090 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2091 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2092 | mddev_t *mddev = rdev->mddev; |
| 2093 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2094 | |
| 2095 | if (!entry->show) |
| 2096 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2097 | |
| 2098 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2099 | if (!rv) { |
| 2100 | if (rdev->mddev == NULL) |
| 2101 | rv = -EBUSY; |
| 2102 | else |
| 2103 | rv = entry->show(rdev, page); |
| 2104 | mddev_unlock(mddev); |
| 2105 | } |
| 2106 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | static ssize_t |
| 2110 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2111 | const char *page, size_t length) |
| 2112 | { |
| 2113 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2114 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2115 | ssize_t rv; |
| 2116 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2117 | |
| 2118 | if (!entry->store) |
| 2119 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2120 | if (!capable(CAP_SYS_ADMIN)) |
| 2121 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2122 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2123 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2124 | if (rdev->mddev == NULL) |
| 2125 | rv = -EBUSY; |
| 2126 | else |
| 2127 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2128 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2129 | } |
| 2130 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | static void rdev_free(struct kobject *ko) |
| 2134 | { |
| 2135 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2136 | kfree(rdev); |
| 2137 | } |
| 2138 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2139 | .show = rdev_attr_show, |
| 2140 | .store = rdev_attr_store, |
| 2141 | }; |
| 2142 | static struct kobj_type rdev_ktype = { |
| 2143 | .release = rdev_free, |
| 2144 | .sysfs_ops = &rdev_sysfs_ops, |
| 2145 | .default_attrs = rdev_default_attrs, |
| 2146 | }; |
| 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | /* |
| 2149 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2150 | * |
| 2151 | * mark the device faulty if: |
| 2152 | * |
| 2153 | * - the device is nonexistent (zero size) |
| 2154 | * - the device has no valid superblock |
| 2155 | * |
| 2156 | * a faulty rdev _never_ has rdev->sb set. |
| 2157 | */ |
| 2158 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2159 | { |
| 2160 | char b[BDEVNAME_SIZE]; |
| 2161 | int err; |
| 2162 | mdk_rdev_t *rdev; |
| 2163 | sector_t size; |
| 2164 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2165 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | if (!rdev) { |
| 2167 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2168 | return ERR_PTR(-ENOMEM); |
| 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | |
| 2171 | if ((err = alloc_disk_sb(rdev))) |
| 2172 | goto abort_free; |
| 2173 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2174 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | if (err) |
| 2176 | goto abort_free; |
| 2177 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2178 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2181 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2182 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2183 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2185 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2187 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2188 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
| 2190 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2191 | if (!size) { |
| 2192 | printk(KERN_WARNING |
| 2193 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2194 | bdevname(rdev->bdev,b)); |
| 2195 | err = -EINVAL; |
| 2196 | goto abort_free; |
| 2197 | } |
| 2198 | |
| 2199 | if (super_format >= 0) { |
| 2200 | err = super_types[super_format]. |
| 2201 | load_super(rdev, NULL, super_minor); |
| 2202 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2203 | printk(KERN_WARNING |
| 2204 | "md: %s does not have a valid v%d.%d " |
| 2205 | "superblock, not importing!\n", |
| 2206 | bdevname(rdev->bdev,b), |
| 2207 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | goto abort_free; |
| 2209 | } |
| 2210 | if (err < 0) { |
| 2211 | printk(KERN_WARNING |
| 2212 | "md: could not read %s's sb, not importing!\n", |
| 2213 | bdevname(rdev->bdev,b)); |
| 2214 | goto abort_free; |
| 2215 | } |
| 2216 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2219 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
| 2221 | return rdev; |
| 2222 | |
| 2223 | abort_free: |
| 2224 | if (rdev->sb_page) { |
| 2225 | if (rdev->bdev) |
| 2226 | unlock_rdev(rdev); |
| 2227 | free_disk_sb(rdev); |
| 2228 | } |
| 2229 | kfree(rdev); |
| 2230 | return ERR_PTR(err); |
| 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | * Check a full RAID array for plausibility |
| 2235 | */ |
| 2236 | |
| 2237 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2238 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | { |
| 2240 | int i; |
| 2241 | struct list_head *tmp; |
| 2242 | mdk_rdev_t *rdev, *freshest; |
| 2243 | char b[BDEVNAME_SIZE]; |
| 2244 | |
| 2245 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2246 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | switch (super_types[mddev->major_version]. |
| 2248 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2249 | case 1: |
| 2250 | freshest = rdev; |
| 2251 | break; |
| 2252 | case 0: |
| 2253 | break; |
| 2254 | default: |
| 2255 | printk( KERN_ERR \ |
| 2256 | "md: fatal superblock inconsistency in %s" |
| 2257 | " -- removing from array\n", |
| 2258 | bdevname(rdev->bdev,b)); |
| 2259 | kick_rdev_from_array(rdev); |
| 2260 | } |
| 2261 | |
| 2262 | |
| 2263 | super_types[mddev->major_version]. |
| 2264 | validate_super(mddev, freshest); |
| 2265 | |
| 2266 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2267 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | if (rdev != freshest) |
| 2269 | if (super_types[mddev->major_version]. |
| 2270 | validate_super(mddev, rdev)) { |
| 2271 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2272 | " from array!\n", |
| 2273 | bdevname(rdev->bdev,b)); |
| 2274 | kick_rdev_from_array(rdev); |
| 2275 | continue; |
| 2276 | } |
| 2277 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2278 | rdev->desc_nr = i++; |
| 2279 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2280 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2281 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2282 | rdev->raid_disk = -1; |
| 2283 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | if (mddev->recovery_cp != MaxSector && |
| 2290 | mddev->level >= 1) |
| 2291 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2292 | " -- starting background reconstruction\n", |
| 2293 | mdname(mddev)); |
| 2294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2297 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2298 | safe_delay_show(mddev_t *mddev, char *page) |
| 2299 | { |
| 2300 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2301 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2302 | } |
| 2303 | static ssize_t |
| 2304 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2305 | { |
| 2306 | int scale=1; |
| 2307 | int dot=0; |
| 2308 | int i; |
| 2309 | unsigned long msec; |
| 2310 | char buf[30]; |
| 2311 | char *e; |
| 2312 | /* remove a period, and count digits after it */ |
| 2313 | if (len >= sizeof(buf)) |
| 2314 | return -EINVAL; |
| 2315 | strlcpy(buf, cbuf, len); |
| 2316 | buf[len] = 0; |
| 2317 | for (i=0; i<len; i++) { |
| 2318 | if (dot) { |
| 2319 | if (isdigit(buf[i])) { |
| 2320 | buf[i-1] = buf[i]; |
| 2321 | scale *= 10; |
| 2322 | } |
| 2323 | buf[i] = 0; |
| 2324 | } else if (buf[i] == '.') { |
| 2325 | dot=1; |
| 2326 | buf[i] = 0; |
| 2327 | } |
| 2328 | } |
| 2329 | msec = simple_strtoul(buf, &e, 10); |
| 2330 | if (e == buf || (*e && *e != '\n')) |
| 2331 | return -EINVAL; |
| 2332 | msec = (msec * 1000) / scale; |
| 2333 | if (msec == 0) |
| 2334 | mddev->safemode_delay = 0; |
| 2335 | else { |
| 2336 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2337 | if (mddev->safemode_delay == 0) |
| 2338 | mddev->safemode_delay = 1; |
| 2339 | } |
| 2340 | return len; |
| 2341 | } |
| 2342 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2343 | __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] | 2344 | |
| 2345 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2346 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2347 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2348 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2349 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2350 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2351 | else if (mddev->clevel[0]) |
| 2352 | return sprintf(page, "%s\n", mddev->clevel); |
| 2353 | else if (mddev->level != LEVEL_NONE) |
| 2354 | return sprintf(page, "%d\n", mddev->level); |
| 2355 | else |
| 2356 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2357 | } |
| 2358 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2359 | static ssize_t |
| 2360 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2361 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2362 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2363 | if (mddev->pers) |
| 2364 | return -EBUSY; |
| 2365 | if (len == 0) |
| 2366 | return 0; |
| 2367 | if (len >= sizeof(mddev->clevel)) |
| 2368 | return -ENOSPC; |
| 2369 | strncpy(mddev->clevel, buf, len); |
| 2370 | if (mddev->clevel[len-1] == '\n') |
| 2371 | len--; |
| 2372 | mddev->clevel[len] = 0; |
| 2373 | mddev->level = LEVEL_NONE; |
| 2374 | return rv; |
| 2375 | } |
| 2376 | |
| 2377 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2378 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2379 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2380 | |
| 2381 | static ssize_t |
| 2382 | layout_show(mddev_t *mddev, char *page) |
| 2383 | { |
| 2384 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2385 | if (mddev->reshape_position != MaxSector && |
| 2386 | mddev->layout != mddev->new_layout) |
| 2387 | return sprintf(page, "%d (%d)\n", |
| 2388 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2389 | return sprintf(page, "%d\n", mddev->layout); |
| 2390 | } |
| 2391 | |
| 2392 | static ssize_t |
| 2393 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2394 | { |
| 2395 | char *e; |
| 2396 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2397 | |
| 2398 | if (!*buf || (*e && *e != '\n')) |
| 2399 | return -EINVAL; |
| 2400 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2401 | if (mddev->pers) |
| 2402 | return -EBUSY; |
| 2403 | if (mddev->reshape_position != MaxSector) |
| 2404 | mddev->new_layout = n; |
| 2405 | else |
| 2406 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2407 | return len; |
| 2408 | } |
| 2409 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2410 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2411 | |
| 2412 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2413 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2414 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2415 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2416 | if (mddev->raid_disks == 0) |
| 2417 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2418 | if (mddev->reshape_position != MaxSector && |
| 2419 | mddev->delta_disks != 0) |
| 2420 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2421 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2422 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2423 | } |
| 2424 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2425 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2426 | |
| 2427 | static ssize_t |
| 2428 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2429 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2430 | char *e; |
| 2431 | int rv = 0; |
| 2432 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2433 | |
| 2434 | if (!*buf || (*e && *e != '\n')) |
| 2435 | return -EINVAL; |
| 2436 | |
| 2437 | if (mddev->pers) |
| 2438 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2439 | else if (mddev->reshape_position != MaxSector) { |
| 2440 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2441 | mddev->delta_disks = n - olddisks; |
| 2442 | mddev->raid_disks = n; |
| 2443 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2444 | mddev->raid_disks = n; |
| 2445 | return rv ? rv : len; |
| 2446 | } |
| 2447 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2448 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2449 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2450 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2451 | chunk_size_show(mddev_t *mddev, char *page) |
| 2452 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2453 | if (mddev->reshape_position != MaxSector && |
| 2454 | mddev->chunk_size != mddev->new_chunk) |
| 2455 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2456 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2457 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2458 | } |
| 2459 | |
| 2460 | static ssize_t |
| 2461 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2462 | { |
| 2463 | /* can only set chunk_size if array is not yet active */ |
| 2464 | char *e; |
| 2465 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2466 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2467 | if (!*buf || (*e && *e != '\n')) |
| 2468 | return -EINVAL; |
| 2469 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2470 | if (mddev->pers) |
| 2471 | return -EBUSY; |
| 2472 | else if (mddev->reshape_position != MaxSector) |
| 2473 | mddev->new_chunk = n; |
| 2474 | else |
| 2475 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2476 | return len; |
| 2477 | } |
| 2478 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2479 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2480 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2481 | static ssize_t |
| 2482 | resync_start_show(mddev_t *mddev, char *page) |
| 2483 | { |
| 2484 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2485 | } |
| 2486 | |
| 2487 | static ssize_t |
| 2488 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2489 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2490 | char *e; |
| 2491 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2492 | |
| 2493 | if (mddev->pers) |
| 2494 | return -EBUSY; |
| 2495 | if (!*buf || (*e && *e != '\n')) |
| 2496 | return -EINVAL; |
| 2497 | |
| 2498 | mddev->recovery_cp = n; |
| 2499 | return len; |
| 2500 | } |
| 2501 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2502 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2503 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2504 | /* |
| 2505 | * The array state can be: |
| 2506 | * |
| 2507 | * clear |
| 2508 | * No devices, no size, no level |
| 2509 | * Equivalent to STOP_ARRAY ioctl |
| 2510 | * inactive |
| 2511 | * May have some settings, but array is not active |
| 2512 | * all IO results in error |
| 2513 | * When written, doesn't tear down array, but just stops it |
| 2514 | * suspended (not supported yet) |
| 2515 | * All IO requests will block. The array can be reconfigured. |
| 2516 | * Writing this, if accepted, will block until array is quiessent |
| 2517 | * readonly |
| 2518 | * no resync can happen. no superblocks get written. |
| 2519 | * write requests fail |
| 2520 | * read-auto |
| 2521 | * like readonly, but behaves like 'clean' on a write request. |
| 2522 | * |
| 2523 | * clean - no pending writes, but otherwise active. |
| 2524 | * When written to inactive array, starts without resync |
| 2525 | * If a write request arrives then |
| 2526 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2527 | * if not known, block and switch to write-pending |
| 2528 | * If written to an active array that has pending writes, then fails. |
| 2529 | * active |
| 2530 | * fully active: IO and resync can be happening. |
| 2531 | * When written to inactive array, starts with resync |
| 2532 | * |
| 2533 | * write-pending |
| 2534 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2535 | * |
| 2536 | * active-idle |
| 2537 | * like active, but no writes have been seen for a while (100msec). |
| 2538 | * |
| 2539 | */ |
| 2540 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2541 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2542 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2543 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2544 | "write-pending", "active-idle", NULL }; |
| 2545 | |
| 2546 | static int match_word(const char *word, char **list) |
| 2547 | { |
| 2548 | int n; |
| 2549 | for (n=0; list[n]; n++) |
| 2550 | if (cmd_match(word, list[n])) |
| 2551 | break; |
| 2552 | return n; |
| 2553 | } |
| 2554 | |
| 2555 | static ssize_t |
| 2556 | array_state_show(mddev_t *mddev, char *page) |
| 2557 | { |
| 2558 | enum array_state st = inactive; |
| 2559 | |
| 2560 | if (mddev->pers) |
| 2561 | switch(mddev->ro) { |
| 2562 | case 1: |
| 2563 | st = readonly; |
| 2564 | break; |
| 2565 | case 2: |
| 2566 | st = read_auto; |
| 2567 | break; |
| 2568 | case 0: |
| 2569 | if (mddev->in_sync) |
| 2570 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2571 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2572 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2573 | else if (mddev->safemode) |
| 2574 | st = active_idle; |
| 2575 | else |
| 2576 | st = active; |
| 2577 | } |
| 2578 | else { |
| 2579 | if (list_empty(&mddev->disks) && |
| 2580 | mddev->raid_disks == 0 && |
| 2581 | mddev->size == 0) |
| 2582 | st = clear; |
| 2583 | else |
| 2584 | st = inactive; |
| 2585 | } |
| 2586 | return sprintf(page, "%s\n", array_states[st]); |
| 2587 | } |
| 2588 | |
| 2589 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2590 | static int do_md_run(mddev_t * mddev); |
| 2591 | static int restart_array(mddev_t *mddev); |
| 2592 | |
| 2593 | static ssize_t |
| 2594 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2595 | { |
| 2596 | int err = -EINVAL; |
| 2597 | enum array_state st = match_word(buf, array_states); |
| 2598 | switch(st) { |
| 2599 | case bad_word: |
| 2600 | break; |
| 2601 | case clear: |
| 2602 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2603 | if (atomic_read(&mddev->active) > 1) |
| 2604 | return -EBUSY; |
| 2605 | err = do_md_stop(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2606 | break; |
| 2607 | case inactive: |
| 2608 | /* stopping an active array */ |
| 2609 | if (mddev->pers) { |
| 2610 | if (atomic_read(&mddev->active) > 1) |
| 2611 | return -EBUSY; |
| 2612 | err = do_md_stop(mddev, 2); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2613 | } else |
| 2614 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2615 | break; |
| 2616 | case suspended: |
| 2617 | break; /* not supported yet */ |
| 2618 | case readonly: |
| 2619 | if (mddev->pers) |
| 2620 | err = do_md_stop(mddev, 1); |
| 2621 | else { |
| 2622 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2623 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2624 | err = do_md_run(mddev); |
| 2625 | } |
| 2626 | break; |
| 2627 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2628 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2629 | if (mddev->ro != 1) |
| 2630 | err = do_md_stop(mddev, 1); |
| 2631 | else |
| 2632 | err = restart_array(mddev); |
| 2633 | if (err == 0) { |
| 2634 | mddev->ro = 2; |
| 2635 | set_disk_ro(mddev->gendisk, 0); |
| 2636 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2637 | } else { |
| 2638 | mddev->ro = 2; |
| 2639 | err = do_md_run(mddev); |
| 2640 | } |
| 2641 | break; |
| 2642 | case clean: |
| 2643 | if (mddev->pers) { |
| 2644 | restart_array(mddev); |
| 2645 | spin_lock_irq(&mddev->write_lock); |
| 2646 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2647 | if (mddev->in_sync == 0) { |
| 2648 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2649 | if (mddev->safemode == 1) |
| 2650 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2651 | if (mddev->persistent) |
| 2652 | set_bit(MD_CHANGE_CLEAN, |
| 2653 | &mddev->flags); |
| 2654 | } |
| 2655 | err = 0; |
| 2656 | } else |
| 2657 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2658 | spin_unlock_irq(&mddev->write_lock); |
| 2659 | } else { |
| 2660 | mddev->ro = 0; |
| 2661 | mddev->recovery_cp = MaxSector; |
| 2662 | err = do_md_run(mddev); |
| 2663 | } |
| 2664 | break; |
| 2665 | case active: |
| 2666 | if (mddev->pers) { |
| 2667 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2668 | if (mddev->external) |
| 2669 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2670 | wake_up(&mddev->sb_wait); |
| 2671 | err = 0; |
| 2672 | } else { |
| 2673 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2674 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2675 | err = do_md_run(mddev); |
| 2676 | } |
| 2677 | break; |
| 2678 | case write_pending: |
| 2679 | case active_idle: |
| 2680 | /* these cannot be set */ |
| 2681 | break; |
| 2682 | } |
| 2683 | if (err) |
| 2684 | return err; |
| 2685 | else |
| 2686 | return len; |
| 2687 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2688 | static struct md_sysfs_entry md_array_state = |
| 2689 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2690 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2691 | static ssize_t |
| 2692 | null_show(mddev_t *mddev, char *page) |
| 2693 | { |
| 2694 | return -EINVAL; |
| 2695 | } |
| 2696 | |
| 2697 | static ssize_t |
| 2698 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2699 | { |
| 2700 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2701 | /* The new device is added to the array. |
| 2702 | * If the array has a persistent superblock, we read the |
| 2703 | * superblock to initialise info and check validity. |
| 2704 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2705 | * which mainly checks size. |
| 2706 | */ |
| 2707 | char *e; |
| 2708 | int major = simple_strtoul(buf, &e, 10); |
| 2709 | int minor; |
| 2710 | dev_t dev; |
| 2711 | mdk_rdev_t *rdev; |
| 2712 | int err; |
| 2713 | |
| 2714 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2715 | return -EINVAL; |
| 2716 | minor = simple_strtoul(e+1, &e, 10); |
| 2717 | if (*e && *e != '\n') |
| 2718 | return -EINVAL; |
| 2719 | dev = MKDEV(major, minor); |
| 2720 | if (major != MAJOR(dev) || |
| 2721 | minor != MINOR(dev)) |
| 2722 | return -EOVERFLOW; |
| 2723 | |
| 2724 | |
| 2725 | if (mddev->persistent) { |
| 2726 | rdev = md_import_device(dev, mddev->major_version, |
| 2727 | mddev->minor_version); |
| 2728 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2729 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2730 | mdk_rdev_t, same_set); |
| 2731 | err = super_types[mddev->major_version] |
| 2732 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2733 | if (err < 0) |
| 2734 | goto out; |
| 2735 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2736 | } else if (mddev->external) |
| 2737 | rdev = md_import_device(dev, -2, -1); |
| 2738 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2739 | rdev = md_import_device(dev, -1, -1); |
| 2740 | |
| 2741 | if (IS_ERR(rdev)) |
| 2742 | return PTR_ERR(rdev); |
| 2743 | err = bind_rdev_to_array(rdev, mddev); |
| 2744 | out: |
| 2745 | if (err) |
| 2746 | export_rdev(rdev); |
| 2747 | return err ? err : len; |
| 2748 | } |
| 2749 | |
| 2750 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2751 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2752 | |
| 2753 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2754 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2755 | { |
| 2756 | char *end; |
| 2757 | unsigned long chunk, end_chunk; |
| 2758 | |
| 2759 | if (!mddev->bitmap) |
| 2760 | goto out; |
| 2761 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2762 | while (*buf) { |
| 2763 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2764 | if (buf == end) break; |
| 2765 | if (*end == '-') { /* range */ |
| 2766 | buf = end + 1; |
| 2767 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2768 | if (buf == end) break; |
| 2769 | } |
| 2770 | if (*end && !isspace(*end)) break; |
| 2771 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2772 | buf = end; |
| 2773 | while (isspace(*buf)) buf++; |
| 2774 | } |
| 2775 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2776 | out: |
| 2777 | return len; |
| 2778 | } |
| 2779 | |
| 2780 | static struct md_sysfs_entry md_bitmap = |
| 2781 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2782 | |
| 2783 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2784 | size_show(mddev_t *mddev, char *page) |
| 2785 | { |
| 2786 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2787 | } |
| 2788 | |
| 2789 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2790 | |
| 2791 | static ssize_t |
| 2792 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2793 | { |
| 2794 | /* If array is inactive, we can reduce the component size, but |
| 2795 | * not increase it (except from 0). |
| 2796 | * If array is active, we can try an on-line resize |
| 2797 | */ |
| 2798 | char *e; |
| 2799 | int err = 0; |
| 2800 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2801 | if (!*buf || *buf == '\n' || |
| 2802 | (*e && *e != '\n')) |
| 2803 | return -EINVAL; |
| 2804 | |
| 2805 | if (mddev->pers) { |
| 2806 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2807 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2808 | } else { |
| 2809 | if (mddev->size == 0 || |
| 2810 | mddev->size > size) |
| 2811 | mddev->size = size; |
| 2812 | else |
| 2813 | err = -ENOSPC; |
| 2814 | } |
| 2815 | return err ? err : len; |
| 2816 | } |
| 2817 | |
| 2818 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2819 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2820 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2821 | |
| 2822 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2823 | * This is one of |
| 2824 | * 'none' for arrays with no metadata (good luck...) |
| 2825 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2826 | * or N.M for internally known formats |
| 2827 | */ |
| 2828 | static ssize_t |
| 2829 | metadata_show(mddev_t *mddev, char *page) |
| 2830 | { |
| 2831 | if (mddev->persistent) |
| 2832 | return sprintf(page, "%d.%d\n", |
| 2833 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2834 | else if (mddev->external) |
| 2835 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2836 | else |
| 2837 | return sprintf(page, "none\n"); |
| 2838 | } |
| 2839 | |
| 2840 | static ssize_t |
| 2841 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2842 | { |
| 2843 | int major, minor; |
| 2844 | char *e; |
| 2845 | if (!list_empty(&mddev->disks)) |
| 2846 | return -EBUSY; |
| 2847 | |
| 2848 | if (cmd_match(buf, "none")) { |
| 2849 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2850 | mddev->external = 0; |
| 2851 | mddev->major_version = 0; |
| 2852 | mddev->minor_version = 90; |
| 2853 | return len; |
| 2854 | } |
| 2855 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2856 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2857 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2858 | namelen = sizeof(mddev->metadata_type)-1; |
| 2859 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2860 | mddev->metadata_type[namelen] = 0; |
| 2861 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2862 | mddev->metadata_type[--namelen] = 0; |
| 2863 | mddev->persistent = 0; |
| 2864 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2865 | mddev->major_version = 0; |
| 2866 | mddev->minor_version = 90; |
| 2867 | return len; |
| 2868 | } |
| 2869 | major = simple_strtoul(buf, &e, 10); |
| 2870 | if (e==buf || *e != '.') |
| 2871 | return -EINVAL; |
| 2872 | buf = e+1; |
| 2873 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2874 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2875 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2876 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2877 | return -ENOENT; |
| 2878 | mddev->major_version = major; |
| 2879 | mddev->minor_version = minor; |
| 2880 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2881 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2882 | return len; |
| 2883 | } |
| 2884 | |
| 2885 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2886 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2887 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2888 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2889 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2890 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2891 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2892 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2893 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2894 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2895 | type = "reshape"; |
| 2896 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2897 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2898 | type = "resync"; |
| 2899 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2900 | type = "check"; |
| 2901 | else |
| 2902 | type = "repair"; |
| 2903 | } else |
| 2904 | type = "recover"; |
| 2905 | } |
| 2906 | return sprintf(page, "%s\n", type); |
| 2907 | } |
| 2908 | |
| 2909 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2910 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2911 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2912 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2913 | return -EINVAL; |
| 2914 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2915 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2916 | if (mddev->sync_thread) { |
| 2917 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2918 | md_unregister_thread(mddev->sync_thread); |
| 2919 | mddev->sync_thread = NULL; |
| 2920 | mddev->recovery = 0; |
| 2921 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2922 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2923 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2924 | return -EBUSY; |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2925 | else if (cmd_match(page, "resync") || cmd_match(page, "recover")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2926 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2927 | else if (cmd_match(page, "reshape")) { |
| 2928 | int err; |
| 2929 | if (mddev->pers->start_reshape == NULL) |
| 2930 | return -EINVAL; |
| 2931 | err = mddev->pers->start_reshape(mddev); |
| 2932 | if (err) |
| 2933 | return err; |
| 2934 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2935 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2936 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2937 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2938 | return -EINVAL; |
| 2939 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2940 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2941 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2942 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2943 | md_wakeup_thread(mddev->thread); |
| 2944 | return len; |
| 2945 | } |
| 2946 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2947 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2948 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2949 | { |
| 2950 | return sprintf(page, "%llu\n", |
| 2951 | (unsigned long long) mddev->resync_mismatches); |
| 2952 | } |
| 2953 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2954 | static struct md_sysfs_entry md_scan_mode = |
| 2955 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2956 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2957 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2958 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2959 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2960 | static ssize_t |
| 2961 | sync_min_show(mddev_t *mddev, char *page) |
| 2962 | { |
| 2963 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 2964 | mddev->sync_speed_min ? "local": "system"); |
| 2965 | } |
| 2966 | |
| 2967 | static ssize_t |
| 2968 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 2969 | { |
| 2970 | int min; |
| 2971 | char *e; |
| 2972 | if (strncmp(buf, "system", 6)==0) { |
| 2973 | mddev->sync_speed_min = 0; |
| 2974 | return len; |
| 2975 | } |
| 2976 | min = simple_strtoul(buf, &e, 10); |
| 2977 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 2978 | return -EINVAL; |
| 2979 | mddev->sync_speed_min = min; |
| 2980 | return len; |
| 2981 | } |
| 2982 | |
| 2983 | static struct md_sysfs_entry md_sync_min = |
| 2984 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 2985 | |
| 2986 | static ssize_t |
| 2987 | sync_max_show(mddev_t *mddev, char *page) |
| 2988 | { |
| 2989 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 2990 | mddev->sync_speed_max ? "local": "system"); |
| 2991 | } |
| 2992 | |
| 2993 | static ssize_t |
| 2994 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 2995 | { |
| 2996 | int max; |
| 2997 | char *e; |
| 2998 | if (strncmp(buf, "system", 6)==0) { |
| 2999 | mddev->sync_speed_max = 0; |
| 3000 | return len; |
| 3001 | } |
| 3002 | max = simple_strtoul(buf, &e, 10); |
| 3003 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3004 | return -EINVAL; |
| 3005 | mddev->sync_speed_max = max; |
| 3006 | return len; |
| 3007 | } |
| 3008 | |
| 3009 | static struct md_sysfs_entry md_sync_max = |
| 3010 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3011 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3012 | static ssize_t |
| 3013 | degraded_show(mddev_t *mddev, char *page) |
| 3014 | { |
| 3015 | return sprintf(page, "%d\n", mddev->degraded); |
| 3016 | } |
| 3017 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3018 | |
| 3019 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3020 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3021 | { |
| 3022 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3023 | } |
| 3024 | |
| 3025 | static ssize_t |
| 3026 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3027 | { |
| 3028 | long n; |
| 3029 | |
| 3030 | if (strict_strtol(buf, 10, &n)) |
| 3031 | return -EINVAL; |
| 3032 | |
| 3033 | if (n != 0 && n != 1) |
| 3034 | return -EINVAL; |
| 3035 | |
| 3036 | mddev->parallel_resync = n; |
| 3037 | |
| 3038 | if (mddev->sync_thread) |
| 3039 | wake_up(&resync_wait); |
| 3040 | |
| 3041 | return len; |
| 3042 | } |
| 3043 | |
| 3044 | /* force parallel resync, even with shared block devices */ |
| 3045 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3046 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3047 | sync_force_parallel_show, sync_force_parallel_store); |
| 3048 | |
| 3049 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3050 | sync_speed_show(mddev_t *mddev, char *page) |
| 3051 | { |
| 3052 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 3053 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3054 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 3055 | if (!dt) dt++; |
| 3056 | db = resync - (mddev->resync_mark_cnt); |
| 3057 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 3058 | } |
| 3059 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3060 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3061 | |
| 3062 | static ssize_t |
| 3063 | sync_completed_show(mddev_t *mddev, char *page) |
| 3064 | { |
| 3065 | unsigned long max_blocks, resync; |
| 3066 | |
| 3067 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3068 | max_blocks = mddev->resync_max_sectors; |
| 3069 | else |
| 3070 | max_blocks = mddev->size << 1; |
| 3071 | |
| 3072 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3073 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3074 | } |
| 3075 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3076 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3077 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3078 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3079 | min_sync_show(mddev_t *mddev, char *page) |
| 3080 | { |
| 3081 | return sprintf(page, "%llu\n", |
| 3082 | (unsigned long long)mddev->resync_min); |
| 3083 | } |
| 3084 | static ssize_t |
| 3085 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3086 | { |
| 3087 | unsigned long long min; |
| 3088 | if (strict_strtoull(buf, 10, &min)) |
| 3089 | return -EINVAL; |
| 3090 | if (min > mddev->resync_max) |
| 3091 | return -EINVAL; |
| 3092 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3093 | return -EBUSY; |
| 3094 | |
| 3095 | /* Must be a multiple of chunk_size */ |
| 3096 | if (mddev->chunk_size) { |
| 3097 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3098 | return -EINVAL; |
| 3099 | } |
| 3100 | mddev->resync_min = min; |
| 3101 | |
| 3102 | return len; |
| 3103 | } |
| 3104 | |
| 3105 | static struct md_sysfs_entry md_min_sync = |
| 3106 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3107 | |
| 3108 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3109 | max_sync_show(mddev_t *mddev, char *page) |
| 3110 | { |
| 3111 | if (mddev->resync_max == MaxSector) |
| 3112 | return sprintf(page, "max\n"); |
| 3113 | else |
| 3114 | return sprintf(page, "%llu\n", |
| 3115 | (unsigned long long)mddev->resync_max); |
| 3116 | } |
| 3117 | static ssize_t |
| 3118 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3119 | { |
| 3120 | if (strncmp(buf, "max", 3) == 0) |
| 3121 | mddev->resync_max = MaxSector; |
| 3122 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3123 | unsigned long long max; |
| 3124 | if (strict_strtoull(buf, 10, &max)) |
| 3125 | return -EINVAL; |
| 3126 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3127 | return -EINVAL; |
| 3128 | if (max < mddev->resync_max && |
| 3129 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3130 | return -EBUSY; |
| 3131 | |
| 3132 | /* Must be a multiple of chunk_size */ |
| 3133 | if (mddev->chunk_size) { |
| 3134 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3135 | return -EINVAL; |
| 3136 | } |
| 3137 | mddev->resync_max = max; |
| 3138 | } |
| 3139 | wake_up(&mddev->recovery_wait); |
| 3140 | return len; |
| 3141 | } |
| 3142 | |
| 3143 | static struct md_sysfs_entry md_max_sync = |
| 3144 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3145 | |
| 3146 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3147 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3148 | { |
| 3149 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3150 | } |
| 3151 | |
| 3152 | static ssize_t |
| 3153 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3154 | { |
| 3155 | char *e; |
| 3156 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3157 | |
| 3158 | if (mddev->pers->quiesce == NULL) |
| 3159 | return -EINVAL; |
| 3160 | if (buf == e || (*e && *e != '\n')) |
| 3161 | return -EINVAL; |
| 3162 | if (new >= mddev->suspend_hi || |
| 3163 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3164 | mddev->suspend_lo = new; |
| 3165 | mddev->pers->quiesce(mddev, 2); |
| 3166 | return len; |
| 3167 | } else |
| 3168 | return -EINVAL; |
| 3169 | } |
| 3170 | static struct md_sysfs_entry md_suspend_lo = |
| 3171 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3172 | |
| 3173 | |
| 3174 | static ssize_t |
| 3175 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3176 | { |
| 3177 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3178 | } |
| 3179 | |
| 3180 | static ssize_t |
| 3181 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3182 | { |
| 3183 | char *e; |
| 3184 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3185 | |
| 3186 | if (mddev->pers->quiesce == NULL) |
| 3187 | return -EINVAL; |
| 3188 | if (buf == e || (*e && *e != '\n')) |
| 3189 | return -EINVAL; |
| 3190 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3191 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3192 | mddev->suspend_hi = new; |
| 3193 | mddev->pers->quiesce(mddev, 1); |
| 3194 | mddev->pers->quiesce(mddev, 0); |
| 3195 | return len; |
| 3196 | } else |
| 3197 | return -EINVAL; |
| 3198 | } |
| 3199 | static struct md_sysfs_entry md_suspend_hi = |
| 3200 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3201 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3202 | static ssize_t |
| 3203 | reshape_position_show(mddev_t *mddev, char *page) |
| 3204 | { |
| 3205 | if (mddev->reshape_position != MaxSector) |
| 3206 | return sprintf(page, "%llu\n", |
| 3207 | (unsigned long long)mddev->reshape_position); |
| 3208 | strcpy(page, "none\n"); |
| 3209 | return 5; |
| 3210 | } |
| 3211 | |
| 3212 | static ssize_t |
| 3213 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3214 | { |
| 3215 | char *e; |
| 3216 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3217 | if (mddev->pers) |
| 3218 | return -EBUSY; |
| 3219 | if (buf == e || (*e && *e != '\n')) |
| 3220 | return -EINVAL; |
| 3221 | mddev->reshape_position = new; |
| 3222 | mddev->delta_disks = 0; |
| 3223 | mddev->new_level = mddev->level; |
| 3224 | mddev->new_layout = mddev->layout; |
| 3225 | mddev->new_chunk = mddev->chunk_size; |
| 3226 | return len; |
| 3227 | } |
| 3228 | |
| 3229 | static struct md_sysfs_entry md_reshape_position = |
| 3230 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3231 | reshape_position_store); |
| 3232 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3233 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3234 | static struct attribute *md_default_attrs[] = { |
| 3235 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3236 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3237 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3238 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3239 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3240 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3241 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3242 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3243 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3244 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3245 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3246 | NULL, |
| 3247 | }; |
| 3248 | |
| 3249 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3250 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3251 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3252 | &md_sync_min.attr, |
| 3253 | &md_sync_max.attr, |
| 3254 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3255 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3256 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3257 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3258 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3259 | &md_suspend_lo.attr, |
| 3260 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3261 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3262 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3263 | NULL, |
| 3264 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3265 | static struct attribute_group md_redundancy_group = { |
| 3266 | .name = NULL, |
| 3267 | .attrs = md_redundancy_attrs, |
| 3268 | }; |
| 3269 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3270 | |
| 3271 | static ssize_t |
| 3272 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3273 | { |
| 3274 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3275 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3276 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3277 | |
| 3278 | if (!entry->show) |
| 3279 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3280 | rv = mddev_lock(mddev); |
| 3281 | if (!rv) { |
| 3282 | rv = entry->show(mddev, page); |
| 3283 | mddev_unlock(mddev); |
| 3284 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3285 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | static ssize_t |
| 3289 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3290 | const char *page, size_t length) |
| 3291 | { |
| 3292 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3293 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3294 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3295 | |
| 3296 | if (!entry->store) |
| 3297 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3298 | if (!capable(CAP_SYS_ADMIN)) |
| 3299 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3300 | rv = mddev_lock(mddev); |
| 3301 | if (!rv) { |
| 3302 | rv = entry->store(mddev, page, length); |
| 3303 | mddev_unlock(mddev); |
| 3304 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3305 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | static void md_free(struct kobject *ko) |
| 3309 | { |
| 3310 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3311 | kfree(mddev); |
| 3312 | } |
| 3313 | |
| 3314 | static struct sysfs_ops md_sysfs_ops = { |
| 3315 | .show = md_attr_show, |
| 3316 | .store = md_attr_store, |
| 3317 | }; |
| 3318 | static struct kobj_type md_ktype = { |
| 3319 | .release = md_free, |
| 3320 | .sysfs_ops = &md_sysfs_ops, |
| 3321 | .default_attrs = md_default_attrs, |
| 3322 | }; |
| 3323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | int mdp_major = 0; |
| 3325 | |
| 3326 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3327 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3328 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | mddev_t *mddev = mddev_find(dev); |
| 3330 | struct gendisk *disk; |
| 3331 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3332 | int shift = partitioned ? MdpMinorShift : 0; |
| 3333 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3334 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | |
| 3336 | if (!mddev) |
| 3337 | return NULL; |
| 3338 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3339 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3341 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | mddev_put(mddev); |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | disk = alloc_disk(1 << shift); |
| 3346 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3347 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | mddev_put(mddev); |
| 3349 | return NULL; |
| 3350 | } |
| 3351 | disk->major = MAJOR(dev); |
| 3352 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3353 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3355 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | disk->fops = &md_fops; |
| 3358 | disk->private_data = mddev; |
| 3359 | disk->queue = mddev->queue; |
| 3360 | add_disk(disk); |
| 3361 | mddev->gendisk = disk; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3362 | 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] | 3363 | "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame^] | 3364 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3365 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3366 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3367 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3368 | else |
| 3369 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | return NULL; |
| 3371 | } |
| 3372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | static void md_safemode_timeout(unsigned long data) |
| 3374 | { |
| 3375 | mddev_t *mddev = (mddev_t *) data; |
| 3376 | |
| 3377 | mddev->safemode = 1; |
| 3378 | md_wakeup_thread(mddev->thread); |
| 3379 | } |
| 3380 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3381 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | |
| 3383 | static int do_md_run(mddev_t * mddev) |
| 3384 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3385 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | int chunk_size; |
| 3387 | struct list_head *tmp; |
| 3388 | mdk_rdev_t *rdev; |
| 3389 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3390 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | char b[BDEVNAME_SIZE]; |
| 3392 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3393 | if (list_empty(&mddev->disks)) |
| 3394 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | |
| 3397 | if (mddev->pers) |
| 3398 | return -EBUSY; |
| 3399 | |
| 3400 | /* |
| 3401 | * Analyze all RAID superblock(s) |
| 3402 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3403 | if (!mddev->raid_disks) { |
| 3404 | if (!mddev->persistent) |
| 3405 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3406 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
| 3409 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3411 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3413 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3414 | chunk_size, MAX_CHUNK_SIZE); |
| 3415 | return -EINVAL; |
| 3416 | } |
| 3417 | /* |
| 3418 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3419 | */ |
| 3420 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3421 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | return -EINVAL; |
| 3423 | } |
| 3424 | if (chunk_size < PAGE_SIZE) { |
| 3425 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3426 | chunk_size, PAGE_SIZE); |
| 3427 | return -EINVAL; |
| 3428 | } |
| 3429 | |
| 3430 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3431 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3432 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | continue; |
| 3434 | if (rdev->size < chunk_size / 1024) { |
| 3435 | printk(KERN_WARNING |
| 3436 | "md: Dev %s smaller than chunk_size:" |
| 3437 | " %lluk < %dk\n", |
| 3438 | bdevname(rdev->bdev,b), |
| 3439 | (unsigned long long)rdev->size, |
| 3440 | chunk_size / 1024); |
| 3441 | return -EINVAL; |
| 3442 | } |
| 3443 | } |
| 3444 | } |
| 3445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3447 | if (mddev->level != LEVEL_NONE) |
| 3448 | request_module("md-level-%d", mddev->level); |
| 3449 | else if (mddev->clevel[0]) |
| 3450 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | #endif |
| 3452 | |
| 3453 | /* |
| 3454 | * Drop all container device buffers, from now on |
| 3455 | * the only valid external interface is through the md |
| 3456 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3458 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3459 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | continue; |
| 3461 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3462 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3463 | |
| 3464 | /* perform some consistency tests on the device. |
| 3465 | * We don't want the data to overlap the metadata, |
| 3466 | * Internal Bitmap issues has handled elsewhere. |
| 3467 | */ |
| 3468 | if (rdev->data_offset < rdev->sb_offset) { |
| 3469 | if (mddev->size && |
| 3470 | rdev->data_offset + mddev->size*2 |
| 3471 | > rdev->sb_offset*2) { |
| 3472 | printk("md: %s: data overlaps metadata\n", |
| 3473 | mdname(mddev)); |
| 3474 | return -EINVAL; |
| 3475 | } |
| 3476 | } else { |
| 3477 | if (rdev->sb_offset*2 + rdev->sb_size/512 |
| 3478 | > rdev->data_offset) { |
| 3479 | printk("md: %s: metadata overlaps data\n", |
| 3480 | mdname(mddev)); |
| 3481 | return -EINVAL; |
| 3482 | } |
| 3483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | md_probe(mddev->unit, NULL, NULL); |
| 3487 | disk = mddev->gendisk; |
| 3488 | if (!disk) |
| 3489 | return -ENOMEM; |
| 3490 | |
| 3491 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3492 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3493 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3495 | if (mddev->level != LEVEL_NONE) |
| 3496 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3497 | mddev->level); |
| 3498 | else |
| 3499 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3500 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | return -EINVAL; |
| 3502 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3503 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3505 | mddev->level = pers->level; |
| 3506 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3508 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3509 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3510 | /* This personality cannot handle reshaping... */ |
| 3511 | mddev->pers = NULL; |
| 3512 | module_put(pers->owner); |
| 3513 | return -EINVAL; |
| 3514 | } |
| 3515 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3516 | if (pers->sync_request) { |
| 3517 | /* Warn if this is a potentially silly |
| 3518 | * configuration. |
| 3519 | */ |
| 3520 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3521 | mdk_rdev_t *rdev2; |
| 3522 | struct list_head *tmp2; |
| 3523 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3524 | rdev_for_each(rdev, tmp, mddev) { |
| 3525 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3526 | if (rdev < rdev2 && |
| 3527 | rdev->bdev->bd_contains == |
| 3528 | rdev2->bdev->bd_contains) { |
| 3529 | printk(KERN_WARNING |
| 3530 | "%s: WARNING: %s appears to be" |
| 3531 | " on the same physical disk as" |
| 3532 | " %s.\n", |
| 3533 | mdname(mddev), |
| 3534 | bdevname(rdev->bdev,b), |
| 3535 | bdevname(rdev2->bdev,b2)); |
| 3536 | warned = 1; |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | if (warned) |
| 3541 | printk(KERN_WARNING |
| 3542 | "True protection against single-disk" |
| 3543 | " failure might be compromised.\n"); |
| 3544 | } |
| 3545 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3546 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3548 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3549 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3551 | if (start_readonly) |
| 3552 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3553 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3554 | err = mddev->pers->run(mddev); |
| 3555 | if (!err && mddev->pers->sync_request) { |
| 3556 | err = bitmap_create(mddev); |
| 3557 | if (err) { |
| 3558 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3559 | mdname(mddev), err); |
| 3560 | mddev->pers->stop(mddev); |
| 3561 | } |
| 3562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | if (err) { |
| 3564 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3565 | module_put(mddev->pers->owner); |
| 3566 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3567 | bitmap_destroy(mddev); |
| 3568 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3570 | if (mddev->pers->sync_request) { |
| 3571 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3572 | printk(KERN_WARNING |
| 3573 | "md: cannot register extra attributes for %s\n", |
| 3574 | mdname(mddev)); |
| 3575 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3576 | mddev->ro = 0; |
| 3577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | atomic_set(&mddev->writes_pending,0); |
| 3579 | mddev->safemode = 0; |
| 3580 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3581 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3582 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3584 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3585 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3586 | if (rdev->raid_disk >= 0) { |
| 3587 | char nm[20]; |
| 3588 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3589 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3590 | printk("md: cannot register %s for %s\n", |
| 3591 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | |
| 3594 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3595 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3596 | if (mddev->flags) |
| 3597 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | |
| 3599 | set_capacity(disk, mddev->array_size<<1); |
| 3600 | |
| 3601 | /* If we call blk_queue_make_request here, it will |
| 3602 | * re-initialise max_sectors etc which may have been |
| 3603 | * refined inside -> run. So just set the bits we need to set. |
| 3604 | * Most initialisation happended when we called |
| 3605 | * blk_queue_make_request(..., md_fail_request) |
| 3606 | * earlier. |
| 3607 | */ |
| 3608 | mddev->queue->queuedata = mddev; |
| 3609 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3610 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3611 | /* If there is a partially-recovered drive we need to |
| 3612 | * start recovery here. If we leave it to md_check_recovery, |
| 3613 | * it will remove the drives and not do the right thing |
| 3614 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3615 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3616 | struct list_head *rtmp; |
| 3617 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3618 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3619 | if (rdev->raid_disk >= 0 && |
| 3620 | !test_bit(In_sync, &rdev->flags) && |
| 3621 | !test_bit(Faulty, &rdev->flags)) |
| 3622 | /* complete an interrupted recovery */ |
| 3623 | spares++; |
| 3624 | if (spares && mddev->pers->sync_request) { |
| 3625 | mddev->recovery = 0; |
| 3626 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3627 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3628 | mddev, |
| 3629 | "%s_resync"); |
| 3630 | if (!mddev->sync_thread) { |
| 3631 | printk(KERN_ERR "%s: could not start resync" |
| 3632 | " thread...\n", |
| 3633 | mdname(mddev)); |
| 3634 | /* leave the spares where they are, it shouldn't hurt */ |
| 3635 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3636 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3637 | } |
| 3638 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3639 | md_wakeup_thread(mddev->thread); |
| 3640 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3641 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3642 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3643 | md_new_event(mddev); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3644 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | return 0; |
| 3646 | } |
| 3647 | |
| 3648 | static int restart_array(mddev_t *mddev) |
| 3649 | { |
| 3650 | struct gendisk *disk = mddev->gendisk; |
| 3651 | int err; |
| 3652 | |
| 3653 | /* |
| 3654 | * Complain if it has no devices |
| 3655 | */ |
| 3656 | err = -ENXIO; |
| 3657 | if (list_empty(&mddev->disks)) |
| 3658 | goto out; |
| 3659 | |
| 3660 | if (mddev->pers) { |
| 3661 | err = -EBUSY; |
| 3662 | if (!mddev->ro) |
| 3663 | goto out; |
| 3664 | |
| 3665 | mddev->safemode = 0; |
| 3666 | mddev->ro = 0; |
| 3667 | set_disk_ro(disk, 0); |
| 3668 | |
| 3669 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3670 | mdname(mddev)); |
| 3671 | /* |
| 3672 | * Kick recovery or resync if necessary |
| 3673 | */ |
| 3674 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3675 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3676 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3678 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | |
| 3681 | out: |
| 3682 | return err; |
| 3683 | } |
| 3684 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3685 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3686 | * to the file ourselves */ |
| 3687 | static int deny_bitmap_write_access(struct file * file) |
| 3688 | { |
| 3689 | struct inode *inode = file->f_mapping->host; |
| 3690 | |
| 3691 | spin_lock(&inode->i_lock); |
| 3692 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3693 | spin_unlock(&inode->i_lock); |
| 3694 | return -ETXTBSY; |
| 3695 | } |
| 3696 | atomic_set(&inode->i_writecount, -1); |
| 3697 | spin_unlock(&inode->i_lock); |
| 3698 | |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
| 3702 | static void restore_bitmap_write_access(struct file *file) |
| 3703 | { |
| 3704 | struct inode *inode = file->f_mapping->host; |
| 3705 | |
| 3706 | spin_lock(&inode->i_lock); |
| 3707 | atomic_set(&inode->i_writecount, 1); |
| 3708 | spin_unlock(&inode->i_lock); |
| 3709 | } |
| 3710 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3711 | /* mode: |
| 3712 | * 0 - completely stop and dis-assemble array |
| 3713 | * 1 - switch to readonly |
| 3714 | * 2 - stop but do not disassemble array |
| 3715 | */ |
| 3716 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | { |
| 3718 | int err = 0; |
| 3719 | struct gendisk *disk = mddev->gendisk; |
| 3720 | |
| 3721 | if (mddev->pers) { |
| 3722 | if (atomic_read(&mddev->active)>2) { |
| 3723 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3724 | return -EBUSY; |
| 3725 | } |
| 3726 | |
| 3727 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3728 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3730 | md_unregister_thread(mddev->sync_thread); |
| 3731 | mddev->sync_thread = NULL; |
| 3732 | } |
| 3733 | |
| 3734 | del_timer_sync(&mddev->safemode_timer); |
| 3735 | |
| 3736 | invalidate_partition(disk, 0); |
| 3737 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3738 | switch(mode) { |
| 3739 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3741 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | goto out; |
| 3743 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3744 | break; |
| 3745 | case 0: /* disassemble */ |
| 3746 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3747 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3748 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | if (mddev->ro) |
| 3750 | set_disk_ro(disk, 0); |
| 3751 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3752 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3753 | mddev->queue->merge_bvec_fn = NULL; |
| 3754 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3755 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3756 | if (mddev->pers->sync_request) |
| 3757 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | module_put(mddev->pers->owner); |
| 3760 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3761 | /* tell userspace to handle 'inactive' */ |
| 3762 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3763 | |
| 3764 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3765 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | if (mddev->ro) |
| 3768 | mddev->ro = 0; |
| 3769 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3770 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | /* mark array as shutdown cleanly */ |
| 3772 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3773 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3777 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | /* |
| 3781 | * Free resources if final stop |
| 3782 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3783 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3784 | mdk_rdev_t *rdev; |
| 3785 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3788 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3789 | bitmap_destroy(mddev); |
| 3790 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3791 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3792 | fput(mddev->bitmap_file); |
| 3793 | mddev->bitmap_file = NULL; |
| 3794 | } |
| 3795 | mddev->bitmap_offset = 0; |
| 3796 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3797 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3798 | if (rdev->raid_disk >= 0) { |
| 3799 | char nm[20]; |
| 3800 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3801 | sysfs_remove_link(&mddev->kobj, nm); |
| 3802 | } |
| 3803 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3804 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3805 | flush_scheduled_work(); |
| 3806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | export_array(mddev); |
| 3808 | |
| 3809 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3810 | mddev->size = 0; |
| 3811 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3812 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3813 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3814 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3815 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3816 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3817 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3818 | mddev->level = LEVEL_NONE; |
| 3819 | mddev->clevel[0] = 0; |
| 3820 | mddev->flags = 0; |
| 3821 | mddev->ro = 0; |
| 3822 | mddev->metadata_type[0] = 0; |
| 3823 | mddev->chunk_size = 0; |
| 3824 | mddev->ctime = mddev->utime = 0; |
| 3825 | mddev->layout = 0; |
| 3826 | mddev->max_disks = 0; |
| 3827 | mddev->events = 0; |
| 3828 | mddev->delta_disks = 0; |
| 3829 | mddev->new_level = LEVEL_NONE; |
| 3830 | mddev->new_layout = 0; |
| 3831 | mddev->new_chunk = 0; |
| 3832 | mddev->curr_resync = 0; |
| 3833 | mddev->resync_mismatches = 0; |
| 3834 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3835 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3836 | mddev->recovery = 0; |
| 3837 | mddev->in_sync = 0; |
| 3838 | mddev->changed = 0; |
| 3839 | mddev->degraded = 0; |
| 3840 | mddev->barriers_work = 0; |
| 3841 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3842 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3843 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3845 | mdname(mddev)); |
| 3846 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3847 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | out: |
| 3849 | return err; |
| 3850 | } |
| 3851 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3852 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | static void autorun_array(mddev_t *mddev) |
| 3854 | { |
| 3855 | mdk_rdev_t *rdev; |
| 3856 | struct list_head *tmp; |
| 3857 | int err; |
| 3858 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3859 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | |
| 3862 | printk(KERN_INFO "md: running: "); |
| 3863 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3864 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | char b[BDEVNAME_SIZE]; |
| 3866 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3867 | } |
| 3868 | printk("\n"); |
| 3869 | |
| 3870 | err = do_md_run (mddev); |
| 3871 | if (err) { |
| 3872 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3873 | do_md_stop (mddev, 0); |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | /* |
| 3878 | * lets try to run arrays based on all disks that have arrived |
| 3879 | * until now. (those are in pending_raid_disks) |
| 3880 | * |
| 3881 | * the method: pick the first pending disk, collect all disks with |
| 3882 | * the same UUID, remove all from the pending list and put them into |
| 3883 | * the 'same_array' list. Then order this list based on superblock |
| 3884 | * update time (freshest comes first), kick out 'old' disks and |
| 3885 | * compare superblocks. If everything's fine then run it. |
| 3886 | * |
| 3887 | * If "unit" is allocated, then bump its reference count |
| 3888 | */ |
| 3889 | static void autorun_devices(int part) |
| 3890 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | struct list_head *tmp; |
| 3892 | mdk_rdev_t *rdev0, *rdev; |
| 3893 | mddev_t *mddev; |
| 3894 | char b[BDEVNAME_SIZE]; |
| 3895 | |
| 3896 | printk(KERN_INFO "md: autorun ...\n"); |
| 3897 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3898 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3900 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | rdev0 = list_entry(pending_raid_disks.next, |
| 3902 | mdk_rdev_t, same_set); |
| 3903 | |
| 3904 | printk(KERN_INFO "md: considering %s ...\n", |
| 3905 | bdevname(rdev0->bdev,b)); |
| 3906 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3907 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3909 | printk(KERN_INFO "md: adding %s ...\n", |
| 3910 | bdevname(rdev->bdev,b)); |
| 3911 | list_move(&rdev->same_set, &candidates); |
| 3912 | } |
| 3913 | /* |
| 3914 | * now we have a set of devices, with all of them having |
| 3915 | * mostly sane superblocks. It's time to allocate the |
| 3916 | * mddev. |
| 3917 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3918 | if (part) { |
| 3919 | dev = MKDEV(mdp_major, |
| 3920 | rdev0->preferred_minor << MdpMinorShift); |
| 3921 | unit = MINOR(dev) >> MdpMinorShift; |
| 3922 | } else { |
| 3923 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3924 | unit = MINOR(dev); |
| 3925 | } |
| 3926 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3928 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3929 | break; |
| 3930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | |
| 3932 | md_probe(dev, NULL, NULL); |
| 3933 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 3934 | if (!mddev || !mddev->gendisk) { |
| 3935 | if (mddev) |
| 3936 | mddev_put(mddev); |
| 3937 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | "md: cannot allocate memory for md drive.\n"); |
| 3939 | break; |
| 3940 | } |
| 3941 | if (mddev_lock(mddev)) |
| 3942 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3943 | mdname(mddev)); |
| 3944 | else if (mddev->raid_disks || mddev->major_version |
| 3945 | || !list_empty(&mddev->disks)) { |
| 3946 | printk(KERN_WARNING |
| 3947 | "md: %s already running, cannot run %s\n", |
| 3948 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 3949 | mddev_unlock(mddev); |
| 3950 | } else { |
| 3951 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3952 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3953 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | list_del_init(&rdev->same_set); |
| 3955 | if (bind_rdev_to_array(rdev, mddev)) |
| 3956 | export_rdev(rdev); |
| 3957 | } |
| 3958 | autorun_array(mddev); |
| 3959 | mddev_unlock(mddev); |
| 3960 | } |
| 3961 | /* on success, candidates will be empty, on error |
| 3962 | * it won't... |
| 3963 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3964 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | export_rdev(rdev); |
| 3966 | mddev_put(mddev); |
| 3967 | } |
| 3968 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 3969 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3970 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | static int get_version(void __user * arg) |
| 3973 | { |
| 3974 | mdu_version_t ver; |
| 3975 | |
| 3976 | ver.major = MD_MAJOR_VERSION; |
| 3977 | ver.minor = MD_MINOR_VERSION; |
| 3978 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 3979 | |
| 3980 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 3981 | return -EFAULT; |
| 3982 | |
| 3983 | return 0; |
| 3984 | } |
| 3985 | |
| 3986 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 3987 | { |
| 3988 | mdu_array_info_t info; |
| 3989 | int nr,working,active,failed,spare; |
| 3990 | mdk_rdev_t *rdev; |
| 3991 | struct list_head *tmp; |
| 3992 | |
| 3993 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3994 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3996 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | failed++; |
| 3998 | else { |
| 3999 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4000 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | active++; |
| 4002 | else |
| 4003 | spare++; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | info.major_version = mddev->major_version; |
| 4008 | info.minor_version = mddev->minor_version; |
| 4009 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4010 | info.ctime = mddev->ctime; |
| 4011 | info.level = mddev->level; |
| 4012 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4013 | if (info.size != mddev->size) /* overflow */ |
| 4014 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | info.nr_disks = nr; |
| 4016 | info.raid_disks = mddev->raid_disks; |
| 4017 | info.md_minor = mddev->md_minor; |
| 4018 | info.not_persistent= !mddev->persistent; |
| 4019 | |
| 4020 | info.utime = mddev->utime; |
| 4021 | info.state = 0; |
| 4022 | if (mddev->in_sync) |
| 4023 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4024 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4025 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | info.active_disks = active; |
| 4027 | info.working_disks = working; |
| 4028 | info.failed_disks = failed; |
| 4029 | info.spare_disks = spare; |
| 4030 | |
| 4031 | info.layout = mddev->layout; |
| 4032 | info.chunk_size = mddev->chunk_size; |
| 4033 | |
| 4034 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4035 | return -EFAULT; |
| 4036 | |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4040 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4041 | { |
| 4042 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4043 | char *ptr, *buf = NULL; |
| 4044 | int err = -ENOMEM; |
| 4045 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4046 | md_allow_write(mddev); |
| 4047 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4048 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 4049 | if (!file) |
| 4050 | goto out; |
| 4051 | |
| 4052 | /* bitmap disabled, zero the first byte and copy out */ |
| 4053 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4054 | file->pathname[0] = '\0'; |
| 4055 | goto copy_out; |
| 4056 | } |
| 4057 | |
| 4058 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4059 | if (!buf) |
| 4060 | goto out; |
| 4061 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4062 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4063 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4064 | goto out; |
| 4065 | |
| 4066 | strcpy(file->pathname, ptr); |
| 4067 | |
| 4068 | copy_out: |
| 4069 | err = 0; |
| 4070 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4071 | err = -EFAULT; |
| 4072 | out: |
| 4073 | kfree(buf); |
| 4074 | kfree(file); |
| 4075 | return err; |
| 4076 | } |
| 4077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4079 | { |
| 4080 | mdu_disk_info_t info; |
| 4081 | unsigned int nr; |
| 4082 | mdk_rdev_t *rdev; |
| 4083 | |
| 4084 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4085 | return -EFAULT; |
| 4086 | |
| 4087 | nr = info.number; |
| 4088 | |
| 4089 | rdev = find_rdev_nr(mddev, nr); |
| 4090 | if (rdev) { |
| 4091 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4092 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4093 | info.raid_disk = rdev->raid_disk; |
| 4094 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4095 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4097 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4099 | info.state |= (1<<MD_DISK_SYNC); |
| 4100 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4101 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4102 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | } else { |
| 4104 | info.major = info.minor = 0; |
| 4105 | info.raid_disk = -1; |
| 4106 | info.state = (1<<MD_DISK_REMOVED); |
| 4107 | } |
| 4108 | |
| 4109 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4110 | return -EFAULT; |
| 4111 | |
| 4112 | return 0; |
| 4113 | } |
| 4114 | |
| 4115 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4116 | { |
| 4117 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4118 | mdk_rdev_t *rdev; |
| 4119 | dev_t dev = MKDEV(info->major,info->minor); |
| 4120 | |
| 4121 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4122 | return -EOVERFLOW; |
| 4123 | |
| 4124 | if (!mddev->raid_disks) { |
| 4125 | int err; |
| 4126 | /* expecting a device which has a superblock */ |
| 4127 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4128 | if (IS_ERR(rdev)) { |
| 4129 | printk(KERN_WARNING |
| 4130 | "md: md_import_device returned %ld\n", |
| 4131 | PTR_ERR(rdev)); |
| 4132 | return PTR_ERR(rdev); |
| 4133 | } |
| 4134 | if (!list_empty(&mddev->disks)) { |
| 4135 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4136 | mdk_rdev_t, same_set); |
| 4137 | int err = super_types[mddev->major_version] |
| 4138 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4139 | if (err < 0) { |
| 4140 | printk(KERN_WARNING |
| 4141 | "md: %s has different UUID to %s\n", |
| 4142 | bdevname(rdev->bdev,b), |
| 4143 | bdevname(rdev0->bdev,b2)); |
| 4144 | export_rdev(rdev); |
| 4145 | return -EINVAL; |
| 4146 | } |
| 4147 | } |
| 4148 | err = bind_rdev_to_array(rdev, mddev); |
| 4149 | if (err) |
| 4150 | export_rdev(rdev); |
| 4151 | return err; |
| 4152 | } |
| 4153 | |
| 4154 | /* |
| 4155 | * add_new_disk can be used once the array is assembled |
| 4156 | * to add "hot spares". They must already have a superblock |
| 4157 | * written |
| 4158 | */ |
| 4159 | if (mddev->pers) { |
| 4160 | int err; |
| 4161 | if (!mddev->pers->hot_add_disk) { |
| 4162 | printk(KERN_WARNING |
| 4163 | "%s: personality does not support diskops!\n", |
| 4164 | mdname(mddev)); |
| 4165 | return -EINVAL; |
| 4166 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4167 | if (mddev->persistent) |
| 4168 | rdev = md_import_device(dev, mddev->major_version, |
| 4169 | mddev->minor_version); |
| 4170 | else |
| 4171 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | if (IS_ERR(rdev)) { |
| 4173 | printk(KERN_WARNING |
| 4174 | "md: md_import_device returned %ld\n", |
| 4175 | PTR_ERR(rdev)); |
| 4176 | return PTR_ERR(rdev); |
| 4177 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4178 | /* set save_raid_disk if appropriate */ |
| 4179 | if (!mddev->persistent) { |
| 4180 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4181 | info->raid_disk < mddev->raid_disks) |
| 4182 | rdev->raid_disk = info->raid_disk; |
| 4183 | else |
| 4184 | rdev->raid_disk = -1; |
| 4185 | } else |
| 4186 | super_types[mddev->major_version]. |
| 4187 | validate_super(mddev, rdev); |
| 4188 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4189 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4190 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4191 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4192 | set_bit(WriteMostly, &rdev->flags); |
| 4193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | rdev->raid_disk = -1; |
| 4195 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4196 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4197 | /* If there is hot_add_disk but no hot_remove_disk |
| 4198 | * then added disks for geometry changes, |
| 4199 | * and should be added immediately. |
| 4200 | */ |
| 4201 | super_types[mddev->major_version]. |
| 4202 | validate_super(mddev, rdev); |
| 4203 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4204 | if (err) |
| 4205 | unbind_rdev_from_array(rdev); |
| 4206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | if (err) |
| 4208 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4209 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4210 | md_update_sb(mddev, 1); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4211 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4212 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | return err; |
| 4214 | } |
| 4215 | |
| 4216 | /* otherwise, add_new_disk is only allowed |
| 4217 | * for major_version==0 superblocks |
| 4218 | */ |
| 4219 | if (mddev->major_version != 0) { |
| 4220 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4221 | mdname(mddev)); |
| 4222 | return -EINVAL; |
| 4223 | } |
| 4224 | |
| 4225 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4226 | int err; |
| 4227 | rdev = md_import_device (dev, -1, 0); |
| 4228 | if (IS_ERR(rdev)) { |
| 4229 | printk(KERN_WARNING |
| 4230 | "md: error, md_import_device() returned %ld\n", |
| 4231 | PTR_ERR(rdev)); |
| 4232 | return PTR_ERR(rdev); |
| 4233 | } |
| 4234 | rdev->desc_nr = info->number; |
| 4235 | if (info->raid_disk < mddev->raid_disks) |
| 4236 | rdev->raid_disk = info->raid_disk; |
| 4237 | else |
| 4238 | rdev->raid_disk = -1; |
| 4239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4241 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4242 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4244 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4245 | set_bit(WriteMostly, &rdev->flags); |
| 4246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | if (!mddev->persistent) { |
| 4248 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4249 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4250 | } else |
| 4251 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4252 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4253 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4254 | err = bind_rdev_to_array(rdev, mddev); |
| 4255 | if (err) { |
| 4256 | export_rdev(rdev); |
| 4257 | return err; |
| 4258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | return 0; |
| 4262 | } |
| 4263 | |
| 4264 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4265 | { |
| 4266 | char b[BDEVNAME_SIZE]; |
| 4267 | mdk_rdev_t *rdev; |
| 4268 | |
| 4269 | if (!mddev->pers) |
| 4270 | return -ENODEV; |
| 4271 | |
| 4272 | rdev = find_rdev(mddev, dev); |
| 4273 | if (!rdev) |
| 4274 | return -ENXIO; |
| 4275 | |
| 4276 | if (rdev->raid_disk >= 0) |
| 4277 | goto busy; |
| 4278 | |
| 4279 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4280 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4281 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
| 4283 | return 0; |
| 4284 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4285 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4287 | return -EBUSY; |
| 4288 | } |
| 4289 | |
| 4290 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4291 | { |
| 4292 | char b[BDEVNAME_SIZE]; |
| 4293 | int err; |
| 4294 | unsigned int size; |
| 4295 | mdk_rdev_t *rdev; |
| 4296 | |
| 4297 | if (!mddev->pers) |
| 4298 | return -ENODEV; |
| 4299 | |
| 4300 | if (mddev->major_version != 0) { |
| 4301 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4302 | " version-0 superblocks.\n", |
| 4303 | mdname(mddev)); |
| 4304 | return -EINVAL; |
| 4305 | } |
| 4306 | if (!mddev->pers->hot_add_disk) { |
| 4307 | printk(KERN_WARNING |
| 4308 | "%s: personality does not support diskops!\n", |
| 4309 | mdname(mddev)); |
| 4310 | return -EINVAL; |
| 4311 | } |
| 4312 | |
| 4313 | rdev = md_import_device (dev, -1, 0); |
| 4314 | if (IS_ERR(rdev)) { |
| 4315 | printk(KERN_WARNING |
| 4316 | "md: error, md_import_device() returned %ld\n", |
| 4317 | PTR_ERR(rdev)); |
| 4318 | return -EINVAL; |
| 4319 | } |
| 4320 | |
| 4321 | if (mddev->persistent) |
| 4322 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4323 | else |
| 4324 | rdev->sb_offset = |
| 4325 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4326 | |
| 4327 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4328 | rdev->size = size; |
| 4329 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4330 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | printk(KERN_WARNING |
| 4332 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4333 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4334 | err = -EINVAL; |
| 4335 | goto abort_export; |
| 4336 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4337 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4339 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4340 | err = bind_rdev_to_array(rdev, mddev); |
| 4341 | if (err) |
| 4342 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
| 4344 | /* |
| 4345 | * The rest should better be atomic, we can have disk failures |
| 4346 | * noticed in interrupt contexts ... |
| 4347 | */ |
| 4348 | |
| 4349 | if (rdev->desc_nr == mddev->max_disks) { |
| 4350 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4351 | mdname(mddev)); |
| 4352 | err = -EBUSY; |
| 4353 | goto abort_unbind_export; |
| 4354 | } |
| 4355 | |
| 4356 | rdev->raid_disk = -1; |
| 4357 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4358 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | |
| 4360 | /* |
| 4361 | * Kick recovery, maybe this spare has to be added to the |
| 4362 | * array immediately. |
| 4363 | */ |
| 4364 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4365 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4366 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | return 0; |
| 4368 | |
| 4369 | abort_unbind_export: |
| 4370 | unbind_rdev_from_array(rdev); |
| 4371 | |
| 4372 | abort_export: |
| 4373 | export_rdev(rdev); |
| 4374 | return err; |
| 4375 | } |
| 4376 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4377 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4378 | { |
| 4379 | int err; |
| 4380 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4381 | if (mddev->pers) { |
| 4382 | if (!mddev->pers->quiesce) |
| 4383 | return -EBUSY; |
| 4384 | if (mddev->recovery || mddev->sync_thread) |
| 4385 | return -EBUSY; |
| 4386 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4389 | |
| 4390 | if (fd >= 0) { |
| 4391 | if (mddev->bitmap) |
| 4392 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4393 | mddev->bitmap_file = fget(fd); |
| 4394 | |
| 4395 | if (mddev->bitmap_file == NULL) { |
| 4396 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4397 | mdname(mddev)); |
| 4398 | return -EBADF; |
| 4399 | } |
| 4400 | |
| 4401 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4402 | if (err) { |
| 4403 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4404 | mdname(mddev)); |
| 4405 | fput(mddev->bitmap_file); |
| 4406 | mddev->bitmap_file = NULL; |
| 4407 | return err; |
| 4408 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4409 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4410 | } else if (mddev->bitmap == NULL) |
| 4411 | return -ENOENT; /* cannot remove what isn't there */ |
| 4412 | err = 0; |
| 4413 | if (mddev->pers) { |
| 4414 | mddev->pers->quiesce(mddev, 1); |
| 4415 | if (fd >= 0) |
| 4416 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4417 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4418 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4419 | fd = -1; /* make sure to put the file */ |
| 4420 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4421 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4422 | } |
| 4423 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4424 | if (mddev->bitmap_file) { |
| 4425 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4426 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4427 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4428 | mddev->bitmap_file = NULL; |
| 4429 | } |
| 4430 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4431 | return err; |
| 4432 | } |
| 4433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | /* |
| 4435 | * set_array_info is used two different ways |
| 4436 | * The original usage is when creating a new array. |
| 4437 | * In this usage, raid_disks is > 0 and it together with |
| 4438 | * level, size, not_persistent,layout,chunksize determine the |
| 4439 | * shape of the array. |
| 4440 | * This will always create an array with a type-0.90.0 superblock. |
| 4441 | * The newer usage is when assembling an array. |
| 4442 | * In this case raid_disks will be 0, and the major_version field is |
| 4443 | * use to determine which style super-blocks are to be found on the devices. |
| 4444 | * The minor and patch _version numbers are also kept incase the |
| 4445 | * super_block handler wishes to interpret them. |
| 4446 | */ |
| 4447 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4448 | { |
| 4449 | |
| 4450 | if (info->raid_disks == 0) { |
| 4451 | /* just setting version number for superblock loading */ |
| 4452 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4453 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | super_types[info->major_version].name == NULL) { |
| 4455 | /* maybe try to auto-load a module? */ |
| 4456 | printk(KERN_INFO |
| 4457 | "md: superblock version %d not known\n", |
| 4458 | info->major_version); |
| 4459 | return -EINVAL; |
| 4460 | } |
| 4461 | mddev->major_version = info->major_version; |
| 4462 | mddev->minor_version = info->minor_version; |
| 4463 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4464 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | return 0; |
| 4466 | } |
| 4467 | mddev->major_version = MD_MAJOR_VERSION; |
| 4468 | mddev->minor_version = MD_MINOR_VERSION; |
| 4469 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4470 | mddev->ctime = get_seconds(); |
| 4471 | |
| 4472 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4473 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | mddev->size = info->size; |
| 4475 | mddev->raid_disks = info->raid_disks; |
| 4476 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4477 | * openned |
| 4478 | */ |
| 4479 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4480 | mddev->recovery_cp = MaxSector; |
| 4481 | else |
| 4482 | mddev->recovery_cp = 0; |
| 4483 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4484 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | |
| 4486 | mddev->layout = info->layout; |
| 4487 | mddev->chunk_size = info->chunk_size; |
| 4488 | |
| 4489 | mddev->max_disks = MD_SB_DISKS; |
| 4490 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4491 | if (mddev->persistent) |
| 4492 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4493 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4495 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4496 | mddev->bitmap_offset = 0; |
| 4497 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4498 | mddev->reshape_position = MaxSector; |
| 4499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | /* |
| 4501 | * Generate a 128 bit UUID |
| 4502 | */ |
| 4503 | get_random_bytes(mddev->uuid, 16); |
| 4504 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4505 | mddev->new_level = mddev->level; |
| 4506 | mddev->new_chunk = mddev->chunk_size; |
| 4507 | mddev->new_layout = mddev->layout; |
| 4508 | mddev->delta_disks = 0; |
| 4509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | return 0; |
| 4511 | } |
| 4512 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4513 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4514 | { |
| 4515 | mdk_rdev_t * rdev; |
| 4516 | int rv; |
| 4517 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4518 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4519 | |
| 4520 | if (mddev->pers->resize == NULL) |
| 4521 | return -EINVAL; |
| 4522 | /* The "size" is the amount of each device that is used. |
| 4523 | * This can only make sense for arrays with redundancy. |
| 4524 | * linear and raid0 always use whatever space is available |
| 4525 | * We can only consider changing the size if no resync |
| 4526 | * or reconstruction is happening, and if the new size |
| 4527 | * is acceptable. It must fit before the sb_offset or, |
| 4528 | * if that is <data_offset, it must fit before the |
| 4529 | * size of each device. |
| 4530 | * If size is zero, we find the largest size that fits. |
| 4531 | */ |
| 4532 | if (mddev->sync_thread) |
| 4533 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4534 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4535 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4536 | avail = rdev->size * 2; |
| 4537 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4538 | if (fit && (size == 0 || size > avail/2)) |
| 4539 | size = avail/2; |
| 4540 | if (avail < ((sector_t)size << 1)) |
| 4541 | return -ENOSPC; |
| 4542 | } |
| 4543 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4544 | if (!rv) { |
| 4545 | struct block_device *bdev; |
| 4546 | |
| 4547 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4548 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4549 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4550 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4551 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4552 | bdput(bdev); |
| 4553 | } |
| 4554 | } |
| 4555 | return rv; |
| 4556 | } |
| 4557 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4558 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4559 | { |
| 4560 | int rv; |
| 4561 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4562 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4563 | return -EINVAL; |
| 4564 | if (raid_disks <= 0 || |
| 4565 | raid_disks >= mddev->max_disks) |
| 4566 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4567 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4568 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4569 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4570 | |
| 4571 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4572 | return rv; |
| 4573 | } |
| 4574 | |
| 4575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | /* |
| 4577 | * update_array_info is used to change the configuration of an |
| 4578 | * on-line array. |
| 4579 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4580 | * fields in the info are checked against the array. |
| 4581 | * Any differences that cannot be handled will cause an error. |
| 4582 | * Normally, only one change can be managed at a time. |
| 4583 | */ |
| 4584 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4585 | { |
| 4586 | int rv = 0; |
| 4587 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4588 | int state = 0; |
| 4589 | |
| 4590 | /* calculate expected state,ignoring low bits */ |
| 4591 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4592 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
| 4594 | if (mddev->major_version != info->major_version || |
| 4595 | mddev->minor_version != info->minor_version || |
| 4596 | /* mddev->patch_version != info->patch_version || */ |
| 4597 | mddev->ctime != info->ctime || |
| 4598 | mddev->level != info->level || |
| 4599 | /* mddev->layout != info->layout || */ |
| 4600 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4601 | mddev->chunk_size != info->chunk_size || |
| 4602 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4603 | ((state^info->state) & 0xfffffe00) |
| 4604 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | return -EINVAL; |
| 4606 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4607 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4609 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4610 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | if (cnt == 0) return 0; |
| 4612 | if (cnt > 1) return -EINVAL; |
| 4613 | |
| 4614 | if (mddev->layout != info->layout) { |
| 4615 | /* Change layout |
| 4616 | * we don't need to do anything at the md level, the |
| 4617 | * personality will take care of it all. |
| 4618 | */ |
| 4619 | if (mddev->pers->reconfig == NULL) |
| 4620 | return -EINVAL; |
| 4621 | else |
| 4622 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4623 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4624 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4625 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4627 | if (mddev->raid_disks != info->raid_disks) |
| 4628 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4630 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4631 | if (mddev->pers->quiesce == NULL) |
| 4632 | return -EINVAL; |
| 4633 | if (mddev->recovery || mddev->sync_thread) |
| 4634 | return -EBUSY; |
| 4635 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4636 | /* add the bitmap */ |
| 4637 | if (mddev->bitmap) |
| 4638 | return -EEXIST; |
| 4639 | if (mddev->default_bitmap_offset == 0) |
| 4640 | return -EINVAL; |
| 4641 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4642 | mddev->pers->quiesce(mddev, 1); |
| 4643 | rv = bitmap_create(mddev); |
| 4644 | if (rv) |
| 4645 | bitmap_destroy(mddev); |
| 4646 | mddev->pers->quiesce(mddev, 0); |
| 4647 | } else { |
| 4648 | /* remove the bitmap */ |
| 4649 | if (!mddev->bitmap) |
| 4650 | return -ENOENT; |
| 4651 | if (mddev->bitmap->file) |
| 4652 | return -EINVAL; |
| 4653 | mddev->pers->quiesce(mddev, 1); |
| 4654 | bitmap_destroy(mddev); |
| 4655 | mddev->pers->quiesce(mddev, 0); |
| 4656 | mddev->bitmap_offset = 0; |
| 4657 | } |
| 4658 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4659 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | return rv; |
| 4661 | } |
| 4662 | |
| 4663 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4664 | { |
| 4665 | mdk_rdev_t *rdev; |
| 4666 | |
| 4667 | if (mddev->pers == NULL) |
| 4668 | return -ENODEV; |
| 4669 | |
| 4670 | rdev = find_rdev(mddev, dev); |
| 4671 | if (!rdev) |
| 4672 | return -ENODEV; |
| 4673 | |
| 4674 | md_error(mddev, rdev); |
| 4675 | return 0; |
| 4676 | } |
| 4677 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4678 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4679 | { |
| 4680 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4681 | |
| 4682 | geo->heads = 2; |
| 4683 | geo->sectors = 4; |
| 4684 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4685 | return 0; |
| 4686 | } |
| 4687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4689 | unsigned int cmd, unsigned long arg) |
| 4690 | { |
| 4691 | int err = 0; |
| 4692 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | mddev_t *mddev = NULL; |
| 4694 | |
| 4695 | if (!capable(CAP_SYS_ADMIN)) |
| 4696 | return -EACCES; |
| 4697 | |
| 4698 | /* |
| 4699 | * Commands dealing with the RAID driver but not any |
| 4700 | * particular array: |
| 4701 | */ |
| 4702 | switch (cmd) |
| 4703 | { |
| 4704 | case RAID_VERSION: |
| 4705 | err = get_version(argp); |
| 4706 | goto done; |
| 4707 | |
| 4708 | case PRINT_RAID_DEBUG: |
| 4709 | err = 0; |
| 4710 | md_print_devices(); |
| 4711 | goto done; |
| 4712 | |
| 4713 | #ifndef MODULE |
| 4714 | case RAID_AUTORUN: |
| 4715 | err = 0; |
| 4716 | autostart_arrays(arg); |
| 4717 | goto done; |
| 4718 | #endif |
| 4719 | default:; |
| 4720 | } |
| 4721 | |
| 4722 | /* |
| 4723 | * Commands creating/starting a new array: |
| 4724 | */ |
| 4725 | |
| 4726 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4727 | |
| 4728 | if (!mddev) { |
| 4729 | BUG(); |
| 4730 | goto abort; |
| 4731 | } |
| 4732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | err = mddev_lock(mddev); |
| 4734 | if (err) { |
| 4735 | printk(KERN_INFO |
| 4736 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4737 | err, cmd); |
| 4738 | goto abort; |
| 4739 | } |
| 4740 | |
| 4741 | switch (cmd) |
| 4742 | { |
| 4743 | case SET_ARRAY_INFO: |
| 4744 | { |
| 4745 | mdu_array_info_t info; |
| 4746 | if (!arg) |
| 4747 | memset(&info, 0, sizeof(info)); |
| 4748 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4749 | err = -EFAULT; |
| 4750 | goto abort_unlock; |
| 4751 | } |
| 4752 | if (mddev->pers) { |
| 4753 | err = update_array_info(mddev, &info); |
| 4754 | if (err) { |
| 4755 | printk(KERN_WARNING "md: couldn't update" |
| 4756 | " array info. %d\n", err); |
| 4757 | goto abort_unlock; |
| 4758 | } |
| 4759 | goto done_unlock; |
| 4760 | } |
| 4761 | if (!list_empty(&mddev->disks)) { |
| 4762 | printk(KERN_WARNING |
| 4763 | "md: array %s already has disks!\n", |
| 4764 | mdname(mddev)); |
| 4765 | err = -EBUSY; |
| 4766 | goto abort_unlock; |
| 4767 | } |
| 4768 | if (mddev->raid_disks) { |
| 4769 | printk(KERN_WARNING |
| 4770 | "md: array %s already initialised!\n", |
| 4771 | mdname(mddev)); |
| 4772 | err = -EBUSY; |
| 4773 | goto abort_unlock; |
| 4774 | } |
| 4775 | err = set_array_info(mddev, &info); |
| 4776 | if (err) { |
| 4777 | printk(KERN_WARNING "md: couldn't set" |
| 4778 | " array info. %d\n", err); |
| 4779 | goto abort_unlock; |
| 4780 | } |
| 4781 | } |
| 4782 | goto done_unlock; |
| 4783 | |
| 4784 | default:; |
| 4785 | } |
| 4786 | |
| 4787 | /* |
| 4788 | * Commands querying/configuring an existing array: |
| 4789 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4790 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4791 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4792 | if ((!mddev->raid_disks && !mddev->external) |
| 4793 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4794 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4795 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | err = -ENODEV; |
| 4797 | goto abort_unlock; |
| 4798 | } |
| 4799 | |
| 4800 | /* |
| 4801 | * Commands even a read-only array can execute: |
| 4802 | */ |
| 4803 | switch (cmd) |
| 4804 | { |
| 4805 | case GET_ARRAY_INFO: |
| 4806 | err = get_array_info(mddev, argp); |
| 4807 | goto done_unlock; |
| 4808 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4809 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4810 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4811 | goto done_unlock; |
| 4812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | case GET_DISK_INFO: |
| 4814 | err = get_disk_info(mddev, argp); |
| 4815 | goto done_unlock; |
| 4816 | |
| 4817 | case RESTART_ARRAY_RW: |
| 4818 | err = restart_array(mddev); |
| 4819 | goto done_unlock; |
| 4820 | |
| 4821 | case STOP_ARRAY: |
| 4822 | err = do_md_stop (mddev, 0); |
| 4823 | goto done_unlock; |
| 4824 | |
| 4825 | case STOP_ARRAY_RO: |
| 4826 | err = do_md_stop (mddev, 1); |
| 4827 | goto done_unlock; |
| 4828 | |
| 4829 | /* |
| 4830 | * We have a problem here : there is no easy way to give a CHS |
| 4831 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4832 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4833 | * dosfs just mad... ;-) |
| 4834 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4839 | * superblock, so we do not allow them on read-only arrays. |
| 4840 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4841 | * here and hit the 'default' below, so only disallow |
| 4842 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4844 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4845 | mddev->ro && mddev->pers) { |
| 4846 | if (mddev->ro == 2) { |
| 4847 | mddev->ro = 0; |
| 4848 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4849 | md_wakeup_thread(mddev->thread); |
| 4850 | |
| 4851 | } else { |
| 4852 | err = -EROFS; |
| 4853 | goto abort_unlock; |
| 4854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | switch (cmd) |
| 4858 | { |
| 4859 | case ADD_NEW_DISK: |
| 4860 | { |
| 4861 | mdu_disk_info_t info; |
| 4862 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4863 | err = -EFAULT; |
| 4864 | else |
| 4865 | err = add_new_disk(mddev, &info); |
| 4866 | goto done_unlock; |
| 4867 | } |
| 4868 | |
| 4869 | case HOT_REMOVE_DISK: |
| 4870 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4871 | goto done_unlock; |
| 4872 | |
| 4873 | case HOT_ADD_DISK: |
| 4874 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4875 | goto done_unlock; |
| 4876 | |
| 4877 | case SET_DISK_FAULTY: |
| 4878 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4879 | goto done_unlock; |
| 4880 | |
| 4881 | case RUN_ARRAY: |
| 4882 | err = do_md_run (mddev); |
| 4883 | goto done_unlock; |
| 4884 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4885 | case SET_BITMAP_FILE: |
| 4886 | err = set_bitmap_file(mddev, (int)arg); |
| 4887 | goto done_unlock; |
| 4888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | err = -EINVAL; |
| 4891 | goto abort_unlock; |
| 4892 | } |
| 4893 | |
| 4894 | done_unlock: |
| 4895 | abort_unlock: |
| 4896 | mddev_unlock(mddev); |
| 4897 | |
| 4898 | return err; |
| 4899 | done: |
| 4900 | if (err) |
| 4901 | MD_BUG(); |
| 4902 | abort: |
| 4903 | return err; |
| 4904 | } |
| 4905 | |
| 4906 | static int md_open(struct inode *inode, struct file *file) |
| 4907 | { |
| 4908 | /* |
| 4909 | * Succeed if we can lock the mddev, which confirms that |
| 4910 | * it isn't being stopped right now. |
| 4911 | */ |
| 4912 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4913 | int err; |
| 4914 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 4915 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | goto out; |
| 4917 | |
| 4918 | err = 0; |
| 4919 | mddev_get(mddev); |
| 4920 | mddev_unlock(mddev); |
| 4921 | |
| 4922 | check_disk_change(inode->i_bdev); |
| 4923 | out: |
| 4924 | return err; |
| 4925 | } |
| 4926 | |
| 4927 | static int md_release(struct inode *inode, struct file * file) |
| 4928 | { |
| 4929 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4930 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4931 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | mddev_put(mddev); |
| 4933 | |
| 4934 | return 0; |
| 4935 | } |
| 4936 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4937 | static int md_media_changed(struct gendisk *disk) |
| 4938 | { |
| 4939 | mddev_t *mddev = disk->private_data; |
| 4940 | |
| 4941 | return mddev->changed; |
| 4942 | } |
| 4943 | |
| 4944 | static int md_revalidate(struct gendisk *disk) |
| 4945 | { |
| 4946 | mddev_t *mddev = disk->private_data; |
| 4947 | |
| 4948 | mddev->changed = 0; |
| 4949 | return 0; |
| 4950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | static struct block_device_operations md_fops = |
| 4952 | { |
| 4953 | .owner = THIS_MODULE, |
| 4954 | .open = md_open, |
| 4955 | .release = md_release, |
| 4956 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4957 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4958 | .media_changed = md_media_changed, |
| 4959 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | }; |
| 4961 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4962 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | { |
| 4964 | mdk_thread_t *thread = arg; |
| 4965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | /* |
| 4967 | * md_thread is a 'system-thread', it's priority should be very |
| 4968 | * high. We avoid resource deadlocks individually in each |
| 4969 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 4970 | * the very same RT priority as kswapd, thus we will never get |
| 4971 | * into a priority inversion deadlock. |
| 4972 | * |
| 4973 | * we definitely have to have equal or higher priority than |
| 4974 | * bdflush, otherwise bdflush will deadlock if there are too |
| 4975 | * many dirty RAID5 blocks. |
| 4976 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4978 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4979 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 4981 | /* We need to wait INTERRUPTIBLE so that |
| 4982 | * we don't add to the load-average. |
| 4983 | * That means we need to be sure no signals are |
| 4984 | * pending |
| 4985 | */ |
| 4986 | if (signal_pending(current)) |
| 4987 | flush_signals(current); |
| 4988 | |
| 4989 | wait_event_interruptible_timeout |
| 4990 | (thread->wqueue, |
| 4991 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 4992 | || kthread_should_stop(), |
| 4993 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | |
| 4995 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 4996 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 4997 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5004 | { |
| 5005 | if (thread) { |
| 5006 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5007 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5008 | wake_up(&thread->wqueue); |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5013 | const char *name) |
| 5014 | { |
| 5015 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5017 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | if (!thread) |
| 5019 | return NULL; |
| 5020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | init_waitqueue_head(&thread->wqueue); |
| 5022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | thread->run = run; |
| 5024 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5025 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5026 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5027 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | kfree(thread); |
| 5029 | return NULL; |
| 5030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | return thread; |
| 5032 | } |
| 5033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | void md_unregister_thread(mdk_thread_t *thread) |
| 5035 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5036 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5037 | |
| 5038 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | kfree(thread); |
| 5040 | } |
| 5041 | |
| 5042 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5043 | { |
| 5044 | if (!mddev) { |
| 5045 | MD_BUG(); |
| 5046 | return; |
| 5047 | } |
| 5048 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5049 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5051 | |
| 5052 | if (mddev->external) |
| 5053 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5054 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5056 | mdname(mddev), |
| 5057 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5058 | __builtin_return_address(0),__builtin_return_address(1), |
| 5059 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5060 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5061 | if (!mddev->pers) |
| 5062 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | if (!mddev->pers->error_handler) |
| 5064 | return; |
| 5065 | mddev->pers->error_handler(mddev,rdev); |
| 5066 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5067 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5068 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5069 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | /* seq_file implementation /proc/mdstat */ |
| 5073 | |
| 5074 | static void status_unused(struct seq_file *seq) |
| 5075 | { |
| 5076 | int i = 0; |
| 5077 | mdk_rdev_t *rdev; |
| 5078 | struct list_head *tmp; |
| 5079 | |
| 5080 | seq_printf(seq, "unused devices: "); |
| 5081 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5082 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | char b[BDEVNAME_SIZE]; |
| 5084 | i++; |
| 5085 | seq_printf(seq, "%s ", |
| 5086 | bdevname(rdev->bdev,b)); |
| 5087 | } |
| 5088 | if (!i) |
| 5089 | seq_printf(seq, "<none>"); |
| 5090 | |
| 5091 | seq_printf(seq, "\n"); |
| 5092 | } |
| 5093 | |
| 5094 | |
| 5095 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5096 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5097 | sector_t max_blocks, resync, res; |
| 5098 | unsigned long dt, db, rt; |
| 5099 | int scale; |
| 5100 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | |
| 5102 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5103 | |
| 5104 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5105 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5106 | else |
| 5107 | max_blocks = mddev->size; |
| 5108 | |
| 5109 | /* |
| 5110 | * Should not happen. |
| 5111 | */ |
| 5112 | if (!max_blocks) { |
| 5113 | MD_BUG(); |
| 5114 | return; |
| 5115 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5116 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5117 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5118 | * u32, as those are the requirements for sector_div. |
| 5119 | * Thus 'scale' must be at least 10 |
| 5120 | */ |
| 5121 | scale = 10; |
| 5122 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5123 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5124 | scale++; |
| 5125 | } |
| 5126 | res = (resync>>scale)*1000; |
| 5127 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5128 | |
| 5129 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5131 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | seq_printf(seq, "["); |
| 5133 | for (i = 0; i < x; i++) |
| 5134 | seq_printf(seq, "="); |
| 5135 | seq_printf(seq, ">"); |
| 5136 | for (i = 0; i < y; i++) |
| 5137 | seq_printf(seq, "."); |
| 5138 | seq_printf(seq, "] "); |
| 5139 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5140 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5141 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5142 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5143 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5144 | "check" : |
| 5145 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5146 | "resync" : "recovery"))), |
| 5147 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5148 | (unsigned long long) resync, |
| 5149 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | |
| 5151 | /* |
| 5152 | * We do not want to overflow, so the order of operands and |
| 5153 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5154 | * safe against division by zero. We only estimate anyway. |
| 5155 | * |
| 5156 | * dt: time from mark until now |
| 5157 | * db: blocks written from mark until now |
| 5158 | * rt: remaining time |
| 5159 | */ |
| 5160 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5161 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5162 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5163 | - mddev->resync_mark_cnt; |
| 5164 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | |
| 5166 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5167 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5168 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5172 | { |
| 5173 | struct list_head *tmp; |
| 5174 | loff_t l = *pos; |
| 5175 | mddev_t *mddev; |
| 5176 | |
| 5177 | if (l >= 0x10000) |
| 5178 | return NULL; |
| 5179 | if (!l--) |
| 5180 | /* header */ |
| 5181 | return (void*)1; |
| 5182 | |
| 5183 | spin_lock(&all_mddevs_lock); |
| 5184 | list_for_each(tmp,&all_mddevs) |
| 5185 | if (!l--) { |
| 5186 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5187 | mddev_get(mddev); |
| 5188 | spin_unlock(&all_mddevs_lock); |
| 5189 | return mddev; |
| 5190 | } |
| 5191 | spin_unlock(&all_mddevs_lock); |
| 5192 | if (!l--) |
| 5193 | return (void*)2;/* tail */ |
| 5194 | return NULL; |
| 5195 | } |
| 5196 | |
| 5197 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5198 | { |
| 5199 | struct list_head *tmp; |
| 5200 | mddev_t *next_mddev, *mddev = v; |
| 5201 | |
| 5202 | ++*pos; |
| 5203 | if (v == (void*)2) |
| 5204 | return NULL; |
| 5205 | |
| 5206 | spin_lock(&all_mddevs_lock); |
| 5207 | if (v == (void*)1) |
| 5208 | tmp = all_mddevs.next; |
| 5209 | else |
| 5210 | tmp = mddev->all_mddevs.next; |
| 5211 | if (tmp != &all_mddevs) |
| 5212 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5213 | else { |
| 5214 | next_mddev = (void*)2; |
| 5215 | *pos = 0x10000; |
| 5216 | } |
| 5217 | spin_unlock(&all_mddevs_lock); |
| 5218 | |
| 5219 | if (v != (void*)1) |
| 5220 | mddev_put(mddev); |
| 5221 | return next_mddev; |
| 5222 | |
| 5223 | } |
| 5224 | |
| 5225 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5226 | { |
| 5227 | mddev_t *mddev = v; |
| 5228 | |
| 5229 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5230 | mddev_put(mddev); |
| 5231 | } |
| 5232 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5233 | struct mdstat_info { |
| 5234 | int event; |
| 5235 | }; |
| 5236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5238 | { |
| 5239 | mddev_t *mddev = v; |
| 5240 | sector_t size; |
| 5241 | struct list_head *tmp2; |
| 5242 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5243 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5244 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | |
| 5246 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5247 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | seq_printf(seq, "Personalities : "); |
| 5249 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5250 | list_for_each_entry(pers, &pers_list, list) |
| 5251 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | |
| 5253 | spin_unlock(&pers_lock); |
| 5254 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5255 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | return 0; |
| 5257 | } |
| 5258 | if (v == (void*)2) { |
| 5259 | status_unused(seq); |
| 5260 | return 0; |
| 5261 | } |
| 5262 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5263 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5267 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5268 | mddev->pers ? "" : "in"); |
| 5269 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5270 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5272 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5273 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | seq_printf(seq, " %s", mddev->pers->name); |
| 5275 | } |
| 5276 | |
| 5277 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5278 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | char b[BDEVNAME_SIZE]; |
| 5280 | seq_printf(seq, " %s[%d]", |
| 5281 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5282 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5283 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5284 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | seq_printf(seq, "(F)"); |
| 5286 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5287 | } else if (rdev->raid_disk < 0) |
| 5288 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | size += rdev->size; |
| 5290 | } |
| 5291 | |
| 5292 | if (!list_empty(&mddev->disks)) { |
| 5293 | if (mddev->pers) |
| 5294 | seq_printf(seq, "\n %llu blocks", |
| 5295 | (unsigned long long)mddev->array_size); |
| 5296 | else |
| 5297 | seq_printf(seq, "\n %llu blocks", |
| 5298 | (unsigned long long)size); |
| 5299 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5300 | if (mddev->persistent) { |
| 5301 | if (mddev->major_version != 0 || |
| 5302 | mddev->minor_version != 90) { |
| 5303 | seq_printf(seq," super %d.%d", |
| 5304 | mddev->major_version, |
| 5305 | mddev->minor_version); |
| 5306 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5307 | } else if (mddev->external) |
| 5308 | seq_printf(seq, " super external:%s", |
| 5309 | mddev->metadata_type); |
| 5310 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5311 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | |
| 5313 | if (mddev->pers) { |
| 5314 | mddev->pers->status (seq, mddev); |
| 5315 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5316 | if (mddev->pers->sync_request) { |
| 5317 | if (mddev->curr_resync > 2) { |
| 5318 | status_resync (seq, mddev); |
| 5319 | seq_printf(seq, "\n "); |
| 5320 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5321 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5322 | else if (mddev->recovery_cp < MaxSector) |
| 5323 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5324 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5325 | } else |
| 5326 | seq_printf(seq, "\n "); |
| 5327 | |
| 5328 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5329 | unsigned long chunk_kb; |
| 5330 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5331 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5332 | chunk_kb = bitmap->chunksize >> 10; |
| 5333 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5334 | "%lu%s chunk", |
| 5335 | bitmap->pages - bitmap->missing_pages, |
| 5336 | bitmap->pages, |
| 5337 | (bitmap->pages - bitmap->missing_pages) |
| 5338 | << (PAGE_SHIFT - 10), |
| 5339 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5340 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5341 | if (bitmap->file) { |
| 5342 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5343 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5344 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5345 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5346 | seq_printf(seq, "\n"); |
| 5347 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | seq_printf(seq, "\n"); |
| 5351 | } |
| 5352 | mddev_unlock(mddev); |
| 5353 | |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | static struct seq_operations md_seq_ops = { |
| 5358 | .start = md_seq_start, |
| 5359 | .next = md_seq_next, |
| 5360 | .stop = md_seq_stop, |
| 5361 | .show = md_seq_show, |
| 5362 | }; |
| 5363 | |
| 5364 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5365 | { |
| 5366 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5367 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5368 | if (mi == NULL) |
| 5369 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | |
| 5371 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5372 | if (error) |
| 5373 | kfree(mi); |
| 5374 | else { |
| 5375 | struct seq_file *p = file->private_data; |
| 5376 | p->private = mi; |
| 5377 | mi->event = atomic_read(&md_event_count); |
| 5378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | return error; |
| 5380 | } |
| 5381 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5382 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5383 | { |
| 5384 | struct seq_file *m = filp->private_data; |
| 5385 | struct mdstat_info *mi = m->private; |
| 5386 | int mask; |
| 5387 | |
| 5388 | poll_wait(filp, &md_event_waiters, wait); |
| 5389 | |
| 5390 | /* always allow read */ |
| 5391 | mask = POLLIN | POLLRDNORM; |
| 5392 | |
| 5393 | if (mi->event != atomic_read(&md_event_count)) |
| 5394 | mask |= POLLERR | POLLPRI; |
| 5395 | return mask; |
| 5396 | } |
| 5397 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5398 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5399 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | .open = md_seq_open, |
| 5401 | .read = seq_read, |
| 5402 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5403 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5404 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | }; |
| 5406 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5407 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5410 | list_add_tail(&p->list, &pers_list); |
| 5411 | 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] | 5412 | spin_unlock(&pers_lock); |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5416 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5418 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5420 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | spin_unlock(&pers_lock); |
| 5422 | return 0; |
| 5423 | } |
| 5424 | |
| 5425 | static int is_mddev_idle(mddev_t *mddev) |
| 5426 | { |
| 5427 | mdk_rdev_t * rdev; |
| 5428 | struct list_head *tmp; |
| 5429 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5430 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | |
| 5432 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5433 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5435 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5436 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5437 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5438 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5439 | * as sync_io is counted when a request starts, and |
| 5440 | * disk_stats is counted when it completes. |
| 5441 | * So resync activity will cause curr_events to be smaller than |
| 5442 | * when there was no such activity. |
| 5443 | * non-sync IO will cause disk_stat to increase without |
| 5444 | * increasing sync_io so curr_events will (eventually) |
| 5445 | * be larger than it was before. Once it becomes |
| 5446 | * substantially larger, the test below will cause |
| 5447 | * the array to appear non-idle, and resync will slow |
| 5448 | * down. |
| 5449 | * If there is a lot of outstanding resync activity when |
| 5450 | * we set last_event to curr_events, then all that activity |
| 5451 | * completing might cause the array to appear non-idle |
| 5452 | * and resync will be slowed down even though there might |
| 5453 | * not have been non-resync activity. This will only |
| 5454 | * happen once though. 'last_events' will soon reflect |
| 5455 | * the state where there is little or no outstanding |
| 5456 | * resync requests, and further resync activity will |
| 5457 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5458 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5460 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | rdev->last_events = curr_events; |
| 5462 | idle = 0; |
| 5463 | } |
| 5464 | } |
| 5465 | return idle; |
| 5466 | } |
| 5467 | |
| 5468 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5469 | { |
| 5470 | /* another "blocks" (512byte) blocks have been synced */ |
| 5471 | atomic_sub(blocks, &mddev->recovery_active); |
| 5472 | wake_up(&mddev->recovery_wait); |
| 5473 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5474 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | md_wakeup_thread(mddev->thread); |
| 5476 | // stop recovery, signal do_sync .... |
| 5477 | } |
| 5478 | } |
| 5479 | |
| 5480 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5481 | /* md_write_start(mddev, bi) |
| 5482 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5483 | * in superblock) before writing, schedule a superblock update |
| 5484 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5485 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5486 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5488 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5489 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5490 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5491 | BUG_ON(mddev->ro == 1); |
| 5492 | if (mddev->ro == 2) { |
| 5493 | /* need to switch to read/write */ |
| 5494 | mddev->ro = 0; |
| 5495 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5496 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5497 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5498 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5499 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5500 | if (mddev->safemode == 1) |
| 5501 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5502 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5503 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5504 | if (mddev->in_sync) { |
| 5505 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5506 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5507 | md_wakeup_thread(mddev->thread); |
| 5508 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5509 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5510 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5511 | } |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5512 | wait_event(mddev->sb_wait, |
| 5513 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5514 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | void md_write_end(mddev_t *mddev) |
| 5518 | { |
| 5519 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5520 | if (mddev->safemode == 2) |
| 5521 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5522 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5524 | } |
| 5525 | } |
| 5526 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5527 | /* md_allow_write(mddev) |
| 5528 | * Calling this ensures that the array is marked 'active' so that writes |
| 5529 | * may proceed without blocking. It is important to call this before |
| 5530 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5531 | * Must be called with mddev_lock held. |
| 5532 | */ |
| 5533 | void md_allow_write(mddev_t *mddev) |
| 5534 | { |
| 5535 | if (!mddev->pers) |
| 5536 | return; |
| 5537 | if (mddev->ro) |
| 5538 | return; |
| 5539 | |
| 5540 | spin_lock_irq(&mddev->write_lock); |
| 5541 | if (mddev->in_sync) { |
| 5542 | mddev->in_sync = 0; |
| 5543 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5544 | if (mddev->safemode_delay && |
| 5545 | mddev->safemode == 0) |
| 5546 | mddev->safemode = 1; |
| 5547 | spin_unlock_irq(&mddev->write_lock); |
| 5548 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5549 | |
| 5550 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 5551 | /* wait for the dirty state to be recorded in the metadata */ |
| 5552 | wait_event(mddev->sb_wait, |
| 5553 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5554 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5555 | } else |
| 5556 | spin_unlock_irq(&mddev->write_lock); |
| 5557 | } |
| 5558 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | #define SYNC_MARKS 10 |
| 5561 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5562 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | { |
| 5564 | mddev_t *mddev2; |
| 5565 | unsigned int currspeed = 0, |
| 5566 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5567 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | unsigned long mark[SYNC_MARKS]; |
| 5569 | sector_t mark_cnt[SYNC_MARKS]; |
| 5570 | int last_mark,m; |
| 5571 | struct list_head *tmp; |
| 5572 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5573 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5574 | struct list_head *rtmp; |
| 5575 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5576 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | |
| 5578 | /* just incase thread restarts... */ |
| 5579 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5580 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5581 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5582 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5584 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5585 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5586 | desc = "data-check"; |
| 5587 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5588 | desc = "requested-resync"; |
| 5589 | else |
| 5590 | desc = "resync"; |
| 5591 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5592 | desc = "reshape"; |
| 5593 | else |
| 5594 | desc = "recovery"; |
| 5595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | /* we overload curr_resync somewhat here. |
| 5597 | * 0 == not engaged in resync at all |
| 5598 | * 2 == checking that there is no conflict with another sync |
| 5599 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5600 | * commense |
| 5601 | * other == active in resync - this many blocks |
| 5602 | * |
| 5603 | * Before starting a resync we must have set curr_resync to |
| 5604 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5605 | * less than ours. When we find one that is the same or higher |
| 5606 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5607 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5608 | * This will mean we have to start checking from the beginning again. |
| 5609 | * |
| 5610 | */ |
| 5611 | |
| 5612 | do { |
| 5613 | mddev->curr_resync = 2; |
| 5614 | |
| 5615 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5616 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5617 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | goto skip; |
| 5619 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5620 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | if (mddev2 == mddev) |
| 5622 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5623 | if (!mddev->parallel_resync |
| 5624 | && mddev2->curr_resync |
| 5625 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | DEFINE_WAIT(wq); |
| 5627 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5628 | /* arbitrarily yield */ |
| 5629 | mddev->curr_resync = 1; |
| 5630 | wake_up(&resync_wait); |
| 5631 | } |
| 5632 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5633 | /* no need to wait here, we can wait the next |
| 5634 | * time 'round when curr_resync == 2 |
| 5635 | */ |
| 5636 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5637 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5638 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5639 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5640 | printk(KERN_INFO "md: delaying %s of %s" |
| 5641 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5643 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | mddev_put(mddev2); |
| 5645 | schedule(); |
| 5646 | finish_wait(&resync_wait, &wq); |
| 5647 | goto try_again; |
| 5648 | } |
| 5649 | finish_wait(&resync_wait, &wq); |
| 5650 | } |
| 5651 | } |
| 5652 | } while (mddev->curr_resync < 2); |
| 5653 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5654 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5655 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5657 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | */ |
| 5659 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5660 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5661 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5662 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5663 | j = mddev->resync_min; |
| 5664 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5665 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5666 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5667 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5668 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5669 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | /* recovery follows the physical size of devices */ |
| 5671 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5672 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5673 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5674 | if (rdev->raid_disk >= 0 && |
| 5675 | !test_bit(Faulty, &rdev->flags) && |
| 5676 | !test_bit(In_sync, &rdev->flags) && |
| 5677 | rdev->recovery_offset < j) |
| 5678 | j = rdev->recovery_offset; |
| 5679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5681 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5682 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5683 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5684 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5685 | "(but not more than %d KB/sec) for %s.\n", |
| 5686 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | |
| 5688 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5689 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5690 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5692 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5693 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | } |
| 5695 | last_mark = 0; |
| 5696 | mddev->resync_mark = mark[last_mark]; |
| 5697 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5698 | |
| 5699 | /* |
| 5700 | * Tune reconstruction: |
| 5701 | */ |
| 5702 | window = 32*(PAGE_SIZE/512); |
| 5703 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5704 | window/2,(unsigned long long) max_sectors/2); |
| 5705 | |
| 5706 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | last_check = 0; |
| 5708 | |
| 5709 | if (j>2) { |
| 5710 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5711 | "md: resuming %s of %s from checkpoint.\n", |
| 5712 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | mddev->curr_resync = j; |
| 5714 | } |
| 5715 | |
| 5716 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5717 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5719 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5720 | if (j >= mddev->resync_max) { |
| 5721 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5722 | wait_event(mddev->recovery_wait, |
| 5723 | mddev->resync_max > j |
| 5724 | || kthread_should_stop()); |
| 5725 | } |
| 5726 | if (kthread_should_stop()) |
| 5727 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5728 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5729 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5730 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5731 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | goto out; |
| 5733 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5734 | |
| 5735 | if (!skipped) { /* actual IO requested */ |
| 5736 | io_sectors += sectors; |
| 5737 | atomic_add(sectors, &mddev->recovery_active); |
| 5738 | } |
| 5739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | j += sectors; |
| 5741 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5742 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5743 | if (last_check == 0) |
| 5744 | /* this is the earliers that rebuilt will be |
| 5745 | * visible in /proc/mdstat |
| 5746 | */ |
| 5747 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5748 | |
| 5749 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | continue; |
| 5751 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5752 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5754 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | break; |
| 5756 | |
| 5757 | repeat: |
| 5758 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5759 | /* step marks */ |
| 5760 | int next = (last_mark+1) % SYNC_MARKS; |
| 5761 | |
| 5762 | mddev->resync_mark = mark[next]; |
| 5763 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5764 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5765 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | last_mark = next; |
| 5767 | } |
| 5768 | |
| 5769 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5770 | if (kthread_should_stop()) |
| 5771 | goto interrupted; |
| 5772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | |
| 5774 | /* |
| 5775 | * this loop exits only if either when we are slower than |
| 5776 | * the 'hard' speed limit, or the system was IO-idle for |
| 5777 | * a jiffy. |
| 5778 | * the system might be non-idle CPU-wise, but we only care |
| 5779 | * about not overloading the IO subsystem. (things like an |
| 5780 | * e2fsck being done on the RAID array should execute fast) |
| 5781 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5782 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | cond_resched(); |
| 5784 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5785 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5786 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5788 | if (currspeed > speed_min(mddev)) { |
| 5789 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5791 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | goto repeat; |
| 5793 | } |
| 5794 | } |
| 5795 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5796 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | /* |
| 5798 | * this also signals 'finished resyncing' to md_stop |
| 5799 | */ |
| 5800 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5801 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5802 | |
| 5803 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5804 | |
| 5805 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5806 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5808 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5809 | mddev->curr_resync > 2) { |
| 5810 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5811 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5812 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5813 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5814 | "md: checkpointing %s of %s.\n", |
| 5815 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5816 | mddev->recovery_cp = mddev->curr_resync; |
| 5817 | } |
| 5818 | } else |
| 5819 | mddev->recovery_cp = MaxSector; |
| 5820 | } else { |
| 5821 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5822 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5823 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5824 | if (rdev->raid_disk >= 0 && |
| 5825 | !test_bit(Faulty, &rdev->flags) && |
| 5826 | !test_bit(In_sync, &rdev->flags) && |
| 5827 | rdev->recovery_offset < mddev->curr_resync) |
| 5828 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5831 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | skip: |
| 5834 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5835 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5836 | mddev->resync_max = MaxSector; |
| 5837 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | wake_up(&resync_wait); |
| 5839 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5840 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5841 | return; |
| 5842 | |
| 5843 | interrupted: |
| 5844 | /* |
| 5845 | * got a signal, exit. |
| 5846 | */ |
| 5847 | printk(KERN_INFO |
| 5848 | "md: md_do_sync() got signal ... exiting\n"); |
| 5849 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5850 | goto out; |
| 5851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5853 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | |
| 5855 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5856 | static int remove_and_add_spares(mddev_t *mddev) |
| 5857 | { |
| 5858 | mdk_rdev_t *rdev; |
| 5859 | struct list_head *rtmp; |
| 5860 | int spares = 0; |
| 5861 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5862 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5863 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5864 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5865 | (test_bit(Faulty, &rdev->flags) || |
| 5866 | ! test_bit(In_sync, &rdev->flags)) && |
| 5867 | atomic_read(&rdev->nr_pending)==0) { |
| 5868 | if (mddev->pers->hot_remove_disk( |
| 5869 | mddev, rdev->raid_disk)==0) { |
| 5870 | char nm[20]; |
| 5871 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5872 | sysfs_remove_link(&mddev->kobj, nm); |
| 5873 | rdev->raid_disk = -1; |
| 5874 | } |
| 5875 | } |
| 5876 | |
| 5877 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5878 | rdev_for_each(rdev, rtmp, mddev) { |
| 5879 | if (rdev->raid_disk >= 0 && |
| 5880 | !test_bit(In_sync, &rdev->flags)) |
| 5881 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5882 | if (rdev->raid_disk < 0 |
| 5883 | && !test_bit(Faulty, &rdev->flags)) { |
| 5884 | rdev->recovery_offset = 0; |
| 5885 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 5886 | char nm[20]; |
| 5887 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5888 | if (sysfs_create_link(&mddev->kobj, |
| 5889 | &rdev->kobj, nm)) |
| 5890 | printk(KERN_WARNING |
| 5891 | "md: cannot register " |
| 5892 | "%s for %s\n", |
| 5893 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5894 | spares++; |
| 5895 | md_new_event(mddev); |
| 5896 | } else |
| 5897 | break; |
| 5898 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5899 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5900 | } |
| 5901 | return spares; |
| 5902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | /* |
| 5904 | * This routine is regularly called by all per-raid-array threads to |
| 5905 | * deal with generic issues like resync and super-block update. |
| 5906 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5907 | * need this as they never do any recovery or update the superblock. |
| 5908 | * |
| 5909 | * It does not do any resync itself, but rather "forks" off other threads |
| 5910 | * to do that as needed. |
| 5911 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5912 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5913 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | * and wakeups up this thread which will reap the thread and finish up. |
| 5915 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5916 | * |
| 5917 | * The overall approach is: |
| 5918 | * 1/ if the superblock needs updating, update it. |
| 5919 | * 2/ If a recovery thread is running, don't do anything else. |
| 5920 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5921 | * 4/ If there are any faulty devices, remove them. |
| 5922 | * 5/ If array is degraded, try to add spares devices |
| 5923 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5924 | */ |
| 5925 | void md_check_recovery(mddev_t *mddev) |
| 5926 | { |
| 5927 | mdk_rdev_t *rdev; |
| 5928 | struct list_head *rtmp; |
| 5929 | |
| 5930 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5931 | if (mddev->bitmap) |
| 5932 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | |
| 5934 | if (mddev->ro) |
| 5935 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5936 | |
| 5937 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5938 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5939 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 5940 | mdname(mddev)); |
| 5941 | mddev->safemode = 2; |
| 5942 | } |
| 5943 | flush_signals(current); |
| 5944 | } |
| 5945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5947 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5949 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5950 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5951 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 5952 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | )) |
| 5954 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5955 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5956 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5957 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5958 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5959 | if (!mddev->external) { |
| 5960 | spin_lock_irq(&mddev->write_lock); |
| 5961 | if (mddev->safemode && |
| 5962 | !atomic_read(&mddev->writes_pending) && |
| 5963 | !mddev->in_sync && |
| 5964 | mddev->recovery_cp == MaxSector) { |
| 5965 | mddev->in_sync = 1; |
| 5966 | if (mddev->persistent) |
| 5967 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5968 | } |
| 5969 | if (mddev->safemode == 1) |
| 5970 | mddev->safemode = 0; |
| 5971 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5972 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5973 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5974 | if (mddev->flags) |
| 5975 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5976 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 5979 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 5980 | /* resync/recovery still happening */ |
| 5981 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5982 | goto unlock; |
| 5983 | } |
| 5984 | if (mddev->sync_thread) { |
| 5985 | /* resync has finished, collect result */ |
| 5986 | md_unregister_thread(mddev->sync_thread); |
| 5987 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5988 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | /* success...*/ |
| 5990 | /* activate any spares */ |
| 5991 | mddev->pers->spare_active(mddev); |
| 5992 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5993 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5994 | |
| 5995 | /* if array is no-longer degraded, then any saved_raid_disk |
| 5996 | * information must be scrapped |
| 5997 | */ |
| 5998 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5999 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6000 | rdev->saved_raid_disk = -1; |
| 6001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | mddev->recovery = 0; |
| 6003 | /* flag recovery needed just to double check */ |
| 6004 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6005 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | goto unlock; |
| 6007 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6008 | /* Clear some bits that don't mean anything, but |
| 6009 | * might be left set |
| 6010 | */ |
| 6011 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6012 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6013 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6015 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6016 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | /* no recovery is running. |
| 6018 | * remove any failed drives, then |
| 6019 | * add spares if possible. |
| 6020 | * Spare are also removed and re-added, to allow |
| 6021 | * the personality to fail the re-add. |
| 6022 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6024 | if (mddev->reshape_position != MaxSector) { |
| 6025 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6026 | /* Cannot proceed */ |
| 6027 | goto unlock; |
| 6028 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 6029 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6030 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6031 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 6032 | } else if (mddev->recovery_cp < MaxSector) { |
| 6033 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6034 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6035 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | if (mddev->pers->sync_request) { |
| 6039 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6040 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6041 | /* We are adding a device or devices to an array |
| 6042 | * which has the bitmap stored on all devices. |
| 6043 | * So make sure all bitmap pages get written |
| 6044 | */ |
| 6045 | bitmap_write_all(mddev->bitmap); |
| 6046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6048 | mddev, |
| 6049 | "%s_resync"); |
| 6050 | if (!mddev->sync_thread) { |
| 6051 | printk(KERN_ERR "%s: could not start resync" |
| 6052 | " thread...\n", |
| 6053 | mdname(mddev)); |
| 6054 | /* leave the spares where they are, it shouldn't hurt */ |
| 6055 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6056 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6058 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | } |
| 6060 | unlock: |
| 6061 | mddev_unlock(mddev); |
| 6062 | } |
| 6063 | } |
| 6064 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6065 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6066 | { |
| 6067 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6068 | wait_event_timeout(rdev->blocked_wait, |
| 6069 | !test_bit(Blocked, &rdev->flags), |
| 6070 | msecs_to_jiffies(5000)); |
| 6071 | rdev_dec_pending(rdev, mddev); |
| 6072 | } |
| 6073 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6074 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6075 | static int md_notify_reboot(struct notifier_block *this, |
| 6076 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | { |
| 6078 | struct list_head *tmp; |
| 6079 | mddev_t *mddev; |
| 6080 | |
| 6081 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6082 | |
| 6083 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6084 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6085 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6086 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6087 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6088 | mddev_unlock(mddev); |
| 6089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | /* |
| 6091 | * certain more exotic SCSI devices are known to be |
| 6092 | * volatile wrt too early system reboots. While the |
| 6093 | * right place to handle this issue is the given |
| 6094 | * driver, we do want to have a safe RAID driver ... |
| 6095 | */ |
| 6096 | mdelay(1000*1); |
| 6097 | } |
| 6098 | return NOTIFY_DONE; |
| 6099 | } |
| 6100 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6101 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | .notifier_call = md_notify_reboot, |
| 6103 | .next = NULL, |
| 6104 | .priority = INT_MAX, /* before any real devices */ |
| 6105 | }; |
| 6106 | |
| 6107 | static void md_geninit(void) |
| 6108 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6110 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6111 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6114 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | if (register_blkdev(MAJOR_NR, "md")) |
| 6117 | return -1; |
| 6118 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6119 | unregister_blkdev(MAJOR_NR, "md"); |
| 6120 | return -1; |
| 6121 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6122 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6123 | md_probe, NULL, NULL); |
| 6124 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | md_probe, NULL, NULL); |
| 6126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6128 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | |
| 6130 | md_geninit(); |
| 6131 | return (0); |
| 6132 | } |
| 6133 | |
| 6134 | |
| 6135 | #ifndef MODULE |
| 6136 | |
| 6137 | /* |
| 6138 | * Searches all registered partitions for autorun RAID arrays |
| 6139 | * at boot time. |
| 6140 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6141 | |
| 6142 | static LIST_HEAD(all_detected_devices); |
| 6143 | struct detected_devices_node { |
| 6144 | struct list_head list; |
| 6145 | dev_t dev; |
| 6146 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | |
| 6148 | void md_autodetect_dev(dev_t dev) |
| 6149 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6150 | struct detected_devices_node *node_detected_dev; |
| 6151 | |
| 6152 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6153 | if (node_detected_dev) { |
| 6154 | node_detected_dev->dev = dev; |
| 6155 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6156 | } else { |
| 6157 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6158 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | |
| 6163 | static void autostart_arrays(int part) |
| 6164 | { |
| 6165 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6166 | struct detected_devices_node *node_detected_dev; |
| 6167 | dev_t dev; |
| 6168 | int i_scanned, i_passed; |
| 6169 | |
| 6170 | i_scanned = 0; |
| 6171 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | |
| 6173 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6174 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6175 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6176 | i_scanned++; |
| 6177 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6178 | struct detected_devices_node, list); |
| 6179 | list_del(&node_detected_dev->list); |
| 6180 | dev = node_detected_dev->dev; |
| 6181 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6182 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | if (IS_ERR(rdev)) |
| 6184 | continue; |
| 6185 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6186 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | MD_BUG(); |
| 6188 | continue; |
| 6189 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6190 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6192 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6194 | |
| 6195 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6196 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | |
| 6198 | autorun_devices(part); |
| 6199 | } |
| 6200 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6201 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | |
| 6203 | static __exit void md_exit(void) |
| 6204 | { |
| 6205 | mddev_t *mddev; |
| 6206 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6207 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6208 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6209 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | |
| 6211 | unregister_blkdev(MAJOR_NR,"md"); |
| 6212 | unregister_blkdev(mdp_major, "mdp"); |
| 6213 | unregister_reboot_notifier(&md_notifier); |
| 6214 | unregister_sysctl_table(raid_table_header); |
| 6215 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6216 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | struct gendisk *disk = mddev->gendisk; |
| 6218 | if (!disk) |
| 6219 | continue; |
| 6220 | export_array(mddev); |
| 6221 | del_gendisk(disk); |
| 6222 | put_disk(disk); |
| 6223 | mddev->gendisk = NULL; |
| 6224 | mddev_put(mddev); |
| 6225 | } |
| 6226 | } |
| 6227 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6228 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | module_exit(md_exit) |
| 6230 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6231 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6232 | { |
| 6233 | return sprintf(buffer, "%d", start_readonly); |
| 6234 | } |
| 6235 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6236 | { |
| 6237 | char *e; |
| 6238 | int num = simple_strtoul(val, &e, 10); |
| 6239 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6240 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6241 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6242 | } |
| 6243 | return -EINVAL; |
| 6244 | } |
| 6245 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6246 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6247 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6248 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | EXPORT_SYMBOL(register_md_personality); |
| 6251 | EXPORT_SYMBOL(unregister_md_personality); |
| 6252 | EXPORT_SYMBOL(md_error); |
| 6253 | EXPORT_SYMBOL(md_done_sync); |
| 6254 | EXPORT_SYMBOL(md_write_start); |
| 6255 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | EXPORT_SYMBOL(md_register_thread); |
| 6257 | EXPORT_SYMBOL(md_unregister_thread); |
| 6258 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | EXPORT_SYMBOL(md_check_recovery); |
| 6260 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6261 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6262 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |