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 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 37 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 42 | #include <linux/hdreg.h> |
| 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/random.h> |
| 45 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 46 | #include <linux/file.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 47 | #include <linux/delay.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define MAJOR_NR MD_MAJOR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* 63 partitions with the alternate major number (mdp) */ |
| 52 | #define MdpMinorShift 6 |
| 53 | |
| 54 | #define DEBUG 0 |
| 55 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 56 | |
| 57 | |
| 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 62 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(pers_lock); |
| 64 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 65 | static void md_print_devices(void); |
| 66 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 67 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 73 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 74 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 75 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 77 | * speed limit - in case reconstruction slows down your system despite |
| 78 | * idle IO detection. |
| 79 | * |
| 80 | * 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] | 81 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | static int sysctl_speed_limit_min = 1000; |
| 85 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 86 | static inline int speed_min(mddev_t *mddev) |
| 87 | { |
| 88 | return mddev->sync_speed_min ? |
| 89 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 90 | } |
| 91 | |
| 92 | static inline int speed_max(mddev_t *mddev) |
| 93 | { |
| 94 | return mddev->sync_speed_max ? |
| 95 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | static struct ctl_table_header *raid_table_header; |
| 99 | |
| 100 | static ctl_table raid_table[] = { |
| 101 | { |
| 102 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 103 | .procname = "speed_limit_min", |
| 104 | .data = &sysctl_speed_limit_min, |
| 105 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 106 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .proc_handler = &proc_dointvec, |
| 108 | }, |
| 109 | { |
| 110 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 111 | .procname = "speed_limit_max", |
| 112 | .data = &sysctl_speed_limit_max, |
| 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 | { .ctl_name = 0 } |
| 118 | }; |
| 119 | |
| 120 | static ctl_table raid_dir_table[] = { |
| 121 | { |
| 122 | .ctl_name = DEV_RAID, |
| 123 | .procname = "raid", |
| 124 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 125 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .child = raid_table, |
| 127 | }, |
| 128 | { .ctl_name = 0 } |
| 129 | }; |
| 130 | |
| 131 | static ctl_table raid_root_table[] = { |
| 132 | { |
| 133 | .ctl_name = CTL_DEV, |
| 134 | .procname = "dev", |
| 135 | .maxlen = 0, |
| 136 | .mode = 0555, |
| 137 | .child = raid_dir_table, |
| 138 | }, |
| 139 | { .ctl_name = 0 } |
| 140 | }; |
| 141 | |
| 142 | static struct block_device_operations md_fops; |
| 143 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 144 | static int start_readonly; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 147 | * We have a system wide 'event count' that is incremented |
| 148 | * on any 'interesting' event, and readers of /proc/mdstat |
| 149 | * can use 'poll' or 'select' to find out when the event |
| 150 | * count increases. |
| 151 | * |
| 152 | * Events are: |
| 153 | * start array, stop array, error, add device, remove device, |
| 154 | * start build, activate spare |
| 155 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 156 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 158 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 159 | { |
| 160 | atomic_inc(&md_event_count); |
| 161 | wake_up(&md_event_waiters); |
| 162 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 164 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 165 | /* Alternate version that can be called from interrupts |
| 166 | * when calling sysfs_notify isn't needed. |
| 167 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 168 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
| 172 | } |
| 173 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 174 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * Enables to iterate over all existing md arrays |
| 176 | * all_mddevs_lock protects this list. |
| 177 | */ |
| 178 | static LIST_HEAD(all_mddevs); |
| 179 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * iterates through all used mddevs in the system. |
| 184 | * We take care to grab the all_mddevs_lock whenever navigating |
| 185 | * the list, and to always hold a refcount when unlocked. |
| 186 | * Any code which breaks out of this loop while own |
| 187 | * a reference to the current mddev and must mddev_put it. |
| 188 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 189 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | \ |
| 191 | for (({ spin_lock(&all_mddevs_lock); \ |
| 192 | tmp = all_mddevs.next; \ |
| 193 | mddev = NULL;}); \ |
| 194 | ({ if (tmp != &all_mddevs) \ |
| 195 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 196 | spin_unlock(&all_mddevs_lock); \ |
| 197 | if (mddev) mddev_put(mddev); \ |
| 198 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 199 | tmp != &all_mddevs;}); \ |
| 200 | ({ spin_lock(&all_mddevs_lock); \ |
| 201 | tmp = tmp->next;}) \ |
| 202 | ) |
| 203 | |
| 204 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 205 | static int md_fail_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 207 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 212 | { |
| 213 | atomic_inc(&mddev->active); |
| 214 | return mddev; |
| 215 | } |
| 216 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 217 | static void mddev_delayed_delete(struct work_struct *ws) |
| 218 | { |
| 219 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 220 | kobject_del(&mddev->kobj); |
| 221 | kobject_put(&mddev->kobj); |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static void mddev_put(mddev_t *mddev) |
| 225 | { |
| 226 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 227 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 228 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 229 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 231 | if (mddev->gendisk) { |
| 232 | /* we did a probe so need to clean up. |
| 233 | * Call schedule_work inside the spinlock |
| 234 | * so that flush_scheduled_work() after |
| 235 | * mddev_find will succeed in waiting for the |
| 236 | * work to be done. |
| 237 | */ |
| 238 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 239 | schedule_work(&mddev->del_work); |
| 240 | } else |
| 241 | kfree(mddev); |
| 242 | } |
| 243 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static mddev_t * mddev_find(dev_t unit) |
| 247 | { |
| 248 | mddev_t *mddev, *new = NULL; |
| 249 | |
| 250 | retry: |
| 251 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 253 | if (unit) { |
| 254 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 255 | if (mddev->unit == unit) { |
| 256 | mddev_get(mddev); |
| 257 | spin_unlock(&all_mddevs_lock); |
| 258 | kfree(new); |
| 259 | return mddev; |
| 260 | } |
| 261 | |
| 262 | if (new) { |
| 263 | list_add(&new->all_mddevs, &all_mddevs); |
| 264 | spin_unlock(&all_mddevs_lock); |
| 265 | new->hold_active = UNTIL_IOCTL; |
| 266 | return new; |
| 267 | } |
| 268 | } else if (new) { |
| 269 | /* find an unused unit number */ |
| 270 | static int next_minor = 512; |
| 271 | int start = next_minor; |
| 272 | int is_free = 0; |
| 273 | int dev = 0; |
| 274 | while (!is_free) { |
| 275 | dev = MKDEV(MD_MAJOR, next_minor); |
| 276 | next_minor++; |
| 277 | if (next_minor > MINORMASK) |
| 278 | next_minor = 0; |
| 279 | if (next_minor == start) { |
| 280 | /* Oh dear, all in use. */ |
| 281 | spin_unlock(&all_mddevs_lock); |
| 282 | kfree(new); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | is_free = 1; |
| 287 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 288 | if (mddev->unit == dev) { |
| 289 | is_free = 0; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | new->unit = dev; |
| 294 | new->md_minor = MINOR(dev); |
| 295 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | list_add(&new->all_mddevs, &all_mddevs); |
| 297 | spin_unlock(&all_mddevs_lock); |
| 298 | return new; |
| 299 | } |
| 300 | spin_unlock(&all_mddevs_lock); |
| 301 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 302 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (!new) |
| 304 | return NULL; |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | new->unit = unit; |
| 307 | if (MAJOR(unit) == MD_MAJOR) |
| 308 | new->md_minor = MINOR(unit); |
| 309 | else |
| 310 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 311 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 312 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | INIT_LIST_HEAD(&new->disks); |
| 314 | INIT_LIST_HEAD(&new->all_mddevs); |
| 315 | init_timer(&new->safemode_timer); |
| 316 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 317 | atomic_set(&new->openers, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 318 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 319 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 320 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 321 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 322 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 323 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 324 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | goto retry; |
| 327 | } |
| 328 | |
| 329 | static inline int mddev_lock(mddev_t * mddev) |
| 330 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 331 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static inline int mddev_trylock(mddev_t * mddev) |
| 335 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 336 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static inline void mddev_unlock(mddev_t * mddev) |
| 340 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 341 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 343 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 346 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 348 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 350 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (rdev->desc_nr == nr) |
| 352 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 358 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | mdk_rdev_t *rdev; |
| 360 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 361 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (rdev->bdev->bd_dev == dev) |
| 363 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
| 367 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 368 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 369 | { |
| 370 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 371 | list_for_each_entry(pers, &pers_list, list) { |
| 372 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 373 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 374 | if (strcmp(pers->name, clevel)==0) |
| 375 | return pers; |
| 376 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
| 379 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 380 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 381 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 383 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 384 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 387 | static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 389 | sector_t num_sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | if (chunk_size) |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 392 | num_sectors &= ~((sector_t)chunk_size/512 - 1); |
| 393 | return num_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 397 | { |
| 398 | if (rdev->sb_page) |
| 399 | MD_BUG(); |
| 400 | |
| 401 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 402 | if (!rdev->sb_page) { |
| 403 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 404 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 411 | { |
| 412 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 413 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | rdev->sb_loaded = 0; |
| 415 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 416 | rdev->sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | rdev->size = 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 422 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 423 | { |
| 424 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 425 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 426 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 427 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 428 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 429 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 430 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 431 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 432 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 433 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 434 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 435 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 436 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 437 | } |
| 438 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 439 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 440 | { |
| 441 | struct bio *bio2 = bio->bi_private; |
| 442 | mdk_rdev_t *rdev = bio2->bi_private; |
| 443 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 444 | |
| 445 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 446 | error == -EOPNOTSUPP) { |
| 447 | unsigned long flags; |
| 448 | /* barriers don't appear to be supported :-( */ |
| 449 | set_bit(BarriersNotsupp, &rdev->flags); |
| 450 | mddev->barriers_work = 0; |
| 451 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 452 | bio2->bi_next = mddev->biolist; |
| 453 | mddev->biolist = bio2; |
| 454 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 455 | wake_up(&mddev->sb_wait); |
| 456 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 457 | } else { |
| 458 | bio_put(bio2); |
| 459 | bio->bi_private = rdev; |
| 460 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 461 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 462 | } |
| 463 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 464 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 465 | sector_t sector, int size, struct page *page) |
| 466 | { |
| 467 | /* write first size bytes of page to sector of rdev |
| 468 | * Increment mddev->pending_writes before returning |
| 469 | * and decrement it on completion, waking up sb_wait |
| 470 | * if zero is reached. |
| 471 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 472 | * |
| 473 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 474 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 475 | */ |
| 476 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame^] | 477 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 478 | |
| 479 | bio->bi_bdev = rdev->bdev; |
| 480 | bio->bi_sector = sector; |
| 481 | bio_add_page(bio, page, size, 0); |
| 482 | bio->bi_private = rdev; |
| 483 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 484 | bio->bi_rw = rw; |
| 485 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 486 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 487 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 488 | struct bio *rbio; |
| 489 | rw |= (1<<BIO_RW_BARRIER); |
| 490 | rbio = bio_clone(bio, GFP_NOIO); |
| 491 | rbio->bi_private = bio; |
| 492 | rbio->bi_end_io = super_written_barrier; |
| 493 | submit_bio(rw, rbio); |
| 494 | } else |
| 495 | submit_bio(rw, bio); |
| 496 | } |
| 497 | |
| 498 | void md_super_wait(mddev_t *mddev) |
| 499 | { |
| 500 | /* wait for all superblock writes that were scheduled to complete. |
| 501 | * if any had to be retried (due to BARRIER problems), retry them |
| 502 | */ |
| 503 | DEFINE_WAIT(wq); |
| 504 | for(;;) { |
| 505 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 506 | if (atomic_read(&mddev->pending_writes)==0) |
| 507 | break; |
| 508 | while (mddev->biolist) { |
| 509 | struct bio *bio; |
| 510 | spin_lock_irq(&mddev->write_lock); |
| 511 | bio = mddev->biolist; |
| 512 | mddev->biolist = bio->bi_next ; |
| 513 | bio->bi_next = NULL; |
| 514 | spin_unlock_irq(&mddev->write_lock); |
| 515 | submit_bio(bio->bi_rw, bio); |
| 516 | } |
| 517 | schedule(); |
| 518 | } |
| 519 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 520 | } |
| 521 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 522 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 527 | 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] | 528 | struct page *page, int rw) |
| 529 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 530 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | struct completion event; |
| 532 | int ret; |
| 533 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame^] | 534 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | bio->bi_bdev = bdev; |
| 537 | bio->bi_sector = sector; |
| 538 | bio_add_page(bio, page, size, 0); |
| 539 | init_completion(&event); |
| 540 | bio->bi_private = &event; |
| 541 | bio->bi_end_io = bi_complete; |
| 542 | submit_bio(rw, bio); |
| 543 | wait_for_completion(&event); |
| 544 | |
| 545 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 546 | bio_put(bio); |
| 547 | return ret; |
| 548 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 549 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 551 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | char b[BDEVNAME_SIZE]; |
| 554 | if (!rdev->sb_page) { |
| 555 | MD_BUG(); |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | if (rdev->sb_loaded) |
| 559 | return 0; |
| 560 | |
| 561 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 562 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | goto fail; |
| 564 | rdev->sb_loaded = 1; |
| 565 | return 0; |
| 566 | |
| 567 | fail: |
| 568 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 569 | bdevname(rdev->bdev,b)); |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 574 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 575 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 576 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 577 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 578 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 582 | { |
| 583 | int ret; |
| 584 | mdp_super_t *tmp1, *tmp2; |
| 585 | |
| 586 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 587 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 588 | |
| 589 | if (!tmp1 || !tmp2) { |
| 590 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 591 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | goto abort; |
| 593 | } |
| 594 | |
| 595 | *tmp1 = *sb1; |
| 596 | *tmp2 = *sb2; |
| 597 | |
| 598 | /* |
| 599 | * nr_disks is not constant |
| 600 | */ |
| 601 | tmp1->nr_disks = 0; |
| 602 | tmp2->nr_disks = 0; |
| 603 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 604 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 606 | kfree(tmp1); |
| 607 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 611 | |
| 612 | static u32 md_csum_fold(u32 csum) |
| 613 | { |
| 614 | csum = (csum & 0xffff) + (csum >> 16); |
| 615 | return (csum & 0xffff) + (csum >> 16); |
| 616 | } |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 619 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 620 | u64 newcsum = 0; |
| 621 | u32 *sb32 = (u32*)sb; |
| 622 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | unsigned int disk_csum, csum; |
| 624 | |
| 625 | disk_csum = sb->sb_csum; |
| 626 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 627 | |
| 628 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 629 | newcsum += sb32[i]; |
| 630 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 631 | |
| 632 | |
| 633 | #ifdef CONFIG_ALPHA |
| 634 | /* This used to use csum_partial, which was wrong for several |
| 635 | * reasons including that different results are returned on |
| 636 | * different architectures. It isn't critical that we get exactly |
| 637 | * the same return value as before (we always csum_fold before |
| 638 | * testing, and that removes any differences). However as we |
| 639 | * know that csum_partial always returned a 16bit value on |
| 640 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 641 | */ |
| 642 | sb->sb_csum = md_csum_fold(disk_csum); |
| 643 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 645 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return csum; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | * Handle superblock details. |
| 652 | * We want to be able to handle multiple superblock formats |
| 653 | * so we have a common interface to them all, and an array of |
| 654 | * different handlers. |
| 655 | * We rely on user-space to write the initial superblock, and support |
| 656 | * reading and updating of superblocks. |
| 657 | * Interface methods are: |
| 658 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 659 | * loads and validates a superblock on dev. |
| 660 | * if refdev != NULL, compare superblocks on both devices |
| 661 | * Return: |
| 662 | * 0 - dev has a superblock that is compatible with refdev |
| 663 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 664 | * so dev should be used as the refdev in future |
| 665 | * -EINVAL superblock incompatible or invalid |
| 666 | * -othererror e.g. -EIO |
| 667 | * |
| 668 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 669 | * Verify that dev is acceptable into mddev. |
| 670 | * The first time, mddev->raid_disks will be 0, and data from |
| 671 | * dev should be merged in. Subsequent calls check that dev |
| 672 | * is new enough. Return 0 or -EINVAL |
| 673 | * |
| 674 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 675 | * Update the superblock for rdev with data in mddev |
| 676 | * This does not write to disc. |
| 677 | * |
| 678 | */ |
| 679 | |
| 680 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 681 | char *name; |
| 682 | struct module *owner; |
| 683 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 684 | int minor_version); |
| 685 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 686 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 687 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 688 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
| 691 | /* |
| 692 | * load_super for 0.90.0 |
| 693 | */ |
| 694 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 695 | { |
| 696 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 697 | mdp_super_t *sb; |
| 698 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
| 700 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 701 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | * it's at the end of the disk. |
| 703 | * |
| 704 | * It also happens to be a multiple of 4Kb. |
| 705 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 706 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 708 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (ret) return ret; |
| 710 | |
| 711 | ret = -EINVAL; |
| 712 | |
| 713 | bdevname(rdev->bdev, b); |
| 714 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 715 | |
| 716 | if (sb->md_magic != MD_SB_MAGIC) { |
| 717 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 718 | b); |
| 719 | goto abort; |
| 720 | } |
| 721 | |
| 722 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 723 | sb->minor_version < 90 || |
| 724 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 726 | sb->major_version, sb->minor_version, |
| 727 | b); |
| 728 | goto abort; |
| 729 | } |
| 730 | |
| 731 | if (sb->raid_disks <= 0) |
| 732 | goto abort; |
| 733 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 734 | 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] | 735 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 736 | b); |
| 737 | goto abort; |
| 738 | } |
| 739 | |
| 740 | rdev->preferred_minor = sb->md_minor; |
| 741 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 742 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 744 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 745 | if (sb->level != 1 && sb->level != 4 |
| 746 | && sb->level != 5 && sb->level != 6 |
| 747 | && sb->level != 10) { |
| 748 | /* FIXME use a better test */ |
| 749 | printk(KERN_WARNING |
| 750 | "md: bitmaps not supported for this level.\n"); |
| 751 | goto abort; |
| 752 | } |
| 753 | } |
| 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (sb->level == LEVEL_MULTIPATH) |
| 756 | rdev->desc_nr = -1; |
| 757 | else |
| 758 | rdev->desc_nr = sb->this_disk.number; |
| 759 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 760 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 762 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | __u64 ev1, ev2; |
| 764 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 765 | if (!uuid_equal(refsb, sb)) { |
| 766 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 767 | b, bdevname(refdev->bdev,b2)); |
| 768 | goto abort; |
| 769 | } |
| 770 | if (!sb_equal(refsb, sb)) { |
| 771 | printk(KERN_WARNING "md: %s has same UUID" |
| 772 | " but different superblock to %s\n", |
| 773 | b, bdevname(refdev->bdev, b2)); |
| 774 | goto abort; |
| 775 | } |
| 776 | ev1 = md_event(sb); |
| 777 | ev2 = md_event(refsb); |
| 778 | if (ev1 > ev2) |
| 779 | ret = 1; |
| 780 | else |
| 781 | ret = 0; |
| 782 | } |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 783 | rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 785 | if (rdev->size < sb->size && sb->level > 1) |
| 786 | /* "this cannot possibly happen" ... */ |
| 787 | ret = -EINVAL; |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | abort: |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * validate_super for 0.90.0 |
| 795 | */ |
| 796 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 797 | { |
| 798 | mdp_disk_t *desc; |
| 799 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 800 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 802 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 803 | clear_bit(Faulty, &rdev->flags); |
| 804 | clear_bit(In_sync, &rdev->flags); |
| 805 | clear_bit(WriteMostly, &rdev->flags); |
| 806 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (mddev->raid_disks == 0) { |
| 809 | mddev->major_version = 0; |
| 810 | mddev->minor_version = sb->minor_version; |
| 811 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 812 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | mddev->chunk_size = sb->chunk_size; |
| 814 | mddev->ctime = sb->ctime; |
| 815 | mddev->utime = sb->utime; |
| 816 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 817 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | mddev->layout = sb->layout; |
| 819 | mddev->raid_disks = sb->raid_disks; |
| 820 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 821 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 822 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 823 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 825 | if (mddev->minor_version >= 91) { |
| 826 | mddev->reshape_position = sb->reshape_position; |
| 827 | mddev->delta_disks = sb->delta_disks; |
| 828 | mddev->new_level = sb->new_level; |
| 829 | mddev->new_layout = sb->new_layout; |
| 830 | mddev->new_chunk = sb->new_chunk; |
| 831 | } else { |
| 832 | mddev->reshape_position = MaxSector; |
| 833 | mddev->delta_disks = 0; |
| 834 | mddev->new_level = mddev->level; |
| 835 | mddev->new_layout = mddev->layout; |
| 836 | mddev->new_chunk = mddev->chunk_size; |
| 837 | } |
| 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 840 | mddev->recovery_cp = MaxSector; |
| 841 | else { |
| 842 | if (sb->events_hi == sb->cp_events_hi && |
| 843 | sb->events_lo == sb->cp_events_lo) { |
| 844 | mddev->recovery_cp = sb->recovery_cp; |
| 845 | } else |
| 846 | mddev->recovery_cp = 0; |
| 847 | } |
| 848 | |
| 849 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 850 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 851 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 852 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 853 | |
| 854 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 855 | |
| 856 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 857 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 858 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 859 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 860 | } else if (mddev->pers == NULL) { |
| 861 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | ++ev1; |
| 863 | if (ev1 < mddev->events) |
| 864 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 865 | } else if (mddev->bitmap) { |
| 866 | /* if adding to array with a bitmap, then we can accept an |
| 867 | * older device ... but not too old. |
| 868 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 869 | if (ev1 < mddev->bitmap->events_cleared) |
| 870 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 871 | } else { |
| 872 | if (ev1 < mddev->events) |
| 873 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 874 | return 0; |
| 875 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | desc = sb->disks + rdev->desc_nr; |
| 879 | |
| 880 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 881 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 882 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 883 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 884 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | rdev->raid_disk = desc->raid_disk; |
| 886 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 887 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 888 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 889 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 890 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * sync_super for 0.90.0 |
| 896 | */ |
| 897 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 898 | { |
| 899 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | mdk_rdev_t *rdev2; |
| 901 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* make rdev->sb match mddev data.. |
| 905 | * |
| 906 | * 1/ zero out disks |
| 907 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 908 | * 3/ any empty disks < next_spare become removed |
| 909 | * |
| 910 | * disks[0] gets initialised to REMOVED because |
| 911 | * we cannot be sure from other fields if it has |
| 912 | * been initialised or not. |
| 913 | */ |
| 914 | int i; |
| 915 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 916 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 917 | rdev->sb_size = MD_SB_BYTES; |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 920 | |
| 921 | memset(sb, 0, sizeof(*sb)); |
| 922 | |
| 923 | sb->md_magic = MD_SB_MAGIC; |
| 924 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | sb->patch_version = mddev->patch_version; |
| 926 | sb->gvalid_words = 0; /* ignored */ |
| 927 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 928 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 929 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 930 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 931 | |
| 932 | sb->ctime = mddev->ctime; |
| 933 | sb->level = mddev->level; |
| 934 | sb->size = mddev->size; |
| 935 | sb->raid_disks = mddev->raid_disks; |
| 936 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 937 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | sb->utime = mddev->utime; |
| 939 | sb->state = 0; |
| 940 | sb->events_hi = (mddev->events>>32); |
| 941 | sb->events_lo = (u32)mddev->events; |
| 942 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 943 | if (mddev->reshape_position == MaxSector) |
| 944 | sb->minor_version = 90; |
| 945 | else { |
| 946 | sb->minor_version = 91; |
| 947 | sb->reshape_position = mddev->reshape_position; |
| 948 | sb->new_level = mddev->new_level; |
| 949 | sb->delta_disks = mddev->delta_disks; |
| 950 | sb->new_layout = mddev->new_layout; |
| 951 | sb->new_chunk = mddev->new_chunk; |
| 952 | } |
| 953 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (mddev->in_sync) |
| 955 | { |
| 956 | sb->recovery_cp = mddev->recovery_cp; |
| 957 | sb->cp_events_hi = (mddev->events>>32); |
| 958 | sb->cp_events_lo = (u32)mddev->events; |
| 959 | if (mddev->recovery_cp == MaxSector) |
| 960 | sb->state = (1<< MD_SB_CLEAN); |
| 961 | } else |
| 962 | sb->recovery_cp = 0; |
| 963 | |
| 964 | sb->layout = mddev->layout; |
| 965 | sb->chunk_size = mddev->chunk_size; |
| 966 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 967 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 968 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 971 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 973 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 974 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 975 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 976 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 978 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 979 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | d = &sb->disks[rdev2->desc_nr]; |
| 981 | nr_disks++; |
| 982 | d->number = rdev2->desc_nr; |
| 983 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 984 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 985 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 986 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | d->raid_disk = rdev2->raid_disk; |
| 988 | else |
| 989 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 990 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 992 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | d->state = (1<<MD_DISK_ACTIVE); |
| 994 | d->state |= (1<<MD_DISK_SYNC); |
| 995 | active++; |
| 996 | working++; |
| 997 | } else { |
| 998 | d->state = 0; |
| 999 | spare++; |
| 1000 | working++; |
| 1001 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1002 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1003 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1006 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1007 | mdp_disk_t *d = &sb->disks[i]; |
| 1008 | if (d->state == 0 && d->number == 0) { |
| 1009 | d->number = i; |
| 1010 | d->raid_disk = i; |
| 1011 | d->state = (1<<MD_DISK_REMOVED); |
| 1012 | d->state |= (1<<MD_DISK_FAULTY); |
| 1013 | failed++; |
| 1014 | } |
| 1015 | } |
| 1016 | sb->nr_disks = nr_disks; |
| 1017 | sb->active_disks = active; |
| 1018 | sb->working_disks = working; |
| 1019 | sb->failed_disks = failed; |
| 1020 | sb->spare_disks = spare; |
| 1021 | |
| 1022 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1023 | sb->sb_csum = calc_sb_csum(sb); |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1027 | * rdev_size_change for 0.90.0 |
| 1028 | */ |
| 1029 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1030 | super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1031 | { |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1032 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1033 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1034 | if (rdev->mddev->bitmap_offset) |
| 1035 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1036 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1037 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1038 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1039 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1040 | rdev->sb_page); |
| 1041 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1042 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | * version 1 superblock |
| 1048 | */ |
| 1049 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1050 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1052 | __le32 disk_csum; |
| 1053 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | unsigned long long newcsum; |
| 1055 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1056 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | int i; |
| 1058 | |
| 1059 | disk_csum = sb->sb_csum; |
| 1060 | sb->sb_csum = 0; |
| 1061 | newcsum = 0; |
| 1062 | for (i=0; size>=4; size -= 4 ) |
| 1063 | newcsum += le32_to_cpu(*isuper++); |
| 1064 | |
| 1065 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1066 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1069 | sb->sb_csum = disk_csum; |
| 1070 | return cpu_to_le32(csum); |
| 1071 | } |
| 1072 | |
| 1073 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1074 | { |
| 1075 | struct mdp_superblock_1 *sb; |
| 1076 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1077 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1079 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1082 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | * It is always aligned to a 4K boundary and |
| 1084 | * depeding on minor_version, it can be: |
| 1085 | * 0: At least 8K, but less than 12K, from end of device |
| 1086 | * 1: At start of device |
| 1087 | * 2: 4K from start of device. |
| 1088 | */ |
| 1089 | switch(minor_version) { |
| 1090 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1091 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1092 | sb_start -= 8*2; |
| 1093 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | break; |
| 1095 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1096 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | break; |
| 1098 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1099 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | break; |
| 1101 | default: |
| 1102 | return -EINVAL; |
| 1103 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1104 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1106 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1107 | * and it is safe to read 4k, so we do that |
| 1108 | */ |
| 1109 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (ret) return ret; |
| 1111 | |
| 1112 | |
| 1113 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1114 | |
| 1115 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1116 | sb->major_version != cpu_to_le32(1) || |
| 1117 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1118 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1119 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | return -EINVAL; |
| 1121 | |
| 1122 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1123 | printk("md: invalid superblock checksum on %s\n", |
| 1124 | bdevname(rdev->bdev,b)); |
| 1125 | return -EINVAL; |
| 1126 | } |
| 1127 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1128 | printk("md: data_size too small on %s\n", |
| 1129 | bdevname(rdev->bdev,b)); |
| 1130 | return -EINVAL; |
| 1131 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1132 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1133 | if (sb->level != cpu_to_le32(1) && |
| 1134 | sb->level != cpu_to_le32(4) && |
| 1135 | sb->level != cpu_to_le32(5) && |
| 1136 | sb->level != cpu_to_le32(6) && |
| 1137 | sb->level != cpu_to_le32(10)) { |
| 1138 | printk(KERN_WARNING |
| 1139 | "md: bitmaps not supported for this level.\n"); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | } |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | rdev->preferred_minor = 0xffff; |
| 1145 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1146 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1148 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1149 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1150 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1151 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1152 | |
| 1153 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1154 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1155 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1156 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1157 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1158 | rdev->desc_nr = -1; |
| 1159 | else |
| 1160 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1161 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1162 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1163 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1164 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | __u64 ev1, ev2; |
| 1166 | struct mdp_superblock_1 *refsb = |
| 1167 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1168 | |
| 1169 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1170 | sb->level != refsb->level || |
| 1171 | sb->layout != refsb->layout || |
| 1172 | sb->chunksize != refsb->chunksize) { |
| 1173 | printk(KERN_WARNING "md: %s has strangely different" |
| 1174 | " superblock to %s\n", |
| 1175 | bdevname(rdev->bdev,b), |
| 1176 | bdevname(refdev->bdev,b2)); |
| 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | ev1 = le64_to_cpu(sb->events); |
| 1180 | ev2 = le64_to_cpu(refsb->events); |
| 1181 | |
| 1182 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1183 | ret = 1; |
| 1184 | else |
| 1185 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1187 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1189 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1190 | rdev->size = rdev->sb_start / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1192 | return -EINVAL; |
| 1193 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1194 | if (le32_to_cpu(sb->chunksize)) |
| 1195 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1196 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1197 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1198 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1199 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1203 | { |
| 1204 | 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] | 1205 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1207 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1208 | clear_bit(Faulty, &rdev->flags); |
| 1209 | clear_bit(In_sync, &rdev->flags); |
| 1210 | clear_bit(WriteMostly, &rdev->flags); |
| 1211 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | if (mddev->raid_disks == 0) { |
| 1214 | mddev->major_version = 1; |
| 1215 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1216 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1218 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1219 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1220 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1221 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | mddev->layout = le32_to_cpu(sb->layout); |
| 1223 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1224 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1225 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1226 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1227 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1230 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1231 | |
| 1232 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1233 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1234 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1235 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1236 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1237 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1238 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1239 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1240 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1241 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1242 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1243 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1244 | } else { |
| 1245 | mddev->reshape_position = MaxSector; |
| 1246 | mddev->delta_disks = 0; |
| 1247 | mddev->new_level = mddev->level; |
| 1248 | mddev->new_layout = mddev->layout; |
| 1249 | mddev->new_chunk = mddev->chunk_size; |
| 1250 | } |
| 1251 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1252 | } else if (mddev->pers == NULL) { |
| 1253 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | ++ev1; |
| 1255 | if (ev1 < mddev->events) |
| 1256 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1257 | } else if (mddev->bitmap) { |
| 1258 | /* If adding to array with a bitmap, then we can accept an |
| 1259 | * older device, but not too old. |
| 1260 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1261 | if (ev1 < mddev->bitmap->events_cleared) |
| 1262 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1263 | } else { |
| 1264 | if (ev1 < mddev->events) |
| 1265 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1266 | return 0; |
| 1267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1269 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1271 | switch(role) { |
| 1272 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | break; |
| 1274 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1275 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | break; |
| 1277 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1278 | if ((le32_to_cpu(sb->feature_map) & |
| 1279 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1280 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1281 | else |
| 1282 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | rdev->raid_disk = role; |
| 1284 | break; |
| 1285 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1286 | if (sb->devflags & WriteMostly1) |
| 1287 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1288 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1289 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1295 | { |
| 1296 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | mdk_rdev_t *rdev2; |
| 1298 | int max_dev, i; |
| 1299 | /* make rdev->sb match mddev and rdev data. */ |
| 1300 | |
| 1301 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1302 | |
| 1303 | sb->feature_map = 0; |
| 1304 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1305 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1307 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1308 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1309 | |
| 1310 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1311 | sb->events = cpu_to_le64(mddev->events); |
| 1312 | if (mddev->in_sync) |
| 1313 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1314 | else |
| 1315 | sb->resync_offset = cpu_to_le64(0); |
| 1316 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1317 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1318 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1319 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1320 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1321 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1322 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1323 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1324 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1325 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1326 | |
| 1327 | if (rdev->raid_disk >= 0 && |
| 1328 | !test_bit(In_sync, &rdev->flags) && |
| 1329 | rdev->recovery_offset > 0) { |
| 1330 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1331 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1332 | } |
| 1333 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1334 | if (mddev->reshape_position != MaxSector) { |
| 1335 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1336 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1337 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1338 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1339 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1340 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1341 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1344 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | if (rdev2->desc_nr+1 > max_dev) |
| 1346 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1349 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | for (i=0; i<max_dev;i++) |
| 1351 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1352 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1353 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1355 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1357 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1359 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1360 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | else |
| 1362 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1363 | } |
| 1364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1366 | } |
| 1367 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1368 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1369 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1370 | { |
| 1371 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1372 | sector_t max_sectors; |
| 1373 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1374 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1375 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1376 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1377 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1378 | max_sectors -= rdev->data_offset; |
| 1379 | if (!num_sectors || num_sectors > max_sectors) |
| 1380 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1381 | } else if (rdev->mddev->bitmap_offset) { |
| 1382 | /* minor version 0 with bitmap we can't move */ |
| 1383 | return 0; |
| 1384 | } else { |
| 1385 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1386 | sector_t sb_start; |
| 1387 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1388 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1389 | max_sectors = rdev->size * 2 + sb_start - rdev->sb_start; |
| 1390 | if (!num_sectors || num_sectors > max_sectors) |
| 1391 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1392 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1393 | } |
| 1394 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1395 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1396 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1397 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1398 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1399 | rdev->sb_page); |
| 1400 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1401 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1404 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | [0] = { |
| 1406 | .name = "0.90.0", |
| 1407 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1408 | .load_super = super_90_load, |
| 1409 | .validate_super = super_90_validate, |
| 1410 | .sync_super = super_90_sync, |
| 1411 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | }, |
| 1413 | [1] = { |
| 1414 | .name = "md-1", |
| 1415 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1416 | .load_super = super_1_load, |
| 1417 | .validate_super = super_1_validate, |
| 1418 | .sync_super = super_1_sync, |
| 1419 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | }, |
| 1421 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
| 1423 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1424 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1425 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1427 | rcu_read_lock(); |
| 1428 | rdev_for_each_rcu(rdev, mddev1) |
| 1429 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1430 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1431 | rdev2->bdev->bd_contains) { |
| 1432 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1433 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1434 | } |
| 1435 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | static LIST_HEAD(pending_raid_disks); |
| 1440 | |
| 1441 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1442 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1443 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1444 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1445 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1446 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
| 1448 | if (rdev->mddev) { |
| 1449 | MD_BUG(); |
| 1450 | return -EINVAL; |
| 1451 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1452 | |
| 1453 | /* prevent duplicates */ |
| 1454 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1455 | return -EEXIST; |
| 1456 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1457 | /* make sure rdev->size exceeds mddev->size */ |
| 1458 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1459 | if (mddev->pers) { |
| 1460 | /* Cannot change size, so fail |
| 1461 | * If mddev->level <= 0, then we don't care |
| 1462 | * about aligning sizes (e.g. linear) |
| 1463 | */ |
| 1464 | if (mddev->level > 0) |
| 1465 | return -ENOSPC; |
| 1466 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1467 | mddev->size = rdev->size; |
| 1468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
| 1470 | /* Verify rdev->desc_nr is unique. |
| 1471 | * If it is -1, assign a free number, else |
| 1472 | * check number is not in use |
| 1473 | */ |
| 1474 | if (rdev->desc_nr < 0) { |
| 1475 | int choice = 0; |
| 1476 | if (mddev->pers) choice = mddev->raid_disks; |
| 1477 | while (find_rdev_nr(mddev, choice)) |
| 1478 | choice++; |
| 1479 | rdev->desc_nr = choice; |
| 1480 | } else { |
| 1481 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1482 | return -EBUSY; |
| 1483 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1484 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1485 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1486 | mdname(mddev), mddev->max_disks); |
| 1487 | return -EBUSY; |
| 1488 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1489 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1490 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1491 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1494 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1495 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1496 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1497 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1498 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1499 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1500 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1501 | kobject_del(&rdev->kobj); |
| 1502 | goto fail; |
| 1503 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1504 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1505 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1506 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1507 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1508 | |
| 1509 | /* May as well allow recovery to be retried once */ |
| 1510 | mddev->recovery_disabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1512 | |
| 1513 | fail: |
| 1514 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1515 | b, mdname(mddev)); |
| 1516 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1519 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1520 | { |
| 1521 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1522 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1523 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1527 | { |
| 1528 | char b[BDEVNAME_SIZE]; |
| 1529 | if (!rdev->mddev) { |
| 1530 | MD_BUG(); |
| 1531 | return; |
| 1532 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1533 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1534 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1536 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1537 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1538 | sysfs_put(rdev->sysfs_state); |
| 1539 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1540 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1541 | * writing to 'remove' to "dev/state". We also need |
| 1542 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1543 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1544 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1545 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1546 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1547 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * prevent the device from being mounted, repartitioned or |
| 1552 | * otherwise reused by a RAID array (or any other kernel |
| 1553 | * subsystem), by bd_claiming the device. |
| 1554 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1555 | 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] | 1556 | { |
| 1557 | int err = 0; |
| 1558 | struct block_device *bdev; |
| 1559 | char b[BDEVNAME_SIZE]; |
| 1560 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1561 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | if (IS_ERR(bdev)) { |
| 1563 | printk(KERN_ERR "md: could not open %s.\n", |
| 1564 | __bdevname(dev, b)); |
| 1565 | return PTR_ERR(bdev); |
| 1566 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1567 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | if (err) { |
| 1569 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1570 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1571 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | return err; |
| 1573 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1574 | if (!shared) |
| 1575 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | rdev->bdev = bdev; |
| 1577 | return err; |
| 1578 | } |
| 1579 | |
| 1580 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1581 | { |
| 1582 | struct block_device *bdev = rdev->bdev; |
| 1583 | rdev->bdev = NULL; |
| 1584 | if (!bdev) |
| 1585 | MD_BUG(); |
| 1586 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1587 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | void md_autodetect_dev(dev_t dev); |
| 1591 | |
| 1592 | static void export_rdev(mdk_rdev_t * rdev) |
| 1593 | { |
| 1594 | char b[BDEVNAME_SIZE]; |
| 1595 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1596 | bdevname(rdev->bdev,b)); |
| 1597 | if (rdev->mddev) |
| 1598 | MD_BUG(); |
| 1599 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1601 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1602 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | #endif |
| 1604 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1605 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1609 | { |
| 1610 | unbind_rdev_from_array(rdev); |
| 1611 | export_rdev(rdev); |
| 1612 | } |
| 1613 | |
| 1614 | static void export_array(mddev_t *mddev) |
| 1615 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1616 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1618 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | if (!rdev->mddev) { |
| 1620 | MD_BUG(); |
| 1621 | continue; |
| 1622 | } |
| 1623 | kick_rdev_from_array(rdev); |
| 1624 | } |
| 1625 | if (!list_empty(&mddev->disks)) |
| 1626 | MD_BUG(); |
| 1627 | mddev->raid_disks = 0; |
| 1628 | mddev->major_version = 0; |
| 1629 | } |
| 1630 | |
| 1631 | static void print_desc(mdp_disk_t *desc) |
| 1632 | { |
| 1633 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1634 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1635 | } |
| 1636 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1637 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
| 1639 | int i; |
| 1640 | |
| 1641 | printk(KERN_INFO |
| 1642 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1643 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1644 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1645 | sb->ctime); |
| 1646 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1647 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1648 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1649 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1650 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1651 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1652 | sb->failed_disks, sb->spare_disks, |
| 1653 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1654 | |
| 1655 | printk(KERN_INFO); |
| 1656 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1657 | mdp_disk_t *desc; |
| 1658 | |
| 1659 | desc = sb->disks + i; |
| 1660 | if (desc->number || desc->major || desc->minor || |
| 1661 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1662 | printk(" D %2d: ", i); |
| 1663 | print_desc(desc); |
| 1664 | } |
| 1665 | } |
| 1666 | printk(KERN_INFO "md: THIS: "); |
| 1667 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1670 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1671 | { |
| 1672 | __u8 *uuid; |
| 1673 | |
| 1674 | uuid = sb->set_uuid; |
| 1675 | printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1676 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1677 | KERN_INFO "md: Name: \"%s\" CT:%llu\n", |
| 1678 | le32_to_cpu(sb->major_version), |
| 1679 | le32_to_cpu(sb->feature_map), |
| 1680 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1681 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1682 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1683 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1684 | sb->set_name, |
| 1685 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1686 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1687 | |
| 1688 | uuid = sb->device_uuid; |
| 1689 | printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
| 1690 | " RO:%llu\n" |
| 1691 | KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1692 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1693 | KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1694 | KERN_INFO "md: (MaxDev:%u) \n", |
| 1695 | le32_to_cpu(sb->level), |
| 1696 | (unsigned long long)le64_to_cpu(sb->size), |
| 1697 | le32_to_cpu(sb->raid_disks), |
| 1698 | le32_to_cpu(sb->layout), |
| 1699 | le32_to_cpu(sb->chunksize), |
| 1700 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1701 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1702 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1703 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1704 | le32_to_cpu(sb->dev_number), |
| 1705 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1706 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1707 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1708 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1709 | sb->devflags, |
| 1710 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1711 | (unsigned long long)le64_to_cpu(sb->events), |
| 1712 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1713 | le32_to_cpu(sb->sb_csum), |
| 1714 | le32_to_cpu(sb->max_dev) |
| 1715 | ); |
| 1716 | } |
| 1717 | |
| 1718 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | { |
| 1720 | char b[BDEVNAME_SIZE]; |
| 1721 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1722 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1723 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1724 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1726 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1727 | switch (major_version) { |
| 1728 | case 0: |
| 1729 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1730 | break; |
| 1731 | case 1: |
| 1732 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1733 | break; |
| 1734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | } else |
| 1736 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1737 | } |
| 1738 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1739 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1741 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | mdk_rdev_t *rdev; |
| 1743 | mddev_t *mddev; |
| 1744 | char b[BDEVNAME_SIZE]; |
| 1745 | |
| 1746 | printk("\n"); |
| 1747 | printk("md: **********************************\n"); |
| 1748 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1749 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1750 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1752 | if (mddev->bitmap) |
| 1753 | bitmap_print_sb(mddev->bitmap); |
| 1754 | else |
| 1755 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1756 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1758 | printk("\n"); |
| 1759 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1760 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1761 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } |
| 1763 | printk("md: **********************************\n"); |
| 1764 | printk("\n"); |
| 1765 | } |
| 1766 | |
| 1767 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1768 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1770 | /* Update each superblock (in-memory image), but |
| 1771 | * if we are allowed to, skip spares which already |
| 1772 | * have the right event counter, or have one earlier |
| 1773 | * (which would mean they aren't being marked as dirty |
| 1774 | * with the rest of the array) |
| 1775 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1778 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1779 | if (rdev->sb_events == mddev->events || |
| 1780 | (nospares && |
| 1781 | rdev->raid_disk < 0 && |
| 1782 | (rdev->sb_events&1)==0 && |
| 1783 | rdev->sb_events+1 == mddev->events)) { |
| 1784 | /* Don't update this superblock */ |
| 1785 | rdev->sb_loaded = 2; |
| 1786 | } else { |
| 1787 | super_types[mddev->major_version]. |
| 1788 | sync_super(mddev, rdev); |
| 1789 | rdev->sb_loaded = 1; |
| 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1794 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1797 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1798 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1800 | if (mddev->external) |
| 1801 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1803 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1804 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1805 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1806 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1807 | force_change = 1; |
| 1808 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1809 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1810 | * though if events isn't the right even/odd, we will have to do |
| 1811 | * spares after all |
| 1812 | */ |
| 1813 | nospares = 1; |
| 1814 | if (force_change) |
| 1815 | nospares = 0; |
| 1816 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1817 | /* If the array is degraded, then skipping spares is both |
| 1818 | * dangerous and fairly pointless. |
| 1819 | * Dangerous because a device that was removed from the array |
| 1820 | * might have a event_count that still looks up-to-date, |
| 1821 | * so it can be re-added without a resync. |
| 1822 | * Pointless because if there are any spares to skip, |
| 1823 | * then a recovery will happen and soon that array won't |
| 1824 | * be degraded any more and the spare can go back to sleep then. |
| 1825 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1826 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1827 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1828 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1830 | |
| 1831 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1832 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1833 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1834 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1835 | && (mddev->events & 1) |
| 1836 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1837 | mddev->events--; |
| 1838 | else { |
| 1839 | /* otherwise we have to go forward and ... */ |
| 1840 | mddev->events ++; |
| 1841 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1842 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1843 | if ((mddev->events&1)==0) { |
| 1844 | mddev->events++; |
| 1845 | nospares = 0; |
| 1846 | } |
| 1847 | } else { |
| 1848 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1849 | if ((mddev->events&1)) { |
| 1850 | mddev->events++; |
| 1851 | nospares = 0; |
| 1852 | } |
| 1853 | } |
| 1854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
| 1856 | if (!mddev->events) { |
| 1857 | /* |
| 1858 | * oops, this 64-bit counter should never wrap. |
| 1859 | * Either we are in around ~1 trillion A.C., assuming |
| 1860 | * 1 reboot per second, or we have a bug: |
| 1861 | */ |
| 1862 | MD_BUG(); |
| 1863 | mddev->events --; |
| 1864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | |
| 1866 | /* |
| 1867 | * do not write anything to disk if using |
| 1868 | * nonpersistent superblocks |
| 1869 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1870 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1871 | if (!mddev->external) |
| 1872 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1873 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1874 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1875 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1877 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1878 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1879 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
| 1881 | dprintk(KERN_INFO |
| 1882 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1883 | mdname(mddev),mddev->in_sync); |
| 1884 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1885 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1886 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | char b[BDEVNAME_SIZE]; |
| 1888 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1889 | if (rdev->sb_loaded != 1) |
| 1890 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1891 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | dprintk("(skipping faulty "); |
| 1893 | |
| 1894 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1895 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1896 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1897 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1898 | rdev->sb_page); |
| 1899 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1900 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1901 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1902 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } else |
| 1905 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1906 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | /* only need to write one superblock... */ |
| 1908 | break; |
| 1909 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1910 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1911 | /* 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] | 1912 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1913 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1914 | if (mddev->in_sync != sync_req || |
| 1915 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1916 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1917 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1918 | goto repeat; |
| 1919 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1920 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1921 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1922 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 1926 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1927 | * We want to accept with case. For this we use cmd_match. |
| 1928 | */ |
| 1929 | static int cmd_match(const char *cmd, const char *str) |
| 1930 | { |
| 1931 | /* See if cmd, written into a sysfs file, matches |
| 1932 | * str. They must either be the same, or cmd can |
| 1933 | * have a trailing newline |
| 1934 | */ |
| 1935 | while (*cmd && *str && *cmd == *str) { |
| 1936 | cmd++; |
| 1937 | str++; |
| 1938 | } |
| 1939 | if (*cmd == '\n') |
| 1940 | cmd++; |
| 1941 | if (*str || *cmd) |
| 1942 | return 0; |
| 1943 | return 1; |
| 1944 | } |
| 1945 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1946 | struct rdev_sysfs_entry { |
| 1947 | struct attribute attr; |
| 1948 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1949 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1950 | }; |
| 1951 | |
| 1952 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1953 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1954 | { |
| 1955 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1956 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1957 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1958 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1959 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1960 | sep = ","; |
| 1961 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1962 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1963 | len += sprintf(page+len, "%sin_sync",sep); |
| 1964 | sep = ","; |
| 1965 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1966 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1967 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1968 | sep = ","; |
| 1969 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1970 | if (test_bit(Blocked, &rdev->flags)) { |
| 1971 | len += sprintf(page+len, "%sblocked", sep); |
| 1972 | sep = ","; |
| 1973 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1974 | if (!test_bit(Faulty, &rdev->flags) && |
| 1975 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1976 | len += sprintf(page+len, "%sspare", sep); |
| 1977 | sep = ","; |
| 1978 | } |
| 1979 | return len+sprintf(page+len, "\n"); |
| 1980 | } |
| 1981 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1982 | static ssize_t |
| 1983 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1984 | { |
| 1985 | /* can write |
| 1986 | * faulty - simulates and error |
| 1987 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1988 | * writemostly - sets write_mostly |
| 1989 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1990 | * blocked - sets the Blocked flag |
| 1991 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1992 | */ |
| 1993 | int err = -EINVAL; |
| 1994 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1995 | md_error(rdev->mddev, rdev); |
| 1996 | err = 0; |
| 1997 | } else if (cmd_match(buf, "remove")) { |
| 1998 | if (rdev->raid_disk >= 0) |
| 1999 | err = -EBUSY; |
| 2000 | else { |
| 2001 | mddev_t *mddev = rdev->mddev; |
| 2002 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2003 | if (mddev->pers) |
| 2004 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2005 | md_new_event(mddev); |
| 2006 | err = 0; |
| 2007 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2008 | } else if (cmd_match(buf, "writemostly")) { |
| 2009 | set_bit(WriteMostly, &rdev->flags); |
| 2010 | err = 0; |
| 2011 | } else if (cmd_match(buf, "-writemostly")) { |
| 2012 | clear_bit(WriteMostly, &rdev->flags); |
| 2013 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2014 | } else if (cmd_match(buf, "blocked")) { |
| 2015 | set_bit(Blocked, &rdev->flags); |
| 2016 | err = 0; |
| 2017 | } else if (cmd_match(buf, "-blocked")) { |
| 2018 | clear_bit(Blocked, &rdev->flags); |
| 2019 | wake_up(&rdev->blocked_wait); |
| 2020 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2021 | md_wakeup_thread(rdev->mddev->thread); |
| 2022 | |
| 2023 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2024 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2025 | if (!err && rdev->sysfs_state) |
| 2026 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2027 | return err ? err : len; |
| 2028 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2029 | static struct rdev_sysfs_entry rdev_state = |
| 2030 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2031 | |
| 2032 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2033 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2034 | { |
| 2035 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2036 | } |
| 2037 | |
| 2038 | static ssize_t |
| 2039 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2040 | { |
| 2041 | char *e; |
| 2042 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2043 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2044 | atomic_set(&rdev->corrected_errors, n); |
| 2045 | return len; |
| 2046 | } |
| 2047 | return -EINVAL; |
| 2048 | } |
| 2049 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2050 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2051 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2052 | static ssize_t |
| 2053 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2054 | { |
| 2055 | if (rdev->raid_disk < 0) |
| 2056 | return sprintf(page, "none\n"); |
| 2057 | else |
| 2058 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2059 | } |
| 2060 | |
| 2061 | static ssize_t |
| 2062 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2063 | { |
| 2064 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2065 | int err; |
| 2066 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2067 | int slot = simple_strtoul(buf, &e, 10); |
| 2068 | if (strncmp(buf, "none", 4)==0) |
| 2069 | slot = -1; |
| 2070 | else if (e==buf || (*e && *e!= '\n')) |
| 2071 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2072 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2073 | /* Setting 'slot' on an active array requires also |
| 2074 | * updating the 'rd%d' link, and communicating |
| 2075 | * with the personality with ->hot_*_disk. |
| 2076 | * For now we only support removing |
| 2077 | * failed/spare devices. This normally happens automatically, |
| 2078 | * but not when the metadata is externally managed. |
| 2079 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2080 | if (rdev->raid_disk == -1) |
| 2081 | return -EEXIST; |
| 2082 | /* personality does all needed checks */ |
| 2083 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2084 | return -EINVAL; |
| 2085 | err = rdev->mddev->pers-> |
| 2086 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2087 | if (err) |
| 2088 | return err; |
| 2089 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2090 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2091 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2092 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2093 | } else if (rdev->mddev->pers) { |
| 2094 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2095 | /* Activating a spare .. or possibly reactivating |
| 2096 | * if we every get bitmaps working here. |
| 2097 | */ |
| 2098 | |
| 2099 | if (rdev->raid_disk != -1) |
| 2100 | return -EBUSY; |
| 2101 | |
| 2102 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2103 | return -EINVAL; |
| 2104 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2105 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2106 | if (rdev2->raid_disk == slot) |
| 2107 | return -EEXIST; |
| 2108 | |
| 2109 | rdev->raid_disk = slot; |
| 2110 | if (test_bit(In_sync, &rdev->flags)) |
| 2111 | rdev->saved_raid_disk = slot; |
| 2112 | else |
| 2113 | rdev->saved_raid_disk = -1; |
| 2114 | err = rdev->mddev->pers-> |
| 2115 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2116 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2117 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2118 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2119 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2120 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2121 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2122 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2123 | printk(KERN_WARNING |
| 2124 | "md: cannot register " |
| 2125 | "%s for %s\n", |
| 2126 | nm, mdname(rdev->mddev)); |
| 2127 | |
| 2128 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2129 | } else { |
| 2130 | if (slot >= rdev->mddev->raid_disks) |
| 2131 | return -ENOSPC; |
| 2132 | rdev->raid_disk = slot; |
| 2133 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2134 | clear_bit(Faulty, &rdev->flags); |
| 2135 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2136 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2137 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2138 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2139 | return len; |
| 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2144 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2145 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2146 | static ssize_t |
| 2147 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2148 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2149 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | static ssize_t |
| 2153 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2154 | { |
| 2155 | char *e; |
| 2156 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2157 | if (e==buf || (*e && *e != '\n')) |
| 2158 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2159 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2160 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2161 | if (rdev->size && rdev->mddev->external) |
| 2162 | /* Must set offset before size, so overlap checks |
| 2163 | * can be sane */ |
| 2164 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2165 | rdev->data_offset = offset; |
| 2166 | return len; |
| 2167 | } |
| 2168 | |
| 2169 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2170 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2171 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2172 | static ssize_t |
| 2173 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2174 | { |
| 2175 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2176 | } |
| 2177 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2178 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2179 | { |
| 2180 | /* check if two start/length pairs overlap */ |
| 2181 | if (s1+l1 <= s2) |
| 2182 | return 0; |
| 2183 | if (s2+l2 <= s1) |
| 2184 | return 0; |
| 2185 | return 1; |
| 2186 | } |
| 2187 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2188 | static ssize_t |
| 2189 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2190 | { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2191 | unsigned long long size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2192 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2193 | mddev_t *my_mddev = rdev->mddev; |
| 2194 | |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2195 | if (strict_strtoull(buf, 10, &size) < 0) |
| 2196 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2197 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2198 | if (my_mddev->persistent) { |
| 2199 | size = super_types[my_mddev->major_version]. |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2200 | rdev_size_change(rdev, size * 2); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2201 | if (!size) |
| 2202 | return -EBUSY; |
| 2203 | } else if (!size) { |
| 2204 | size = (rdev->bdev->bd_inode->i_size >> 10); |
| 2205 | size -= rdev->data_offset/2; |
| 2206 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2207 | } |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2208 | if (size < my_mddev->size) |
| 2209 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2210 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2211 | rdev->size = size; |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2212 | if (size > oldsize && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2213 | /* need to check that all other rdevs with the same ->bdev |
| 2214 | * do not overlap. We need to unlock the mddev to avoid |
| 2215 | * a deadlock. We have already changed rdev->size, and if |
| 2216 | * we have to change it back, we will have the lock again. |
| 2217 | */ |
| 2218 | mddev_t *mddev; |
| 2219 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2220 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2221 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2222 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2223 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2224 | mdk_rdev_t *rdev2; |
| 2225 | |
| 2226 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2227 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2228 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2229 | (rdev->bdev == rdev2->bdev && |
| 2230 | rdev != rdev2 && |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2231 | overlaps(rdev->data_offset, rdev->size * 2, |
| 2232 | rdev2->data_offset, |
| 2233 | rdev2->size * 2))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2234 | overlap = 1; |
| 2235 | break; |
| 2236 | } |
| 2237 | mddev_unlock(mddev); |
| 2238 | if (overlap) { |
| 2239 | mddev_put(mddev); |
| 2240 | break; |
| 2241 | } |
| 2242 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2243 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2244 | if (overlap) { |
| 2245 | /* Someone else could have slipped in a size |
| 2246 | * change here, but doing so is just silly. |
| 2247 | * We put oldsize back because we *know* it is |
| 2248 | * safe, and trust userspace not to race with |
| 2249 | * itself |
| 2250 | */ |
| 2251 | rdev->size = oldsize; |
| 2252 | return -EBUSY; |
| 2253 | } |
| 2254 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2255 | return len; |
| 2256 | } |
| 2257 | |
| 2258 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2259 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2260 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2261 | static struct attribute *rdev_default_attrs[] = { |
| 2262 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2263 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2264 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2265 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2266 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2267 | NULL, |
| 2268 | }; |
| 2269 | static ssize_t |
| 2270 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2271 | { |
| 2272 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2273 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2274 | mddev_t *mddev = rdev->mddev; |
| 2275 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2276 | |
| 2277 | if (!entry->show) |
| 2278 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2279 | |
| 2280 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2281 | if (!rv) { |
| 2282 | if (rdev->mddev == NULL) |
| 2283 | rv = -EBUSY; |
| 2284 | else |
| 2285 | rv = entry->show(rdev, page); |
| 2286 | mddev_unlock(mddev); |
| 2287 | } |
| 2288 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | static ssize_t |
| 2292 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2293 | const char *page, size_t length) |
| 2294 | { |
| 2295 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2296 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2297 | ssize_t rv; |
| 2298 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2299 | |
| 2300 | if (!entry->store) |
| 2301 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2302 | if (!capable(CAP_SYS_ADMIN)) |
| 2303 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2304 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2305 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2306 | if (rdev->mddev == NULL) |
| 2307 | rv = -EBUSY; |
| 2308 | else |
| 2309 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2310 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2311 | } |
| 2312 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | static void rdev_free(struct kobject *ko) |
| 2316 | { |
| 2317 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2318 | kfree(rdev); |
| 2319 | } |
| 2320 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2321 | .show = rdev_attr_show, |
| 2322 | .store = rdev_attr_store, |
| 2323 | }; |
| 2324 | static struct kobj_type rdev_ktype = { |
| 2325 | .release = rdev_free, |
| 2326 | .sysfs_ops = &rdev_sysfs_ops, |
| 2327 | .default_attrs = rdev_default_attrs, |
| 2328 | }; |
| 2329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | /* |
| 2331 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2332 | * |
| 2333 | * mark the device faulty if: |
| 2334 | * |
| 2335 | * - the device is nonexistent (zero size) |
| 2336 | * - the device has no valid superblock |
| 2337 | * |
| 2338 | * a faulty rdev _never_ has rdev->sb set. |
| 2339 | */ |
| 2340 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2341 | { |
| 2342 | char b[BDEVNAME_SIZE]; |
| 2343 | int err; |
| 2344 | mdk_rdev_t *rdev; |
| 2345 | sector_t size; |
| 2346 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2347 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | if (!rdev) { |
| 2349 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2350 | return ERR_PTR(-ENOMEM); |
| 2351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | |
| 2353 | if ((err = alloc_disk_sb(rdev))) |
| 2354 | goto abort_free; |
| 2355 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2356 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | if (err) |
| 2358 | goto abort_free; |
| 2359 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2360 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2363 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2364 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2365 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2367 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2369 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2370 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
| 2372 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2373 | if (!size) { |
| 2374 | printk(KERN_WARNING |
| 2375 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2376 | bdevname(rdev->bdev,b)); |
| 2377 | err = -EINVAL; |
| 2378 | goto abort_free; |
| 2379 | } |
| 2380 | |
| 2381 | if (super_format >= 0) { |
| 2382 | err = super_types[super_format]. |
| 2383 | load_super(rdev, NULL, super_minor); |
| 2384 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2385 | printk(KERN_WARNING |
| 2386 | "md: %s does not have a valid v%d.%d " |
| 2387 | "superblock, not importing!\n", |
| 2388 | bdevname(rdev->bdev,b), |
| 2389 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | goto abort_free; |
| 2391 | } |
| 2392 | if (err < 0) { |
| 2393 | printk(KERN_WARNING |
| 2394 | "md: could not read %s's sb, not importing!\n", |
| 2395 | bdevname(rdev->bdev,b)); |
| 2396 | goto abort_free; |
| 2397 | } |
| 2398 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2401 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
| 2403 | return rdev; |
| 2404 | |
| 2405 | abort_free: |
| 2406 | if (rdev->sb_page) { |
| 2407 | if (rdev->bdev) |
| 2408 | unlock_rdev(rdev); |
| 2409 | free_disk_sb(rdev); |
| 2410 | } |
| 2411 | kfree(rdev); |
| 2412 | return ERR_PTR(err); |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * Check a full RAID array for plausibility |
| 2417 | */ |
| 2418 | |
| 2419 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2420 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | { |
| 2422 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2423 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | char b[BDEVNAME_SIZE]; |
| 2425 | |
| 2426 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2427 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | switch (super_types[mddev->major_version]. |
| 2429 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2430 | case 1: |
| 2431 | freshest = rdev; |
| 2432 | break; |
| 2433 | case 0: |
| 2434 | break; |
| 2435 | default: |
| 2436 | printk( KERN_ERR \ |
| 2437 | "md: fatal superblock inconsistency in %s" |
| 2438 | " -- removing from array\n", |
| 2439 | bdevname(rdev->bdev,b)); |
| 2440 | kick_rdev_from_array(rdev); |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | super_types[mddev->major_version]. |
| 2445 | validate_super(mddev, freshest); |
| 2446 | |
| 2447 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2448 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2449 | if (rdev->desc_nr >= mddev->max_disks || |
| 2450 | i > mddev->max_disks) { |
| 2451 | printk(KERN_WARNING |
| 2452 | "md: %s: %s: only %d devices permitted\n", |
| 2453 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2454 | mddev->max_disks); |
| 2455 | kick_rdev_from_array(rdev); |
| 2456 | continue; |
| 2457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | if (rdev != freshest) |
| 2459 | if (super_types[mddev->major_version]. |
| 2460 | validate_super(mddev, rdev)) { |
| 2461 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2462 | " from array!\n", |
| 2463 | bdevname(rdev->bdev,b)); |
| 2464 | kick_rdev_from_array(rdev); |
| 2465 | continue; |
| 2466 | } |
| 2467 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2468 | rdev->desc_nr = i++; |
| 2469 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2470 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2471 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2472 | rdev->raid_disk = -1; |
| 2473 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | if (mddev->recovery_cp != MaxSector && |
| 2480 | mddev->level >= 1) |
| 2481 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2482 | " -- starting background reconstruction\n", |
| 2483 | mdname(mddev)); |
| 2484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2487 | static void md_safemode_timeout(unsigned long data); |
| 2488 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2489 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2490 | safe_delay_show(mddev_t *mddev, char *page) |
| 2491 | { |
| 2492 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2493 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2494 | } |
| 2495 | static ssize_t |
| 2496 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2497 | { |
| 2498 | int scale=1; |
| 2499 | int dot=0; |
| 2500 | int i; |
| 2501 | unsigned long msec; |
| 2502 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2503 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2504 | /* remove a period, and count digits after it */ |
| 2505 | if (len >= sizeof(buf)) |
| 2506 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2507 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2508 | for (i=0; i<len; i++) { |
| 2509 | if (dot) { |
| 2510 | if (isdigit(buf[i])) { |
| 2511 | buf[i-1] = buf[i]; |
| 2512 | scale *= 10; |
| 2513 | } |
| 2514 | buf[i] = 0; |
| 2515 | } else if (buf[i] == '.') { |
| 2516 | dot=1; |
| 2517 | buf[i] = 0; |
| 2518 | } |
| 2519 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2520 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2521 | return -EINVAL; |
| 2522 | msec = (msec * 1000) / scale; |
| 2523 | if (msec == 0) |
| 2524 | mddev->safemode_delay = 0; |
| 2525 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2526 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2527 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2528 | if (mddev->safemode_delay == 0) |
| 2529 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2530 | if (mddev->safemode_delay < old_delay) |
| 2531 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2532 | } |
| 2533 | return len; |
| 2534 | } |
| 2535 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2536 | __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] | 2537 | |
| 2538 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2539 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2540 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2541 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2542 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2543 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2544 | else if (mddev->clevel[0]) |
| 2545 | return sprintf(page, "%s\n", mddev->clevel); |
| 2546 | else if (mddev->level != LEVEL_NONE) |
| 2547 | return sprintf(page, "%d\n", mddev->level); |
| 2548 | else |
| 2549 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2550 | } |
| 2551 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2552 | static ssize_t |
| 2553 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2554 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2555 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2556 | if (mddev->pers) |
| 2557 | return -EBUSY; |
| 2558 | if (len == 0) |
| 2559 | return 0; |
| 2560 | if (len >= sizeof(mddev->clevel)) |
| 2561 | return -ENOSPC; |
| 2562 | strncpy(mddev->clevel, buf, len); |
| 2563 | if (mddev->clevel[len-1] == '\n') |
| 2564 | len--; |
| 2565 | mddev->clevel[len] = 0; |
| 2566 | mddev->level = LEVEL_NONE; |
| 2567 | return rv; |
| 2568 | } |
| 2569 | |
| 2570 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2571 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2572 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2573 | |
| 2574 | static ssize_t |
| 2575 | layout_show(mddev_t *mddev, char *page) |
| 2576 | { |
| 2577 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2578 | if (mddev->reshape_position != MaxSector && |
| 2579 | mddev->layout != mddev->new_layout) |
| 2580 | return sprintf(page, "%d (%d)\n", |
| 2581 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2582 | return sprintf(page, "%d\n", mddev->layout); |
| 2583 | } |
| 2584 | |
| 2585 | static ssize_t |
| 2586 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2587 | { |
| 2588 | char *e; |
| 2589 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2590 | |
| 2591 | if (!*buf || (*e && *e != '\n')) |
| 2592 | return -EINVAL; |
| 2593 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2594 | if (mddev->pers) |
| 2595 | return -EBUSY; |
| 2596 | if (mddev->reshape_position != MaxSector) |
| 2597 | mddev->new_layout = n; |
| 2598 | else |
| 2599 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2600 | return len; |
| 2601 | } |
| 2602 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2603 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2604 | |
| 2605 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2606 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2607 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2608 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2609 | if (mddev->raid_disks == 0) |
| 2610 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2611 | if (mddev->reshape_position != MaxSector && |
| 2612 | mddev->delta_disks != 0) |
| 2613 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2614 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2615 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2616 | } |
| 2617 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2618 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2619 | |
| 2620 | static ssize_t |
| 2621 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2622 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2623 | char *e; |
| 2624 | int rv = 0; |
| 2625 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2626 | |
| 2627 | if (!*buf || (*e && *e != '\n')) |
| 2628 | return -EINVAL; |
| 2629 | |
| 2630 | if (mddev->pers) |
| 2631 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2632 | else if (mddev->reshape_position != MaxSector) { |
| 2633 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2634 | mddev->delta_disks = n - olddisks; |
| 2635 | mddev->raid_disks = n; |
| 2636 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2637 | mddev->raid_disks = n; |
| 2638 | return rv ? rv : len; |
| 2639 | } |
| 2640 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2641 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2642 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2643 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2644 | chunk_size_show(mddev_t *mddev, char *page) |
| 2645 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2646 | if (mddev->reshape_position != MaxSector && |
| 2647 | mddev->chunk_size != mddev->new_chunk) |
| 2648 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2649 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2650 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2651 | } |
| 2652 | |
| 2653 | static ssize_t |
| 2654 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2655 | { |
| 2656 | /* can only set chunk_size if array is not yet active */ |
| 2657 | char *e; |
| 2658 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2659 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2660 | if (!*buf || (*e && *e != '\n')) |
| 2661 | return -EINVAL; |
| 2662 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2663 | if (mddev->pers) |
| 2664 | return -EBUSY; |
| 2665 | else if (mddev->reshape_position != MaxSector) |
| 2666 | mddev->new_chunk = n; |
| 2667 | else |
| 2668 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2669 | return len; |
| 2670 | } |
| 2671 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2672 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2673 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2674 | static ssize_t |
| 2675 | resync_start_show(mddev_t *mddev, char *page) |
| 2676 | { |
| 2677 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2678 | } |
| 2679 | |
| 2680 | static ssize_t |
| 2681 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2682 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2683 | char *e; |
| 2684 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2685 | |
| 2686 | if (mddev->pers) |
| 2687 | return -EBUSY; |
| 2688 | if (!*buf || (*e && *e != '\n')) |
| 2689 | return -EINVAL; |
| 2690 | |
| 2691 | mddev->recovery_cp = n; |
| 2692 | return len; |
| 2693 | } |
| 2694 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2695 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2696 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2697 | /* |
| 2698 | * The array state can be: |
| 2699 | * |
| 2700 | * clear |
| 2701 | * No devices, no size, no level |
| 2702 | * Equivalent to STOP_ARRAY ioctl |
| 2703 | * inactive |
| 2704 | * May have some settings, but array is not active |
| 2705 | * all IO results in error |
| 2706 | * When written, doesn't tear down array, but just stops it |
| 2707 | * suspended (not supported yet) |
| 2708 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2709 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2710 | * readonly |
| 2711 | * no resync can happen. no superblocks get written. |
| 2712 | * write requests fail |
| 2713 | * read-auto |
| 2714 | * like readonly, but behaves like 'clean' on a write request. |
| 2715 | * |
| 2716 | * clean - no pending writes, but otherwise active. |
| 2717 | * When written to inactive array, starts without resync |
| 2718 | * If a write request arrives then |
| 2719 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2720 | * if not known, block and switch to write-pending |
| 2721 | * If written to an active array that has pending writes, then fails. |
| 2722 | * active |
| 2723 | * fully active: IO and resync can be happening. |
| 2724 | * When written to inactive array, starts with resync |
| 2725 | * |
| 2726 | * write-pending |
| 2727 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2728 | * |
| 2729 | * active-idle |
| 2730 | * like active, but no writes have been seen for a while (100msec). |
| 2731 | * |
| 2732 | */ |
| 2733 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2734 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2735 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2736 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2737 | "write-pending", "active-idle", NULL }; |
| 2738 | |
| 2739 | static int match_word(const char *word, char **list) |
| 2740 | { |
| 2741 | int n; |
| 2742 | for (n=0; list[n]; n++) |
| 2743 | if (cmd_match(word, list[n])) |
| 2744 | break; |
| 2745 | return n; |
| 2746 | } |
| 2747 | |
| 2748 | static ssize_t |
| 2749 | array_state_show(mddev_t *mddev, char *page) |
| 2750 | { |
| 2751 | enum array_state st = inactive; |
| 2752 | |
| 2753 | if (mddev->pers) |
| 2754 | switch(mddev->ro) { |
| 2755 | case 1: |
| 2756 | st = readonly; |
| 2757 | break; |
| 2758 | case 2: |
| 2759 | st = read_auto; |
| 2760 | break; |
| 2761 | case 0: |
| 2762 | if (mddev->in_sync) |
| 2763 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2764 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2765 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2766 | else if (mddev->safemode) |
| 2767 | st = active_idle; |
| 2768 | else |
| 2769 | st = active; |
| 2770 | } |
| 2771 | else { |
| 2772 | if (list_empty(&mddev->disks) && |
| 2773 | mddev->raid_disks == 0 && |
| 2774 | mddev->size == 0) |
| 2775 | st = clear; |
| 2776 | else |
| 2777 | st = inactive; |
| 2778 | } |
| 2779 | return sprintf(page, "%s\n", array_states[st]); |
| 2780 | } |
| 2781 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2782 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2783 | static int do_md_run(mddev_t * mddev); |
| 2784 | static int restart_array(mddev_t *mddev); |
| 2785 | |
| 2786 | static ssize_t |
| 2787 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2788 | { |
| 2789 | int err = -EINVAL; |
| 2790 | enum array_state st = match_word(buf, array_states); |
| 2791 | switch(st) { |
| 2792 | case bad_word: |
| 2793 | break; |
| 2794 | case clear: |
| 2795 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2796 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2797 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2798 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2799 | break; |
| 2800 | case inactive: |
| 2801 | /* stopping an active array */ |
| 2802 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2803 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2804 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2805 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2806 | } else |
| 2807 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2808 | break; |
| 2809 | case suspended: |
| 2810 | break; /* not supported yet */ |
| 2811 | case readonly: |
| 2812 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2813 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2814 | else { |
| 2815 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2816 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2817 | err = do_md_run(mddev); |
| 2818 | } |
| 2819 | break; |
| 2820 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2821 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2822 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2823 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2824 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2825 | err = restart_array(mddev); |
| 2826 | if (err == 0) { |
| 2827 | mddev->ro = 2; |
| 2828 | set_disk_ro(mddev->gendisk, 0); |
| 2829 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2830 | } else { |
| 2831 | mddev->ro = 2; |
| 2832 | err = do_md_run(mddev); |
| 2833 | } |
| 2834 | break; |
| 2835 | case clean: |
| 2836 | if (mddev->pers) { |
| 2837 | restart_array(mddev); |
| 2838 | spin_lock_irq(&mddev->write_lock); |
| 2839 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2840 | if (mddev->in_sync == 0) { |
| 2841 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2842 | if (mddev->safemode == 1) |
| 2843 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2844 | if (mddev->persistent) |
| 2845 | set_bit(MD_CHANGE_CLEAN, |
| 2846 | &mddev->flags); |
| 2847 | } |
| 2848 | err = 0; |
| 2849 | } else |
| 2850 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2851 | spin_unlock_irq(&mddev->write_lock); |
| 2852 | } else { |
| 2853 | mddev->ro = 0; |
| 2854 | mddev->recovery_cp = MaxSector; |
| 2855 | err = do_md_run(mddev); |
| 2856 | } |
| 2857 | break; |
| 2858 | case active: |
| 2859 | if (mddev->pers) { |
| 2860 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2861 | if (mddev->external) |
| 2862 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2863 | wake_up(&mddev->sb_wait); |
| 2864 | err = 0; |
| 2865 | } else { |
| 2866 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2867 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2868 | err = do_md_run(mddev); |
| 2869 | } |
| 2870 | break; |
| 2871 | case write_pending: |
| 2872 | case active_idle: |
| 2873 | /* these cannot be set */ |
| 2874 | break; |
| 2875 | } |
| 2876 | if (err) |
| 2877 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2878 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 2879 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2880 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2881 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2882 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2883 | static struct md_sysfs_entry md_array_state = |
| 2884 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2885 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2886 | static ssize_t |
| 2887 | null_show(mddev_t *mddev, char *page) |
| 2888 | { |
| 2889 | return -EINVAL; |
| 2890 | } |
| 2891 | |
| 2892 | static ssize_t |
| 2893 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2894 | { |
| 2895 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2896 | /* The new device is added to the array. |
| 2897 | * If the array has a persistent superblock, we read the |
| 2898 | * superblock to initialise info and check validity. |
| 2899 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2900 | * which mainly checks size. |
| 2901 | */ |
| 2902 | char *e; |
| 2903 | int major = simple_strtoul(buf, &e, 10); |
| 2904 | int minor; |
| 2905 | dev_t dev; |
| 2906 | mdk_rdev_t *rdev; |
| 2907 | int err; |
| 2908 | |
| 2909 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2910 | return -EINVAL; |
| 2911 | minor = simple_strtoul(e+1, &e, 10); |
| 2912 | if (*e && *e != '\n') |
| 2913 | return -EINVAL; |
| 2914 | dev = MKDEV(major, minor); |
| 2915 | if (major != MAJOR(dev) || |
| 2916 | minor != MINOR(dev)) |
| 2917 | return -EOVERFLOW; |
| 2918 | |
| 2919 | |
| 2920 | if (mddev->persistent) { |
| 2921 | rdev = md_import_device(dev, mddev->major_version, |
| 2922 | mddev->minor_version); |
| 2923 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2924 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2925 | mdk_rdev_t, same_set); |
| 2926 | err = super_types[mddev->major_version] |
| 2927 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2928 | if (err < 0) |
| 2929 | goto out; |
| 2930 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2931 | } else if (mddev->external) |
| 2932 | rdev = md_import_device(dev, -2, -1); |
| 2933 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2934 | rdev = md_import_device(dev, -1, -1); |
| 2935 | |
| 2936 | if (IS_ERR(rdev)) |
| 2937 | return PTR_ERR(rdev); |
| 2938 | err = bind_rdev_to_array(rdev, mddev); |
| 2939 | out: |
| 2940 | if (err) |
| 2941 | export_rdev(rdev); |
| 2942 | return err ? err : len; |
| 2943 | } |
| 2944 | |
| 2945 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2946 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2947 | |
| 2948 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2949 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2950 | { |
| 2951 | char *end; |
| 2952 | unsigned long chunk, end_chunk; |
| 2953 | |
| 2954 | if (!mddev->bitmap) |
| 2955 | goto out; |
| 2956 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2957 | while (*buf) { |
| 2958 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2959 | if (buf == end) break; |
| 2960 | if (*end == '-') { /* range */ |
| 2961 | buf = end + 1; |
| 2962 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2963 | if (buf == end) break; |
| 2964 | } |
| 2965 | if (*end && !isspace(*end)) break; |
| 2966 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2967 | buf = end; |
| 2968 | while (isspace(*buf)) buf++; |
| 2969 | } |
| 2970 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2971 | out: |
| 2972 | return len; |
| 2973 | } |
| 2974 | |
| 2975 | static struct md_sysfs_entry md_bitmap = |
| 2976 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2977 | |
| 2978 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2979 | size_show(mddev_t *mddev, char *page) |
| 2980 | { |
| 2981 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2982 | } |
| 2983 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2984 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2985 | |
| 2986 | static ssize_t |
| 2987 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2988 | { |
| 2989 | /* If array is inactive, we can reduce the component size, but |
| 2990 | * not increase it (except from 0). |
| 2991 | * If array is active, we can try an on-line resize |
| 2992 | */ |
| 2993 | char *e; |
| 2994 | int err = 0; |
| 2995 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2996 | if (!*buf || *buf == '\n' || |
| 2997 | (*e && *e != '\n')) |
| 2998 | return -EINVAL; |
| 2999 | |
| 3000 | if (mddev->pers) { |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3001 | err = update_size(mddev, size * 2); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3002 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3003 | } else { |
| 3004 | if (mddev->size == 0 || |
| 3005 | mddev->size > size) |
| 3006 | mddev->size = size; |
| 3007 | else |
| 3008 | err = -ENOSPC; |
| 3009 | } |
| 3010 | return err ? err : len; |
| 3011 | } |
| 3012 | |
| 3013 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3014 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3015 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3016 | |
| 3017 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3018 | * This is one of |
| 3019 | * 'none' for arrays with no metadata (good luck...) |
| 3020 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3021 | * or N.M for internally known formats |
| 3022 | */ |
| 3023 | static ssize_t |
| 3024 | metadata_show(mddev_t *mddev, char *page) |
| 3025 | { |
| 3026 | if (mddev->persistent) |
| 3027 | return sprintf(page, "%d.%d\n", |
| 3028 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3029 | else if (mddev->external) |
| 3030 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3031 | else |
| 3032 | return sprintf(page, "none\n"); |
| 3033 | } |
| 3034 | |
| 3035 | static ssize_t |
| 3036 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3037 | { |
| 3038 | int major, minor; |
| 3039 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3040 | /* Changing the details of 'external' metadata is |
| 3041 | * always permitted. Otherwise there must be |
| 3042 | * no devices attached to the array. |
| 3043 | */ |
| 3044 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3045 | ; |
| 3046 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3047 | return -EBUSY; |
| 3048 | |
| 3049 | if (cmd_match(buf, "none")) { |
| 3050 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3051 | mddev->external = 0; |
| 3052 | mddev->major_version = 0; |
| 3053 | mddev->minor_version = 90; |
| 3054 | return len; |
| 3055 | } |
| 3056 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3057 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3058 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3059 | namelen = sizeof(mddev->metadata_type)-1; |
| 3060 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3061 | mddev->metadata_type[namelen] = 0; |
| 3062 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3063 | mddev->metadata_type[--namelen] = 0; |
| 3064 | mddev->persistent = 0; |
| 3065 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3066 | mddev->major_version = 0; |
| 3067 | mddev->minor_version = 90; |
| 3068 | return len; |
| 3069 | } |
| 3070 | major = simple_strtoul(buf, &e, 10); |
| 3071 | if (e==buf || *e != '.') |
| 3072 | return -EINVAL; |
| 3073 | buf = e+1; |
| 3074 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3075 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3076 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3077 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3078 | return -ENOENT; |
| 3079 | mddev->major_version = major; |
| 3080 | mddev->minor_version = minor; |
| 3081 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3082 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3083 | return len; |
| 3084 | } |
| 3085 | |
| 3086 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3087 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3088 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3089 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3090 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3091 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3092 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 3093 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3094 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3095 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3096 | type = "reshape"; |
| 3097 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3098 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3099 | type = "resync"; |
| 3100 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3101 | type = "check"; |
| 3102 | else |
| 3103 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3104 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3105 | type = "recover"; |
| 3106 | } |
| 3107 | return sprintf(page, "%s\n", type); |
| 3108 | } |
| 3109 | |
| 3110 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3111 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3112 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3113 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3114 | return -EINVAL; |
| 3115 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3116 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3117 | if (mddev->sync_thread) { |
| 3118 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3119 | md_unregister_thread(mddev->sync_thread); |
| 3120 | mddev->sync_thread = NULL; |
| 3121 | mddev->recovery = 0; |
| 3122 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3123 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3124 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3125 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3126 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3127 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3128 | else if (cmd_match(page, "recover")) { |
| 3129 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3130 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3131 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3132 | int err; |
| 3133 | if (mddev->pers->start_reshape == NULL) |
| 3134 | return -EINVAL; |
| 3135 | err = mddev->pers->start_reshape(mddev); |
| 3136 | if (err) |
| 3137 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3138 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3139 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3140 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3141 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3142 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3143 | return -EINVAL; |
| 3144 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3145 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3146 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3147 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3148 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3149 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3150 | return len; |
| 3151 | } |
| 3152 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3153 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3154 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3155 | { |
| 3156 | return sprintf(page, "%llu\n", |
| 3157 | (unsigned long long) mddev->resync_mismatches); |
| 3158 | } |
| 3159 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3160 | static struct md_sysfs_entry md_scan_mode = |
| 3161 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3162 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3163 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3164 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3165 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3166 | static ssize_t |
| 3167 | sync_min_show(mddev_t *mddev, char *page) |
| 3168 | { |
| 3169 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3170 | mddev->sync_speed_min ? "local": "system"); |
| 3171 | } |
| 3172 | |
| 3173 | static ssize_t |
| 3174 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3175 | { |
| 3176 | int min; |
| 3177 | char *e; |
| 3178 | if (strncmp(buf, "system", 6)==0) { |
| 3179 | mddev->sync_speed_min = 0; |
| 3180 | return len; |
| 3181 | } |
| 3182 | min = simple_strtoul(buf, &e, 10); |
| 3183 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3184 | return -EINVAL; |
| 3185 | mddev->sync_speed_min = min; |
| 3186 | return len; |
| 3187 | } |
| 3188 | |
| 3189 | static struct md_sysfs_entry md_sync_min = |
| 3190 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3191 | |
| 3192 | static ssize_t |
| 3193 | sync_max_show(mddev_t *mddev, char *page) |
| 3194 | { |
| 3195 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3196 | mddev->sync_speed_max ? "local": "system"); |
| 3197 | } |
| 3198 | |
| 3199 | static ssize_t |
| 3200 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3201 | { |
| 3202 | int max; |
| 3203 | char *e; |
| 3204 | if (strncmp(buf, "system", 6)==0) { |
| 3205 | mddev->sync_speed_max = 0; |
| 3206 | return len; |
| 3207 | } |
| 3208 | max = simple_strtoul(buf, &e, 10); |
| 3209 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3210 | return -EINVAL; |
| 3211 | mddev->sync_speed_max = max; |
| 3212 | return len; |
| 3213 | } |
| 3214 | |
| 3215 | static struct md_sysfs_entry md_sync_max = |
| 3216 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3217 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3218 | static ssize_t |
| 3219 | degraded_show(mddev_t *mddev, char *page) |
| 3220 | { |
| 3221 | return sprintf(page, "%d\n", mddev->degraded); |
| 3222 | } |
| 3223 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3224 | |
| 3225 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3226 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3227 | { |
| 3228 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3229 | } |
| 3230 | |
| 3231 | static ssize_t |
| 3232 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3233 | { |
| 3234 | long n; |
| 3235 | |
| 3236 | if (strict_strtol(buf, 10, &n)) |
| 3237 | return -EINVAL; |
| 3238 | |
| 3239 | if (n != 0 && n != 1) |
| 3240 | return -EINVAL; |
| 3241 | |
| 3242 | mddev->parallel_resync = n; |
| 3243 | |
| 3244 | if (mddev->sync_thread) |
| 3245 | wake_up(&resync_wait); |
| 3246 | |
| 3247 | return len; |
| 3248 | } |
| 3249 | |
| 3250 | /* force parallel resync, even with shared block devices */ |
| 3251 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3252 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3253 | sync_force_parallel_show, sync_force_parallel_store); |
| 3254 | |
| 3255 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3256 | sync_speed_show(mddev_t *mddev, char *page) |
| 3257 | { |
| 3258 | unsigned long resync, dt, db; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3259 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3260 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3261 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3262 | db = resync - mddev->resync_mark_cnt; |
| 3263 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3266 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3267 | |
| 3268 | static ssize_t |
| 3269 | sync_completed_show(mddev_t *mddev, char *page) |
| 3270 | { |
| 3271 | unsigned long max_blocks, resync; |
| 3272 | |
| 3273 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3274 | max_blocks = mddev->resync_max_sectors; |
| 3275 | else |
| 3276 | max_blocks = mddev->size << 1; |
| 3277 | |
| 3278 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3279 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3280 | } |
| 3281 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3282 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3283 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3284 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3285 | min_sync_show(mddev_t *mddev, char *page) |
| 3286 | { |
| 3287 | return sprintf(page, "%llu\n", |
| 3288 | (unsigned long long)mddev->resync_min); |
| 3289 | } |
| 3290 | static ssize_t |
| 3291 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3292 | { |
| 3293 | unsigned long long min; |
| 3294 | if (strict_strtoull(buf, 10, &min)) |
| 3295 | return -EINVAL; |
| 3296 | if (min > mddev->resync_max) |
| 3297 | return -EINVAL; |
| 3298 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3299 | return -EBUSY; |
| 3300 | |
| 3301 | /* Must be a multiple of chunk_size */ |
| 3302 | if (mddev->chunk_size) { |
| 3303 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3304 | return -EINVAL; |
| 3305 | } |
| 3306 | mddev->resync_min = min; |
| 3307 | |
| 3308 | return len; |
| 3309 | } |
| 3310 | |
| 3311 | static struct md_sysfs_entry md_min_sync = |
| 3312 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3313 | |
| 3314 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3315 | max_sync_show(mddev_t *mddev, char *page) |
| 3316 | { |
| 3317 | if (mddev->resync_max == MaxSector) |
| 3318 | return sprintf(page, "max\n"); |
| 3319 | else |
| 3320 | return sprintf(page, "%llu\n", |
| 3321 | (unsigned long long)mddev->resync_max); |
| 3322 | } |
| 3323 | static ssize_t |
| 3324 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3325 | { |
| 3326 | if (strncmp(buf, "max", 3) == 0) |
| 3327 | mddev->resync_max = MaxSector; |
| 3328 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3329 | unsigned long long max; |
| 3330 | if (strict_strtoull(buf, 10, &max)) |
| 3331 | return -EINVAL; |
| 3332 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3333 | return -EINVAL; |
| 3334 | if (max < mddev->resync_max && |
| 3335 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3336 | return -EBUSY; |
| 3337 | |
| 3338 | /* Must be a multiple of chunk_size */ |
| 3339 | if (mddev->chunk_size) { |
| 3340 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3341 | return -EINVAL; |
| 3342 | } |
| 3343 | mddev->resync_max = max; |
| 3344 | } |
| 3345 | wake_up(&mddev->recovery_wait); |
| 3346 | return len; |
| 3347 | } |
| 3348 | |
| 3349 | static struct md_sysfs_entry md_max_sync = |
| 3350 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3351 | |
| 3352 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3353 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3354 | { |
| 3355 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3356 | } |
| 3357 | |
| 3358 | static ssize_t |
| 3359 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3360 | { |
| 3361 | char *e; |
| 3362 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3363 | |
| 3364 | if (mddev->pers->quiesce == NULL) |
| 3365 | return -EINVAL; |
| 3366 | if (buf == e || (*e && *e != '\n')) |
| 3367 | return -EINVAL; |
| 3368 | if (new >= mddev->suspend_hi || |
| 3369 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3370 | mddev->suspend_lo = new; |
| 3371 | mddev->pers->quiesce(mddev, 2); |
| 3372 | return len; |
| 3373 | } else |
| 3374 | return -EINVAL; |
| 3375 | } |
| 3376 | static struct md_sysfs_entry md_suspend_lo = |
| 3377 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3378 | |
| 3379 | |
| 3380 | static ssize_t |
| 3381 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3382 | { |
| 3383 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3384 | } |
| 3385 | |
| 3386 | static ssize_t |
| 3387 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3388 | { |
| 3389 | char *e; |
| 3390 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3391 | |
| 3392 | if (mddev->pers->quiesce == NULL) |
| 3393 | return -EINVAL; |
| 3394 | if (buf == e || (*e && *e != '\n')) |
| 3395 | return -EINVAL; |
| 3396 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3397 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3398 | mddev->suspend_hi = new; |
| 3399 | mddev->pers->quiesce(mddev, 1); |
| 3400 | mddev->pers->quiesce(mddev, 0); |
| 3401 | return len; |
| 3402 | } else |
| 3403 | return -EINVAL; |
| 3404 | } |
| 3405 | static struct md_sysfs_entry md_suspend_hi = |
| 3406 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3407 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3408 | static ssize_t |
| 3409 | reshape_position_show(mddev_t *mddev, char *page) |
| 3410 | { |
| 3411 | if (mddev->reshape_position != MaxSector) |
| 3412 | return sprintf(page, "%llu\n", |
| 3413 | (unsigned long long)mddev->reshape_position); |
| 3414 | strcpy(page, "none\n"); |
| 3415 | return 5; |
| 3416 | } |
| 3417 | |
| 3418 | static ssize_t |
| 3419 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3420 | { |
| 3421 | char *e; |
| 3422 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3423 | if (mddev->pers) |
| 3424 | return -EBUSY; |
| 3425 | if (buf == e || (*e && *e != '\n')) |
| 3426 | return -EINVAL; |
| 3427 | mddev->reshape_position = new; |
| 3428 | mddev->delta_disks = 0; |
| 3429 | mddev->new_level = mddev->level; |
| 3430 | mddev->new_layout = mddev->layout; |
| 3431 | mddev->new_chunk = mddev->chunk_size; |
| 3432 | return len; |
| 3433 | } |
| 3434 | |
| 3435 | static struct md_sysfs_entry md_reshape_position = |
| 3436 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3437 | reshape_position_store); |
| 3438 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3439 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3440 | static struct attribute *md_default_attrs[] = { |
| 3441 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3442 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3443 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3444 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3445 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3446 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3447 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3448 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3449 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3450 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3451 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3452 | NULL, |
| 3453 | }; |
| 3454 | |
| 3455 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3456 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3457 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3458 | &md_sync_min.attr, |
| 3459 | &md_sync_max.attr, |
| 3460 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3461 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3462 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3463 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3464 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3465 | &md_suspend_lo.attr, |
| 3466 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3467 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3468 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3469 | NULL, |
| 3470 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3471 | static struct attribute_group md_redundancy_group = { |
| 3472 | .name = NULL, |
| 3473 | .attrs = md_redundancy_attrs, |
| 3474 | }; |
| 3475 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3476 | |
| 3477 | static ssize_t |
| 3478 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3479 | { |
| 3480 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3481 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3482 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3483 | |
| 3484 | if (!entry->show) |
| 3485 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3486 | rv = mddev_lock(mddev); |
| 3487 | if (!rv) { |
| 3488 | rv = entry->show(mddev, page); |
| 3489 | mddev_unlock(mddev); |
| 3490 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3491 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | static ssize_t |
| 3495 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3496 | const char *page, size_t length) |
| 3497 | { |
| 3498 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3499 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3500 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3501 | |
| 3502 | if (!entry->store) |
| 3503 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3504 | if (!capable(CAP_SYS_ADMIN)) |
| 3505 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3506 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3507 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3508 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3509 | if (!rv) { |
| 3510 | rv = entry->store(mddev, page, length); |
| 3511 | mddev_unlock(mddev); |
| 3512 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3513 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | static void md_free(struct kobject *ko) |
| 3517 | { |
| 3518 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3519 | |
| 3520 | if (mddev->sysfs_state) |
| 3521 | sysfs_put(mddev->sysfs_state); |
| 3522 | |
| 3523 | if (mddev->gendisk) { |
| 3524 | del_gendisk(mddev->gendisk); |
| 3525 | put_disk(mddev->gendisk); |
| 3526 | } |
| 3527 | if (mddev->queue) |
| 3528 | blk_cleanup_queue(mddev->queue); |
| 3529 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3530 | kfree(mddev); |
| 3531 | } |
| 3532 | |
| 3533 | static struct sysfs_ops md_sysfs_ops = { |
| 3534 | .show = md_attr_show, |
| 3535 | .store = md_attr_store, |
| 3536 | }; |
| 3537 | static struct kobj_type md_ktype = { |
| 3538 | .release = md_free, |
| 3539 | .sysfs_ops = &md_sysfs_ops, |
| 3540 | .default_attrs = md_default_attrs, |
| 3541 | }; |
| 3542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | int mdp_major = 0; |
| 3544 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3545 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3547 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | mddev_t *mddev = mddev_find(dev); |
| 3549 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3550 | int partitioned; |
| 3551 | int shift; |
| 3552 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3553 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
| 3555 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3556 | return -ENODEV; |
| 3557 | |
| 3558 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3559 | shift = partitioned ? MdpMinorShift : 0; |
| 3560 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3562 | /* wait for any previous instance if this device |
| 3563 | * to be completed removed (mddev_delayed_delete). |
| 3564 | */ |
| 3565 | flush_scheduled_work(); |
| 3566 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3567 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3569 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3571 | return -EEXIST; |
| 3572 | } |
| 3573 | |
| 3574 | if (name) { |
| 3575 | /* Need to ensure that 'name' is not a duplicate. |
| 3576 | */ |
| 3577 | mddev_t *mddev2; |
| 3578 | spin_lock(&all_mddevs_lock); |
| 3579 | |
| 3580 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3581 | if (mddev2->gendisk && |
| 3582 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3583 | spin_unlock(&all_mddevs_lock); |
| 3584 | return -EEXIST; |
| 3585 | } |
| 3586 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3588 | |
| 3589 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
| 3590 | if (!mddev->queue) { |
| 3591 | mutex_unlock(&disks_mutex); |
| 3592 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3593 | return -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3594 | } |
| 3595 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3596 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3597 | |
| 3598 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | disk = alloc_disk(1 << shift); |
| 3601 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3602 | mutex_unlock(&disks_mutex); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3603 | blk_cleanup_queue(mddev->queue); |
| 3604 | mddev->queue = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3606 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3608 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3610 | if (name) |
| 3611 | strcpy(disk->disk_name, name); |
| 3612 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3614 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | disk->fops = &md_fops; |
| 3617 | disk->private_data = mddev; |
| 3618 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3619 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3620 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3621 | * remove it now. |
| 3622 | */ |
| 3623 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | add_disk(disk); |
| 3625 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3626 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3627 | &disk_to_dev(disk)->kobj, "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3628 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3629 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3630 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3631 | disk->disk_name); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3632 | else { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3633 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3634 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3635 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3636 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3637 | return 0; |
| 3638 | } |
| 3639 | |
| 3640 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3641 | { |
| 3642 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | return NULL; |
| 3644 | } |
| 3645 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3646 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3647 | { |
| 3648 | /* val must be "md_*" where * is not all digits. |
| 3649 | * We allocate an array with a large free minor number, and |
| 3650 | * set the name to val. val must not already be an active name. |
| 3651 | */ |
| 3652 | int len = strlen(val); |
| 3653 | char buf[DISK_NAME_LEN]; |
| 3654 | |
| 3655 | while (len && val[len-1] == '\n') |
| 3656 | len--; |
| 3657 | if (len >= DISK_NAME_LEN) |
| 3658 | return -E2BIG; |
| 3659 | strlcpy(buf, val, len+1); |
| 3660 | if (strncmp(buf, "md_", 3) != 0) |
| 3661 | return -EINVAL; |
| 3662 | return md_alloc(0, buf); |
| 3663 | } |
| 3664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | static void md_safemode_timeout(unsigned long data) |
| 3666 | { |
| 3667 | mddev_t *mddev = (mddev_t *) data; |
| 3668 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3669 | if (!atomic_read(&mddev->writes_pending)) { |
| 3670 | mddev->safemode = 1; |
| 3671 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3672 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | md_wakeup_thread(mddev->thread); |
| 3675 | } |
| 3676 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3677 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | |
| 3679 | static int do_md_run(mddev_t * mddev) |
| 3680 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3681 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | int chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | mdk_rdev_t *rdev; |
| 3684 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3685 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | char b[BDEVNAME_SIZE]; |
| 3687 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3688 | if (list_empty(&mddev->disks)) |
| 3689 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | |
| 3692 | if (mddev->pers) |
| 3693 | return -EBUSY; |
| 3694 | |
| 3695 | /* |
| 3696 | * Analyze all RAID superblock(s) |
| 3697 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3698 | if (!mddev->raid_disks) { |
| 3699 | if (!mddev->persistent) |
| 3700 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3701 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | |
| 3704 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3706 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3708 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3709 | chunk_size, MAX_CHUNK_SIZE); |
| 3710 | return -EINVAL; |
| 3711 | } |
| 3712 | /* |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3713 | * chunk-size has to be a power of 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | */ |
| 3715 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3716 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | return -EINVAL; |
| 3718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | |
| 3720 | /* devices must have minimum size of one chunk */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3721 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3722 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | continue; |
| 3724 | if (rdev->size < chunk_size / 1024) { |
| 3725 | printk(KERN_WARNING |
| 3726 | "md: Dev %s smaller than chunk_size:" |
| 3727 | " %lluk < %dk\n", |
| 3728 | bdevname(rdev->bdev,b), |
| 3729 | (unsigned long long)rdev->size, |
| 3730 | chunk_size / 1024); |
| 3731 | return -EINVAL; |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3736 | if (mddev->level != LEVEL_NONE) |
| 3737 | request_module("md-level-%d", mddev->level); |
| 3738 | else if (mddev->clevel[0]) |
| 3739 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | |
| 3741 | /* |
| 3742 | * Drop all container device buffers, from now on |
| 3743 | * the only valid external interface is through the md |
| 3744 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3746 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3747 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | continue; |
| 3749 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3750 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3751 | |
| 3752 | /* perform some consistency tests on the device. |
| 3753 | * We don't want the data to overlap the metadata, |
| 3754 | * Internal Bitmap issues has handled elsewhere. |
| 3755 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3756 | if (rdev->data_offset < rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3757 | if (mddev->size && |
| 3758 | rdev->data_offset + mddev->size*2 |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3759 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3760 | printk("md: %s: data overlaps metadata\n", |
| 3761 | mdname(mddev)); |
| 3762 | return -EINVAL; |
| 3763 | } |
| 3764 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3765 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3766 | > rdev->data_offset) { |
| 3767 | printk("md: %s: metadata overlaps data\n", |
| 3768 | mdname(mddev)); |
| 3769 | return -EINVAL; |
| 3770 | } |
| 3771 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 3772 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | md_probe(mddev->unit, NULL, NULL); |
| 3776 | disk = mddev->gendisk; |
| 3777 | if (!disk) |
| 3778 | return -ENOMEM; |
| 3779 | |
| 3780 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3781 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3782 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3784 | if (mddev->level != LEVEL_NONE) |
| 3785 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3786 | mddev->level); |
| 3787 | else |
| 3788 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3789 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | return -EINVAL; |
| 3791 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3792 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3794 | mddev->level = pers->level; |
| 3795 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3797 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3798 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3799 | /* This personality cannot handle reshaping... */ |
| 3800 | mddev->pers = NULL; |
| 3801 | module_put(pers->owner); |
| 3802 | return -EINVAL; |
| 3803 | } |
| 3804 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3805 | if (pers->sync_request) { |
| 3806 | /* Warn if this is a potentially silly |
| 3807 | * configuration. |
| 3808 | */ |
| 3809 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3810 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3811 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3812 | |
| 3813 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3814 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3815 | if (rdev < rdev2 && |
| 3816 | rdev->bdev->bd_contains == |
| 3817 | rdev2->bdev->bd_contains) { |
| 3818 | printk(KERN_WARNING |
| 3819 | "%s: WARNING: %s appears to be" |
| 3820 | " on the same physical disk as" |
| 3821 | " %s.\n", |
| 3822 | mdname(mddev), |
| 3823 | bdevname(rdev->bdev,b), |
| 3824 | bdevname(rdev2->bdev,b2)); |
| 3825 | warned = 1; |
| 3826 | } |
| 3827 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3828 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3829 | if (warned) |
| 3830 | printk(KERN_WARNING |
| 3831 | "True protection against single-disk" |
| 3832 | " failure might be compromised.\n"); |
| 3833 | } |
| 3834 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3835 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3837 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3838 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3840 | if (start_readonly) |
| 3841 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3842 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3843 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 3844 | if (err) |
| 3845 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3846 | else if (mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3847 | err = bitmap_create(mddev); |
| 3848 | if (err) { |
| 3849 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3850 | mdname(mddev), err); |
| 3851 | mddev->pers->stop(mddev); |
| 3852 | } |
| 3853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | module_put(mddev->pers->owner); |
| 3856 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3857 | bitmap_destroy(mddev); |
| 3858 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3860 | if (mddev->pers->sync_request) { |
| 3861 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3862 | printk(KERN_WARNING |
| 3863 | "md: cannot register extra attributes for %s\n", |
| 3864 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3865 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3866 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3867 | mddev->ro = 0; |
| 3868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | atomic_set(&mddev->writes_pending,0); |
| 3870 | mddev->safemode = 0; |
| 3871 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3872 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3873 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3875 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3876 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3877 | if (rdev->raid_disk >= 0) { |
| 3878 | char nm[20]; |
| 3879 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3880 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3881 | printk("md: cannot register %s for %s\n", |
| 3882 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | |
| 3885 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3886 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3887 | if (mddev->flags) |
| 3888 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 3890 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | |
| 3892 | /* If we call blk_queue_make_request here, it will |
| 3893 | * re-initialise max_sectors etc which may have been |
| 3894 | * refined inside -> run. So just set the bits we need to set. |
| 3895 | * Most initialisation happended when we called |
| 3896 | * blk_queue_make_request(..., md_fail_request) |
| 3897 | * earlier. |
| 3898 | */ |
| 3899 | mddev->queue->queuedata = mddev; |
| 3900 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3901 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3902 | /* If there is a partially-recovered drive we need to |
| 3903 | * start recovery here. If we leave it to md_check_recovery, |
| 3904 | * it will remove the drives and not do the right thing |
| 3905 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3906 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3907 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3908 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3909 | if (rdev->raid_disk >= 0 && |
| 3910 | !test_bit(In_sync, &rdev->flags) && |
| 3911 | !test_bit(Faulty, &rdev->flags)) |
| 3912 | /* complete an interrupted recovery */ |
| 3913 | spares++; |
| 3914 | if (spares && mddev->pers->sync_request) { |
| 3915 | mddev->recovery = 0; |
| 3916 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3917 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3918 | mddev, |
| 3919 | "%s_resync"); |
| 3920 | if (!mddev->sync_thread) { |
| 3921 | printk(KERN_ERR "%s: could not start resync" |
| 3922 | " thread...\n", |
| 3923 | mdname(mddev)); |
| 3924 | /* leave the spares where they are, it shouldn't hurt */ |
| 3925 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3926 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3927 | } |
| 3928 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3929 | md_wakeup_thread(mddev->thread); |
| 3930 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3931 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3932 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3933 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3934 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3935 | if (mddev->sysfs_action) |
| 3936 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3937 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3938 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | return 0; |
| 3940 | } |
| 3941 | |
| 3942 | static int restart_array(mddev_t *mddev) |
| 3943 | { |
| 3944 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3946 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3948 | return -ENXIO; |
| 3949 | if (!mddev->pers) |
| 3950 | return -EINVAL; |
| 3951 | if (!mddev->ro) |
| 3952 | return -EBUSY; |
| 3953 | mddev->safemode = 0; |
| 3954 | mddev->ro = 0; |
| 3955 | set_disk_ro(disk, 0); |
| 3956 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3957 | mdname(mddev)); |
| 3958 | /* Kick recovery or resync if necessary */ |
| 3959 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3960 | md_wakeup_thread(mddev->thread); |
| 3961 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3962 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3963 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3966 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3967 | * to the file ourselves */ |
| 3968 | static int deny_bitmap_write_access(struct file * file) |
| 3969 | { |
| 3970 | struct inode *inode = file->f_mapping->host; |
| 3971 | |
| 3972 | spin_lock(&inode->i_lock); |
| 3973 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3974 | spin_unlock(&inode->i_lock); |
| 3975 | return -ETXTBSY; |
| 3976 | } |
| 3977 | atomic_set(&inode->i_writecount, -1); |
| 3978 | spin_unlock(&inode->i_lock); |
| 3979 | |
| 3980 | return 0; |
| 3981 | } |
| 3982 | |
| 3983 | static void restore_bitmap_write_access(struct file *file) |
| 3984 | { |
| 3985 | struct inode *inode = file->f_mapping->host; |
| 3986 | |
| 3987 | spin_lock(&inode->i_lock); |
| 3988 | atomic_set(&inode->i_writecount, 1); |
| 3989 | spin_unlock(&inode->i_lock); |
| 3990 | } |
| 3991 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3992 | /* mode: |
| 3993 | * 0 - completely stop and dis-assemble array |
| 3994 | * 1 - switch to readonly |
| 3995 | * 2 - stop but do not disassemble array |
| 3996 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3997 | static int do_md_stop(mddev_t * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | { |
| 3999 | int err = 0; |
| 4000 | struct gendisk *disk = mddev->gendisk; |
| 4001 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4002 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4003 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4004 | return -EBUSY; |
| 4005 | } |
| 4006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | |
| 4009 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4010 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4012 | md_unregister_thread(mddev->sync_thread); |
| 4013 | mddev->sync_thread = NULL; |
| 4014 | } |
| 4015 | |
| 4016 | del_timer_sync(&mddev->safemode_timer); |
| 4017 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4018 | switch(mode) { |
| 4019 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4021 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | goto out; |
| 4023 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4024 | break; |
| 4025 | case 0: /* disassemble */ |
| 4026 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4027 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4028 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | if (mddev->ro) |
| 4030 | set_disk_ro(disk, 0); |
| 4031 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 4032 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4033 | mddev->queue->merge_bvec_fn = NULL; |
| 4034 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4035 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4036 | if (mddev->pers->sync_request) { |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4037 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4038 | if (mddev->sysfs_action) |
| 4039 | sysfs_put(mddev->sysfs_action); |
| 4040 | mddev->sysfs_action = NULL; |
| 4041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | module_put(mddev->pers->owner); |
| 4043 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4044 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4045 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4046 | |
| 4047 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4048 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | if (mddev->ro) |
| 4051 | mddev->ro = 0; |
| 4052 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4053 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | /* mark array as shutdown cleanly */ |
| 4055 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4056 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4058 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4060 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | /* |
| 4064 | * Free resources if final stop |
| 4065 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4066 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4067 | mdk_rdev_t *rdev; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4070 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4071 | bitmap_destroy(mddev); |
| 4072 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4073 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4074 | fput(mddev->bitmap_file); |
| 4075 | mddev->bitmap_file = NULL; |
| 4076 | } |
| 4077 | mddev->bitmap_offset = 0; |
| 4078 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4079 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4080 | if (rdev->raid_disk >= 0) { |
| 4081 | char nm[20]; |
| 4082 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4083 | sysfs_remove_link(&mddev->kobj, nm); |
| 4084 | } |
| 4085 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4086 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4087 | flush_scheduled_work(); |
| 4088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | export_array(mddev); |
| 4090 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4091 | mddev->array_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4092 | mddev->size = 0; |
| 4093 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4094 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4095 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4096 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4097 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4098 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4099 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4100 | mddev->level = LEVEL_NONE; |
| 4101 | mddev->clevel[0] = 0; |
| 4102 | mddev->flags = 0; |
| 4103 | mddev->ro = 0; |
| 4104 | mddev->metadata_type[0] = 0; |
| 4105 | mddev->chunk_size = 0; |
| 4106 | mddev->ctime = mddev->utime = 0; |
| 4107 | mddev->layout = 0; |
| 4108 | mddev->max_disks = 0; |
| 4109 | mddev->events = 0; |
| 4110 | mddev->delta_disks = 0; |
| 4111 | mddev->new_level = LEVEL_NONE; |
| 4112 | mddev->new_layout = 0; |
| 4113 | mddev->new_chunk = 0; |
| 4114 | mddev->curr_resync = 0; |
| 4115 | mddev->resync_mismatches = 0; |
| 4116 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4117 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4118 | mddev->recovery = 0; |
| 4119 | mddev->in_sync = 0; |
| 4120 | mddev->changed = 0; |
| 4121 | mddev->degraded = 0; |
| 4122 | mddev->barriers_work = 0; |
| 4123 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4124 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4125 | if (mddev->hold_active == UNTIL_STOP) |
| 4126 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4127 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4128 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4130 | mdname(mddev)); |
| 4131 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4132 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4133 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | out: |
| 4135 | return err; |
| 4136 | } |
| 4137 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4138 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | static void autorun_array(mddev_t *mddev) |
| 4140 | { |
| 4141 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | int err; |
| 4143 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4144 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | |
| 4147 | printk(KERN_INFO "md: running: "); |
| 4148 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4149 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | char b[BDEVNAME_SIZE]; |
| 4151 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4152 | } |
| 4153 | printk("\n"); |
| 4154 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4155 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | if (err) { |
| 4157 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4158 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | /* |
| 4163 | * lets try to run arrays based on all disks that have arrived |
| 4164 | * until now. (those are in pending_raid_disks) |
| 4165 | * |
| 4166 | * the method: pick the first pending disk, collect all disks with |
| 4167 | * the same UUID, remove all from the pending list and put them into |
| 4168 | * the 'same_array' list. Then order this list based on superblock |
| 4169 | * update time (freshest comes first), kick out 'old' disks and |
| 4170 | * compare superblocks. If everything's fine then run it. |
| 4171 | * |
| 4172 | * If "unit" is allocated, then bump its reference count |
| 4173 | */ |
| 4174 | static void autorun_devices(int part) |
| 4175 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4176 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | mddev_t *mddev; |
| 4178 | char b[BDEVNAME_SIZE]; |
| 4179 | |
| 4180 | printk(KERN_INFO "md: autorun ...\n"); |
| 4181 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4182 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4184 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | rdev0 = list_entry(pending_raid_disks.next, |
| 4186 | mdk_rdev_t, same_set); |
| 4187 | |
| 4188 | printk(KERN_INFO "md: considering %s ...\n", |
| 4189 | bdevname(rdev0->bdev,b)); |
| 4190 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4191 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4193 | printk(KERN_INFO "md: adding %s ...\n", |
| 4194 | bdevname(rdev->bdev,b)); |
| 4195 | list_move(&rdev->same_set, &candidates); |
| 4196 | } |
| 4197 | /* |
| 4198 | * now we have a set of devices, with all of them having |
| 4199 | * mostly sane superblocks. It's time to allocate the |
| 4200 | * mddev. |
| 4201 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4202 | if (part) { |
| 4203 | dev = MKDEV(mdp_major, |
| 4204 | rdev0->preferred_minor << MdpMinorShift); |
| 4205 | unit = MINOR(dev) >> MdpMinorShift; |
| 4206 | } else { |
| 4207 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4208 | unit = MINOR(dev); |
| 4209 | } |
| 4210 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4212 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4213 | break; |
| 4214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | |
| 4216 | md_probe(dev, NULL, NULL); |
| 4217 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4218 | if (!mddev || !mddev->gendisk) { |
| 4219 | if (mddev) |
| 4220 | mddev_put(mddev); |
| 4221 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | "md: cannot allocate memory for md drive.\n"); |
| 4223 | break; |
| 4224 | } |
| 4225 | if (mddev_lock(mddev)) |
| 4226 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4227 | mdname(mddev)); |
| 4228 | else if (mddev->raid_disks || mddev->major_version |
| 4229 | || !list_empty(&mddev->disks)) { |
| 4230 | printk(KERN_WARNING |
| 4231 | "md: %s already running, cannot run %s\n", |
| 4232 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4233 | mddev_unlock(mddev); |
| 4234 | } else { |
| 4235 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4236 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4237 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | list_del_init(&rdev->same_set); |
| 4239 | if (bind_rdev_to_array(rdev, mddev)) |
| 4240 | export_rdev(rdev); |
| 4241 | } |
| 4242 | autorun_array(mddev); |
| 4243 | mddev_unlock(mddev); |
| 4244 | } |
| 4245 | /* on success, candidates will be empty, on error |
| 4246 | * it won't... |
| 4247 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4248 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4249 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | mddev_put(mddev); |
| 4253 | } |
| 4254 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4255 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4256 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | static int get_version(void __user * arg) |
| 4259 | { |
| 4260 | mdu_version_t ver; |
| 4261 | |
| 4262 | ver.major = MD_MAJOR_VERSION; |
| 4263 | ver.minor = MD_MINOR_VERSION; |
| 4264 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4265 | |
| 4266 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4267 | return -EFAULT; |
| 4268 | |
| 4269 | return 0; |
| 4270 | } |
| 4271 | |
| 4272 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4273 | { |
| 4274 | mdu_array_info_t info; |
| 4275 | int nr,working,active,failed,spare; |
| 4276 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
| 4278 | nr=working=active=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4279 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4281 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | failed++; |
| 4283 | else { |
| 4284 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4285 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | active++; |
| 4287 | else |
| 4288 | spare++; |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | info.major_version = mddev->major_version; |
| 4293 | info.minor_version = mddev->minor_version; |
| 4294 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4295 | info.ctime = mddev->ctime; |
| 4296 | info.level = mddev->level; |
| 4297 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4298 | if (info.size != mddev->size) /* overflow */ |
| 4299 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | info.nr_disks = nr; |
| 4301 | info.raid_disks = mddev->raid_disks; |
| 4302 | info.md_minor = mddev->md_minor; |
| 4303 | info.not_persistent= !mddev->persistent; |
| 4304 | |
| 4305 | info.utime = mddev->utime; |
| 4306 | info.state = 0; |
| 4307 | if (mddev->in_sync) |
| 4308 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4309 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4310 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | info.active_disks = active; |
| 4312 | info.working_disks = working; |
| 4313 | info.failed_disks = failed; |
| 4314 | info.spare_disks = spare; |
| 4315 | |
| 4316 | info.layout = mddev->layout; |
| 4317 | info.chunk_size = mddev->chunk_size; |
| 4318 | |
| 4319 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4320 | return -EFAULT; |
| 4321 | |
| 4322 | return 0; |
| 4323 | } |
| 4324 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4325 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4326 | { |
| 4327 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4328 | char *ptr, *buf = NULL; |
| 4329 | int err = -ENOMEM; |
| 4330 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4331 | if (md_allow_write(mddev)) |
| 4332 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4333 | else |
| 4334 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4335 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4336 | if (!file) |
| 4337 | goto out; |
| 4338 | |
| 4339 | /* bitmap disabled, zero the first byte and copy out */ |
| 4340 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4341 | file->pathname[0] = '\0'; |
| 4342 | goto copy_out; |
| 4343 | } |
| 4344 | |
| 4345 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4346 | if (!buf) |
| 4347 | goto out; |
| 4348 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4349 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4350 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4351 | goto out; |
| 4352 | |
| 4353 | strcpy(file->pathname, ptr); |
| 4354 | |
| 4355 | copy_out: |
| 4356 | err = 0; |
| 4357 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4358 | err = -EFAULT; |
| 4359 | out: |
| 4360 | kfree(buf); |
| 4361 | kfree(file); |
| 4362 | return err; |
| 4363 | } |
| 4364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4366 | { |
| 4367 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | mdk_rdev_t *rdev; |
| 4369 | |
| 4370 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4371 | return -EFAULT; |
| 4372 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4373 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | if (rdev) { |
| 4375 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4376 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4377 | info.raid_disk = rdev->raid_disk; |
| 4378 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4379 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4381 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4383 | info.state |= (1<<MD_DISK_SYNC); |
| 4384 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4385 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4386 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | } else { |
| 4388 | info.major = info.minor = 0; |
| 4389 | info.raid_disk = -1; |
| 4390 | info.state = (1<<MD_DISK_REMOVED); |
| 4391 | } |
| 4392 | |
| 4393 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4394 | return -EFAULT; |
| 4395 | |
| 4396 | return 0; |
| 4397 | } |
| 4398 | |
| 4399 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4400 | { |
| 4401 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4402 | mdk_rdev_t *rdev; |
| 4403 | dev_t dev = MKDEV(info->major,info->minor); |
| 4404 | |
| 4405 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4406 | return -EOVERFLOW; |
| 4407 | |
| 4408 | if (!mddev->raid_disks) { |
| 4409 | int err; |
| 4410 | /* expecting a device which has a superblock */ |
| 4411 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4412 | if (IS_ERR(rdev)) { |
| 4413 | printk(KERN_WARNING |
| 4414 | "md: md_import_device returned %ld\n", |
| 4415 | PTR_ERR(rdev)); |
| 4416 | return PTR_ERR(rdev); |
| 4417 | } |
| 4418 | if (!list_empty(&mddev->disks)) { |
| 4419 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4420 | mdk_rdev_t, same_set); |
| 4421 | int err = super_types[mddev->major_version] |
| 4422 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4423 | if (err < 0) { |
| 4424 | printk(KERN_WARNING |
| 4425 | "md: %s has different UUID to %s\n", |
| 4426 | bdevname(rdev->bdev,b), |
| 4427 | bdevname(rdev0->bdev,b2)); |
| 4428 | export_rdev(rdev); |
| 4429 | return -EINVAL; |
| 4430 | } |
| 4431 | } |
| 4432 | err = bind_rdev_to_array(rdev, mddev); |
| 4433 | if (err) |
| 4434 | export_rdev(rdev); |
| 4435 | return err; |
| 4436 | } |
| 4437 | |
| 4438 | /* |
| 4439 | * add_new_disk can be used once the array is assembled |
| 4440 | * to add "hot spares". They must already have a superblock |
| 4441 | * written |
| 4442 | */ |
| 4443 | if (mddev->pers) { |
| 4444 | int err; |
| 4445 | if (!mddev->pers->hot_add_disk) { |
| 4446 | printk(KERN_WARNING |
| 4447 | "%s: personality does not support diskops!\n", |
| 4448 | mdname(mddev)); |
| 4449 | return -EINVAL; |
| 4450 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4451 | if (mddev->persistent) |
| 4452 | rdev = md_import_device(dev, mddev->major_version, |
| 4453 | mddev->minor_version); |
| 4454 | else |
| 4455 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | if (IS_ERR(rdev)) { |
| 4457 | printk(KERN_WARNING |
| 4458 | "md: md_import_device returned %ld\n", |
| 4459 | PTR_ERR(rdev)); |
| 4460 | return PTR_ERR(rdev); |
| 4461 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4462 | /* set save_raid_disk if appropriate */ |
| 4463 | if (!mddev->persistent) { |
| 4464 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4465 | info->raid_disk < mddev->raid_disks) |
| 4466 | rdev->raid_disk = info->raid_disk; |
| 4467 | else |
| 4468 | rdev->raid_disk = -1; |
| 4469 | } else |
| 4470 | super_types[mddev->major_version]. |
| 4471 | validate_super(mddev, rdev); |
| 4472 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4473 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4474 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4475 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4476 | set_bit(WriteMostly, &rdev->flags); |
| 4477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | rdev->raid_disk = -1; |
| 4479 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4480 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4481 | /* If there is hot_add_disk but no hot_remove_disk |
| 4482 | * then added disks for geometry changes, |
| 4483 | * and should be added immediately. |
| 4484 | */ |
| 4485 | super_types[mddev->major_version]. |
| 4486 | validate_super(mddev, rdev); |
| 4487 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4488 | if (err) |
| 4489 | unbind_rdev_from_array(rdev); |
| 4490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | if (err) |
| 4492 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4493 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4494 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4495 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4496 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4497 | if (mddev->degraded) |
| 4498 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4499 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4500 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | return err; |
| 4502 | } |
| 4503 | |
| 4504 | /* otherwise, add_new_disk is only allowed |
| 4505 | * for major_version==0 superblocks |
| 4506 | */ |
| 4507 | if (mddev->major_version != 0) { |
| 4508 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4509 | mdname(mddev)); |
| 4510 | return -EINVAL; |
| 4511 | } |
| 4512 | |
| 4513 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4514 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4515 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | if (IS_ERR(rdev)) { |
| 4517 | printk(KERN_WARNING |
| 4518 | "md: error, md_import_device() returned %ld\n", |
| 4519 | PTR_ERR(rdev)); |
| 4520 | return PTR_ERR(rdev); |
| 4521 | } |
| 4522 | rdev->desc_nr = info->number; |
| 4523 | if (info->raid_disk < mddev->raid_disks) |
| 4524 | rdev->raid_disk = info->raid_disk; |
| 4525 | else |
| 4526 | rdev->raid_disk = -1; |
| 4527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4529 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4530 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4532 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4533 | set_bit(WriteMostly, &rdev->flags); |
| 4534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | if (!mddev->persistent) { |
| 4536 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4537 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4539 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4540 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4542 | err = bind_rdev_to_array(rdev, mddev); |
| 4543 | if (err) { |
| 4544 | export_rdev(rdev); |
| 4545 | return err; |
| 4546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | return 0; |
| 4550 | } |
| 4551 | |
| 4552 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4553 | { |
| 4554 | char b[BDEVNAME_SIZE]; |
| 4555 | mdk_rdev_t *rdev; |
| 4556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | rdev = find_rdev(mddev, dev); |
| 4558 | if (!rdev) |
| 4559 | return -ENXIO; |
| 4560 | |
| 4561 | if (rdev->raid_disk >= 0) |
| 4562 | goto busy; |
| 4563 | |
| 4564 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4565 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4566 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
| 4568 | return 0; |
| 4569 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4570 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4572 | return -EBUSY; |
| 4573 | } |
| 4574 | |
| 4575 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4576 | { |
| 4577 | char b[BDEVNAME_SIZE]; |
| 4578 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | mdk_rdev_t *rdev; |
| 4580 | |
| 4581 | if (!mddev->pers) |
| 4582 | return -ENODEV; |
| 4583 | |
| 4584 | if (mddev->major_version != 0) { |
| 4585 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4586 | " version-0 superblocks.\n", |
| 4587 | mdname(mddev)); |
| 4588 | return -EINVAL; |
| 4589 | } |
| 4590 | if (!mddev->pers->hot_add_disk) { |
| 4591 | printk(KERN_WARNING |
| 4592 | "%s: personality does not support diskops!\n", |
| 4593 | mdname(mddev)); |
| 4594 | return -EINVAL; |
| 4595 | } |
| 4596 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4597 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | if (IS_ERR(rdev)) { |
| 4599 | printk(KERN_WARNING |
| 4600 | "md: error, md_import_device() returned %ld\n", |
| 4601 | PTR_ERR(rdev)); |
| 4602 | return -EINVAL; |
| 4603 | } |
| 4604 | |
| 4605 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4606 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4608 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4610 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4612 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | printk(KERN_WARNING |
| 4614 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4615 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4616 | err = -EINVAL; |
| 4617 | goto abort_export; |
| 4618 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4619 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4621 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4622 | err = bind_rdev_to_array(rdev, mddev); |
| 4623 | if (err) |
| 4624 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
| 4626 | /* |
| 4627 | * The rest should better be atomic, we can have disk failures |
| 4628 | * noticed in interrupt contexts ... |
| 4629 | */ |
| 4630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | rdev->raid_disk = -1; |
| 4632 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4633 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
| 4635 | /* |
| 4636 | * Kick recovery, maybe this spare has to be added to the |
| 4637 | * array immediately. |
| 4638 | */ |
| 4639 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4640 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4641 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | return 0; |
| 4643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | abort_export: |
| 4645 | export_rdev(rdev); |
| 4646 | return err; |
| 4647 | } |
| 4648 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4649 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4650 | { |
| 4651 | int err; |
| 4652 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4653 | if (mddev->pers) { |
| 4654 | if (!mddev->pers->quiesce) |
| 4655 | return -EBUSY; |
| 4656 | if (mddev->recovery || mddev->sync_thread) |
| 4657 | return -EBUSY; |
| 4658 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4661 | |
| 4662 | if (fd >= 0) { |
| 4663 | if (mddev->bitmap) |
| 4664 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4665 | mddev->bitmap_file = fget(fd); |
| 4666 | |
| 4667 | if (mddev->bitmap_file == NULL) { |
| 4668 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4669 | mdname(mddev)); |
| 4670 | return -EBADF; |
| 4671 | } |
| 4672 | |
| 4673 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4674 | if (err) { |
| 4675 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4676 | mdname(mddev)); |
| 4677 | fput(mddev->bitmap_file); |
| 4678 | mddev->bitmap_file = NULL; |
| 4679 | return err; |
| 4680 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4681 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4682 | } else if (mddev->bitmap == NULL) |
| 4683 | return -ENOENT; /* cannot remove what isn't there */ |
| 4684 | err = 0; |
| 4685 | if (mddev->pers) { |
| 4686 | mddev->pers->quiesce(mddev, 1); |
| 4687 | if (fd >= 0) |
| 4688 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4689 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4690 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4691 | fd = -1; /* make sure to put the file */ |
| 4692 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4693 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4694 | } |
| 4695 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4696 | if (mddev->bitmap_file) { |
| 4697 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4698 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4699 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4700 | mddev->bitmap_file = NULL; |
| 4701 | } |
| 4702 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4703 | return err; |
| 4704 | } |
| 4705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | /* |
| 4707 | * set_array_info is used two different ways |
| 4708 | * The original usage is when creating a new array. |
| 4709 | * In this usage, raid_disks is > 0 and it together with |
| 4710 | * level, size, not_persistent,layout,chunksize determine the |
| 4711 | * shape of the array. |
| 4712 | * This will always create an array with a type-0.90.0 superblock. |
| 4713 | * The newer usage is when assembling an array. |
| 4714 | * In this case raid_disks will be 0, and the major_version field is |
| 4715 | * use to determine which style super-blocks are to be found on the devices. |
| 4716 | * The minor and patch _version numbers are also kept incase the |
| 4717 | * super_block handler wishes to interpret them. |
| 4718 | */ |
| 4719 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4720 | { |
| 4721 | |
| 4722 | if (info->raid_disks == 0) { |
| 4723 | /* just setting version number for superblock loading */ |
| 4724 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4725 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | super_types[info->major_version].name == NULL) { |
| 4727 | /* maybe try to auto-load a module? */ |
| 4728 | printk(KERN_INFO |
| 4729 | "md: superblock version %d not known\n", |
| 4730 | info->major_version); |
| 4731 | return -EINVAL; |
| 4732 | } |
| 4733 | mddev->major_version = info->major_version; |
| 4734 | mddev->minor_version = info->minor_version; |
| 4735 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4736 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | return 0; |
| 4738 | } |
| 4739 | mddev->major_version = MD_MAJOR_VERSION; |
| 4740 | mddev->minor_version = MD_MINOR_VERSION; |
| 4741 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4742 | mddev->ctime = get_seconds(); |
| 4743 | |
| 4744 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4745 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | mddev->size = info->size; |
| 4747 | mddev->raid_disks = info->raid_disks; |
| 4748 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4749 | * openned |
| 4750 | */ |
| 4751 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4752 | mddev->recovery_cp = MaxSector; |
| 4753 | else |
| 4754 | mddev->recovery_cp = 0; |
| 4755 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4756 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | |
| 4758 | mddev->layout = info->layout; |
| 4759 | mddev->chunk_size = info->chunk_size; |
| 4760 | |
| 4761 | mddev->max_disks = MD_SB_DISKS; |
| 4762 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4763 | if (mddev->persistent) |
| 4764 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4765 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4767 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4768 | mddev->bitmap_offset = 0; |
| 4769 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4770 | mddev->reshape_position = MaxSector; |
| 4771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | /* |
| 4773 | * Generate a 128 bit UUID |
| 4774 | */ |
| 4775 | get_random_bytes(mddev->uuid, 16); |
| 4776 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4777 | mddev->new_level = mddev->level; |
| 4778 | mddev->new_chunk = mddev->chunk_size; |
| 4779 | mddev->new_layout = mddev->layout; |
| 4780 | mddev->delta_disks = 0; |
| 4781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | return 0; |
| 4783 | } |
| 4784 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4785 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4786 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4787 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4788 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4789 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4790 | |
| 4791 | if (mddev->pers->resize == NULL) |
| 4792 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4793 | /* The "num_sectors" is the number of sectors of each device that |
| 4794 | * is used. This can only make sense for arrays with redundancy. |
| 4795 | * linear and raid0 always use whatever space is available. We can only |
| 4796 | * consider changing this number if no resync or reconstruction is |
| 4797 | * happening, and if the new size is acceptable. It must fit before the |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4798 | * sb_start or, if that is <data_offset, it must fit before the size |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4799 | * of each device. If num_sectors is zero, we find the largest size |
| 4800 | * that fits. |
| 4801 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4802 | */ |
| 4803 | if (mddev->sync_thread) |
| 4804 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 4805 | if (mddev->bitmap) |
| 4806 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 4807 | * grow, and re-add. |
| 4808 | */ |
| 4809 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4810 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4811 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4812 | avail = rdev->size * 2; |
| 4813 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4814 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 4815 | num_sectors = avail; |
| 4816 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4817 | return -ENOSPC; |
| 4818 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4819 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4820 | if (!rv) { |
| 4821 | struct block_device *bdev; |
| 4822 | |
| 4823 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4824 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4825 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4826 | i_size_write(bdev->bd_inode, |
| 4827 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4828 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4829 | bdput(bdev); |
| 4830 | } |
| 4831 | } |
| 4832 | return rv; |
| 4833 | } |
| 4834 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4835 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4836 | { |
| 4837 | int rv; |
| 4838 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4839 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4840 | return -EINVAL; |
| 4841 | if (raid_disks <= 0 || |
| 4842 | raid_disks >= mddev->max_disks) |
| 4843 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4844 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4845 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4846 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4847 | |
| 4848 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4849 | return rv; |
| 4850 | } |
| 4851 | |
| 4852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | /* |
| 4854 | * update_array_info is used to change the configuration of an |
| 4855 | * on-line array. |
| 4856 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4857 | * fields in the info are checked against the array. |
| 4858 | * Any differences that cannot be handled will cause an error. |
| 4859 | * Normally, only one change can be managed at a time. |
| 4860 | */ |
| 4861 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4862 | { |
| 4863 | int rv = 0; |
| 4864 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4865 | int state = 0; |
| 4866 | |
| 4867 | /* calculate expected state,ignoring low bits */ |
| 4868 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4869 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | |
| 4871 | if (mddev->major_version != info->major_version || |
| 4872 | mddev->minor_version != info->minor_version || |
| 4873 | /* mddev->patch_version != info->patch_version || */ |
| 4874 | mddev->ctime != info->ctime || |
| 4875 | mddev->level != info->level || |
| 4876 | /* mddev->layout != info->layout || */ |
| 4877 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4878 | mddev->chunk_size != info->chunk_size || |
| 4879 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4880 | ((state^info->state) & 0xfffffe00) |
| 4881 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | return -EINVAL; |
| 4883 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4884 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4886 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4887 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | if (cnt == 0) return 0; |
| 4889 | if (cnt > 1) return -EINVAL; |
| 4890 | |
| 4891 | if (mddev->layout != info->layout) { |
| 4892 | /* Change layout |
| 4893 | * we don't need to do anything at the md level, the |
| 4894 | * personality will take care of it all. |
| 4895 | */ |
| 4896 | if (mddev->pers->reconfig == NULL) |
| 4897 | return -EINVAL; |
| 4898 | else |
| 4899 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4900 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4901 | if (info->size >= 0 && mddev->size != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4902 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4904 | if (mddev->raid_disks != info->raid_disks) |
| 4905 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4907 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4908 | if (mddev->pers->quiesce == NULL) |
| 4909 | return -EINVAL; |
| 4910 | if (mddev->recovery || mddev->sync_thread) |
| 4911 | return -EBUSY; |
| 4912 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4913 | /* add the bitmap */ |
| 4914 | if (mddev->bitmap) |
| 4915 | return -EEXIST; |
| 4916 | if (mddev->default_bitmap_offset == 0) |
| 4917 | return -EINVAL; |
| 4918 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4919 | mddev->pers->quiesce(mddev, 1); |
| 4920 | rv = bitmap_create(mddev); |
| 4921 | if (rv) |
| 4922 | bitmap_destroy(mddev); |
| 4923 | mddev->pers->quiesce(mddev, 0); |
| 4924 | } else { |
| 4925 | /* remove the bitmap */ |
| 4926 | if (!mddev->bitmap) |
| 4927 | return -ENOENT; |
| 4928 | if (mddev->bitmap->file) |
| 4929 | return -EINVAL; |
| 4930 | mddev->pers->quiesce(mddev, 1); |
| 4931 | bitmap_destroy(mddev); |
| 4932 | mddev->pers->quiesce(mddev, 0); |
| 4933 | mddev->bitmap_offset = 0; |
| 4934 | } |
| 4935 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4936 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | return rv; |
| 4938 | } |
| 4939 | |
| 4940 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4941 | { |
| 4942 | mdk_rdev_t *rdev; |
| 4943 | |
| 4944 | if (mddev->pers == NULL) |
| 4945 | return -ENODEV; |
| 4946 | |
| 4947 | rdev = find_rdev(mddev, dev); |
| 4948 | if (!rdev) |
| 4949 | return -ENODEV; |
| 4950 | |
| 4951 | md_error(mddev, rdev); |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 4955 | /* |
| 4956 | * We have a problem here : there is no easy way to give a CHS |
| 4957 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4958 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4959 | * dosfs just mad... ;-) |
| 4960 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4961 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4962 | { |
| 4963 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4964 | |
| 4965 | geo->heads = 2; |
| 4966 | geo->sectors = 4; |
| 4967 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4968 | return 0; |
| 4969 | } |
| 4970 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 4971 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | unsigned int cmd, unsigned long arg) |
| 4973 | { |
| 4974 | int err = 0; |
| 4975 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | mddev_t *mddev = NULL; |
| 4977 | |
| 4978 | if (!capable(CAP_SYS_ADMIN)) |
| 4979 | return -EACCES; |
| 4980 | |
| 4981 | /* |
| 4982 | * Commands dealing with the RAID driver but not any |
| 4983 | * particular array: |
| 4984 | */ |
| 4985 | switch (cmd) |
| 4986 | { |
| 4987 | case RAID_VERSION: |
| 4988 | err = get_version(argp); |
| 4989 | goto done; |
| 4990 | |
| 4991 | case PRINT_RAID_DEBUG: |
| 4992 | err = 0; |
| 4993 | md_print_devices(); |
| 4994 | goto done; |
| 4995 | |
| 4996 | #ifndef MODULE |
| 4997 | case RAID_AUTORUN: |
| 4998 | err = 0; |
| 4999 | autostart_arrays(arg); |
| 5000 | goto done; |
| 5001 | #endif |
| 5002 | default:; |
| 5003 | } |
| 5004 | |
| 5005 | /* |
| 5006 | * Commands creating/starting a new array: |
| 5007 | */ |
| 5008 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5009 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | |
| 5011 | if (!mddev) { |
| 5012 | BUG(); |
| 5013 | goto abort; |
| 5014 | } |
| 5015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | err = mddev_lock(mddev); |
| 5017 | if (err) { |
| 5018 | printk(KERN_INFO |
| 5019 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5020 | err, cmd); |
| 5021 | goto abort; |
| 5022 | } |
| 5023 | |
| 5024 | switch (cmd) |
| 5025 | { |
| 5026 | case SET_ARRAY_INFO: |
| 5027 | { |
| 5028 | mdu_array_info_t info; |
| 5029 | if (!arg) |
| 5030 | memset(&info, 0, sizeof(info)); |
| 5031 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5032 | err = -EFAULT; |
| 5033 | goto abort_unlock; |
| 5034 | } |
| 5035 | if (mddev->pers) { |
| 5036 | err = update_array_info(mddev, &info); |
| 5037 | if (err) { |
| 5038 | printk(KERN_WARNING "md: couldn't update" |
| 5039 | " array info. %d\n", err); |
| 5040 | goto abort_unlock; |
| 5041 | } |
| 5042 | goto done_unlock; |
| 5043 | } |
| 5044 | if (!list_empty(&mddev->disks)) { |
| 5045 | printk(KERN_WARNING |
| 5046 | "md: array %s already has disks!\n", |
| 5047 | mdname(mddev)); |
| 5048 | err = -EBUSY; |
| 5049 | goto abort_unlock; |
| 5050 | } |
| 5051 | if (mddev->raid_disks) { |
| 5052 | printk(KERN_WARNING |
| 5053 | "md: array %s already initialised!\n", |
| 5054 | mdname(mddev)); |
| 5055 | err = -EBUSY; |
| 5056 | goto abort_unlock; |
| 5057 | } |
| 5058 | err = set_array_info(mddev, &info); |
| 5059 | if (err) { |
| 5060 | printk(KERN_WARNING "md: couldn't set" |
| 5061 | " array info. %d\n", err); |
| 5062 | goto abort_unlock; |
| 5063 | } |
| 5064 | } |
| 5065 | goto done_unlock; |
| 5066 | |
| 5067 | default:; |
| 5068 | } |
| 5069 | |
| 5070 | /* |
| 5071 | * Commands querying/configuring an existing array: |
| 5072 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5073 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5074 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5075 | if ((!mddev->raid_disks && !mddev->external) |
| 5076 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5077 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5078 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | err = -ENODEV; |
| 5080 | goto abort_unlock; |
| 5081 | } |
| 5082 | |
| 5083 | /* |
| 5084 | * Commands even a read-only array can execute: |
| 5085 | */ |
| 5086 | switch (cmd) |
| 5087 | { |
| 5088 | case GET_ARRAY_INFO: |
| 5089 | err = get_array_info(mddev, argp); |
| 5090 | goto done_unlock; |
| 5091 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5092 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5093 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5094 | goto done_unlock; |
| 5095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | case GET_DISK_INFO: |
| 5097 | err = get_disk_info(mddev, argp); |
| 5098 | goto done_unlock; |
| 5099 | |
| 5100 | case RESTART_ARRAY_RW: |
| 5101 | err = restart_array(mddev); |
| 5102 | goto done_unlock; |
| 5103 | |
| 5104 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5105 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | goto done_unlock; |
| 5107 | |
| 5108 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5109 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | goto done_unlock; |
| 5111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | /* |
| 5115 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5116 | * superblock, so we do not allow them on read-only arrays. |
| 5117 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5118 | * here and hit the 'default' below, so only disallow |
| 5119 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5121 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5122 | if (mddev->ro == 2) { |
| 5123 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5124 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5125 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5126 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5127 | } else { |
| 5128 | err = -EROFS; |
| 5129 | goto abort_unlock; |
| 5130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | switch (cmd) |
| 5134 | { |
| 5135 | case ADD_NEW_DISK: |
| 5136 | { |
| 5137 | mdu_disk_info_t info; |
| 5138 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5139 | err = -EFAULT; |
| 5140 | else |
| 5141 | err = add_new_disk(mddev, &info); |
| 5142 | goto done_unlock; |
| 5143 | } |
| 5144 | |
| 5145 | case HOT_REMOVE_DISK: |
| 5146 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5147 | goto done_unlock; |
| 5148 | |
| 5149 | case HOT_ADD_DISK: |
| 5150 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5151 | goto done_unlock; |
| 5152 | |
| 5153 | case SET_DISK_FAULTY: |
| 5154 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5155 | goto done_unlock; |
| 5156 | |
| 5157 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5158 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | goto done_unlock; |
| 5160 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5161 | case SET_BITMAP_FILE: |
| 5162 | err = set_bitmap_file(mddev, (int)arg); |
| 5163 | goto done_unlock; |
| 5164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | err = -EINVAL; |
| 5167 | goto abort_unlock; |
| 5168 | } |
| 5169 | |
| 5170 | done_unlock: |
| 5171 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5172 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5173 | err != -EINVAL) |
| 5174 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | mddev_unlock(mddev); |
| 5176 | |
| 5177 | return err; |
| 5178 | done: |
| 5179 | if (err) |
| 5180 | MD_BUG(); |
| 5181 | abort: |
| 5182 | return err; |
| 5183 | } |
| 5184 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5185 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | { |
| 5187 | /* |
| 5188 | * Succeed if we can lock the mddev, which confirms that |
| 5189 | * it isn't being stopped right now. |
| 5190 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5191 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | int err; |
| 5193 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5194 | if (mddev->gendisk != bdev->bd_disk) { |
| 5195 | /* we are racing with mddev_put which is discarding this |
| 5196 | * bd_disk. |
| 5197 | */ |
| 5198 | mddev_put(mddev); |
| 5199 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5200 | flush_scheduled_work(); |
| 5201 | /* Then retry the open from the top */ |
| 5202 | return -ERESTARTSYS; |
| 5203 | } |
| 5204 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5205 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5206 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | goto out; |
| 5208 | |
| 5209 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5210 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | mddev_unlock(mddev); |
| 5212 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5213 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | out: |
| 5215 | return err; |
| 5216 | } |
| 5217 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5218 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5220 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5222 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5223 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | mddev_put(mddev); |
| 5225 | |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5229 | static int md_media_changed(struct gendisk *disk) |
| 5230 | { |
| 5231 | mddev_t *mddev = disk->private_data; |
| 5232 | |
| 5233 | return mddev->changed; |
| 5234 | } |
| 5235 | |
| 5236 | static int md_revalidate(struct gendisk *disk) |
| 5237 | { |
| 5238 | mddev_t *mddev = disk->private_data; |
| 5239 | |
| 5240 | mddev->changed = 0; |
| 5241 | return 0; |
| 5242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | static struct block_device_operations md_fops = |
| 5244 | { |
| 5245 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5246 | .open = md_open, |
| 5247 | .release = md_release, |
| 5248 | .locked_ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5249 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5250 | .media_changed = md_media_changed, |
| 5251 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | }; |
| 5253 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5254 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | { |
| 5256 | mdk_thread_t *thread = arg; |
| 5257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | /* |
| 5259 | * md_thread is a 'system-thread', it's priority should be very |
| 5260 | * high. We avoid resource deadlocks individually in each |
| 5261 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5262 | * the very same RT priority as kswapd, thus we will never get |
| 5263 | * into a priority inversion deadlock. |
| 5264 | * |
| 5265 | * we definitely have to have equal or higher priority than |
| 5266 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5267 | * many dirty RAID5 blocks. |
| 5268 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5270 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5271 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5273 | /* We need to wait INTERRUPTIBLE so that |
| 5274 | * we don't add to the load-average. |
| 5275 | * That means we need to be sure no signals are |
| 5276 | * pending |
| 5277 | */ |
| 5278 | if (signal_pending(current)) |
| 5279 | flush_signals(current); |
| 5280 | |
| 5281 | wait_event_interruptible_timeout |
| 5282 | (thread->wqueue, |
| 5283 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5284 | || kthread_should_stop(), |
| 5285 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | |
| 5287 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5288 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5289 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | return 0; |
| 5293 | } |
| 5294 | |
| 5295 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5296 | { |
| 5297 | if (thread) { |
| 5298 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5299 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5300 | wake_up(&thread->wqueue); |
| 5301 | } |
| 5302 | } |
| 5303 | |
| 5304 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5305 | const char *name) |
| 5306 | { |
| 5307 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5309 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | if (!thread) |
| 5311 | return NULL; |
| 5312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | init_waitqueue_head(&thread->wqueue); |
| 5314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | thread->run = run; |
| 5316 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5317 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5318 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5319 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | kfree(thread); |
| 5321 | return NULL; |
| 5322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | return thread; |
| 5324 | } |
| 5325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | void md_unregister_thread(mdk_thread_t *thread) |
| 5327 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5328 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5329 | |
| 5330 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | kfree(thread); |
| 5332 | } |
| 5333 | |
| 5334 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5335 | { |
| 5336 | if (!mddev) { |
| 5337 | MD_BUG(); |
| 5338 | return; |
| 5339 | } |
| 5340 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5341 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5343 | |
| 5344 | if (mddev->external) |
| 5345 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5346 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5348 | mdname(mddev), |
| 5349 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5350 | __builtin_return_address(0),__builtin_return_address(1), |
| 5351 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5352 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5353 | if (!mddev->pers) |
| 5354 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | if (!mddev->pers->error_handler) |
| 5356 | return; |
| 5357 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5358 | if (mddev->degraded) |
| 5359 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5360 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5362 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5363 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5364 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | /* seq_file implementation /proc/mdstat */ |
| 5368 | |
| 5369 | static void status_unused(struct seq_file *seq) |
| 5370 | { |
| 5371 | int i = 0; |
| 5372 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | |
| 5374 | seq_printf(seq, "unused devices: "); |
| 5375 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5376 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | char b[BDEVNAME_SIZE]; |
| 5378 | i++; |
| 5379 | seq_printf(seq, "%s ", |
| 5380 | bdevname(rdev->bdev,b)); |
| 5381 | } |
| 5382 | if (!i) |
| 5383 | seq_printf(seq, "<none>"); |
| 5384 | |
| 5385 | seq_printf(seq, "\n"); |
| 5386 | } |
| 5387 | |
| 5388 | |
| 5389 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5390 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5391 | sector_t max_blocks, resync, res; |
| 5392 | unsigned long dt, db, rt; |
| 5393 | int scale; |
| 5394 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | |
| 5396 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5397 | |
| 5398 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5399 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5400 | else |
| 5401 | max_blocks = mddev->size; |
| 5402 | |
| 5403 | /* |
| 5404 | * Should not happen. |
| 5405 | */ |
| 5406 | if (!max_blocks) { |
| 5407 | MD_BUG(); |
| 5408 | return; |
| 5409 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5410 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5411 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5412 | * u32, as those are the requirements for sector_div. |
| 5413 | * Thus 'scale' must be at least 10 |
| 5414 | */ |
| 5415 | scale = 10; |
| 5416 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5417 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5418 | scale++; |
| 5419 | } |
| 5420 | res = (resync>>scale)*1000; |
| 5421 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5422 | |
| 5423 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5425 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | seq_printf(seq, "["); |
| 5427 | for (i = 0; i < x; i++) |
| 5428 | seq_printf(seq, "="); |
| 5429 | seq_printf(seq, ">"); |
| 5430 | for (i = 0; i < y; i++) |
| 5431 | seq_printf(seq, "."); |
| 5432 | seq_printf(seq, "] "); |
| 5433 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5434 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5435 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5436 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5437 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5438 | "check" : |
| 5439 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5440 | "resync" : "recovery"))), |
| 5441 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5442 | (unsigned long long) resync, |
| 5443 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | |
| 5445 | /* |
| 5446 | * We do not want to overflow, so the order of operands and |
| 5447 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5448 | * safe against division by zero. We only estimate anyway. |
| 5449 | * |
| 5450 | * dt: time from mark until now |
| 5451 | * db: blocks written from mark until now |
| 5452 | * rt: remaining time |
| 5453 | */ |
| 5454 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5455 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5456 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5457 | - mddev->resync_mark_cnt; |
| 5458 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | |
| 5460 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5461 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5462 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5466 | { |
| 5467 | struct list_head *tmp; |
| 5468 | loff_t l = *pos; |
| 5469 | mddev_t *mddev; |
| 5470 | |
| 5471 | if (l >= 0x10000) |
| 5472 | return NULL; |
| 5473 | if (!l--) |
| 5474 | /* header */ |
| 5475 | return (void*)1; |
| 5476 | |
| 5477 | spin_lock(&all_mddevs_lock); |
| 5478 | list_for_each(tmp,&all_mddevs) |
| 5479 | if (!l--) { |
| 5480 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5481 | mddev_get(mddev); |
| 5482 | spin_unlock(&all_mddevs_lock); |
| 5483 | return mddev; |
| 5484 | } |
| 5485 | spin_unlock(&all_mddevs_lock); |
| 5486 | if (!l--) |
| 5487 | return (void*)2;/* tail */ |
| 5488 | return NULL; |
| 5489 | } |
| 5490 | |
| 5491 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5492 | { |
| 5493 | struct list_head *tmp; |
| 5494 | mddev_t *next_mddev, *mddev = v; |
| 5495 | |
| 5496 | ++*pos; |
| 5497 | if (v == (void*)2) |
| 5498 | return NULL; |
| 5499 | |
| 5500 | spin_lock(&all_mddevs_lock); |
| 5501 | if (v == (void*)1) |
| 5502 | tmp = all_mddevs.next; |
| 5503 | else |
| 5504 | tmp = mddev->all_mddevs.next; |
| 5505 | if (tmp != &all_mddevs) |
| 5506 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5507 | else { |
| 5508 | next_mddev = (void*)2; |
| 5509 | *pos = 0x10000; |
| 5510 | } |
| 5511 | spin_unlock(&all_mddevs_lock); |
| 5512 | |
| 5513 | if (v != (void*)1) |
| 5514 | mddev_put(mddev); |
| 5515 | return next_mddev; |
| 5516 | |
| 5517 | } |
| 5518 | |
| 5519 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5520 | { |
| 5521 | mddev_t *mddev = v; |
| 5522 | |
| 5523 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5524 | mddev_put(mddev); |
| 5525 | } |
| 5526 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5527 | struct mdstat_info { |
| 5528 | int event; |
| 5529 | }; |
| 5530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5532 | { |
| 5533 | mddev_t *mddev = v; |
| 5534 | sector_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5536 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5537 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | |
| 5539 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5540 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | seq_printf(seq, "Personalities : "); |
| 5542 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5543 | list_for_each_entry(pers, &pers_list, list) |
| 5544 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | |
| 5546 | spin_unlock(&pers_lock); |
| 5547 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5548 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | return 0; |
| 5550 | } |
| 5551 | if (v == (void*)2) { |
| 5552 | status_unused(seq); |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5556 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5560 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5561 | mddev->pers ? "" : "in"); |
| 5562 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5563 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5565 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5566 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | seq_printf(seq, " %s", mddev->pers->name); |
| 5568 | } |
| 5569 | |
| 5570 | size = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5571 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | char b[BDEVNAME_SIZE]; |
| 5573 | seq_printf(seq, " %s[%d]", |
| 5574 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5575 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5576 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5577 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | seq_printf(seq, "(F)"); |
| 5579 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5580 | } else if (rdev->raid_disk < 0) |
| 5581 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | size += rdev->size; |
| 5583 | } |
| 5584 | |
| 5585 | if (!list_empty(&mddev->disks)) { |
| 5586 | if (mddev->pers) |
| 5587 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5588 | (unsigned long long) |
| 5589 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | else |
| 5591 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5592 | (unsigned long long)size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5594 | if (mddev->persistent) { |
| 5595 | if (mddev->major_version != 0 || |
| 5596 | mddev->minor_version != 90) { |
| 5597 | seq_printf(seq," super %d.%d", |
| 5598 | mddev->major_version, |
| 5599 | mddev->minor_version); |
| 5600 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5601 | } else if (mddev->external) |
| 5602 | seq_printf(seq, " super external:%s", |
| 5603 | mddev->metadata_type); |
| 5604 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5605 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | |
| 5607 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5608 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5610 | if (mddev->pers->sync_request) { |
| 5611 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5612 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5613 | seq_printf(seq, "\n "); |
| 5614 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5615 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5616 | else if (mddev->recovery_cp < MaxSector) |
| 5617 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5618 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5619 | } else |
| 5620 | seq_printf(seq, "\n "); |
| 5621 | |
| 5622 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5623 | unsigned long chunk_kb; |
| 5624 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5625 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5626 | chunk_kb = bitmap->chunksize >> 10; |
| 5627 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5628 | "%lu%s chunk", |
| 5629 | bitmap->pages - bitmap->missing_pages, |
| 5630 | bitmap->pages, |
| 5631 | (bitmap->pages - bitmap->missing_pages) |
| 5632 | << (PAGE_SHIFT - 10), |
| 5633 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5634 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5635 | if (bitmap->file) { |
| 5636 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5637 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5638 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5639 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5640 | seq_printf(seq, "\n"); |
| 5641 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | seq_printf(seq, "\n"); |
| 5645 | } |
| 5646 | mddev_unlock(mddev); |
| 5647 | |
| 5648 | return 0; |
| 5649 | } |
| 5650 | |
| 5651 | static struct seq_operations md_seq_ops = { |
| 5652 | .start = md_seq_start, |
| 5653 | .next = md_seq_next, |
| 5654 | .stop = md_seq_stop, |
| 5655 | .show = md_seq_show, |
| 5656 | }; |
| 5657 | |
| 5658 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5659 | { |
| 5660 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5661 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5662 | if (mi == NULL) |
| 5663 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | |
| 5665 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5666 | if (error) |
| 5667 | kfree(mi); |
| 5668 | else { |
| 5669 | struct seq_file *p = file->private_data; |
| 5670 | p->private = mi; |
| 5671 | mi->event = atomic_read(&md_event_count); |
| 5672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | return error; |
| 5674 | } |
| 5675 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5676 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5677 | { |
| 5678 | struct seq_file *m = filp->private_data; |
| 5679 | struct mdstat_info *mi = m->private; |
| 5680 | int mask; |
| 5681 | |
| 5682 | poll_wait(filp, &md_event_waiters, wait); |
| 5683 | |
| 5684 | /* always allow read */ |
| 5685 | mask = POLLIN | POLLRDNORM; |
| 5686 | |
| 5687 | if (mi->event != atomic_read(&md_event_count)) |
| 5688 | mask |= POLLERR | POLLPRI; |
| 5689 | return mask; |
| 5690 | } |
| 5691 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5692 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5693 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | .open = md_seq_open, |
| 5695 | .read = seq_read, |
| 5696 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5697 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5698 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | }; |
| 5700 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5701 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5704 | list_add_tail(&p->list, &pers_list); |
| 5705 | 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] | 5706 | spin_unlock(&pers_lock); |
| 5707 | return 0; |
| 5708 | } |
| 5709 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5710 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5712 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5714 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | spin_unlock(&pers_lock); |
| 5716 | return 0; |
| 5717 | } |
| 5718 | |
| 5719 | static int is_mddev_idle(mddev_t *mddev) |
| 5720 | { |
| 5721 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5723 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
| 5725 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5726 | rcu_read_lock(); |
| 5727 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 5729 | curr_events = part_stat_read(&disk->part0, sectors[0]) + |
| 5730 | part_stat_read(&disk->part0, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5732 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5733 | * as sync_io is counted when a request starts, and |
| 5734 | * disk_stats is counted when it completes. |
| 5735 | * So resync activity will cause curr_events to be smaller than |
| 5736 | * when there was no such activity. |
| 5737 | * non-sync IO will cause disk_stat to increase without |
| 5738 | * increasing sync_io so curr_events will (eventually) |
| 5739 | * be larger than it was before. Once it becomes |
| 5740 | * substantially larger, the test below will cause |
| 5741 | * the array to appear non-idle, and resync will slow |
| 5742 | * down. |
| 5743 | * If there is a lot of outstanding resync activity when |
| 5744 | * we set last_event to curr_events, then all that activity |
| 5745 | * completing might cause the array to appear non-idle |
| 5746 | * and resync will be slowed down even though there might |
| 5747 | * not have been non-resync activity. This will only |
| 5748 | * happen once though. 'last_events' will soon reflect |
| 5749 | * the state where there is little or no outstanding |
| 5750 | * resync requests, and further resync activity will |
| 5751 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5752 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5754 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | rdev->last_events = curr_events; |
| 5756 | idle = 0; |
| 5757 | } |
| 5758 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5759 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | return idle; |
| 5761 | } |
| 5762 | |
| 5763 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5764 | { |
| 5765 | /* another "blocks" (512byte) blocks have been synced */ |
| 5766 | atomic_sub(blocks, &mddev->recovery_active); |
| 5767 | wake_up(&mddev->recovery_wait); |
| 5768 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5769 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | md_wakeup_thread(mddev->thread); |
| 5771 | // stop recovery, signal do_sync .... |
| 5772 | } |
| 5773 | } |
| 5774 | |
| 5775 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5776 | /* md_write_start(mddev, bi) |
| 5777 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5778 | * in superblock) before writing, schedule a superblock update |
| 5779 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5780 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5781 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5783 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5784 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5785 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5786 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5787 | BUG_ON(mddev->ro == 1); |
| 5788 | if (mddev->ro == 2) { |
| 5789 | /* need to switch to read/write */ |
| 5790 | mddev->ro = 0; |
| 5791 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5792 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5793 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5794 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5795 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5796 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5797 | if (mddev->safemode == 1) |
| 5798 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5799 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5800 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5801 | if (mddev->in_sync) { |
| 5802 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5803 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5804 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5805 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5806 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5807 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5808 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5809 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5810 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5811 | wait_event(mddev->sb_wait, |
| 5812 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5813 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | void md_write_end(mddev_t *mddev) |
| 5817 | { |
| 5818 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5819 | if (mddev->safemode == 2) |
| 5820 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5821 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5823 | } |
| 5824 | } |
| 5825 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5826 | /* md_allow_write(mddev) |
| 5827 | * Calling this ensures that the array is marked 'active' so that writes |
| 5828 | * may proceed without blocking. It is important to call this before |
| 5829 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5830 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5831 | * |
| 5832 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5833 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5834 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5835 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5836 | { |
| 5837 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5838 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5839 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5840 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5841 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5842 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5843 | |
| 5844 | spin_lock_irq(&mddev->write_lock); |
| 5845 | if (mddev->in_sync) { |
| 5846 | mddev->in_sync = 0; |
| 5847 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5848 | if (mddev->safemode_delay && |
| 5849 | mddev->safemode == 0) |
| 5850 | mddev->safemode = 1; |
| 5851 | spin_unlock_irq(&mddev->write_lock); |
| 5852 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5853 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5854 | } else |
| 5855 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5856 | |
| 5857 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5858 | return -EAGAIN; |
| 5859 | else |
| 5860 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5861 | } |
| 5862 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | #define SYNC_MARKS 10 |
| 5865 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5866 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | { |
| 5868 | mddev_t *mddev2; |
| 5869 | unsigned int currspeed = 0, |
| 5870 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5871 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | unsigned long mark[SYNC_MARKS]; |
| 5873 | sector_t mark_cnt[SYNC_MARKS]; |
| 5874 | int last_mark,m; |
| 5875 | struct list_head *tmp; |
| 5876 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5877 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5878 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5879 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | |
| 5881 | /* just incase thread restarts... */ |
| 5882 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5883 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5884 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5885 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5887 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5888 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5889 | desc = "data-check"; |
| 5890 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5891 | desc = "requested-resync"; |
| 5892 | else |
| 5893 | desc = "resync"; |
| 5894 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5895 | desc = "reshape"; |
| 5896 | else |
| 5897 | desc = "recovery"; |
| 5898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | /* we overload curr_resync somewhat here. |
| 5900 | * 0 == not engaged in resync at all |
| 5901 | * 2 == checking that there is no conflict with another sync |
| 5902 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5903 | * commense |
| 5904 | * other == active in resync - this many blocks |
| 5905 | * |
| 5906 | * Before starting a resync we must have set curr_resync to |
| 5907 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5908 | * less than ours. When we find one that is the same or higher |
| 5909 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5910 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5911 | * This will mean we have to start checking from the beginning again. |
| 5912 | * |
| 5913 | */ |
| 5914 | |
| 5915 | do { |
| 5916 | mddev->curr_resync = 2; |
| 5917 | |
| 5918 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5919 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5920 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | goto skip; |
| 5922 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5923 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | if (mddev2 == mddev) |
| 5925 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5926 | if (!mddev->parallel_resync |
| 5927 | && mddev2->curr_resync |
| 5928 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | DEFINE_WAIT(wq); |
| 5930 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5931 | /* arbitrarily yield */ |
| 5932 | mddev->curr_resync = 1; |
| 5933 | wake_up(&resync_wait); |
| 5934 | } |
| 5935 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5936 | /* no need to wait here, we can wait the next |
| 5937 | * time 'round when curr_resync == 2 |
| 5938 | */ |
| 5939 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5940 | /* We need to wait 'interruptible' so as not to |
| 5941 | * contribute to the load average, and not to |
| 5942 | * be caught by 'softlockup' |
| 5943 | */ |
| 5944 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5945 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5946 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5947 | printk(KERN_INFO "md: delaying %s of %s" |
| 5948 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5950 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5952 | if (signal_pending(current)) |
| 5953 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | schedule(); |
| 5955 | finish_wait(&resync_wait, &wq); |
| 5956 | goto try_again; |
| 5957 | } |
| 5958 | finish_wait(&resync_wait, &wq); |
| 5959 | } |
| 5960 | } |
| 5961 | } while (mddev->curr_resync < 2); |
| 5962 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5963 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5964 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5966 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | */ |
| 5968 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5969 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5970 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5971 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5972 | j = mddev->resync_min; |
| 5973 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5974 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5975 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5976 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5977 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5978 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | /* recovery follows the physical size of devices */ |
| 5980 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5981 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5982 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5983 | if (rdev->raid_disk >= 0 && |
| 5984 | !test_bit(Faulty, &rdev->flags) && |
| 5985 | !test_bit(In_sync, &rdev->flags) && |
| 5986 | rdev->recovery_offset < j) |
| 5987 | j = rdev->recovery_offset; |
| 5988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5990 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5991 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5992 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5993 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5994 | "(but not more than %d KB/sec) for %s.\n", |
| 5995 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | |
| 5997 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5998 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5999 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6001 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6002 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | } |
| 6004 | last_mark = 0; |
| 6005 | mddev->resync_mark = mark[last_mark]; |
| 6006 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6007 | |
| 6008 | /* |
| 6009 | * Tune reconstruction: |
| 6010 | */ |
| 6011 | window = 32*(PAGE_SIZE/512); |
| 6012 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6013 | window/2,(unsigned long long) max_sectors/2); |
| 6014 | |
| 6015 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | last_check = 0; |
| 6017 | |
| 6018 | if (j>2) { |
| 6019 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6020 | "md: resuming %s of %s from checkpoint.\n", |
| 6021 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | mddev->curr_resync = j; |
| 6023 | } |
| 6024 | |
| 6025 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6026 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6028 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6029 | if (j >= mddev->resync_max) { |
| 6030 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 6031 | wait_event(mddev->recovery_wait, |
| 6032 | mddev->resync_max > j |
| 6033 | || kthread_should_stop()); |
| 6034 | } |
| 6035 | if (kthread_should_stop()) |
| 6036 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6037 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6038 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6039 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6040 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | goto out; |
| 6042 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6043 | |
| 6044 | if (!skipped) { /* actual IO requested */ |
| 6045 | io_sectors += sectors; |
| 6046 | atomic_add(sectors, &mddev->recovery_active); |
| 6047 | } |
| 6048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | j += sectors; |
| 6050 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6051 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6052 | if (last_check == 0) |
| 6053 | /* this is the earliers that rebuilt will be |
| 6054 | * visible in /proc/mdstat |
| 6055 | */ |
| 6056 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6057 | |
| 6058 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | continue; |
| 6060 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6061 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6063 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | break; |
| 6065 | |
| 6066 | repeat: |
| 6067 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6068 | /* step marks */ |
| 6069 | int next = (last_mark+1) % SYNC_MARKS; |
| 6070 | |
| 6071 | mddev->resync_mark = mark[next]; |
| 6072 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6073 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6074 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | last_mark = next; |
| 6076 | } |
| 6077 | |
| 6078 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6079 | if (kthread_should_stop()) |
| 6080 | goto interrupted; |
| 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | |
| 6083 | /* |
| 6084 | * this loop exits only if either when we are slower than |
| 6085 | * the 'hard' speed limit, or the system was IO-idle for |
| 6086 | * a jiffy. |
| 6087 | * the system might be non-idle CPU-wise, but we only care |
| 6088 | * about not overloading the IO subsystem. (things like an |
| 6089 | * e2fsck being done on the RAID array should execute fast) |
| 6090 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6091 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | cond_resched(); |
| 6093 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6094 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6095 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6097 | if (currspeed > speed_min(mddev)) { |
| 6098 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6100 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | goto repeat; |
| 6102 | } |
| 6103 | } |
| 6104 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6105 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | /* |
| 6107 | * this also signals 'finished resyncing' to md_stop |
| 6108 | */ |
| 6109 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6110 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | |
| 6112 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6113 | |
| 6114 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6115 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6117 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6118 | mddev->curr_resync > 2) { |
| 6119 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6120 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6121 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6122 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6123 | "md: checkpointing %s of %s.\n", |
| 6124 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6125 | mddev->recovery_cp = mddev->curr_resync; |
| 6126 | } |
| 6127 | } else |
| 6128 | mddev->recovery_cp = MaxSector; |
| 6129 | } else { |
| 6130 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6131 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6132 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6133 | if (rdev->raid_disk >= 0 && |
| 6134 | !test_bit(Faulty, &rdev->flags) && |
| 6135 | !test_bit(In_sync, &rdev->flags) && |
| 6136 | rdev->recovery_offset < mddev->curr_resync) |
| 6137 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6140 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | skip: |
| 6143 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6144 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6145 | mddev->resync_max = MaxSector; |
| 6146 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | wake_up(&resync_wait); |
| 6148 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6149 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6150 | return; |
| 6151 | |
| 6152 | interrupted: |
| 6153 | /* |
| 6154 | * got a signal, exit. |
| 6155 | */ |
| 6156 | printk(KERN_INFO |
| 6157 | "md: md_do_sync() got signal ... exiting\n"); |
| 6158 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6159 | goto out; |
| 6160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6162 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | |
| 6164 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6165 | static int remove_and_add_spares(mddev_t *mddev) |
| 6166 | { |
| 6167 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6168 | int spares = 0; |
| 6169 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6170 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6171 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6172 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6173 | (test_bit(Faulty, &rdev->flags) || |
| 6174 | ! test_bit(In_sync, &rdev->flags)) && |
| 6175 | atomic_read(&rdev->nr_pending)==0) { |
| 6176 | if (mddev->pers->hot_remove_disk( |
| 6177 | mddev, rdev->raid_disk)==0) { |
| 6178 | char nm[20]; |
| 6179 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6180 | sysfs_remove_link(&mddev->kobj, nm); |
| 6181 | rdev->raid_disk = -1; |
| 6182 | } |
| 6183 | } |
| 6184 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6185 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6186 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6187 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6188 | !test_bit(In_sync, &rdev->flags) && |
| 6189 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6190 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6191 | if (rdev->raid_disk < 0 |
| 6192 | && !test_bit(Faulty, &rdev->flags)) { |
| 6193 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6194 | if (mddev->pers-> |
| 6195 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6196 | char nm[20]; |
| 6197 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6198 | if (sysfs_create_link(&mddev->kobj, |
| 6199 | &rdev->kobj, nm)) |
| 6200 | printk(KERN_WARNING |
| 6201 | "md: cannot register " |
| 6202 | "%s for %s\n", |
| 6203 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6204 | spares++; |
| 6205 | md_new_event(mddev); |
| 6206 | } else |
| 6207 | break; |
| 6208 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6209 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6210 | } |
| 6211 | return spares; |
| 6212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | /* |
| 6214 | * This routine is regularly called by all per-raid-array threads to |
| 6215 | * deal with generic issues like resync and super-block update. |
| 6216 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6217 | * need this as they never do any recovery or update the superblock. |
| 6218 | * |
| 6219 | * It does not do any resync itself, but rather "forks" off other threads |
| 6220 | * to do that as needed. |
| 6221 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6222 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6223 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | * and wakeups up this thread which will reap the thread and finish up. |
| 6225 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6226 | * |
| 6227 | * The overall approach is: |
| 6228 | * 1/ if the superblock needs updating, update it. |
| 6229 | * 2/ If a recovery thread is running, don't do anything else. |
| 6230 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6231 | * 4/ If there are any faulty devices, remove them. |
| 6232 | * 5/ If array is degraded, try to add spares devices |
| 6233 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6234 | */ |
| 6235 | void md_check_recovery(mddev_t *mddev) |
| 6236 | { |
| 6237 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | |
| 6239 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6240 | if (mddev->bitmap) |
| 6241 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | |
| 6243 | if (mddev->ro) |
| 6244 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6245 | |
| 6246 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6247 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6248 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6249 | mdname(mddev)); |
| 6250 | mddev->safemode = 2; |
| 6251 | } |
| 6252 | flush_signals(current); |
| 6253 | } |
| 6254 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6255 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6256 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6258 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6260 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6261 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6262 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6263 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | )) |
| 6265 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6266 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6267 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6268 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6269 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6270 | if (mddev->ro) { |
| 6271 | /* Only thing we do on a ro array is remove |
| 6272 | * failed devices. |
| 6273 | */ |
| 6274 | remove_and_add_spares(mddev); |
| 6275 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6276 | goto unlock; |
| 6277 | } |
| 6278 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6279 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6280 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6281 | spin_lock_irq(&mddev->write_lock); |
| 6282 | if (mddev->safemode && |
| 6283 | !atomic_read(&mddev->writes_pending) && |
| 6284 | !mddev->in_sync && |
| 6285 | mddev->recovery_cp == MaxSector) { |
| 6286 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6287 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6288 | if (mddev->persistent) |
| 6289 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6290 | } |
| 6291 | if (mddev->safemode == 1) |
| 6292 | mddev->safemode = 0; |
| 6293 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6294 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6295 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6296 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6297 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6298 | if (mddev->flags) |
| 6299 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6300 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6301 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6302 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6303 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6304 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6307 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6308 | /* resync/recovery still happening */ |
| 6309 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6310 | goto unlock; |
| 6311 | } |
| 6312 | if (mddev->sync_thread) { |
| 6313 | /* resync has finished, collect result */ |
| 6314 | md_unregister_thread(mddev->sync_thread); |
| 6315 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6316 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6317 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | /* success...*/ |
| 6319 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6320 | if (mddev->pers->spare_active(mddev)) |
| 6321 | sysfs_notify(&mddev->kobj, NULL, |
| 6322 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6324 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6325 | |
| 6326 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6327 | * information must be scrapped |
| 6328 | */ |
| 6329 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6330 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6331 | rdev->saved_raid_disk = -1; |
| 6332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | mddev->recovery = 0; |
| 6334 | /* flag recovery needed just to double check */ |
| 6335 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6336 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6337 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | goto unlock; |
| 6339 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6340 | /* Set RUNNING before clearing NEEDED to avoid |
| 6341 | * any transients in the value of "sync_action". |
| 6342 | */ |
| 6343 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6344 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6345 | /* Clear some bits that don't mean anything, but |
| 6346 | * might be left set |
| 6347 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6348 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6349 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6351 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6352 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | /* no recovery is running. |
| 6354 | * remove any failed drives, then |
| 6355 | * add spares if possible. |
| 6356 | * Spare are also removed and re-added, to allow |
| 6357 | * the personality to fail the re-add. |
| 6358 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6360 | if (mddev->reshape_position != MaxSector) { |
| 6361 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6362 | /* Cannot proceed */ |
| 6363 | goto unlock; |
| 6364 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6365 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6366 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6367 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6368 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6369 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6370 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6371 | } else if (mddev->recovery_cp < MaxSector) { |
| 6372 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6373 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6374 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6375 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6379 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6380 | /* We are adding a device or devices to an array |
| 6381 | * which has the bitmap stored on all devices. |
| 6382 | * So make sure all bitmap pages get written |
| 6383 | */ |
| 6384 | bitmap_write_all(mddev->bitmap); |
| 6385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6387 | mddev, |
| 6388 | "%s_resync"); |
| 6389 | if (!mddev->sync_thread) { |
| 6390 | printk(KERN_ERR "%s: could not start resync" |
| 6391 | " thread...\n", |
| 6392 | mdname(mddev)); |
| 6393 | /* leave the spares where they are, it shouldn't hurt */ |
| 6394 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6395 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6397 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6398 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | } |
| 6400 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6401 | if (!mddev->sync_thread) { |
| 6402 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6403 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6404 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6405 | if (mddev->sysfs_action) |
| 6406 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | mddev_unlock(mddev); |
| 6409 | } |
| 6410 | } |
| 6411 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6412 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6413 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6414 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6415 | wait_event_timeout(rdev->blocked_wait, |
| 6416 | !test_bit(Blocked, &rdev->flags), |
| 6417 | msecs_to_jiffies(5000)); |
| 6418 | rdev_dec_pending(rdev, mddev); |
| 6419 | } |
| 6420 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6421 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6422 | static int md_notify_reboot(struct notifier_block *this, |
| 6423 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | { |
| 6425 | struct list_head *tmp; |
| 6426 | mddev_t *mddev; |
| 6427 | |
| 6428 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6429 | |
| 6430 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6431 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6432 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6433 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6434 | /* Force a switch to readonly even array |
| 6435 | * appears to still be in use. Hence |
| 6436 | * the '100'. |
| 6437 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6438 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6439 | mddev_unlock(mddev); |
| 6440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | /* |
| 6442 | * certain more exotic SCSI devices are known to be |
| 6443 | * volatile wrt too early system reboots. While the |
| 6444 | * right place to handle this issue is the given |
| 6445 | * driver, we do want to have a safe RAID driver ... |
| 6446 | */ |
| 6447 | mdelay(1000*1); |
| 6448 | } |
| 6449 | return NOTIFY_DONE; |
| 6450 | } |
| 6451 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6452 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | .notifier_call = md_notify_reboot, |
| 6454 | .next = NULL, |
| 6455 | .priority = INT_MAX, /* before any real devices */ |
| 6456 | }; |
| 6457 | |
| 6458 | static void md_geninit(void) |
| 6459 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6461 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6462 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | } |
| 6464 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6465 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | if (register_blkdev(MAJOR_NR, "md")) |
| 6468 | return -1; |
| 6469 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6470 | unregister_blkdev(MAJOR_NR, "md"); |
| 6471 | return -1; |
| 6472 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6473 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6474 | md_probe, NULL, NULL); |
| 6475 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | md_probe, NULL, NULL); |
| 6477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6479 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | |
| 6481 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6482 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | } |
| 6484 | |
| 6485 | |
| 6486 | #ifndef MODULE |
| 6487 | |
| 6488 | /* |
| 6489 | * Searches all registered partitions for autorun RAID arrays |
| 6490 | * at boot time. |
| 6491 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6492 | |
| 6493 | static LIST_HEAD(all_detected_devices); |
| 6494 | struct detected_devices_node { |
| 6495 | struct list_head list; |
| 6496 | dev_t dev; |
| 6497 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
| 6499 | void md_autodetect_dev(dev_t dev) |
| 6500 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6501 | struct detected_devices_node *node_detected_dev; |
| 6502 | |
| 6503 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6504 | if (node_detected_dev) { |
| 6505 | node_detected_dev->dev = dev; |
| 6506 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6507 | } else { |
| 6508 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6509 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | |
| 6514 | static void autostart_arrays(int part) |
| 6515 | { |
| 6516 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6517 | struct detected_devices_node *node_detected_dev; |
| 6518 | dev_t dev; |
| 6519 | int i_scanned, i_passed; |
| 6520 | |
| 6521 | i_scanned = 0; |
| 6522 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | |
| 6524 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6525 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6526 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6527 | i_scanned++; |
| 6528 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6529 | struct detected_devices_node, list); |
| 6530 | list_del(&node_detected_dev->list); |
| 6531 | dev = node_detected_dev->dev; |
| 6532 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6533 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | if (IS_ERR(rdev)) |
| 6535 | continue; |
| 6536 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6537 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | MD_BUG(); |
| 6539 | continue; |
| 6540 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6541 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6543 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6545 | |
| 6546 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6547 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | |
| 6549 | autorun_devices(part); |
| 6550 | } |
| 6551 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6552 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | |
| 6554 | static __exit void md_exit(void) |
| 6555 | { |
| 6556 | mddev_t *mddev; |
| 6557 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6558 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6559 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6560 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | unregister_blkdev(MAJOR_NR,"md"); |
| 6563 | unregister_blkdev(mdp_major, "mdp"); |
| 6564 | unregister_reboot_notifier(&md_notifier); |
| 6565 | unregister_sysctl_table(raid_table_header); |
| 6566 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6567 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6569 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | } |
| 6571 | } |
| 6572 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6573 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | module_exit(md_exit) |
| 6575 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6576 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6577 | { |
| 6578 | return sprintf(buffer, "%d", start_readonly); |
| 6579 | } |
| 6580 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6581 | { |
| 6582 | char *e; |
| 6583 | int num = simple_strtoul(val, &e, 10); |
| 6584 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6585 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6586 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6587 | } |
| 6588 | return -EINVAL; |
| 6589 | } |
| 6590 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6591 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6592 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6593 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6594 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | EXPORT_SYMBOL(register_md_personality); |
| 6597 | EXPORT_SYMBOL(unregister_md_personality); |
| 6598 | EXPORT_SYMBOL(md_error); |
| 6599 | EXPORT_SYMBOL(md_done_sync); |
| 6600 | EXPORT_SYMBOL(md_write_start); |
| 6601 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | EXPORT_SYMBOL(md_register_thread); |
| 6603 | EXPORT_SYMBOL(md_unregister_thread); |
| 6604 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | EXPORT_SYMBOL(md_check_recovery); |
| 6606 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6607 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6608 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |