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