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