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> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.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 | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 48 | #include <linux/raid/md_p.h> |
| 49 | #include <linux/raid/md_u.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 51 | #include "bitmap.h" |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define DEBUG 0 |
| 54 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 55 | |
| 56 | |
| 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 61 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static DEFINE_SPINLOCK(pers_lock); |
| 63 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 64 | static void md_print_devices(void); |
| 65 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 66 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 67 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 68 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 72 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 73 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 74 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 76 | * speed limit - in case reconstruction slows down your system despite |
| 77 | * idle IO detection. |
| 78 | * |
| 79 | * 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] | 80 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | static int sysctl_speed_limit_min = 1000; |
| 84 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 85 | static inline int speed_min(mddev_t *mddev) |
| 86 | { |
| 87 | return mddev->sync_speed_min ? |
| 88 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 89 | } |
| 90 | |
| 91 | static inline int speed_max(mddev_t *mddev) |
| 92 | { |
| 93 | return mddev->sync_speed_max ? |
| 94 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | static struct ctl_table_header *raid_table_header; |
| 98 | |
| 99 | static ctl_table raid_table[] = { |
| 100 | { |
| 101 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 102 | .procname = "speed_limit_min", |
| 103 | .data = &sysctl_speed_limit_min, |
| 104 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 105 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .proc_handler = &proc_dointvec, |
| 107 | }, |
| 108 | { |
| 109 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 110 | .procname = "speed_limit_max", |
| 111 | .data = &sysctl_speed_limit_max, |
| 112 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 113 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .proc_handler = &proc_dointvec, |
| 115 | }, |
| 116 | { .ctl_name = 0 } |
| 117 | }; |
| 118 | |
| 119 | static ctl_table raid_dir_table[] = { |
| 120 | { |
| 121 | .ctl_name = DEV_RAID, |
| 122 | .procname = "raid", |
| 123 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .child = raid_table, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_root_table[] = { |
| 131 | { |
| 132 | .ctl_name = CTL_DEV, |
| 133 | .procname = "dev", |
| 134 | .maxlen = 0, |
| 135 | .mode = 0555, |
| 136 | .child = raid_dir_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static struct block_device_operations md_fops; |
| 142 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 143 | static int start_readonly; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 146 | * We have a system wide 'event count' that is incremented |
| 147 | * on any 'interesting' event, and readers of /proc/mdstat |
| 148 | * can use 'poll' or 'select' to find out when the event |
| 149 | * count increases. |
| 150 | * |
| 151 | * Events are: |
| 152 | * start array, stop array, error, add device, remove device, |
| 153 | * start build, activate spare |
| 154 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 155 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 156 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 157 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 158 | { |
| 159 | atomic_inc(&md_event_count); |
| 160 | wake_up(&md_event_waiters); |
| 161 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 162 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 163 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 164 | /* Alternate version that can be called from interrupts |
| 165 | * when calling sysfs_notify isn't needed. |
| 166 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 167 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 168 | { |
| 169 | atomic_inc(&md_event_count); |
| 170 | wake_up(&md_event_waiters); |
| 171 | } |
| 172 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 173 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | * Enables to iterate over all existing md arrays |
| 175 | * all_mddevs_lock protects this list. |
| 176 | */ |
| 177 | static LIST_HEAD(all_mddevs); |
| 178 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * iterates through all used mddevs in the system. |
| 183 | * We take care to grab the all_mddevs_lock whenever navigating |
| 184 | * the list, and to always hold a refcount when unlocked. |
| 185 | * Any code which breaks out of this loop while own |
| 186 | * a reference to the current mddev and must mddev_put it. |
| 187 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 188 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | \ |
| 190 | for (({ spin_lock(&all_mddevs_lock); \ |
| 191 | tmp = all_mddevs.next; \ |
| 192 | mddev = NULL;}); \ |
| 193 | ({ if (tmp != &all_mddevs) \ |
| 194 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 195 | spin_unlock(&all_mddevs_lock); \ |
| 196 | if (mddev) mddev_put(mddev); \ |
| 197 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 198 | tmp != &all_mddevs;}); \ |
| 199 | ({ spin_lock(&all_mddevs_lock); \ |
| 200 | tmp = tmp->next;}) \ |
| 201 | ) |
| 202 | |
| 203 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 204 | static int md_fail_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 206 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 211 | { |
| 212 | atomic_inc(&mddev->active); |
| 213 | return mddev; |
| 214 | } |
| 215 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 216 | static void mddev_delayed_delete(struct work_struct *ws) |
| 217 | { |
| 218 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 219 | kobject_del(&mddev->kobj); |
| 220 | kobject_put(&mddev->kobj); |
| 221 | } |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | static void mddev_put(mddev_t *mddev) |
| 224 | { |
| 225 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 226 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 227 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 228 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 230 | if (mddev->gendisk) { |
| 231 | /* we did a probe so need to clean up. |
| 232 | * Call schedule_work inside the spinlock |
| 233 | * so that flush_scheduled_work() after |
| 234 | * mddev_find will succeed in waiting for the |
| 235 | * work to be done. |
| 236 | */ |
| 237 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 238 | schedule_work(&mddev->del_work); |
| 239 | } else |
| 240 | kfree(mddev); |
| 241 | } |
| 242 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static mddev_t * mddev_find(dev_t unit) |
| 246 | { |
| 247 | mddev_t *mddev, *new = NULL; |
| 248 | |
| 249 | retry: |
| 250 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 252 | if (unit) { |
| 253 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 254 | if (mddev->unit == unit) { |
| 255 | mddev_get(mddev); |
| 256 | spin_unlock(&all_mddevs_lock); |
| 257 | kfree(new); |
| 258 | return mddev; |
| 259 | } |
| 260 | |
| 261 | if (new) { |
| 262 | list_add(&new->all_mddevs, &all_mddevs); |
| 263 | spin_unlock(&all_mddevs_lock); |
| 264 | new->hold_active = UNTIL_IOCTL; |
| 265 | return new; |
| 266 | } |
| 267 | } else if (new) { |
| 268 | /* find an unused unit number */ |
| 269 | static int next_minor = 512; |
| 270 | int start = next_minor; |
| 271 | int is_free = 0; |
| 272 | int dev = 0; |
| 273 | while (!is_free) { |
| 274 | dev = MKDEV(MD_MAJOR, next_minor); |
| 275 | next_minor++; |
| 276 | if (next_minor > MINORMASK) |
| 277 | next_minor = 0; |
| 278 | if (next_minor == start) { |
| 279 | /* Oh dear, all in use. */ |
| 280 | spin_unlock(&all_mddevs_lock); |
| 281 | kfree(new); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | is_free = 1; |
| 286 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 287 | if (mddev->unit == dev) { |
| 288 | is_free = 0; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | new->unit = dev; |
| 293 | new->md_minor = MINOR(dev); |
| 294 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | list_add(&new->all_mddevs, &all_mddevs); |
| 296 | spin_unlock(&all_mddevs_lock); |
| 297 | return new; |
| 298 | } |
| 299 | spin_unlock(&all_mddevs_lock); |
| 300 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 301 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!new) |
| 303 | return NULL; |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | new->unit = unit; |
| 306 | if (MAJOR(unit) == MD_MAJOR) |
| 307 | new->md_minor = MINOR(unit); |
| 308 | else |
| 309 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 310 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 311 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | INIT_LIST_HEAD(&new->disks); |
| 313 | INIT_LIST_HEAD(&new->all_mddevs); |
| 314 | init_timer(&new->safemode_timer); |
| 315 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 316 | atomic_set(&new->openers, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 317 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 318 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 319 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 320 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 321 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 322 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 323 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | goto retry; |
| 326 | } |
| 327 | |
| 328 | static inline int mddev_lock(mddev_t * mddev) |
| 329 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 330 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | static inline int mddev_trylock(mddev_t * mddev) |
| 334 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 335 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static inline void mddev_unlock(mddev_t * mddev) |
| 339 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 340 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 342 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 345 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 347 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 349 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (rdev->desc_nr == nr) |
| 351 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 357 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | mdk_rdev_t *rdev; |
| 359 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 360 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (rdev->bdev->bd_dev == dev) |
| 362 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return NULL; |
| 365 | } |
| 366 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 367 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 368 | { |
| 369 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 370 | list_for_each_entry(pers, &pers_list, list) { |
| 371 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 372 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 373 | if (strcmp(pers->name, clevel)==0) |
| 374 | return pers; |
| 375 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 376 | return NULL; |
| 377 | } |
| 378 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 379 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 380 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 382 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 383 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 386 | 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] | 387 | { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 388 | sector_t num_sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (chunk_size) |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 391 | num_sectors &= ~((sector_t)chunk_size/512 - 1); |
| 392 | return num_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 396 | { |
| 397 | if (rdev->sb_page) |
| 398 | MD_BUG(); |
| 399 | |
| 400 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 401 | if (!rdev->sb_page) { |
| 402 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 403 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 410 | { |
| 411 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 412 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | rdev->sb_loaded = 0; |
| 414 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 415 | rdev->sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | rdev->size = 0; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 421 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 422 | { |
| 423 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 424 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 425 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 426 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 427 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 428 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 429 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 430 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 431 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 432 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 433 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 434 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 435 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 436 | } |
| 437 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 438 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 439 | { |
| 440 | struct bio *bio2 = bio->bi_private; |
| 441 | mdk_rdev_t *rdev = bio2->bi_private; |
| 442 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 443 | |
| 444 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 445 | error == -EOPNOTSUPP) { |
| 446 | unsigned long flags; |
| 447 | /* barriers don't appear to be supported :-( */ |
| 448 | set_bit(BarriersNotsupp, &rdev->flags); |
| 449 | mddev->barriers_work = 0; |
| 450 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 451 | bio2->bi_next = mddev->biolist; |
| 452 | mddev->biolist = bio2; |
| 453 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 454 | wake_up(&mddev->sb_wait); |
| 455 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 456 | } else { |
| 457 | bio_put(bio2); |
| 458 | bio->bi_private = rdev; |
| 459 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 460 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 461 | } |
| 462 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 463 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 464 | sector_t sector, int size, struct page *page) |
| 465 | { |
| 466 | /* write first size bytes of page to sector of rdev |
| 467 | * Increment mddev->pending_writes before returning |
| 468 | * and decrement it on completion, waking up sb_wait |
| 469 | * if zero is reached. |
| 470 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 471 | * |
| 472 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 473 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 474 | */ |
| 475 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 476 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 477 | |
| 478 | bio->bi_bdev = rdev->bdev; |
| 479 | bio->bi_sector = sector; |
| 480 | bio_add_page(bio, page, size, 0); |
| 481 | bio->bi_private = rdev; |
| 482 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 483 | bio->bi_rw = rw; |
| 484 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 485 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 486 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 487 | struct bio *rbio; |
| 488 | rw |= (1<<BIO_RW_BARRIER); |
| 489 | rbio = bio_clone(bio, GFP_NOIO); |
| 490 | rbio->bi_private = bio; |
| 491 | rbio->bi_end_io = super_written_barrier; |
| 492 | submit_bio(rw, rbio); |
| 493 | } else |
| 494 | submit_bio(rw, bio); |
| 495 | } |
| 496 | |
| 497 | void md_super_wait(mddev_t *mddev) |
| 498 | { |
| 499 | /* wait for all superblock writes that were scheduled to complete. |
| 500 | * if any had to be retried (due to BARRIER problems), retry them |
| 501 | */ |
| 502 | DEFINE_WAIT(wq); |
| 503 | for(;;) { |
| 504 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 505 | if (atomic_read(&mddev->pending_writes)==0) |
| 506 | break; |
| 507 | while (mddev->biolist) { |
| 508 | struct bio *bio; |
| 509 | spin_lock_irq(&mddev->write_lock); |
| 510 | bio = mddev->biolist; |
| 511 | mddev->biolist = bio->bi_next ; |
| 512 | bio->bi_next = NULL; |
| 513 | spin_unlock_irq(&mddev->write_lock); |
| 514 | submit_bio(bio->bi_rw, bio); |
| 515 | } |
| 516 | schedule(); |
| 517 | } |
| 518 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 519 | } |
| 520 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 521 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 526 | 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] | 527 | struct page *page, int rw) |
| 528 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 529 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | struct completion event; |
| 531 | int ret; |
| 532 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 533 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | bio->bi_bdev = bdev; |
| 536 | bio->bi_sector = sector; |
| 537 | bio_add_page(bio, page, size, 0); |
| 538 | init_completion(&event); |
| 539 | bio->bi_private = &event; |
| 540 | bio->bi_end_io = bi_complete; |
| 541 | submit_bio(rw, bio); |
| 542 | wait_for_completion(&event); |
| 543 | |
| 544 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 545 | bio_put(bio); |
| 546 | return ret; |
| 547 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 548 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 550 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
| 552 | char b[BDEVNAME_SIZE]; |
| 553 | if (!rdev->sb_page) { |
| 554 | MD_BUG(); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | if (rdev->sb_loaded) |
| 558 | return 0; |
| 559 | |
| 560 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 561 | 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] | 562 | goto fail; |
| 563 | rdev->sb_loaded = 1; |
| 564 | return 0; |
| 565 | |
| 566 | fail: |
| 567 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 568 | bdevname(rdev->bdev,b)); |
| 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 573 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 574 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 575 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 576 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 577 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 581 | { |
| 582 | int ret; |
| 583 | mdp_super_t *tmp1, *tmp2; |
| 584 | |
| 585 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 586 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 587 | |
| 588 | if (!tmp1 || !tmp2) { |
| 589 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 590 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | goto abort; |
| 592 | } |
| 593 | |
| 594 | *tmp1 = *sb1; |
| 595 | *tmp2 = *sb2; |
| 596 | |
| 597 | /* |
| 598 | * nr_disks is not constant |
| 599 | */ |
| 600 | tmp1->nr_disks = 0; |
| 601 | tmp2->nr_disks = 0; |
| 602 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 603 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 605 | kfree(tmp1); |
| 606 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 610 | |
| 611 | static u32 md_csum_fold(u32 csum) |
| 612 | { |
| 613 | csum = (csum & 0xffff) + (csum >> 16); |
| 614 | return (csum & 0xffff) + (csum >> 16); |
| 615 | } |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 618 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 619 | u64 newcsum = 0; |
| 620 | u32 *sb32 = (u32*)sb; |
| 621 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | unsigned int disk_csum, csum; |
| 623 | |
| 624 | disk_csum = sb->sb_csum; |
| 625 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 626 | |
| 627 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 628 | newcsum += sb32[i]; |
| 629 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 630 | |
| 631 | |
| 632 | #ifdef CONFIG_ALPHA |
| 633 | /* This used to use csum_partial, which was wrong for several |
| 634 | * reasons including that different results are returned on |
| 635 | * different architectures. It isn't critical that we get exactly |
| 636 | * the same return value as before (we always csum_fold before |
| 637 | * testing, and that removes any differences). However as we |
| 638 | * know that csum_partial always returned a 16bit value on |
| 639 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 640 | */ |
| 641 | sb->sb_csum = md_csum_fold(disk_csum); |
| 642 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 644 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | return csum; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | /* |
| 650 | * Handle superblock details. |
| 651 | * We want to be able to handle multiple superblock formats |
| 652 | * so we have a common interface to them all, and an array of |
| 653 | * different handlers. |
| 654 | * We rely on user-space to write the initial superblock, and support |
| 655 | * reading and updating of superblocks. |
| 656 | * Interface methods are: |
| 657 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 658 | * loads and validates a superblock on dev. |
| 659 | * if refdev != NULL, compare superblocks on both devices |
| 660 | * Return: |
| 661 | * 0 - dev has a superblock that is compatible with refdev |
| 662 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 663 | * so dev should be used as the refdev in future |
| 664 | * -EINVAL superblock incompatible or invalid |
| 665 | * -othererror e.g. -EIO |
| 666 | * |
| 667 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 668 | * Verify that dev is acceptable into mddev. |
| 669 | * The first time, mddev->raid_disks will be 0, and data from |
| 670 | * dev should be merged in. Subsequent calls check that dev |
| 671 | * is new enough. Return 0 or -EINVAL |
| 672 | * |
| 673 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 674 | * Update the superblock for rdev with data in mddev |
| 675 | * This does not write to disc. |
| 676 | * |
| 677 | */ |
| 678 | |
| 679 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 680 | char *name; |
| 681 | struct module *owner; |
| 682 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 683 | int minor_version); |
| 684 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 685 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 686 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 687 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | /* |
| 691 | * load_super for 0.90.0 |
| 692 | */ |
| 693 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 694 | { |
| 695 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 696 | mdp_super_t *sb; |
| 697 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 700 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | * it's at the end of the disk. |
| 702 | * |
| 703 | * It also happens to be a multiple of 4Kb. |
| 704 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 705 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 707 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (ret) return ret; |
| 709 | |
| 710 | ret = -EINVAL; |
| 711 | |
| 712 | bdevname(rdev->bdev, b); |
| 713 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 714 | |
| 715 | if (sb->md_magic != MD_SB_MAGIC) { |
| 716 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 717 | b); |
| 718 | goto abort; |
| 719 | } |
| 720 | |
| 721 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 722 | sb->minor_version < 90 || |
| 723 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 725 | sb->major_version, sb->minor_version, |
| 726 | b); |
| 727 | goto abort; |
| 728 | } |
| 729 | |
| 730 | if (sb->raid_disks <= 0) |
| 731 | goto abort; |
| 732 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 733 | 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] | 734 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 735 | b); |
| 736 | goto abort; |
| 737 | } |
| 738 | |
| 739 | rdev->preferred_minor = sb->md_minor; |
| 740 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 741 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 743 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 744 | if (sb->level != 1 && sb->level != 4 |
| 745 | && sb->level != 5 && sb->level != 6 |
| 746 | && sb->level != 10) { |
| 747 | /* FIXME use a better test */ |
| 748 | printk(KERN_WARNING |
| 749 | "md: bitmaps not supported for this level.\n"); |
| 750 | goto abort; |
| 751 | } |
| 752 | } |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | if (sb->level == LEVEL_MULTIPATH) |
| 755 | rdev->desc_nr = -1; |
| 756 | else |
| 757 | rdev->desc_nr = sb->this_disk.number; |
| 758 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 759 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 761 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | __u64 ev1, ev2; |
| 763 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 764 | if (!uuid_equal(refsb, sb)) { |
| 765 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 766 | b, bdevname(refdev->bdev,b2)); |
| 767 | goto abort; |
| 768 | } |
| 769 | if (!sb_equal(refsb, sb)) { |
| 770 | printk(KERN_WARNING "md: %s has same UUID" |
| 771 | " but different superblock to %s\n", |
| 772 | b, bdevname(refdev->bdev, b2)); |
| 773 | goto abort; |
| 774 | } |
| 775 | ev1 = md_event(sb); |
| 776 | ev2 = md_event(refsb); |
| 777 | if (ev1 > ev2) |
| 778 | ret = 1; |
| 779 | else |
| 780 | ret = 0; |
| 781 | } |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 782 | rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 784 | if (rdev->size < sb->size && sb->level > 1) |
| 785 | /* "this cannot possibly happen" ... */ |
| 786 | ret = -EINVAL; |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | abort: |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * validate_super for 0.90.0 |
| 794 | */ |
| 795 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 796 | { |
| 797 | mdp_disk_t *desc; |
| 798 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 799 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 801 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 802 | clear_bit(Faulty, &rdev->flags); |
| 803 | clear_bit(In_sync, &rdev->flags); |
| 804 | clear_bit(WriteMostly, &rdev->flags); |
| 805 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | if (mddev->raid_disks == 0) { |
| 808 | mddev->major_version = 0; |
| 809 | mddev->minor_version = sb->minor_version; |
| 810 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 811 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | mddev->chunk_size = sb->chunk_size; |
| 813 | mddev->ctime = sb->ctime; |
| 814 | mddev->utime = sb->utime; |
| 815 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 816 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | mddev->layout = sb->layout; |
| 818 | mddev->raid_disks = sb->raid_disks; |
| 819 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 820 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 821 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 822 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 824 | if (mddev->minor_version >= 91) { |
| 825 | mddev->reshape_position = sb->reshape_position; |
| 826 | mddev->delta_disks = sb->delta_disks; |
| 827 | mddev->new_level = sb->new_level; |
| 828 | mddev->new_layout = sb->new_layout; |
| 829 | mddev->new_chunk = sb->new_chunk; |
| 830 | } else { |
| 831 | mddev->reshape_position = MaxSector; |
| 832 | mddev->delta_disks = 0; |
| 833 | mddev->new_level = mddev->level; |
| 834 | mddev->new_layout = mddev->layout; |
| 835 | mddev->new_chunk = mddev->chunk_size; |
| 836 | } |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 839 | mddev->recovery_cp = MaxSector; |
| 840 | else { |
| 841 | if (sb->events_hi == sb->cp_events_hi && |
| 842 | sb->events_lo == sb->cp_events_lo) { |
| 843 | mddev->recovery_cp = sb->recovery_cp; |
| 844 | } else |
| 845 | mddev->recovery_cp = 0; |
| 846 | } |
| 847 | |
| 848 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 849 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 850 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 851 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 852 | |
| 853 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 854 | |
| 855 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 856 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 857 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 858 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 859 | } else if (mddev->pers == NULL) { |
| 860 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | ++ev1; |
| 862 | if (ev1 < mddev->events) |
| 863 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 864 | } else if (mddev->bitmap) { |
| 865 | /* if adding to array with a bitmap, then we can accept an |
| 866 | * older device ... but not too old. |
| 867 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 868 | if (ev1 < mddev->bitmap->events_cleared) |
| 869 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 870 | } else { |
| 871 | if (ev1 < mddev->events) |
| 872 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 873 | return 0; |
| 874 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | desc = sb->disks + rdev->desc_nr; |
| 878 | |
| 879 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 880 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 881 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 882 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 883 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | rdev->raid_disk = desc->raid_disk; |
| 885 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 886 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 887 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 888 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 889 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * sync_super for 0.90.0 |
| 895 | */ |
| 896 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 897 | { |
| 898 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | mdk_rdev_t *rdev2; |
| 900 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | /* make rdev->sb match mddev data.. |
| 904 | * |
| 905 | * 1/ zero out disks |
| 906 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 907 | * 3/ any empty disks < next_spare become removed |
| 908 | * |
| 909 | * disks[0] gets initialised to REMOVED because |
| 910 | * we cannot be sure from other fields if it has |
| 911 | * been initialised or not. |
| 912 | */ |
| 913 | int i; |
| 914 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 915 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 916 | rdev->sb_size = MD_SB_BYTES; |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 919 | |
| 920 | memset(sb, 0, sizeof(*sb)); |
| 921 | |
| 922 | sb->md_magic = MD_SB_MAGIC; |
| 923 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | sb->patch_version = mddev->patch_version; |
| 925 | sb->gvalid_words = 0; /* ignored */ |
| 926 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 927 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 928 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 929 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 930 | |
| 931 | sb->ctime = mddev->ctime; |
| 932 | sb->level = mddev->level; |
| 933 | sb->size = mddev->size; |
| 934 | sb->raid_disks = mddev->raid_disks; |
| 935 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 936 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | sb->utime = mddev->utime; |
| 938 | sb->state = 0; |
| 939 | sb->events_hi = (mddev->events>>32); |
| 940 | sb->events_lo = (u32)mddev->events; |
| 941 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 942 | if (mddev->reshape_position == MaxSector) |
| 943 | sb->minor_version = 90; |
| 944 | else { |
| 945 | sb->minor_version = 91; |
| 946 | sb->reshape_position = mddev->reshape_position; |
| 947 | sb->new_level = mddev->new_level; |
| 948 | sb->delta_disks = mddev->delta_disks; |
| 949 | sb->new_layout = mddev->new_layout; |
| 950 | sb->new_chunk = mddev->new_chunk; |
| 951 | } |
| 952 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | if (mddev->in_sync) |
| 954 | { |
| 955 | sb->recovery_cp = mddev->recovery_cp; |
| 956 | sb->cp_events_hi = (mddev->events>>32); |
| 957 | sb->cp_events_lo = (u32)mddev->events; |
| 958 | if (mddev->recovery_cp == MaxSector) |
| 959 | sb->state = (1<< MD_SB_CLEAN); |
| 960 | } else |
| 961 | sb->recovery_cp = 0; |
| 962 | |
| 963 | sb->layout = mddev->layout; |
| 964 | sb->chunk_size = mddev->chunk_size; |
| 965 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 966 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 967 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 970 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 972 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 973 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 974 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 975 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 977 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 978 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | d = &sb->disks[rdev2->desc_nr]; |
| 980 | nr_disks++; |
| 981 | d->number = rdev2->desc_nr; |
| 982 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 983 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 984 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 985 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | d->raid_disk = rdev2->raid_disk; |
| 987 | else |
| 988 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 989 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 991 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | d->state = (1<<MD_DISK_ACTIVE); |
| 993 | d->state |= (1<<MD_DISK_SYNC); |
| 994 | active++; |
| 995 | working++; |
| 996 | } else { |
| 997 | d->state = 0; |
| 998 | spare++; |
| 999 | working++; |
| 1000 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1001 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1002 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1005 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1006 | mdp_disk_t *d = &sb->disks[i]; |
| 1007 | if (d->state == 0 && d->number == 0) { |
| 1008 | d->number = i; |
| 1009 | d->raid_disk = i; |
| 1010 | d->state = (1<<MD_DISK_REMOVED); |
| 1011 | d->state |= (1<<MD_DISK_FAULTY); |
| 1012 | failed++; |
| 1013 | } |
| 1014 | } |
| 1015 | sb->nr_disks = nr_disks; |
| 1016 | sb->active_disks = active; |
| 1017 | sb->working_disks = working; |
| 1018 | sb->failed_disks = failed; |
| 1019 | sb->spare_disks = spare; |
| 1020 | |
| 1021 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1022 | sb->sb_csum = calc_sb_csum(sb); |
| 1023 | } |
| 1024 | |
| 1025 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1026 | * rdev_size_change for 0.90.0 |
| 1027 | */ |
| 1028 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1029 | 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] | 1030 | { |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1031 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1032 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1033 | if (rdev->mddev->bitmap_offset) |
| 1034 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1035 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1036 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1037 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1038 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1039 | rdev->sb_page); |
| 1040 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1041 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | * version 1 superblock |
| 1047 | */ |
| 1048 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1049 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1051 | __le32 disk_csum; |
| 1052 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | unsigned long long newcsum; |
| 1054 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1055 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | int i; |
| 1057 | |
| 1058 | disk_csum = sb->sb_csum; |
| 1059 | sb->sb_csum = 0; |
| 1060 | newcsum = 0; |
| 1061 | for (i=0; size>=4; size -= 4 ) |
| 1062 | newcsum += le32_to_cpu(*isuper++); |
| 1063 | |
| 1064 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1065 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1068 | sb->sb_csum = disk_csum; |
| 1069 | return cpu_to_le32(csum); |
| 1070 | } |
| 1071 | |
| 1072 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1073 | { |
| 1074 | struct mdp_superblock_1 *sb; |
| 1075 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1076 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1078 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1081 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * It is always aligned to a 4K boundary and |
| 1083 | * depeding on minor_version, it can be: |
| 1084 | * 0: At least 8K, but less than 12K, from end of device |
| 1085 | * 1: At start of device |
| 1086 | * 2: 4K from start of device. |
| 1087 | */ |
| 1088 | switch(minor_version) { |
| 1089 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1090 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1091 | sb_start -= 8*2; |
| 1092 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | break; |
| 1094 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1095 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | break; |
| 1097 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1098 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | break; |
| 1100 | default: |
| 1101 | return -EINVAL; |
| 1102 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1103 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1105 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1106 | * and it is safe to read 4k, so we do that |
| 1107 | */ |
| 1108 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | if (ret) return ret; |
| 1110 | |
| 1111 | |
| 1112 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1113 | |
| 1114 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1115 | sb->major_version != cpu_to_le32(1) || |
| 1116 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1117 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1118 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return -EINVAL; |
| 1120 | |
| 1121 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1122 | printk("md: invalid superblock checksum on %s\n", |
| 1123 | bdevname(rdev->bdev,b)); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1127 | printk("md: data_size too small on %s\n", |
| 1128 | bdevname(rdev->bdev,b)); |
| 1129 | return -EINVAL; |
| 1130 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1131 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1132 | if (sb->level != cpu_to_le32(1) && |
| 1133 | sb->level != cpu_to_le32(4) && |
| 1134 | sb->level != cpu_to_le32(5) && |
| 1135 | sb->level != cpu_to_le32(6) && |
| 1136 | sb->level != cpu_to_le32(10)) { |
| 1137 | printk(KERN_WARNING |
| 1138 | "md: bitmaps not supported for this level.\n"); |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | rdev->preferred_minor = 0xffff; |
| 1144 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1145 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1147 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1148 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1149 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1150 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1151 | |
| 1152 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1153 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1154 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1155 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1156 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1157 | rdev->desc_nr = -1; |
| 1158 | else |
| 1159 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1160 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1161 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1162 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1163 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | __u64 ev1, ev2; |
| 1165 | struct mdp_superblock_1 *refsb = |
| 1166 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1167 | |
| 1168 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1169 | sb->level != refsb->level || |
| 1170 | sb->layout != refsb->layout || |
| 1171 | sb->chunksize != refsb->chunksize) { |
| 1172 | printk(KERN_WARNING "md: %s has strangely different" |
| 1173 | " superblock to %s\n", |
| 1174 | bdevname(rdev->bdev,b), |
| 1175 | bdevname(refdev->bdev,b2)); |
| 1176 | return -EINVAL; |
| 1177 | } |
| 1178 | ev1 = le64_to_cpu(sb->events); |
| 1179 | ev2 = le64_to_cpu(refsb->events); |
| 1180 | |
| 1181 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1182 | ret = 1; |
| 1183 | else |
| 1184 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1186 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1188 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1189 | rdev->size = rdev->sb_start / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1191 | return -EINVAL; |
| 1192 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1193 | if (le32_to_cpu(sb->chunksize)) |
| 1194 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1195 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1196 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1197 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1198 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1202 | { |
| 1203 | 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] | 1204 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1206 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1207 | clear_bit(Faulty, &rdev->flags); |
| 1208 | clear_bit(In_sync, &rdev->flags); |
| 1209 | clear_bit(WriteMostly, &rdev->flags); |
| 1210 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | if (mddev->raid_disks == 0) { |
| 1213 | mddev->major_version = 1; |
| 1214 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1215 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1217 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1218 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1219 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1220 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | mddev->layout = le32_to_cpu(sb->layout); |
| 1222 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1223 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1224 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1225 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1226 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1229 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1230 | |
| 1231 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1232 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1233 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1234 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1235 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1236 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1237 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1238 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1239 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1240 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1241 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1242 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1243 | } else { |
| 1244 | mddev->reshape_position = MaxSector; |
| 1245 | mddev->delta_disks = 0; |
| 1246 | mddev->new_level = mddev->level; |
| 1247 | mddev->new_layout = mddev->layout; |
| 1248 | mddev->new_chunk = mddev->chunk_size; |
| 1249 | } |
| 1250 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1251 | } else if (mddev->pers == NULL) { |
| 1252 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | ++ev1; |
| 1254 | if (ev1 < mddev->events) |
| 1255 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1256 | } else if (mddev->bitmap) { |
| 1257 | /* If adding to array with a bitmap, then we can accept an |
| 1258 | * older device, but not too old. |
| 1259 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1260 | if (ev1 < mddev->bitmap->events_cleared) |
| 1261 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1262 | } else { |
| 1263 | if (ev1 < mddev->events) |
| 1264 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1265 | return 0; |
| 1266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1268 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1270 | switch(role) { |
| 1271 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1274 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | break; |
| 1276 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1277 | if ((le32_to_cpu(sb->feature_map) & |
| 1278 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1279 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1280 | else |
| 1281 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | rdev->raid_disk = role; |
| 1283 | break; |
| 1284 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1285 | if (sb->devflags & WriteMostly1) |
| 1286 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1287 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1288 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1294 | { |
| 1295 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | mdk_rdev_t *rdev2; |
| 1297 | int max_dev, i; |
| 1298 | /* make rdev->sb match mddev and rdev data. */ |
| 1299 | |
| 1300 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1301 | |
| 1302 | sb->feature_map = 0; |
| 1303 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1304 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1306 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1307 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1308 | |
| 1309 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1310 | sb->events = cpu_to_le64(mddev->events); |
| 1311 | if (mddev->in_sync) |
| 1312 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1313 | else |
| 1314 | sb->resync_offset = cpu_to_le64(0); |
| 1315 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1316 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1317 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1318 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1319 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1320 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1321 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1322 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1323 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1324 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1327 | !test_bit(In_sync, &rdev->flags)) { |
| 1328 | if (mddev->curr_resync_completed > rdev->recovery_offset) |
| 1329 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1330 | if (rdev->recovery_offset > 0) { |
| 1331 | sb->feature_map |= |
| 1332 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1333 | sb->recovery_offset = |
| 1334 | cpu_to_le64(rdev->recovery_offset); |
| 1335 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1338 | if (mddev->reshape_position != MaxSector) { |
| 1339 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1340 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1341 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1342 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1343 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1344 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1345 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1348 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | if (rdev2->desc_nr+1 > max_dev) |
| 1350 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1353 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | for (i=0; i<max_dev;i++) |
| 1355 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1356 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1357 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1359 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1361 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1363 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1364 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | else |
| 1366 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1367 | } |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1370 | } |
| 1371 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1372 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1373 | 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] | 1374 | { |
| 1375 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1376 | sector_t max_sectors; |
| 1377 | if (num_sectors && num_sectors < rdev->mddev->size * 2) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1378 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1379 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1380 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1381 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1382 | max_sectors -= rdev->data_offset; |
| 1383 | if (!num_sectors || num_sectors > max_sectors) |
| 1384 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1385 | } else if (rdev->mddev->bitmap_offset) { |
| 1386 | /* minor version 0 with bitmap we can't move */ |
| 1387 | return 0; |
| 1388 | } else { |
| 1389 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1390 | sector_t sb_start; |
| 1391 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1392 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1393 | max_sectors = rdev->size * 2 + sb_start - rdev->sb_start; |
| 1394 | if (!num_sectors || num_sectors > max_sectors) |
| 1395 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1396 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1397 | } |
| 1398 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1399 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1400 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1401 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1402 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1403 | rdev->sb_page); |
| 1404 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1405 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1408 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | [0] = { |
| 1410 | .name = "0.90.0", |
| 1411 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1412 | .load_super = super_90_load, |
| 1413 | .validate_super = super_90_validate, |
| 1414 | .sync_super = super_90_sync, |
| 1415 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | }, |
| 1417 | [1] = { |
| 1418 | .name = "md-1", |
| 1419 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1420 | .load_super = super_1_load, |
| 1421 | .validate_super = super_1_validate, |
| 1422 | .sync_super = super_1_sync, |
| 1423 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | }, |
| 1425 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
| 1427 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1428 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1429 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1431 | rcu_read_lock(); |
| 1432 | rdev_for_each_rcu(rdev, mddev1) |
| 1433 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1434 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1435 | rdev2->bdev->bd_contains) { |
| 1436 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1437 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1438 | } |
| 1439 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | static LIST_HEAD(pending_raid_disks); |
| 1444 | |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1445 | static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1446 | { |
| 1447 | struct mdk_personality *pers = mddev->pers; |
| 1448 | struct gendisk *disk = mddev->gendisk; |
| 1449 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1450 | struct blk_integrity *bi_mddev = blk_get_integrity(disk); |
| 1451 | |
| 1452 | /* Data integrity passthrough not supported on RAID 4, 5 and 6 */ |
| 1453 | if (pers && pers->level >= 4 && pers->level <= 6) |
| 1454 | return; |
| 1455 | |
| 1456 | /* If rdev is integrity capable, register profile for mddev */ |
| 1457 | if (!bi_mddev && bi_rdev) { |
| 1458 | if (blk_integrity_register(disk, bi_rdev)) |
| 1459 | printk(KERN_ERR "%s: %s Could not register integrity!\n", |
| 1460 | __func__, disk->disk_name); |
| 1461 | else |
| 1462 | printk(KERN_NOTICE "Enabling data integrity on %s\n", |
| 1463 | disk->disk_name); |
| 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | /* Check that mddev and rdev have matching profiles */ |
| 1468 | if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) { |
| 1469 | printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__, |
| 1470 | disk->disk_name, rdev->bdev->bd_disk->disk_name); |
| 1471 | printk(KERN_NOTICE "Disabling data integrity on %s\n", |
| 1472 | disk->disk_name); |
| 1473 | blk_integrity_unregister(disk); |
| 1474 | } |
| 1475 | } |
| 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1478 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1479 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1480 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1481 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1482 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
| 1484 | if (rdev->mddev) { |
| 1485 | MD_BUG(); |
| 1486 | return -EINVAL; |
| 1487 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1488 | |
| 1489 | /* prevent duplicates */ |
| 1490 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1491 | return -EEXIST; |
| 1492 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1493 | /* make sure rdev->size exceeds mddev->size */ |
| 1494 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1495 | if (mddev->pers) { |
| 1496 | /* Cannot change size, so fail |
| 1497 | * If mddev->level <= 0, then we don't care |
| 1498 | * about aligning sizes (e.g. linear) |
| 1499 | */ |
| 1500 | if (mddev->level > 0) |
| 1501 | return -ENOSPC; |
| 1502 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1503 | mddev->size = rdev->size; |
| 1504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* Verify rdev->desc_nr is unique. |
| 1507 | * If it is -1, assign a free number, else |
| 1508 | * check number is not in use |
| 1509 | */ |
| 1510 | if (rdev->desc_nr < 0) { |
| 1511 | int choice = 0; |
| 1512 | if (mddev->pers) choice = mddev->raid_disks; |
| 1513 | while (find_rdev_nr(mddev, choice)) |
| 1514 | choice++; |
| 1515 | rdev->desc_nr = choice; |
| 1516 | } else { |
| 1517 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1518 | return -EBUSY; |
| 1519 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1520 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1521 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1522 | mdname(mddev), mddev->max_disks); |
| 1523 | return -EBUSY; |
| 1524 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1525 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1526 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1527 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1530 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1531 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1532 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1533 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1534 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1535 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1536 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1537 | kobject_del(&rdev->kobj); |
| 1538 | goto fail; |
| 1539 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1540 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1541 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1542 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1543 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1544 | |
| 1545 | /* May as well allow recovery to be retried once */ |
| 1546 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1547 | |
| 1548 | md_integrity_check(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1550 | |
| 1551 | fail: |
| 1552 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1553 | b, mdname(mddev)); |
| 1554 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1557 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1558 | { |
| 1559 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1560 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1561 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1565 | { |
| 1566 | char b[BDEVNAME_SIZE]; |
| 1567 | if (!rdev->mddev) { |
| 1568 | MD_BUG(); |
| 1569 | return; |
| 1570 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1571 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1572 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1574 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1575 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1576 | sysfs_put(rdev->sysfs_state); |
| 1577 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1578 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1579 | * writing to 'remove' to "dev/state". We also need |
| 1580 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1581 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1582 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1583 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1584 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1585 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * prevent the device from being mounted, repartitioned or |
| 1590 | * otherwise reused by a RAID array (or any other kernel |
| 1591 | * subsystem), by bd_claiming the device. |
| 1592 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1593 | 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] | 1594 | { |
| 1595 | int err = 0; |
| 1596 | struct block_device *bdev; |
| 1597 | char b[BDEVNAME_SIZE]; |
| 1598 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1599 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | if (IS_ERR(bdev)) { |
| 1601 | printk(KERN_ERR "md: could not open %s.\n", |
| 1602 | __bdevname(dev, b)); |
| 1603 | return PTR_ERR(bdev); |
| 1604 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1605 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | if (err) { |
| 1607 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1608 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1609 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | return err; |
| 1611 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1612 | if (!shared) |
| 1613 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | rdev->bdev = bdev; |
| 1615 | return err; |
| 1616 | } |
| 1617 | |
| 1618 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1619 | { |
| 1620 | struct block_device *bdev = rdev->bdev; |
| 1621 | rdev->bdev = NULL; |
| 1622 | if (!bdev) |
| 1623 | MD_BUG(); |
| 1624 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1625 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | void md_autodetect_dev(dev_t dev); |
| 1629 | |
| 1630 | static void export_rdev(mdk_rdev_t * rdev) |
| 1631 | { |
| 1632 | char b[BDEVNAME_SIZE]; |
| 1633 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1634 | bdevname(rdev->bdev,b)); |
| 1635 | if (rdev->mddev) |
| 1636 | MD_BUG(); |
| 1637 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1639 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1640 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | #endif |
| 1642 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1643 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1647 | { |
| 1648 | unbind_rdev_from_array(rdev); |
| 1649 | export_rdev(rdev); |
| 1650 | } |
| 1651 | |
| 1652 | static void export_array(mddev_t *mddev) |
| 1653 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1654 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1656 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | if (!rdev->mddev) { |
| 1658 | MD_BUG(); |
| 1659 | continue; |
| 1660 | } |
| 1661 | kick_rdev_from_array(rdev); |
| 1662 | } |
| 1663 | if (!list_empty(&mddev->disks)) |
| 1664 | MD_BUG(); |
| 1665 | mddev->raid_disks = 0; |
| 1666 | mddev->major_version = 0; |
| 1667 | } |
| 1668 | |
| 1669 | static void print_desc(mdp_disk_t *desc) |
| 1670 | { |
| 1671 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1672 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1673 | } |
| 1674 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1675 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | { |
| 1677 | int i; |
| 1678 | |
| 1679 | printk(KERN_INFO |
| 1680 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1681 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1682 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1683 | sb->ctime); |
| 1684 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1685 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1686 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1687 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1688 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1689 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1690 | sb->failed_disks, sb->spare_disks, |
| 1691 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1692 | |
| 1693 | printk(KERN_INFO); |
| 1694 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1695 | mdp_disk_t *desc; |
| 1696 | |
| 1697 | desc = sb->disks + i; |
| 1698 | if (desc->number || desc->major || desc->minor || |
| 1699 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1700 | printk(" D %2d: ", i); |
| 1701 | print_desc(desc); |
| 1702 | } |
| 1703 | } |
| 1704 | printk(KERN_INFO "md: THIS: "); |
| 1705 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1708 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1709 | { |
| 1710 | __u8 *uuid; |
| 1711 | |
| 1712 | uuid = sb->set_uuid; |
| 1713 | printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1714 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1715 | KERN_INFO "md: Name: \"%s\" CT:%llu\n", |
| 1716 | le32_to_cpu(sb->major_version), |
| 1717 | le32_to_cpu(sb->feature_map), |
| 1718 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1719 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1720 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1721 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1722 | sb->set_name, |
| 1723 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1724 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1725 | |
| 1726 | uuid = sb->device_uuid; |
| 1727 | printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
| 1728 | " RO:%llu\n" |
| 1729 | KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1730 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1731 | KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1732 | KERN_INFO "md: (MaxDev:%u) \n", |
| 1733 | le32_to_cpu(sb->level), |
| 1734 | (unsigned long long)le64_to_cpu(sb->size), |
| 1735 | le32_to_cpu(sb->raid_disks), |
| 1736 | le32_to_cpu(sb->layout), |
| 1737 | le32_to_cpu(sb->chunksize), |
| 1738 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1739 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1740 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1741 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1742 | le32_to_cpu(sb->dev_number), |
| 1743 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1744 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1745 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1746 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1747 | sb->devflags, |
| 1748 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1749 | (unsigned long long)le64_to_cpu(sb->events), |
| 1750 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1751 | le32_to_cpu(sb->sb_csum), |
| 1752 | le32_to_cpu(sb->max_dev) |
| 1753 | ); |
| 1754 | } |
| 1755 | |
| 1756 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | { |
| 1758 | char b[BDEVNAME_SIZE]; |
| 1759 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1760 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1761 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1762 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1764 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1765 | switch (major_version) { |
| 1766 | case 0: |
| 1767 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1768 | break; |
| 1769 | case 1: |
| 1770 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1771 | break; |
| 1772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | } else |
| 1774 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1775 | } |
| 1776 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1777 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1779 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | mdk_rdev_t *rdev; |
| 1781 | mddev_t *mddev; |
| 1782 | char b[BDEVNAME_SIZE]; |
| 1783 | |
| 1784 | printk("\n"); |
| 1785 | printk("md: **********************************\n"); |
| 1786 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1787 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1788 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1790 | if (mddev->bitmap) |
| 1791 | bitmap_print_sb(mddev->bitmap); |
| 1792 | else |
| 1793 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1794 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1796 | printk("\n"); |
| 1797 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1798 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1799 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
| 1801 | printk("md: **********************************\n"); |
| 1802 | printk("\n"); |
| 1803 | } |
| 1804 | |
| 1805 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1806 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1808 | /* Update each superblock (in-memory image), but |
| 1809 | * if we are allowed to, skip spares which already |
| 1810 | * have the right event counter, or have one earlier |
| 1811 | * (which would mean they aren't being marked as dirty |
| 1812 | * with the rest of the array) |
| 1813 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1816 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1817 | if (rdev->sb_events == mddev->events || |
| 1818 | (nospares && |
| 1819 | rdev->raid_disk < 0 && |
| 1820 | (rdev->sb_events&1)==0 && |
| 1821 | rdev->sb_events+1 == mddev->events)) { |
| 1822 | /* Don't update this superblock */ |
| 1823 | rdev->sb_loaded = 2; |
| 1824 | } else { |
| 1825 | super_types[mddev->major_version]. |
| 1826 | sync_super(mddev, rdev); |
| 1827 | rdev->sb_loaded = 1; |
| 1828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1832 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1835 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1836 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1838 | if (mddev->external) |
| 1839 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1841 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1842 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1843 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1844 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1845 | force_change = 1; |
| 1846 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1847 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1848 | * though if events isn't the right even/odd, we will have to do |
| 1849 | * spares after all |
| 1850 | */ |
| 1851 | nospares = 1; |
| 1852 | if (force_change) |
| 1853 | nospares = 0; |
| 1854 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1855 | /* If the array is degraded, then skipping spares is both |
| 1856 | * dangerous and fairly pointless. |
| 1857 | * Dangerous because a device that was removed from the array |
| 1858 | * might have a event_count that still looks up-to-date, |
| 1859 | * so it can be re-added without a resync. |
| 1860 | * Pointless because if there are any spares to skip, |
| 1861 | * then a recovery will happen and soon that array won't |
| 1862 | * be degraded any more and the spare can go back to sleep then. |
| 1863 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1864 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1865 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1866 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1868 | |
| 1869 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1870 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1871 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1872 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1873 | && (mddev->events & 1) |
| 1874 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1875 | mddev->events--; |
| 1876 | else { |
| 1877 | /* otherwise we have to go forward and ... */ |
| 1878 | mddev->events ++; |
| 1879 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1880 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1881 | if ((mddev->events&1)==0) { |
| 1882 | mddev->events++; |
| 1883 | nospares = 0; |
| 1884 | } |
| 1885 | } else { |
| 1886 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1887 | if ((mddev->events&1)) { |
| 1888 | mddev->events++; |
| 1889 | nospares = 0; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
| 1894 | if (!mddev->events) { |
| 1895 | /* |
| 1896 | * oops, this 64-bit counter should never wrap. |
| 1897 | * Either we are in around ~1 trillion A.C., assuming |
| 1898 | * 1 reboot per second, or we have a bug: |
| 1899 | */ |
| 1900 | MD_BUG(); |
| 1901 | mddev->events --; |
| 1902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
| 1904 | /* |
| 1905 | * do not write anything to disk if using |
| 1906 | * nonpersistent superblocks |
| 1907 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1908 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1909 | if (!mddev->external) |
| 1910 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1911 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1912 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1913 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1915 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1916 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1917 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | |
| 1919 | dprintk(KERN_INFO |
| 1920 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1921 | mdname(mddev),mddev->in_sync); |
| 1922 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1923 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1924 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | char b[BDEVNAME_SIZE]; |
| 1926 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1927 | if (rdev->sb_loaded != 1) |
| 1928 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1929 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | dprintk("(skipping faulty "); |
| 1931 | |
| 1932 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1933 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1934 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1935 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1936 | rdev->sb_page); |
| 1937 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1938 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1939 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1940 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } else |
| 1943 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1944 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | /* only need to write one superblock... */ |
| 1946 | break; |
| 1947 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1948 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1949 | /* 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] | 1950 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1951 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1952 | if (mddev->in_sync != sync_req || |
| 1953 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1954 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1955 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1956 | goto repeat; |
| 1957 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1958 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1959 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1960 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 1964 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1965 | * We want to accept with case. For this we use cmd_match. |
| 1966 | */ |
| 1967 | static int cmd_match(const char *cmd, const char *str) |
| 1968 | { |
| 1969 | /* See if cmd, written into a sysfs file, matches |
| 1970 | * str. They must either be the same, or cmd can |
| 1971 | * have a trailing newline |
| 1972 | */ |
| 1973 | while (*cmd && *str && *cmd == *str) { |
| 1974 | cmd++; |
| 1975 | str++; |
| 1976 | } |
| 1977 | if (*cmd == '\n') |
| 1978 | cmd++; |
| 1979 | if (*str || *cmd) |
| 1980 | return 0; |
| 1981 | return 1; |
| 1982 | } |
| 1983 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1984 | struct rdev_sysfs_entry { |
| 1985 | struct attribute attr; |
| 1986 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1987 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1988 | }; |
| 1989 | |
| 1990 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1991 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1992 | { |
| 1993 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1994 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1995 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1996 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1997 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1998 | sep = ","; |
| 1999 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2000 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2001 | len += sprintf(page+len, "%sin_sync",sep); |
| 2002 | sep = ","; |
| 2003 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2004 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2005 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2006 | sep = ","; |
| 2007 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2008 | if (test_bit(Blocked, &rdev->flags)) { |
| 2009 | len += sprintf(page+len, "%sblocked", sep); |
| 2010 | sep = ","; |
| 2011 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2012 | if (!test_bit(Faulty, &rdev->flags) && |
| 2013 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2014 | len += sprintf(page+len, "%sspare", sep); |
| 2015 | sep = ","; |
| 2016 | } |
| 2017 | return len+sprintf(page+len, "\n"); |
| 2018 | } |
| 2019 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2020 | static ssize_t |
| 2021 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2022 | { |
| 2023 | /* can write |
| 2024 | * faulty - simulates and error |
| 2025 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2026 | * writemostly - sets write_mostly |
| 2027 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2028 | * blocked - sets the Blocked flag |
| 2029 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2030 | */ |
| 2031 | int err = -EINVAL; |
| 2032 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2033 | md_error(rdev->mddev, rdev); |
| 2034 | err = 0; |
| 2035 | } else if (cmd_match(buf, "remove")) { |
| 2036 | if (rdev->raid_disk >= 0) |
| 2037 | err = -EBUSY; |
| 2038 | else { |
| 2039 | mddev_t *mddev = rdev->mddev; |
| 2040 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2041 | if (mddev->pers) |
| 2042 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2043 | md_new_event(mddev); |
| 2044 | err = 0; |
| 2045 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2046 | } else if (cmd_match(buf, "writemostly")) { |
| 2047 | set_bit(WriteMostly, &rdev->flags); |
| 2048 | err = 0; |
| 2049 | } else if (cmd_match(buf, "-writemostly")) { |
| 2050 | clear_bit(WriteMostly, &rdev->flags); |
| 2051 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2052 | } else if (cmd_match(buf, "blocked")) { |
| 2053 | set_bit(Blocked, &rdev->flags); |
| 2054 | err = 0; |
| 2055 | } else if (cmd_match(buf, "-blocked")) { |
| 2056 | clear_bit(Blocked, &rdev->flags); |
| 2057 | wake_up(&rdev->blocked_wait); |
| 2058 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2059 | md_wakeup_thread(rdev->mddev->thread); |
| 2060 | |
| 2061 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2062 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2063 | if (!err && rdev->sysfs_state) |
| 2064 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2065 | return err ? err : len; |
| 2066 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2067 | static struct rdev_sysfs_entry rdev_state = |
| 2068 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2069 | |
| 2070 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2071 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2072 | { |
| 2073 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2074 | } |
| 2075 | |
| 2076 | static ssize_t |
| 2077 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2078 | { |
| 2079 | char *e; |
| 2080 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2081 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2082 | atomic_set(&rdev->corrected_errors, n); |
| 2083 | return len; |
| 2084 | } |
| 2085 | return -EINVAL; |
| 2086 | } |
| 2087 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2088 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2089 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2090 | static ssize_t |
| 2091 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2092 | { |
| 2093 | if (rdev->raid_disk < 0) |
| 2094 | return sprintf(page, "none\n"); |
| 2095 | else |
| 2096 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2097 | } |
| 2098 | |
| 2099 | static ssize_t |
| 2100 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2101 | { |
| 2102 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2103 | int err; |
| 2104 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2105 | int slot = simple_strtoul(buf, &e, 10); |
| 2106 | if (strncmp(buf, "none", 4)==0) |
| 2107 | slot = -1; |
| 2108 | else if (e==buf || (*e && *e!= '\n')) |
| 2109 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2110 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2111 | /* Setting 'slot' on an active array requires also |
| 2112 | * updating the 'rd%d' link, and communicating |
| 2113 | * with the personality with ->hot_*_disk. |
| 2114 | * For now we only support removing |
| 2115 | * failed/spare devices. This normally happens automatically, |
| 2116 | * but not when the metadata is externally managed. |
| 2117 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2118 | if (rdev->raid_disk == -1) |
| 2119 | return -EEXIST; |
| 2120 | /* personality does all needed checks */ |
| 2121 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2122 | return -EINVAL; |
| 2123 | err = rdev->mddev->pers-> |
| 2124 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2125 | if (err) |
| 2126 | return err; |
| 2127 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2128 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2129 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2130 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2131 | } else if (rdev->mddev->pers) { |
| 2132 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2133 | /* Activating a spare .. or possibly reactivating |
| 2134 | * if we every get bitmaps working here. |
| 2135 | */ |
| 2136 | |
| 2137 | if (rdev->raid_disk != -1) |
| 2138 | return -EBUSY; |
| 2139 | |
| 2140 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2141 | return -EINVAL; |
| 2142 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2143 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2144 | if (rdev2->raid_disk == slot) |
| 2145 | return -EEXIST; |
| 2146 | |
| 2147 | rdev->raid_disk = slot; |
| 2148 | if (test_bit(In_sync, &rdev->flags)) |
| 2149 | rdev->saved_raid_disk = slot; |
| 2150 | else |
| 2151 | rdev->saved_raid_disk = -1; |
| 2152 | err = rdev->mddev->pers-> |
| 2153 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2154 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2155 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2156 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2157 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2158 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2159 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2160 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2161 | printk(KERN_WARNING |
| 2162 | "md: cannot register " |
| 2163 | "%s for %s\n", |
| 2164 | nm, mdname(rdev->mddev)); |
| 2165 | |
| 2166 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2167 | } else { |
| 2168 | if (slot >= rdev->mddev->raid_disks) |
| 2169 | return -ENOSPC; |
| 2170 | rdev->raid_disk = slot; |
| 2171 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2172 | clear_bit(Faulty, &rdev->flags); |
| 2173 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2174 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2175 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2176 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2177 | return len; |
| 2178 | } |
| 2179 | |
| 2180 | |
| 2181 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2182 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2183 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2184 | static ssize_t |
| 2185 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2186 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2187 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static ssize_t |
| 2191 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2192 | { |
| 2193 | char *e; |
| 2194 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2195 | if (e==buf || (*e && *e != '\n')) |
| 2196 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2197 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2198 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2199 | if (rdev->size && rdev->mddev->external) |
| 2200 | /* Must set offset before size, so overlap checks |
| 2201 | * can be sane */ |
| 2202 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2203 | rdev->data_offset = offset; |
| 2204 | return len; |
| 2205 | } |
| 2206 | |
| 2207 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2208 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2209 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2210 | static ssize_t |
| 2211 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2212 | { |
| 2213 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2214 | } |
| 2215 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2216 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2217 | { |
| 2218 | /* check if two start/length pairs overlap */ |
| 2219 | if (s1+l1 <= s2) |
| 2220 | return 0; |
| 2221 | if (s2+l2 <= s1) |
| 2222 | return 0; |
| 2223 | return 1; |
| 2224 | } |
| 2225 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2226 | static ssize_t |
| 2227 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2228 | { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2229 | unsigned long long size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2230 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2231 | mddev_t *my_mddev = rdev->mddev; |
| 2232 | |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2233 | if (strict_strtoull(buf, 10, &size) < 0) |
| 2234 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2235 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2236 | if (my_mddev->persistent) { |
| 2237 | size = super_types[my_mddev->major_version]. |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2238 | rdev_size_change(rdev, size * 2); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2239 | if (!size) |
| 2240 | return -EBUSY; |
| 2241 | } else if (!size) { |
| 2242 | size = (rdev->bdev->bd_inode->i_size >> 10); |
| 2243 | size -= rdev->data_offset/2; |
| 2244 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2245 | } |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2246 | if (size < my_mddev->size) |
| 2247 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2248 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2249 | rdev->size = size; |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2250 | if (size > oldsize && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2251 | /* need to check that all other rdevs with the same ->bdev |
| 2252 | * do not overlap. We need to unlock the mddev to avoid |
| 2253 | * a deadlock. We have already changed rdev->size, and if |
| 2254 | * we have to change it back, we will have the lock again. |
| 2255 | */ |
| 2256 | mddev_t *mddev; |
| 2257 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2258 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2259 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2260 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2261 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2262 | mdk_rdev_t *rdev2; |
| 2263 | |
| 2264 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2265 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2266 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2267 | (rdev->bdev == rdev2->bdev && |
| 2268 | rdev != rdev2 && |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2269 | overlaps(rdev->data_offset, rdev->size * 2, |
| 2270 | rdev2->data_offset, |
| 2271 | rdev2->size * 2))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2272 | overlap = 1; |
| 2273 | break; |
| 2274 | } |
| 2275 | mddev_unlock(mddev); |
| 2276 | if (overlap) { |
| 2277 | mddev_put(mddev); |
| 2278 | break; |
| 2279 | } |
| 2280 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2281 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2282 | if (overlap) { |
| 2283 | /* Someone else could have slipped in a size |
| 2284 | * change here, but doing so is just silly. |
| 2285 | * We put oldsize back because we *know* it is |
| 2286 | * safe, and trust userspace not to race with |
| 2287 | * itself |
| 2288 | */ |
| 2289 | rdev->size = oldsize; |
| 2290 | return -EBUSY; |
| 2291 | } |
| 2292 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2293 | return len; |
| 2294 | } |
| 2295 | |
| 2296 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2297 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2298 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2299 | static struct attribute *rdev_default_attrs[] = { |
| 2300 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2301 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2302 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2303 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2304 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2305 | NULL, |
| 2306 | }; |
| 2307 | static ssize_t |
| 2308 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2309 | { |
| 2310 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2311 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2312 | mddev_t *mddev = rdev->mddev; |
| 2313 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2314 | |
| 2315 | if (!entry->show) |
| 2316 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2317 | |
| 2318 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2319 | if (!rv) { |
| 2320 | if (rdev->mddev == NULL) |
| 2321 | rv = -EBUSY; |
| 2322 | else |
| 2323 | rv = entry->show(rdev, page); |
| 2324 | mddev_unlock(mddev); |
| 2325 | } |
| 2326 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | static ssize_t |
| 2330 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2331 | const char *page, size_t length) |
| 2332 | { |
| 2333 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2334 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2335 | ssize_t rv; |
| 2336 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2337 | |
| 2338 | if (!entry->store) |
| 2339 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2340 | if (!capable(CAP_SYS_ADMIN)) |
| 2341 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2342 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2343 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2344 | if (rdev->mddev == NULL) |
| 2345 | rv = -EBUSY; |
| 2346 | else |
| 2347 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2348 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2349 | } |
| 2350 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | static void rdev_free(struct kobject *ko) |
| 2354 | { |
| 2355 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2356 | kfree(rdev); |
| 2357 | } |
| 2358 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2359 | .show = rdev_attr_show, |
| 2360 | .store = rdev_attr_store, |
| 2361 | }; |
| 2362 | static struct kobj_type rdev_ktype = { |
| 2363 | .release = rdev_free, |
| 2364 | .sysfs_ops = &rdev_sysfs_ops, |
| 2365 | .default_attrs = rdev_default_attrs, |
| 2366 | }; |
| 2367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | /* |
| 2369 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2370 | * |
| 2371 | * mark the device faulty if: |
| 2372 | * |
| 2373 | * - the device is nonexistent (zero size) |
| 2374 | * - the device has no valid superblock |
| 2375 | * |
| 2376 | * a faulty rdev _never_ has rdev->sb set. |
| 2377 | */ |
| 2378 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2379 | { |
| 2380 | char b[BDEVNAME_SIZE]; |
| 2381 | int err; |
| 2382 | mdk_rdev_t *rdev; |
| 2383 | sector_t size; |
| 2384 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2385 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | if (!rdev) { |
| 2387 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2388 | return ERR_PTR(-ENOMEM); |
| 2389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | |
| 2391 | if ((err = alloc_disk_sb(rdev))) |
| 2392 | goto abort_free; |
| 2393 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2394 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | if (err) |
| 2396 | goto abort_free; |
| 2397 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2398 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2401 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2402 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2403 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2405 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2407 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2408 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
| 2410 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2411 | if (!size) { |
| 2412 | printk(KERN_WARNING |
| 2413 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2414 | bdevname(rdev->bdev,b)); |
| 2415 | err = -EINVAL; |
| 2416 | goto abort_free; |
| 2417 | } |
| 2418 | |
| 2419 | if (super_format >= 0) { |
| 2420 | err = super_types[super_format]. |
| 2421 | load_super(rdev, NULL, super_minor); |
| 2422 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2423 | printk(KERN_WARNING |
| 2424 | "md: %s does not have a valid v%d.%d " |
| 2425 | "superblock, not importing!\n", |
| 2426 | bdevname(rdev->bdev,b), |
| 2427 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | goto abort_free; |
| 2429 | } |
| 2430 | if (err < 0) { |
| 2431 | printk(KERN_WARNING |
| 2432 | "md: could not read %s's sb, not importing!\n", |
| 2433 | bdevname(rdev->bdev,b)); |
| 2434 | goto abort_free; |
| 2435 | } |
| 2436 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2439 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | |
| 2441 | return rdev; |
| 2442 | |
| 2443 | abort_free: |
| 2444 | if (rdev->sb_page) { |
| 2445 | if (rdev->bdev) |
| 2446 | unlock_rdev(rdev); |
| 2447 | free_disk_sb(rdev); |
| 2448 | } |
| 2449 | kfree(rdev); |
| 2450 | return ERR_PTR(err); |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | * Check a full RAID array for plausibility |
| 2455 | */ |
| 2456 | |
| 2457 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2458 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | { |
| 2460 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2461 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | char b[BDEVNAME_SIZE]; |
| 2463 | |
| 2464 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2465 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | switch (super_types[mddev->major_version]. |
| 2467 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2468 | case 1: |
| 2469 | freshest = rdev; |
| 2470 | break; |
| 2471 | case 0: |
| 2472 | break; |
| 2473 | default: |
| 2474 | printk( KERN_ERR \ |
| 2475 | "md: fatal superblock inconsistency in %s" |
| 2476 | " -- removing from array\n", |
| 2477 | bdevname(rdev->bdev,b)); |
| 2478 | kick_rdev_from_array(rdev); |
| 2479 | } |
| 2480 | |
| 2481 | |
| 2482 | super_types[mddev->major_version]. |
| 2483 | validate_super(mddev, freshest); |
| 2484 | |
| 2485 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2486 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2487 | if (rdev->desc_nr >= mddev->max_disks || |
| 2488 | i > mddev->max_disks) { |
| 2489 | printk(KERN_WARNING |
| 2490 | "md: %s: %s: only %d devices permitted\n", |
| 2491 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2492 | mddev->max_disks); |
| 2493 | kick_rdev_from_array(rdev); |
| 2494 | continue; |
| 2495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | if (rdev != freshest) |
| 2497 | if (super_types[mddev->major_version]. |
| 2498 | validate_super(mddev, rdev)) { |
| 2499 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2500 | " from array!\n", |
| 2501 | bdevname(rdev->bdev,b)); |
| 2502 | kick_rdev_from_array(rdev); |
| 2503 | continue; |
| 2504 | } |
| 2505 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2506 | rdev->desc_nr = i++; |
| 2507 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2508 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2509 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2510 | rdev->raid_disk = -1; |
| 2511 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | if (mddev->recovery_cp != MaxSector && |
| 2518 | mddev->level >= 1) |
| 2519 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2520 | " -- starting background reconstruction\n", |
| 2521 | mdname(mddev)); |
| 2522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2525 | static void md_safemode_timeout(unsigned long data); |
| 2526 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2527 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2528 | safe_delay_show(mddev_t *mddev, char *page) |
| 2529 | { |
| 2530 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2531 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2532 | } |
| 2533 | static ssize_t |
| 2534 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2535 | { |
| 2536 | int scale=1; |
| 2537 | int dot=0; |
| 2538 | int i; |
| 2539 | unsigned long msec; |
| 2540 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2541 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2542 | /* remove a period, and count digits after it */ |
| 2543 | if (len >= sizeof(buf)) |
| 2544 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2545 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2546 | for (i=0; i<len; i++) { |
| 2547 | if (dot) { |
| 2548 | if (isdigit(buf[i])) { |
| 2549 | buf[i-1] = buf[i]; |
| 2550 | scale *= 10; |
| 2551 | } |
| 2552 | buf[i] = 0; |
| 2553 | } else if (buf[i] == '.') { |
| 2554 | dot=1; |
| 2555 | buf[i] = 0; |
| 2556 | } |
| 2557 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2558 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2559 | return -EINVAL; |
| 2560 | msec = (msec * 1000) / scale; |
| 2561 | if (msec == 0) |
| 2562 | mddev->safemode_delay = 0; |
| 2563 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2564 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2565 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2566 | if (mddev->safemode_delay == 0) |
| 2567 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2568 | if (mddev->safemode_delay < old_delay) |
| 2569 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2570 | } |
| 2571 | return len; |
| 2572 | } |
| 2573 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2574 | __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] | 2575 | |
| 2576 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2577 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2578 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2579 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2580 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2581 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2582 | else if (mddev->clevel[0]) |
| 2583 | return sprintf(page, "%s\n", mddev->clevel); |
| 2584 | else if (mddev->level != LEVEL_NONE) |
| 2585 | return sprintf(page, "%d\n", mddev->level); |
| 2586 | else |
| 2587 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2588 | } |
| 2589 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2590 | static ssize_t |
| 2591 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2592 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2593 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2594 | if (mddev->pers) |
| 2595 | return -EBUSY; |
| 2596 | if (len == 0) |
| 2597 | return 0; |
| 2598 | if (len >= sizeof(mddev->clevel)) |
| 2599 | return -ENOSPC; |
| 2600 | strncpy(mddev->clevel, buf, len); |
| 2601 | if (mddev->clevel[len-1] == '\n') |
| 2602 | len--; |
| 2603 | mddev->clevel[len] = 0; |
| 2604 | mddev->level = LEVEL_NONE; |
| 2605 | return rv; |
| 2606 | } |
| 2607 | |
| 2608 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2609 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2610 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2611 | |
| 2612 | static ssize_t |
| 2613 | layout_show(mddev_t *mddev, char *page) |
| 2614 | { |
| 2615 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2616 | if (mddev->reshape_position != MaxSector && |
| 2617 | mddev->layout != mddev->new_layout) |
| 2618 | return sprintf(page, "%d (%d)\n", |
| 2619 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2620 | return sprintf(page, "%d\n", mddev->layout); |
| 2621 | } |
| 2622 | |
| 2623 | static ssize_t |
| 2624 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2625 | { |
| 2626 | char *e; |
| 2627 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2628 | |
| 2629 | if (!*buf || (*e && *e != '\n')) |
| 2630 | return -EINVAL; |
| 2631 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2632 | if (mddev->pers) |
| 2633 | return -EBUSY; |
| 2634 | if (mddev->reshape_position != MaxSector) |
| 2635 | mddev->new_layout = n; |
| 2636 | else |
| 2637 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2638 | return len; |
| 2639 | } |
| 2640 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2641 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2642 | |
| 2643 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2644 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2645 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2646 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2647 | if (mddev->raid_disks == 0) |
| 2648 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2649 | if (mddev->reshape_position != MaxSector && |
| 2650 | mddev->delta_disks != 0) |
| 2651 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2652 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2653 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2654 | } |
| 2655 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2656 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2657 | |
| 2658 | static ssize_t |
| 2659 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2660 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2661 | char *e; |
| 2662 | int rv = 0; |
| 2663 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2664 | |
| 2665 | if (!*buf || (*e && *e != '\n')) |
| 2666 | return -EINVAL; |
| 2667 | |
| 2668 | if (mddev->pers) |
| 2669 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2670 | else if (mddev->reshape_position != MaxSector) { |
| 2671 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2672 | mddev->delta_disks = n - olddisks; |
| 2673 | mddev->raid_disks = n; |
| 2674 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2675 | mddev->raid_disks = n; |
| 2676 | return rv ? rv : len; |
| 2677 | } |
| 2678 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2679 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2680 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2681 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2682 | chunk_size_show(mddev_t *mddev, char *page) |
| 2683 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2684 | if (mddev->reshape_position != MaxSector && |
| 2685 | mddev->chunk_size != mddev->new_chunk) |
| 2686 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2687 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2688 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2689 | } |
| 2690 | |
| 2691 | static ssize_t |
| 2692 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2693 | { |
| 2694 | /* can only set chunk_size if array is not yet active */ |
| 2695 | char *e; |
| 2696 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2697 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2698 | if (!*buf || (*e && *e != '\n')) |
| 2699 | return -EINVAL; |
| 2700 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2701 | if (mddev->pers) |
| 2702 | return -EBUSY; |
| 2703 | else if (mddev->reshape_position != MaxSector) |
| 2704 | mddev->new_chunk = n; |
| 2705 | else |
| 2706 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2707 | return len; |
| 2708 | } |
| 2709 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2710 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2711 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2712 | static ssize_t |
| 2713 | resync_start_show(mddev_t *mddev, char *page) |
| 2714 | { |
| 2715 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2716 | } |
| 2717 | |
| 2718 | static ssize_t |
| 2719 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2720 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2721 | char *e; |
| 2722 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2723 | |
| 2724 | if (mddev->pers) |
| 2725 | return -EBUSY; |
| 2726 | if (!*buf || (*e && *e != '\n')) |
| 2727 | return -EINVAL; |
| 2728 | |
| 2729 | mddev->recovery_cp = n; |
| 2730 | return len; |
| 2731 | } |
| 2732 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2733 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2734 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2735 | /* |
| 2736 | * The array state can be: |
| 2737 | * |
| 2738 | * clear |
| 2739 | * No devices, no size, no level |
| 2740 | * Equivalent to STOP_ARRAY ioctl |
| 2741 | * inactive |
| 2742 | * May have some settings, but array is not active |
| 2743 | * all IO results in error |
| 2744 | * When written, doesn't tear down array, but just stops it |
| 2745 | * suspended (not supported yet) |
| 2746 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2747 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2748 | * readonly |
| 2749 | * no resync can happen. no superblocks get written. |
| 2750 | * write requests fail |
| 2751 | * read-auto |
| 2752 | * like readonly, but behaves like 'clean' on a write request. |
| 2753 | * |
| 2754 | * clean - no pending writes, but otherwise active. |
| 2755 | * When written to inactive array, starts without resync |
| 2756 | * If a write request arrives then |
| 2757 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2758 | * if not known, block and switch to write-pending |
| 2759 | * If written to an active array that has pending writes, then fails. |
| 2760 | * active |
| 2761 | * fully active: IO and resync can be happening. |
| 2762 | * When written to inactive array, starts with resync |
| 2763 | * |
| 2764 | * write-pending |
| 2765 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2766 | * |
| 2767 | * active-idle |
| 2768 | * like active, but no writes have been seen for a while (100msec). |
| 2769 | * |
| 2770 | */ |
| 2771 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2772 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2773 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2774 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2775 | "write-pending", "active-idle", NULL }; |
| 2776 | |
| 2777 | static int match_word(const char *word, char **list) |
| 2778 | { |
| 2779 | int n; |
| 2780 | for (n=0; list[n]; n++) |
| 2781 | if (cmd_match(word, list[n])) |
| 2782 | break; |
| 2783 | return n; |
| 2784 | } |
| 2785 | |
| 2786 | static ssize_t |
| 2787 | array_state_show(mddev_t *mddev, char *page) |
| 2788 | { |
| 2789 | enum array_state st = inactive; |
| 2790 | |
| 2791 | if (mddev->pers) |
| 2792 | switch(mddev->ro) { |
| 2793 | case 1: |
| 2794 | st = readonly; |
| 2795 | break; |
| 2796 | case 2: |
| 2797 | st = read_auto; |
| 2798 | break; |
| 2799 | case 0: |
| 2800 | if (mddev->in_sync) |
| 2801 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2802 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2803 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2804 | else if (mddev->safemode) |
| 2805 | st = active_idle; |
| 2806 | else |
| 2807 | st = active; |
| 2808 | } |
| 2809 | else { |
| 2810 | if (list_empty(&mddev->disks) && |
| 2811 | mddev->raid_disks == 0 && |
| 2812 | mddev->size == 0) |
| 2813 | st = clear; |
| 2814 | else |
| 2815 | st = inactive; |
| 2816 | } |
| 2817 | return sprintf(page, "%s\n", array_states[st]); |
| 2818 | } |
| 2819 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2820 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2821 | static int do_md_run(mddev_t * mddev); |
| 2822 | static int restart_array(mddev_t *mddev); |
| 2823 | |
| 2824 | static ssize_t |
| 2825 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2826 | { |
| 2827 | int err = -EINVAL; |
| 2828 | enum array_state st = match_word(buf, array_states); |
| 2829 | switch(st) { |
| 2830 | case bad_word: |
| 2831 | break; |
| 2832 | case clear: |
| 2833 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2834 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2835 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2836 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2837 | break; |
| 2838 | case inactive: |
| 2839 | /* stopping an active array */ |
| 2840 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2841 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2842 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2843 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2844 | } else |
| 2845 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2846 | break; |
| 2847 | case suspended: |
| 2848 | break; /* not supported yet */ |
| 2849 | case readonly: |
| 2850 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2851 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2852 | else { |
| 2853 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2854 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2855 | err = do_md_run(mddev); |
| 2856 | } |
| 2857 | break; |
| 2858 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2859 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2860 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2861 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2862 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2863 | err = restart_array(mddev); |
| 2864 | if (err == 0) { |
| 2865 | mddev->ro = 2; |
| 2866 | set_disk_ro(mddev->gendisk, 0); |
| 2867 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2868 | } else { |
| 2869 | mddev->ro = 2; |
| 2870 | err = do_md_run(mddev); |
| 2871 | } |
| 2872 | break; |
| 2873 | case clean: |
| 2874 | if (mddev->pers) { |
| 2875 | restart_array(mddev); |
| 2876 | spin_lock_irq(&mddev->write_lock); |
| 2877 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2878 | if (mddev->in_sync == 0) { |
| 2879 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2880 | if (mddev->safemode == 1) |
| 2881 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2882 | if (mddev->persistent) |
| 2883 | set_bit(MD_CHANGE_CLEAN, |
| 2884 | &mddev->flags); |
| 2885 | } |
| 2886 | err = 0; |
| 2887 | } else |
| 2888 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2889 | spin_unlock_irq(&mddev->write_lock); |
| 2890 | } else { |
| 2891 | mddev->ro = 0; |
| 2892 | mddev->recovery_cp = MaxSector; |
| 2893 | err = do_md_run(mddev); |
| 2894 | } |
| 2895 | break; |
| 2896 | case active: |
| 2897 | if (mddev->pers) { |
| 2898 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2899 | if (mddev->external) |
| 2900 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2901 | wake_up(&mddev->sb_wait); |
| 2902 | err = 0; |
| 2903 | } else { |
| 2904 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2905 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2906 | err = do_md_run(mddev); |
| 2907 | } |
| 2908 | break; |
| 2909 | case write_pending: |
| 2910 | case active_idle: |
| 2911 | /* these cannot be set */ |
| 2912 | break; |
| 2913 | } |
| 2914 | if (err) |
| 2915 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2916 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 2917 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2918 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 2919 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2920 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2921 | static struct md_sysfs_entry md_array_state = |
| 2922 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2923 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2924 | static ssize_t |
| 2925 | null_show(mddev_t *mddev, char *page) |
| 2926 | { |
| 2927 | return -EINVAL; |
| 2928 | } |
| 2929 | |
| 2930 | static ssize_t |
| 2931 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2932 | { |
| 2933 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2934 | /* The new device is added to the array. |
| 2935 | * If the array has a persistent superblock, we read the |
| 2936 | * superblock to initialise info and check validity. |
| 2937 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2938 | * which mainly checks size. |
| 2939 | */ |
| 2940 | char *e; |
| 2941 | int major = simple_strtoul(buf, &e, 10); |
| 2942 | int minor; |
| 2943 | dev_t dev; |
| 2944 | mdk_rdev_t *rdev; |
| 2945 | int err; |
| 2946 | |
| 2947 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2948 | return -EINVAL; |
| 2949 | minor = simple_strtoul(e+1, &e, 10); |
| 2950 | if (*e && *e != '\n') |
| 2951 | return -EINVAL; |
| 2952 | dev = MKDEV(major, minor); |
| 2953 | if (major != MAJOR(dev) || |
| 2954 | minor != MINOR(dev)) |
| 2955 | return -EOVERFLOW; |
| 2956 | |
| 2957 | |
| 2958 | if (mddev->persistent) { |
| 2959 | rdev = md_import_device(dev, mddev->major_version, |
| 2960 | mddev->minor_version); |
| 2961 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2962 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2963 | mdk_rdev_t, same_set); |
| 2964 | err = super_types[mddev->major_version] |
| 2965 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2966 | if (err < 0) |
| 2967 | goto out; |
| 2968 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2969 | } else if (mddev->external) |
| 2970 | rdev = md_import_device(dev, -2, -1); |
| 2971 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2972 | rdev = md_import_device(dev, -1, -1); |
| 2973 | |
| 2974 | if (IS_ERR(rdev)) |
| 2975 | return PTR_ERR(rdev); |
| 2976 | err = bind_rdev_to_array(rdev, mddev); |
| 2977 | out: |
| 2978 | if (err) |
| 2979 | export_rdev(rdev); |
| 2980 | return err ? err : len; |
| 2981 | } |
| 2982 | |
| 2983 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2984 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2985 | |
| 2986 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2987 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2988 | { |
| 2989 | char *end; |
| 2990 | unsigned long chunk, end_chunk; |
| 2991 | |
| 2992 | if (!mddev->bitmap) |
| 2993 | goto out; |
| 2994 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2995 | while (*buf) { |
| 2996 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2997 | if (buf == end) break; |
| 2998 | if (*end == '-') { /* range */ |
| 2999 | buf = end + 1; |
| 3000 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3001 | if (buf == end) break; |
| 3002 | } |
| 3003 | if (*end && !isspace(*end)) break; |
| 3004 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3005 | buf = end; |
| 3006 | while (isspace(*buf)) buf++; |
| 3007 | } |
| 3008 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3009 | out: |
| 3010 | return len; |
| 3011 | } |
| 3012 | |
| 3013 | static struct md_sysfs_entry md_bitmap = |
| 3014 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3015 | |
| 3016 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3017 | size_show(mddev_t *mddev, char *page) |
| 3018 | { |
| 3019 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 3020 | } |
| 3021 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3022 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3023 | |
| 3024 | static ssize_t |
| 3025 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3026 | { |
| 3027 | /* If array is inactive, we can reduce the component size, but |
| 3028 | * not increase it (except from 0). |
| 3029 | * If array is active, we can try an on-line resize |
| 3030 | */ |
| 3031 | char *e; |
| 3032 | int err = 0; |
| 3033 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 3034 | if (!*buf || *buf == '\n' || |
| 3035 | (*e && *e != '\n')) |
| 3036 | return -EINVAL; |
| 3037 | |
| 3038 | if (mddev->pers) { |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3039 | err = update_size(mddev, size * 2); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3040 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3041 | } else { |
| 3042 | if (mddev->size == 0 || |
| 3043 | mddev->size > size) |
| 3044 | mddev->size = size; |
| 3045 | else |
| 3046 | err = -ENOSPC; |
| 3047 | } |
| 3048 | return err ? err : len; |
| 3049 | } |
| 3050 | |
| 3051 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3052 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3053 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3054 | |
| 3055 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3056 | * This is one of |
| 3057 | * 'none' for arrays with no metadata (good luck...) |
| 3058 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3059 | * or N.M for internally known formats |
| 3060 | */ |
| 3061 | static ssize_t |
| 3062 | metadata_show(mddev_t *mddev, char *page) |
| 3063 | { |
| 3064 | if (mddev->persistent) |
| 3065 | return sprintf(page, "%d.%d\n", |
| 3066 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3067 | else if (mddev->external) |
| 3068 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3069 | else |
| 3070 | return sprintf(page, "none\n"); |
| 3071 | } |
| 3072 | |
| 3073 | static ssize_t |
| 3074 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3075 | { |
| 3076 | int major, minor; |
| 3077 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3078 | /* Changing the details of 'external' metadata is |
| 3079 | * always permitted. Otherwise there must be |
| 3080 | * no devices attached to the array. |
| 3081 | */ |
| 3082 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3083 | ; |
| 3084 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3085 | return -EBUSY; |
| 3086 | |
| 3087 | if (cmd_match(buf, "none")) { |
| 3088 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3089 | mddev->external = 0; |
| 3090 | mddev->major_version = 0; |
| 3091 | mddev->minor_version = 90; |
| 3092 | return len; |
| 3093 | } |
| 3094 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3095 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3096 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3097 | namelen = sizeof(mddev->metadata_type)-1; |
| 3098 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3099 | mddev->metadata_type[namelen] = 0; |
| 3100 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3101 | mddev->metadata_type[--namelen] = 0; |
| 3102 | mddev->persistent = 0; |
| 3103 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3104 | mddev->major_version = 0; |
| 3105 | mddev->minor_version = 90; |
| 3106 | return len; |
| 3107 | } |
| 3108 | major = simple_strtoul(buf, &e, 10); |
| 3109 | if (e==buf || *e != '.') |
| 3110 | return -EINVAL; |
| 3111 | buf = e+1; |
| 3112 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3113 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3114 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3115 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3116 | return -ENOENT; |
| 3117 | mddev->major_version = major; |
| 3118 | mddev->minor_version = minor; |
| 3119 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3120 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3121 | return len; |
| 3122 | } |
| 3123 | |
| 3124 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3125 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3126 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3127 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3128 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3129 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3130 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 3131 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3132 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3133 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3134 | type = "reshape"; |
| 3135 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3136 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3137 | type = "resync"; |
| 3138 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3139 | type = "check"; |
| 3140 | else |
| 3141 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3142 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3143 | type = "recover"; |
| 3144 | } |
| 3145 | return sprintf(page, "%s\n", type); |
| 3146 | } |
| 3147 | |
| 3148 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3149 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3150 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3151 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3152 | return -EINVAL; |
| 3153 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3154 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3155 | if (mddev->sync_thread) { |
| 3156 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3157 | md_unregister_thread(mddev->sync_thread); |
| 3158 | mddev->sync_thread = NULL; |
| 3159 | mddev->recovery = 0; |
| 3160 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3161 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3162 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3163 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3164 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3165 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3166 | else if (cmd_match(page, "recover")) { |
| 3167 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3168 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3169 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3170 | int err; |
| 3171 | if (mddev->pers->start_reshape == NULL) |
| 3172 | return -EINVAL; |
| 3173 | err = mddev->pers->start_reshape(mddev); |
| 3174 | if (err) |
| 3175 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3176 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3177 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3178 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3179 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3180 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3181 | return -EINVAL; |
| 3182 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3183 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3184 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3185 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3186 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3187 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3188 | return len; |
| 3189 | } |
| 3190 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3191 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3192 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3193 | { |
| 3194 | return sprintf(page, "%llu\n", |
| 3195 | (unsigned long long) mddev->resync_mismatches); |
| 3196 | } |
| 3197 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3198 | static struct md_sysfs_entry md_scan_mode = |
| 3199 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3200 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3201 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3202 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3203 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3204 | static ssize_t |
| 3205 | sync_min_show(mddev_t *mddev, char *page) |
| 3206 | { |
| 3207 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3208 | mddev->sync_speed_min ? "local": "system"); |
| 3209 | } |
| 3210 | |
| 3211 | static ssize_t |
| 3212 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3213 | { |
| 3214 | int min; |
| 3215 | char *e; |
| 3216 | if (strncmp(buf, "system", 6)==0) { |
| 3217 | mddev->sync_speed_min = 0; |
| 3218 | return len; |
| 3219 | } |
| 3220 | min = simple_strtoul(buf, &e, 10); |
| 3221 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3222 | return -EINVAL; |
| 3223 | mddev->sync_speed_min = min; |
| 3224 | return len; |
| 3225 | } |
| 3226 | |
| 3227 | static struct md_sysfs_entry md_sync_min = |
| 3228 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3229 | |
| 3230 | static ssize_t |
| 3231 | sync_max_show(mddev_t *mddev, char *page) |
| 3232 | { |
| 3233 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3234 | mddev->sync_speed_max ? "local": "system"); |
| 3235 | } |
| 3236 | |
| 3237 | static ssize_t |
| 3238 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3239 | { |
| 3240 | int max; |
| 3241 | char *e; |
| 3242 | if (strncmp(buf, "system", 6)==0) { |
| 3243 | mddev->sync_speed_max = 0; |
| 3244 | return len; |
| 3245 | } |
| 3246 | max = simple_strtoul(buf, &e, 10); |
| 3247 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3248 | return -EINVAL; |
| 3249 | mddev->sync_speed_max = max; |
| 3250 | return len; |
| 3251 | } |
| 3252 | |
| 3253 | static struct md_sysfs_entry md_sync_max = |
| 3254 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3255 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3256 | static ssize_t |
| 3257 | degraded_show(mddev_t *mddev, char *page) |
| 3258 | { |
| 3259 | return sprintf(page, "%d\n", mddev->degraded); |
| 3260 | } |
| 3261 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3262 | |
| 3263 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3264 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3265 | { |
| 3266 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3267 | } |
| 3268 | |
| 3269 | static ssize_t |
| 3270 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3271 | { |
| 3272 | long n; |
| 3273 | |
| 3274 | if (strict_strtol(buf, 10, &n)) |
| 3275 | return -EINVAL; |
| 3276 | |
| 3277 | if (n != 0 && n != 1) |
| 3278 | return -EINVAL; |
| 3279 | |
| 3280 | mddev->parallel_resync = n; |
| 3281 | |
| 3282 | if (mddev->sync_thread) |
| 3283 | wake_up(&resync_wait); |
| 3284 | |
| 3285 | return len; |
| 3286 | } |
| 3287 | |
| 3288 | /* force parallel resync, even with shared block devices */ |
| 3289 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3290 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3291 | sync_force_parallel_show, sync_force_parallel_store); |
| 3292 | |
| 3293 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3294 | sync_speed_show(mddev_t *mddev, char *page) |
| 3295 | { |
| 3296 | unsigned long resync, dt, db; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3297 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3298 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3299 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3300 | db = resync - mddev->resync_mark_cnt; |
| 3301 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3302 | } |
| 3303 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3304 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3305 | |
| 3306 | static ssize_t |
| 3307 | sync_completed_show(mddev_t *mddev, char *page) |
| 3308 | { |
| 3309 | unsigned long max_blocks, resync; |
| 3310 | |
| 3311 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3312 | max_blocks = mddev->resync_max_sectors; |
| 3313 | else |
| 3314 | max_blocks = mddev->size << 1; |
| 3315 | |
| 3316 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3317 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3318 | } |
| 3319 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3320 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3321 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3322 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3323 | min_sync_show(mddev_t *mddev, char *page) |
| 3324 | { |
| 3325 | return sprintf(page, "%llu\n", |
| 3326 | (unsigned long long)mddev->resync_min); |
| 3327 | } |
| 3328 | static ssize_t |
| 3329 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3330 | { |
| 3331 | unsigned long long min; |
| 3332 | if (strict_strtoull(buf, 10, &min)) |
| 3333 | return -EINVAL; |
| 3334 | if (min > mddev->resync_max) |
| 3335 | return -EINVAL; |
| 3336 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3337 | return -EBUSY; |
| 3338 | |
| 3339 | /* Must be a multiple of chunk_size */ |
| 3340 | if (mddev->chunk_size) { |
| 3341 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3342 | return -EINVAL; |
| 3343 | } |
| 3344 | mddev->resync_min = min; |
| 3345 | |
| 3346 | return len; |
| 3347 | } |
| 3348 | |
| 3349 | static struct md_sysfs_entry md_min_sync = |
| 3350 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3351 | |
| 3352 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3353 | max_sync_show(mddev_t *mddev, char *page) |
| 3354 | { |
| 3355 | if (mddev->resync_max == MaxSector) |
| 3356 | return sprintf(page, "max\n"); |
| 3357 | else |
| 3358 | return sprintf(page, "%llu\n", |
| 3359 | (unsigned long long)mddev->resync_max); |
| 3360 | } |
| 3361 | static ssize_t |
| 3362 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3363 | { |
| 3364 | if (strncmp(buf, "max", 3) == 0) |
| 3365 | mddev->resync_max = MaxSector; |
| 3366 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3367 | unsigned long long max; |
| 3368 | if (strict_strtoull(buf, 10, &max)) |
| 3369 | return -EINVAL; |
| 3370 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3371 | return -EINVAL; |
| 3372 | if (max < mddev->resync_max && |
| 3373 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3374 | return -EBUSY; |
| 3375 | |
| 3376 | /* Must be a multiple of chunk_size */ |
| 3377 | if (mddev->chunk_size) { |
| 3378 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3379 | return -EINVAL; |
| 3380 | } |
| 3381 | mddev->resync_max = max; |
| 3382 | } |
| 3383 | wake_up(&mddev->recovery_wait); |
| 3384 | return len; |
| 3385 | } |
| 3386 | |
| 3387 | static struct md_sysfs_entry md_max_sync = |
| 3388 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3389 | |
| 3390 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3391 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3392 | { |
| 3393 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3394 | } |
| 3395 | |
| 3396 | static ssize_t |
| 3397 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3398 | { |
| 3399 | char *e; |
| 3400 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3401 | |
| 3402 | if (mddev->pers->quiesce == NULL) |
| 3403 | return -EINVAL; |
| 3404 | if (buf == e || (*e && *e != '\n')) |
| 3405 | return -EINVAL; |
| 3406 | if (new >= mddev->suspend_hi || |
| 3407 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3408 | mddev->suspend_lo = new; |
| 3409 | mddev->pers->quiesce(mddev, 2); |
| 3410 | return len; |
| 3411 | } else |
| 3412 | return -EINVAL; |
| 3413 | } |
| 3414 | static struct md_sysfs_entry md_suspend_lo = |
| 3415 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3416 | |
| 3417 | |
| 3418 | static ssize_t |
| 3419 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3420 | { |
| 3421 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3422 | } |
| 3423 | |
| 3424 | static ssize_t |
| 3425 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3426 | { |
| 3427 | char *e; |
| 3428 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3429 | |
| 3430 | if (mddev->pers->quiesce == NULL) |
| 3431 | return -EINVAL; |
| 3432 | if (buf == e || (*e && *e != '\n')) |
| 3433 | return -EINVAL; |
| 3434 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3435 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3436 | mddev->suspend_hi = new; |
| 3437 | mddev->pers->quiesce(mddev, 1); |
| 3438 | mddev->pers->quiesce(mddev, 0); |
| 3439 | return len; |
| 3440 | } else |
| 3441 | return -EINVAL; |
| 3442 | } |
| 3443 | static struct md_sysfs_entry md_suspend_hi = |
| 3444 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3445 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3446 | static ssize_t |
| 3447 | reshape_position_show(mddev_t *mddev, char *page) |
| 3448 | { |
| 3449 | if (mddev->reshape_position != MaxSector) |
| 3450 | return sprintf(page, "%llu\n", |
| 3451 | (unsigned long long)mddev->reshape_position); |
| 3452 | strcpy(page, "none\n"); |
| 3453 | return 5; |
| 3454 | } |
| 3455 | |
| 3456 | static ssize_t |
| 3457 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3458 | { |
| 3459 | char *e; |
| 3460 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3461 | if (mddev->pers) |
| 3462 | return -EBUSY; |
| 3463 | if (buf == e || (*e && *e != '\n')) |
| 3464 | return -EINVAL; |
| 3465 | mddev->reshape_position = new; |
| 3466 | mddev->delta_disks = 0; |
| 3467 | mddev->new_level = mddev->level; |
| 3468 | mddev->new_layout = mddev->layout; |
| 3469 | mddev->new_chunk = mddev->chunk_size; |
| 3470 | return len; |
| 3471 | } |
| 3472 | |
| 3473 | static struct md_sysfs_entry md_reshape_position = |
| 3474 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3475 | reshape_position_store); |
| 3476 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3477 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3478 | static struct attribute *md_default_attrs[] = { |
| 3479 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3480 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3481 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3482 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3483 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3484 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3485 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3486 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3487 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3488 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3489 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3490 | NULL, |
| 3491 | }; |
| 3492 | |
| 3493 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3494 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3495 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3496 | &md_sync_min.attr, |
| 3497 | &md_sync_max.attr, |
| 3498 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3499 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3500 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3501 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3502 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3503 | &md_suspend_lo.attr, |
| 3504 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3505 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3506 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3507 | NULL, |
| 3508 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3509 | static struct attribute_group md_redundancy_group = { |
| 3510 | .name = NULL, |
| 3511 | .attrs = md_redundancy_attrs, |
| 3512 | }; |
| 3513 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3514 | |
| 3515 | static ssize_t |
| 3516 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3517 | { |
| 3518 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3519 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3520 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3521 | |
| 3522 | if (!entry->show) |
| 3523 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3524 | rv = mddev_lock(mddev); |
| 3525 | if (!rv) { |
| 3526 | rv = entry->show(mddev, page); |
| 3527 | mddev_unlock(mddev); |
| 3528 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3529 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | static ssize_t |
| 3533 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3534 | const char *page, size_t length) |
| 3535 | { |
| 3536 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3537 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3538 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3539 | |
| 3540 | if (!entry->store) |
| 3541 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3542 | if (!capable(CAP_SYS_ADMIN)) |
| 3543 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3544 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3545 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3546 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3547 | if (!rv) { |
| 3548 | rv = entry->store(mddev, page, length); |
| 3549 | mddev_unlock(mddev); |
| 3550 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3551 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static void md_free(struct kobject *ko) |
| 3555 | { |
| 3556 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3557 | |
| 3558 | if (mddev->sysfs_state) |
| 3559 | sysfs_put(mddev->sysfs_state); |
| 3560 | |
| 3561 | if (mddev->gendisk) { |
| 3562 | del_gendisk(mddev->gendisk); |
| 3563 | put_disk(mddev->gendisk); |
| 3564 | } |
| 3565 | if (mddev->queue) |
| 3566 | blk_cleanup_queue(mddev->queue); |
| 3567 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3568 | kfree(mddev); |
| 3569 | } |
| 3570 | |
| 3571 | static struct sysfs_ops md_sysfs_ops = { |
| 3572 | .show = md_attr_show, |
| 3573 | .store = md_attr_store, |
| 3574 | }; |
| 3575 | static struct kobj_type md_ktype = { |
| 3576 | .release = md_free, |
| 3577 | .sysfs_ops = &md_sysfs_ops, |
| 3578 | .default_attrs = md_default_attrs, |
| 3579 | }; |
| 3580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | int mdp_major = 0; |
| 3582 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3583 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3585 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | mddev_t *mddev = mddev_find(dev); |
| 3587 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3588 | int partitioned; |
| 3589 | int shift; |
| 3590 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3591 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | |
| 3593 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3594 | return -ENODEV; |
| 3595 | |
| 3596 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3597 | shift = partitioned ? MdpMinorShift : 0; |
| 3598 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3600 | /* wait for any previous instance if this device |
| 3601 | * to be completed removed (mddev_delayed_delete). |
| 3602 | */ |
| 3603 | flush_scheduled_work(); |
| 3604 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3605 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3607 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3609 | return -EEXIST; |
| 3610 | } |
| 3611 | |
| 3612 | if (name) { |
| 3613 | /* Need to ensure that 'name' is not a duplicate. |
| 3614 | */ |
| 3615 | mddev_t *mddev2; |
| 3616 | spin_lock(&all_mddevs_lock); |
| 3617 | |
| 3618 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3619 | if (mddev2->gendisk && |
| 3620 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3621 | spin_unlock(&all_mddevs_lock); |
| 3622 | return -EEXIST; |
| 3623 | } |
| 3624 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3626 | |
| 3627 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
| 3628 | if (!mddev->queue) { |
| 3629 | mutex_unlock(&disks_mutex); |
| 3630 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3631 | return -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3632 | } |
| 3633 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3634 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3635 | |
| 3636 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | disk = alloc_disk(1 << shift); |
| 3639 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3640 | mutex_unlock(&disks_mutex); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3641 | blk_cleanup_queue(mddev->queue); |
| 3642 | mddev->queue = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3644 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3646 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3648 | if (name) |
| 3649 | strcpy(disk->disk_name, name); |
| 3650 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3652 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | disk->fops = &md_fops; |
| 3655 | disk->private_data = mddev; |
| 3656 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3657 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3658 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3659 | * remove it now. |
| 3660 | */ |
| 3661 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | add_disk(disk); |
| 3663 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3664 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3665 | &disk_to_dev(disk)->kobj, "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3666 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3667 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3668 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3669 | disk->disk_name); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3670 | else { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3671 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3672 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3673 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3674 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3675 | return 0; |
| 3676 | } |
| 3677 | |
| 3678 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3679 | { |
| 3680 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | return NULL; |
| 3682 | } |
| 3683 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3684 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3685 | { |
| 3686 | /* val must be "md_*" where * is not all digits. |
| 3687 | * We allocate an array with a large free minor number, and |
| 3688 | * set the name to val. val must not already be an active name. |
| 3689 | */ |
| 3690 | int len = strlen(val); |
| 3691 | char buf[DISK_NAME_LEN]; |
| 3692 | |
| 3693 | while (len && val[len-1] == '\n') |
| 3694 | len--; |
| 3695 | if (len >= DISK_NAME_LEN) |
| 3696 | return -E2BIG; |
| 3697 | strlcpy(buf, val, len+1); |
| 3698 | if (strncmp(buf, "md_", 3) != 0) |
| 3699 | return -EINVAL; |
| 3700 | return md_alloc(0, buf); |
| 3701 | } |
| 3702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | static void md_safemode_timeout(unsigned long data) |
| 3704 | { |
| 3705 | mddev_t *mddev = (mddev_t *) data; |
| 3706 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3707 | if (!atomic_read(&mddev->writes_pending)) { |
| 3708 | mddev->safemode = 1; |
| 3709 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3710 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | md_wakeup_thread(mddev->thread); |
| 3713 | } |
| 3714 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3715 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
| 3717 | static int do_md_run(mddev_t * mddev) |
| 3718 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3719 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | int chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | mdk_rdev_t *rdev; |
| 3722 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3723 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | char b[BDEVNAME_SIZE]; |
| 3725 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3726 | if (list_empty(&mddev->disks)) |
| 3727 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | |
| 3730 | if (mddev->pers) |
| 3731 | return -EBUSY; |
| 3732 | |
| 3733 | /* |
| 3734 | * Analyze all RAID superblock(s) |
| 3735 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3736 | if (!mddev->raid_disks) { |
| 3737 | if (!mddev->persistent) |
| 3738 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3739 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | |
| 3742 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3744 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3746 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3747 | chunk_size, MAX_CHUNK_SIZE); |
| 3748 | return -EINVAL; |
| 3749 | } |
| 3750 | /* |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3751 | * chunk-size has to be a power of 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | */ |
| 3753 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3754 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | return -EINVAL; |
| 3756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | |
| 3758 | /* devices must have minimum size of one chunk */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3759 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3760 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | continue; |
| 3762 | if (rdev->size < chunk_size / 1024) { |
| 3763 | printk(KERN_WARNING |
| 3764 | "md: Dev %s smaller than chunk_size:" |
| 3765 | " %lluk < %dk\n", |
| 3766 | bdevname(rdev->bdev,b), |
| 3767 | (unsigned long long)rdev->size, |
| 3768 | chunk_size / 1024); |
| 3769 | return -EINVAL; |
| 3770 | } |
| 3771 | } |
| 3772 | } |
| 3773 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3774 | if (mddev->level != LEVEL_NONE) |
| 3775 | request_module("md-level-%d", mddev->level); |
| 3776 | else if (mddev->clevel[0]) |
| 3777 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | |
| 3779 | /* |
| 3780 | * Drop all container device buffers, from now on |
| 3781 | * the only valid external interface is through the md |
| 3782 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3784 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3785 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | continue; |
| 3787 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3788 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3789 | |
| 3790 | /* perform some consistency tests on the device. |
| 3791 | * We don't want the data to overlap the metadata, |
| 3792 | * Internal Bitmap issues has handled elsewhere. |
| 3793 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3794 | if (rdev->data_offset < rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3795 | if (mddev->size && |
| 3796 | rdev->data_offset + mddev->size*2 |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3797 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3798 | printk("md: %s: data overlaps metadata\n", |
| 3799 | mdname(mddev)); |
| 3800 | return -EINVAL; |
| 3801 | } |
| 3802 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 3803 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3804 | > rdev->data_offset) { |
| 3805 | printk("md: %s: metadata overlaps data\n", |
| 3806 | mdname(mddev)); |
| 3807 | return -EINVAL; |
| 3808 | } |
| 3809 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 3810 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | md_probe(mddev->unit, NULL, NULL); |
| 3814 | disk = mddev->gendisk; |
| 3815 | if (!disk) |
| 3816 | return -ENOMEM; |
| 3817 | |
| 3818 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3819 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3820 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3822 | if (mddev->level != LEVEL_NONE) |
| 3823 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3824 | mddev->level); |
| 3825 | else |
| 3826 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3827 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | return -EINVAL; |
| 3829 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3830 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3832 | mddev->level = pers->level; |
| 3833 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 3835 | if (pers->level >= 4 && pers->level <= 6) |
| 3836 | /* Cannot support integrity (yet) */ |
| 3837 | blk_integrity_unregister(mddev->gendisk); |
| 3838 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3839 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3840 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3841 | /* This personality cannot handle reshaping... */ |
| 3842 | mddev->pers = NULL; |
| 3843 | module_put(pers->owner); |
| 3844 | return -EINVAL; |
| 3845 | } |
| 3846 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3847 | if (pers->sync_request) { |
| 3848 | /* Warn if this is a potentially silly |
| 3849 | * configuration. |
| 3850 | */ |
| 3851 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3852 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3853 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3854 | |
| 3855 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3856 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3857 | if (rdev < rdev2 && |
| 3858 | rdev->bdev->bd_contains == |
| 3859 | rdev2->bdev->bd_contains) { |
| 3860 | printk(KERN_WARNING |
| 3861 | "%s: WARNING: %s appears to be" |
| 3862 | " on the same physical disk as" |
| 3863 | " %s.\n", |
| 3864 | mdname(mddev), |
| 3865 | bdevname(rdev->bdev,b), |
| 3866 | bdevname(rdev2->bdev,b2)); |
| 3867 | warned = 1; |
| 3868 | } |
| 3869 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3870 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3871 | if (warned) |
| 3872 | printk(KERN_WARNING |
| 3873 | "True protection against single-disk" |
| 3874 | " failure might be compromised.\n"); |
| 3875 | } |
| 3876 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3877 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3879 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3880 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3882 | if (start_readonly) |
| 3883 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3884 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3885 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 3886 | if (err) |
| 3887 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3888 | else if (mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3889 | err = bitmap_create(mddev); |
| 3890 | if (err) { |
| 3891 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3892 | mdname(mddev), err); |
| 3893 | mddev->pers->stop(mddev); |
| 3894 | } |
| 3895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | module_put(mddev->pers->owner); |
| 3898 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3899 | bitmap_destroy(mddev); |
| 3900 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3902 | if (mddev->pers->sync_request) { |
| 3903 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3904 | printk(KERN_WARNING |
| 3905 | "md: cannot register extra attributes for %s\n", |
| 3906 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3907 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3908 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3909 | mddev->ro = 0; |
| 3910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | atomic_set(&mddev->writes_pending,0); |
| 3912 | mddev->safemode = 0; |
| 3913 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3914 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3915 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3917 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3918 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3919 | if (rdev->raid_disk >= 0) { |
| 3920 | char nm[20]; |
| 3921 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3922 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3923 | printk("md: cannot register %s for %s\n", |
| 3924 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | |
| 3927 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3928 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3929 | if (mddev->flags) |
| 3930 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 3932 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | |
| 3934 | /* If we call blk_queue_make_request here, it will |
| 3935 | * re-initialise max_sectors etc which may have been |
| 3936 | * refined inside -> run. So just set the bits we need to set. |
| 3937 | * Most initialisation happended when we called |
| 3938 | * blk_queue_make_request(..., md_fail_request) |
| 3939 | * earlier. |
| 3940 | */ |
| 3941 | mddev->queue->queuedata = mddev; |
| 3942 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3943 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3944 | /* If there is a partially-recovered drive we need to |
| 3945 | * start recovery here. If we leave it to md_check_recovery, |
| 3946 | * it will remove the drives and not do the right thing |
| 3947 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3948 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3949 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3950 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3951 | if (rdev->raid_disk >= 0 && |
| 3952 | !test_bit(In_sync, &rdev->flags) && |
| 3953 | !test_bit(Faulty, &rdev->flags)) |
| 3954 | /* complete an interrupted recovery */ |
| 3955 | spares++; |
| 3956 | if (spares && mddev->pers->sync_request) { |
| 3957 | mddev->recovery = 0; |
| 3958 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3959 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3960 | mddev, |
| 3961 | "%s_resync"); |
| 3962 | if (!mddev->sync_thread) { |
| 3963 | printk(KERN_ERR "%s: could not start resync" |
| 3964 | " thread...\n", |
| 3965 | mdname(mddev)); |
| 3966 | /* leave the spares where they are, it shouldn't hurt */ |
| 3967 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3968 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3969 | } |
| 3970 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3971 | md_wakeup_thread(mddev->thread); |
| 3972 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3973 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3974 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3975 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3976 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3977 | if (mddev->sysfs_action) |
| 3978 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3979 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3980 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
| 3983 | |
| 3984 | static int restart_array(mddev_t *mddev) |
| 3985 | { |
| 3986 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3988 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 3990 | return -ENXIO; |
| 3991 | if (!mddev->pers) |
| 3992 | return -EINVAL; |
| 3993 | if (!mddev->ro) |
| 3994 | return -EBUSY; |
| 3995 | mddev->safemode = 0; |
| 3996 | mddev->ro = 0; |
| 3997 | set_disk_ro(disk, 0); |
| 3998 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3999 | mdname(mddev)); |
| 4000 | /* Kick recovery or resync if necessary */ |
| 4001 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4002 | md_wakeup_thread(mddev->thread); |
| 4003 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4004 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4005 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | } |
| 4007 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4008 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4009 | * to the file ourselves */ |
| 4010 | static int deny_bitmap_write_access(struct file * file) |
| 4011 | { |
| 4012 | struct inode *inode = file->f_mapping->host; |
| 4013 | |
| 4014 | spin_lock(&inode->i_lock); |
| 4015 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4016 | spin_unlock(&inode->i_lock); |
| 4017 | return -ETXTBSY; |
| 4018 | } |
| 4019 | atomic_set(&inode->i_writecount, -1); |
| 4020 | spin_unlock(&inode->i_lock); |
| 4021 | |
| 4022 | return 0; |
| 4023 | } |
| 4024 | |
| 4025 | static void restore_bitmap_write_access(struct file *file) |
| 4026 | { |
| 4027 | struct inode *inode = file->f_mapping->host; |
| 4028 | |
| 4029 | spin_lock(&inode->i_lock); |
| 4030 | atomic_set(&inode->i_writecount, 1); |
| 4031 | spin_unlock(&inode->i_lock); |
| 4032 | } |
| 4033 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4034 | /* mode: |
| 4035 | * 0 - completely stop and dis-assemble array |
| 4036 | * 1 - switch to readonly |
| 4037 | * 2 - stop but do not disassemble array |
| 4038 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4039 | 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] | 4040 | { |
| 4041 | int err = 0; |
| 4042 | struct gendisk *disk = mddev->gendisk; |
| 4043 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4044 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4045 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4046 | return -EBUSY; |
| 4047 | } |
| 4048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | |
| 4051 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4052 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4054 | md_unregister_thread(mddev->sync_thread); |
| 4055 | mddev->sync_thread = NULL; |
| 4056 | } |
| 4057 | |
| 4058 | del_timer_sync(&mddev->safemode_timer); |
| 4059 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4060 | switch(mode) { |
| 4061 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4063 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | goto out; |
| 4065 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4066 | break; |
| 4067 | case 0: /* disassemble */ |
| 4068 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4069 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4070 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | if (mddev->ro) |
| 4072 | set_disk_ro(disk, 0); |
| 4073 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 4074 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4075 | mddev->queue->merge_bvec_fn = NULL; |
| 4076 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4077 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4078 | if (mddev->pers->sync_request) { |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4079 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4080 | if (mddev->sysfs_action) |
| 4081 | sysfs_put(mddev->sysfs_action); |
| 4082 | mddev->sysfs_action = NULL; |
| 4083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4084 | module_put(mddev->pers->owner); |
| 4085 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4086 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4087 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4088 | |
| 4089 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4090 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | if (mddev->ro) |
| 4093 | mddev->ro = 0; |
| 4094 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4095 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | /* mark array as shutdown cleanly */ |
| 4097 | mddev->in_sync = 1; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4098 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4100 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4102 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | /* |
| 4106 | * Free resources if final stop |
| 4107 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4108 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4109 | mdk_rdev_t *rdev; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4112 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4113 | bitmap_destroy(mddev); |
| 4114 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4115 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4116 | fput(mddev->bitmap_file); |
| 4117 | mddev->bitmap_file = NULL; |
| 4118 | } |
| 4119 | mddev->bitmap_offset = 0; |
| 4120 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4121 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4122 | if (rdev->raid_disk >= 0) { |
| 4123 | char nm[20]; |
| 4124 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4125 | sysfs_remove_link(&mddev->kobj, nm); |
| 4126 | } |
| 4127 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4128 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4129 | flush_scheduled_work(); |
| 4130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | export_array(mddev); |
| 4132 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4133 | mddev->array_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4134 | mddev->size = 0; |
| 4135 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4136 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4137 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4138 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4139 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4140 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4141 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4142 | mddev->level = LEVEL_NONE; |
| 4143 | mddev->clevel[0] = 0; |
| 4144 | mddev->flags = 0; |
| 4145 | mddev->ro = 0; |
| 4146 | mddev->metadata_type[0] = 0; |
| 4147 | mddev->chunk_size = 0; |
| 4148 | mddev->ctime = mddev->utime = 0; |
| 4149 | mddev->layout = 0; |
| 4150 | mddev->max_disks = 0; |
| 4151 | mddev->events = 0; |
| 4152 | mddev->delta_disks = 0; |
| 4153 | mddev->new_level = LEVEL_NONE; |
| 4154 | mddev->new_layout = 0; |
| 4155 | mddev->new_chunk = 0; |
| 4156 | mddev->curr_resync = 0; |
| 4157 | mddev->resync_mismatches = 0; |
| 4158 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4159 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4160 | mddev->recovery = 0; |
| 4161 | mddev->in_sync = 0; |
| 4162 | mddev->changed = 0; |
| 4163 | mddev->degraded = 0; |
| 4164 | mddev->barriers_work = 0; |
| 4165 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4166 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4167 | if (mddev->hold_active == UNTIL_STOP) |
| 4168 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4169 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4170 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4172 | mdname(mddev)); |
| 4173 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4174 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4175 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4176 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | out: |
| 4178 | return err; |
| 4179 | } |
| 4180 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4181 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | static void autorun_array(mddev_t *mddev) |
| 4183 | { |
| 4184 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | int err; |
| 4186 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4187 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | |
| 4190 | printk(KERN_INFO "md: running: "); |
| 4191 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4192 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | char b[BDEVNAME_SIZE]; |
| 4194 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4195 | } |
| 4196 | printk("\n"); |
| 4197 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4198 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | if (err) { |
| 4200 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4201 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | /* |
| 4206 | * lets try to run arrays based on all disks that have arrived |
| 4207 | * until now. (those are in pending_raid_disks) |
| 4208 | * |
| 4209 | * the method: pick the first pending disk, collect all disks with |
| 4210 | * the same UUID, remove all from the pending list and put them into |
| 4211 | * the 'same_array' list. Then order this list based on superblock |
| 4212 | * update time (freshest comes first), kick out 'old' disks and |
| 4213 | * compare superblocks. If everything's fine then run it. |
| 4214 | * |
| 4215 | * If "unit" is allocated, then bump its reference count |
| 4216 | */ |
| 4217 | static void autorun_devices(int part) |
| 4218 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4219 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | mddev_t *mddev; |
| 4221 | char b[BDEVNAME_SIZE]; |
| 4222 | |
| 4223 | printk(KERN_INFO "md: autorun ...\n"); |
| 4224 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4225 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4227 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | rdev0 = list_entry(pending_raid_disks.next, |
| 4229 | mdk_rdev_t, same_set); |
| 4230 | |
| 4231 | printk(KERN_INFO "md: considering %s ...\n", |
| 4232 | bdevname(rdev0->bdev,b)); |
| 4233 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4234 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4236 | printk(KERN_INFO "md: adding %s ...\n", |
| 4237 | bdevname(rdev->bdev,b)); |
| 4238 | list_move(&rdev->same_set, &candidates); |
| 4239 | } |
| 4240 | /* |
| 4241 | * now we have a set of devices, with all of them having |
| 4242 | * mostly sane superblocks. It's time to allocate the |
| 4243 | * mddev. |
| 4244 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4245 | if (part) { |
| 4246 | dev = MKDEV(mdp_major, |
| 4247 | rdev0->preferred_minor << MdpMinorShift); |
| 4248 | unit = MINOR(dev) >> MdpMinorShift; |
| 4249 | } else { |
| 4250 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4251 | unit = MINOR(dev); |
| 4252 | } |
| 4253 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4255 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4256 | break; |
| 4257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
| 4259 | md_probe(dev, NULL, NULL); |
| 4260 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4261 | if (!mddev || !mddev->gendisk) { |
| 4262 | if (mddev) |
| 4263 | mddev_put(mddev); |
| 4264 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | "md: cannot allocate memory for md drive.\n"); |
| 4266 | break; |
| 4267 | } |
| 4268 | if (mddev_lock(mddev)) |
| 4269 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4270 | mdname(mddev)); |
| 4271 | else if (mddev->raid_disks || mddev->major_version |
| 4272 | || !list_empty(&mddev->disks)) { |
| 4273 | printk(KERN_WARNING |
| 4274 | "md: %s already running, cannot run %s\n", |
| 4275 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4276 | mddev_unlock(mddev); |
| 4277 | } else { |
| 4278 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4279 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4280 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | list_del_init(&rdev->same_set); |
| 4282 | if (bind_rdev_to_array(rdev, mddev)) |
| 4283 | export_rdev(rdev); |
| 4284 | } |
| 4285 | autorun_array(mddev); |
| 4286 | mddev_unlock(mddev); |
| 4287 | } |
| 4288 | /* on success, candidates will be empty, on error |
| 4289 | * it won't... |
| 4290 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4291 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4292 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | mddev_put(mddev); |
| 4296 | } |
| 4297 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4298 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4299 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | static int get_version(void __user * arg) |
| 4302 | { |
| 4303 | mdu_version_t ver; |
| 4304 | |
| 4305 | ver.major = MD_MAJOR_VERSION; |
| 4306 | ver.minor = MD_MINOR_VERSION; |
| 4307 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4308 | |
| 4309 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4310 | return -EFAULT; |
| 4311 | |
| 4312 | return 0; |
| 4313 | } |
| 4314 | |
| 4315 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4316 | { |
| 4317 | mdu_array_info_t info; |
| 4318 | int nr,working,active,failed,spare; |
| 4319 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | |
| 4321 | nr=working=active=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4322 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4324 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | failed++; |
| 4326 | else { |
| 4327 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4328 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | active++; |
| 4330 | else |
| 4331 | spare++; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | info.major_version = mddev->major_version; |
| 4336 | info.minor_version = mddev->minor_version; |
| 4337 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4338 | info.ctime = mddev->ctime; |
| 4339 | info.level = mddev->level; |
| 4340 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4341 | if (info.size != mddev->size) /* overflow */ |
| 4342 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | info.nr_disks = nr; |
| 4344 | info.raid_disks = mddev->raid_disks; |
| 4345 | info.md_minor = mddev->md_minor; |
| 4346 | info.not_persistent= !mddev->persistent; |
| 4347 | |
| 4348 | info.utime = mddev->utime; |
| 4349 | info.state = 0; |
| 4350 | if (mddev->in_sync) |
| 4351 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4352 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4353 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | info.active_disks = active; |
| 4355 | info.working_disks = working; |
| 4356 | info.failed_disks = failed; |
| 4357 | info.spare_disks = spare; |
| 4358 | |
| 4359 | info.layout = mddev->layout; |
| 4360 | info.chunk_size = mddev->chunk_size; |
| 4361 | |
| 4362 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4363 | return -EFAULT; |
| 4364 | |
| 4365 | return 0; |
| 4366 | } |
| 4367 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4368 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4369 | { |
| 4370 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4371 | char *ptr, *buf = NULL; |
| 4372 | int err = -ENOMEM; |
| 4373 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4374 | if (md_allow_write(mddev)) |
| 4375 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4376 | else |
| 4377 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4378 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4379 | if (!file) |
| 4380 | goto out; |
| 4381 | |
| 4382 | /* bitmap disabled, zero the first byte and copy out */ |
| 4383 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4384 | file->pathname[0] = '\0'; |
| 4385 | goto copy_out; |
| 4386 | } |
| 4387 | |
| 4388 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4389 | if (!buf) |
| 4390 | goto out; |
| 4391 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4392 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4393 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4394 | goto out; |
| 4395 | |
| 4396 | strcpy(file->pathname, ptr); |
| 4397 | |
| 4398 | copy_out: |
| 4399 | err = 0; |
| 4400 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4401 | err = -EFAULT; |
| 4402 | out: |
| 4403 | kfree(buf); |
| 4404 | kfree(file); |
| 4405 | return err; |
| 4406 | } |
| 4407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4409 | { |
| 4410 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | mdk_rdev_t *rdev; |
| 4412 | |
| 4413 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4414 | return -EFAULT; |
| 4415 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4416 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | if (rdev) { |
| 4418 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4419 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4420 | info.raid_disk = rdev->raid_disk; |
| 4421 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4422 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4424 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4426 | info.state |= (1<<MD_DISK_SYNC); |
| 4427 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4428 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4429 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | } else { |
| 4431 | info.major = info.minor = 0; |
| 4432 | info.raid_disk = -1; |
| 4433 | info.state = (1<<MD_DISK_REMOVED); |
| 4434 | } |
| 4435 | |
| 4436 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4437 | return -EFAULT; |
| 4438 | |
| 4439 | return 0; |
| 4440 | } |
| 4441 | |
| 4442 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4443 | { |
| 4444 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4445 | mdk_rdev_t *rdev; |
| 4446 | dev_t dev = MKDEV(info->major,info->minor); |
| 4447 | |
| 4448 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4449 | return -EOVERFLOW; |
| 4450 | |
| 4451 | if (!mddev->raid_disks) { |
| 4452 | int err; |
| 4453 | /* expecting a device which has a superblock */ |
| 4454 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4455 | if (IS_ERR(rdev)) { |
| 4456 | printk(KERN_WARNING |
| 4457 | "md: md_import_device returned %ld\n", |
| 4458 | PTR_ERR(rdev)); |
| 4459 | return PTR_ERR(rdev); |
| 4460 | } |
| 4461 | if (!list_empty(&mddev->disks)) { |
| 4462 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4463 | mdk_rdev_t, same_set); |
| 4464 | int err = super_types[mddev->major_version] |
| 4465 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4466 | if (err < 0) { |
| 4467 | printk(KERN_WARNING |
| 4468 | "md: %s has different UUID to %s\n", |
| 4469 | bdevname(rdev->bdev,b), |
| 4470 | bdevname(rdev0->bdev,b2)); |
| 4471 | export_rdev(rdev); |
| 4472 | return -EINVAL; |
| 4473 | } |
| 4474 | } |
| 4475 | err = bind_rdev_to_array(rdev, mddev); |
| 4476 | if (err) |
| 4477 | export_rdev(rdev); |
| 4478 | return err; |
| 4479 | } |
| 4480 | |
| 4481 | /* |
| 4482 | * add_new_disk can be used once the array is assembled |
| 4483 | * to add "hot spares". They must already have a superblock |
| 4484 | * written |
| 4485 | */ |
| 4486 | if (mddev->pers) { |
| 4487 | int err; |
| 4488 | if (!mddev->pers->hot_add_disk) { |
| 4489 | printk(KERN_WARNING |
| 4490 | "%s: personality does not support diskops!\n", |
| 4491 | mdname(mddev)); |
| 4492 | return -EINVAL; |
| 4493 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4494 | if (mddev->persistent) |
| 4495 | rdev = md_import_device(dev, mddev->major_version, |
| 4496 | mddev->minor_version); |
| 4497 | else |
| 4498 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | if (IS_ERR(rdev)) { |
| 4500 | printk(KERN_WARNING |
| 4501 | "md: md_import_device returned %ld\n", |
| 4502 | PTR_ERR(rdev)); |
| 4503 | return PTR_ERR(rdev); |
| 4504 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4505 | /* set save_raid_disk if appropriate */ |
| 4506 | if (!mddev->persistent) { |
| 4507 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4508 | info->raid_disk < mddev->raid_disks) |
| 4509 | rdev->raid_disk = info->raid_disk; |
| 4510 | else |
| 4511 | rdev->raid_disk = -1; |
| 4512 | } else |
| 4513 | super_types[mddev->major_version]. |
| 4514 | validate_super(mddev, rdev); |
| 4515 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4516 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4517 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4518 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4519 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame^] | 4520 | else |
| 4521 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | rdev->raid_disk = -1; |
| 4524 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4525 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4526 | /* If there is hot_add_disk but no hot_remove_disk |
| 4527 | * then added disks for geometry changes, |
| 4528 | * and should be added immediately. |
| 4529 | */ |
| 4530 | super_types[mddev->major_version]. |
| 4531 | validate_super(mddev, rdev); |
| 4532 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4533 | if (err) |
| 4534 | unbind_rdev_from_array(rdev); |
| 4535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | if (err) |
| 4537 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4538 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4539 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4540 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4541 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4542 | if (mddev->degraded) |
| 4543 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4544 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4545 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | return err; |
| 4547 | } |
| 4548 | |
| 4549 | /* otherwise, add_new_disk is only allowed |
| 4550 | * for major_version==0 superblocks |
| 4551 | */ |
| 4552 | if (mddev->major_version != 0) { |
| 4553 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4554 | mdname(mddev)); |
| 4555 | return -EINVAL; |
| 4556 | } |
| 4557 | |
| 4558 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4559 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4560 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | if (IS_ERR(rdev)) { |
| 4562 | printk(KERN_WARNING |
| 4563 | "md: error, md_import_device() returned %ld\n", |
| 4564 | PTR_ERR(rdev)); |
| 4565 | return PTR_ERR(rdev); |
| 4566 | } |
| 4567 | rdev->desc_nr = info->number; |
| 4568 | if (info->raid_disk < mddev->raid_disks) |
| 4569 | rdev->raid_disk = info->raid_disk; |
| 4570 | else |
| 4571 | rdev->raid_disk = -1; |
| 4572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4574 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4575 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4577 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4578 | set_bit(WriteMostly, &rdev->flags); |
| 4579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | if (!mddev->persistent) { |
| 4581 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4582 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4584 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4585 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4587 | err = bind_rdev_to_array(rdev, mddev); |
| 4588 | if (err) { |
| 4589 | export_rdev(rdev); |
| 4590 | return err; |
| 4591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
| 4597 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4598 | { |
| 4599 | char b[BDEVNAME_SIZE]; |
| 4600 | mdk_rdev_t *rdev; |
| 4601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | rdev = find_rdev(mddev, dev); |
| 4603 | if (!rdev) |
| 4604 | return -ENXIO; |
| 4605 | |
| 4606 | if (rdev->raid_disk >= 0) |
| 4607 | goto busy; |
| 4608 | |
| 4609 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4610 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4611 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | |
| 4613 | return 0; |
| 4614 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4615 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4617 | return -EBUSY; |
| 4618 | } |
| 4619 | |
| 4620 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4621 | { |
| 4622 | char b[BDEVNAME_SIZE]; |
| 4623 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | mdk_rdev_t *rdev; |
| 4625 | |
| 4626 | if (!mddev->pers) |
| 4627 | return -ENODEV; |
| 4628 | |
| 4629 | if (mddev->major_version != 0) { |
| 4630 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4631 | " version-0 superblocks.\n", |
| 4632 | mdname(mddev)); |
| 4633 | return -EINVAL; |
| 4634 | } |
| 4635 | if (!mddev->pers->hot_add_disk) { |
| 4636 | printk(KERN_WARNING |
| 4637 | "%s: personality does not support diskops!\n", |
| 4638 | mdname(mddev)); |
| 4639 | return -EINVAL; |
| 4640 | } |
| 4641 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4642 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | if (IS_ERR(rdev)) { |
| 4644 | printk(KERN_WARNING |
| 4645 | "md: error, md_import_device() returned %ld\n", |
| 4646 | PTR_ERR(rdev)); |
| 4647 | return -EINVAL; |
| 4648 | } |
| 4649 | |
| 4650 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4651 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4653 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
Andre Noll | e7debaa | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4655 | rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4657 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | printk(KERN_WARNING |
| 4659 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4660 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4661 | err = -EINVAL; |
| 4662 | goto abort_export; |
| 4663 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4664 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4666 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4667 | err = bind_rdev_to_array(rdev, mddev); |
| 4668 | if (err) |
| 4669 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | |
| 4671 | /* |
| 4672 | * The rest should better be atomic, we can have disk failures |
| 4673 | * noticed in interrupt contexts ... |
| 4674 | */ |
| 4675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | rdev->raid_disk = -1; |
| 4677 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4678 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | |
| 4680 | /* |
| 4681 | * Kick recovery, maybe this spare has to be added to the |
| 4682 | * array immediately. |
| 4683 | */ |
| 4684 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4685 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4686 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | return 0; |
| 4688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | abort_export: |
| 4690 | export_rdev(rdev); |
| 4691 | return err; |
| 4692 | } |
| 4693 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4694 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4695 | { |
| 4696 | int err; |
| 4697 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4698 | if (mddev->pers) { |
| 4699 | if (!mddev->pers->quiesce) |
| 4700 | return -EBUSY; |
| 4701 | if (mddev->recovery || mddev->sync_thread) |
| 4702 | return -EBUSY; |
| 4703 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4706 | |
| 4707 | if (fd >= 0) { |
| 4708 | if (mddev->bitmap) |
| 4709 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4710 | mddev->bitmap_file = fget(fd); |
| 4711 | |
| 4712 | if (mddev->bitmap_file == NULL) { |
| 4713 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4714 | mdname(mddev)); |
| 4715 | return -EBADF; |
| 4716 | } |
| 4717 | |
| 4718 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4719 | if (err) { |
| 4720 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4721 | mdname(mddev)); |
| 4722 | fput(mddev->bitmap_file); |
| 4723 | mddev->bitmap_file = NULL; |
| 4724 | return err; |
| 4725 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4726 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4727 | } else if (mddev->bitmap == NULL) |
| 4728 | return -ENOENT; /* cannot remove what isn't there */ |
| 4729 | err = 0; |
| 4730 | if (mddev->pers) { |
| 4731 | mddev->pers->quiesce(mddev, 1); |
| 4732 | if (fd >= 0) |
| 4733 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4734 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4735 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4736 | fd = -1; /* make sure to put the file */ |
| 4737 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4738 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4739 | } |
| 4740 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4741 | if (mddev->bitmap_file) { |
| 4742 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4743 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4744 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4745 | mddev->bitmap_file = NULL; |
| 4746 | } |
| 4747 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4748 | return err; |
| 4749 | } |
| 4750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | /* |
| 4752 | * set_array_info is used two different ways |
| 4753 | * The original usage is when creating a new array. |
| 4754 | * In this usage, raid_disks is > 0 and it together with |
| 4755 | * level, size, not_persistent,layout,chunksize determine the |
| 4756 | * shape of the array. |
| 4757 | * This will always create an array with a type-0.90.0 superblock. |
| 4758 | * The newer usage is when assembling an array. |
| 4759 | * In this case raid_disks will be 0, and the major_version field is |
| 4760 | * use to determine which style super-blocks are to be found on the devices. |
| 4761 | * The minor and patch _version numbers are also kept incase the |
| 4762 | * super_block handler wishes to interpret them. |
| 4763 | */ |
| 4764 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4765 | { |
| 4766 | |
| 4767 | if (info->raid_disks == 0) { |
| 4768 | /* just setting version number for superblock loading */ |
| 4769 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4770 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | super_types[info->major_version].name == NULL) { |
| 4772 | /* maybe try to auto-load a module? */ |
| 4773 | printk(KERN_INFO |
| 4774 | "md: superblock version %d not known\n", |
| 4775 | info->major_version); |
| 4776 | return -EINVAL; |
| 4777 | } |
| 4778 | mddev->major_version = info->major_version; |
| 4779 | mddev->minor_version = info->minor_version; |
| 4780 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4781 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | return 0; |
| 4783 | } |
| 4784 | mddev->major_version = MD_MAJOR_VERSION; |
| 4785 | mddev->minor_version = MD_MINOR_VERSION; |
| 4786 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4787 | mddev->ctime = get_seconds(); |
| 4788 | |
| 4789 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4790 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | mddev->size = info->size; |
| 4792 | mddev->raid_disks = info->raid_disks; |
| 4793 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4794 | * openned |
| 4795 | */ |
| 4796 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4797 | mddev->recovery_cp = MaxSector; |
| 4798 | else |
| 4799 | mddev->recovery_cp = 0; |
| 4800 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4801 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | |
| 4803 | mddev->layout = info->layout; |
| 4804 | mddev->chunk_size = info->chunk_size; |
| 4805 | |
| 4806 | mddev->max_disks = MD_SB_DISKS; |
| 4807 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4808 | if (mddev->persistent) |
| 4809 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4810 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4812 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4813 | mddev->bitmap_offset = 0; |
| 4814 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4815 | mddev->reshape_position = MaxSector; |
| 4816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | /* |
| 4818 | * Generate a 128 bit UUID |
| 4819 | */ |
| 4820 | get_random_bytes(mddev->uuid, 16); |
| 4821 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4822 | mddev->new_level = mddev->level; |
| 4823 | mddev->new_chunk = mddev->chunk_size; |
| 4824 | mddev->new_layout = mddev->layout; |
| 4825 | mddev->delta_disks = 0; |
| 4826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
| 4829 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4830 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4831 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4832 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4833 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4834 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4835 | |
| 4836 | if (mddev->pers->resize == NULL) |
| 4837 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4838 | /* The "num_sectors" is the number of sectors of each device that |
| 4839 | * is used. This can only make sense for arrays with redundancy. |
| 4840 | * linear and raid0 always use whatever space is available. We can only |
| 4841 | * consider changing this number if no resync or reconstruction is |
| 4842 | * 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] | 4843 | * 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] | 4844 | * of each device. If num_sectors is zero, we find the largest size |
| 4845 | * that fits. |
| 4846 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4847 | */ |
| 4848 | if (mddev->sync_thread) |
| 4849 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 4850 | if (mddev->bitmap) |
| 4851 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 4852 | * grow, and re-add. |
| 4853 | */ |
| 4854 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4855 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4856 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4857 | avail = rdev->size * 2; |
| 4858 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4859 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 4860 | num_sectors = avail; |
| 4861 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4862 | return -ENOSPC; |
| 4863 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4864 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4865 | if (!rv) { |
| 4866 | struct block_device *bdev; |
| 4867 | |
| 4868 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4869 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4870 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4871 | i_size_write(bdev->bd_inode, |
| 4872 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4873 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4874 | bdput(bdev); |
| 4875 | } |
| 4876 | } |
| 4877 | return rv; |
| 4878 | } |
| 4879 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4880 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4881 | { |
| 4882 | int rv; |
| 4883 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4884 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4885 | return -EINVAL; |
| 4886 | if (raid_disks <= 0 || |
| 4887 | raid_disks >= mddev->max_disks) |
| 4888 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4889 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4890 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4891 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4892 | |
| 4893 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4894 | return rv; |
| 4895 | } |
| 4896 | |
| 4897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | /* |
| 4899 | * update_array_info is used to change the configuration of an |
| 4900 | * on-line array. |
| 4901 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4902 | * fields in the info are checked against the array. |
| 4903 | * Any differences that cannot be handled will cause an error. |
| 4904 | * Normally, only one change can be managed at a time. |
| 4905 | */ |
| 4906 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4907 | { |
| 4908 | int rv = 0; |
| 4909 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4910 | int state = 0; |
| 4911 | |
| 4912 | /* calculate expected state,ignoring low bits */ |
| 4913 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4914 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
| 4916 | if (mddev->major_version != info->major_version || |
| 4917 | mddev->minor_version != info->minor_version || |
| 4918 | /* mddev->patch_version != info->patch_version || */ |
| 4919 | mddev->ctime != info->ctime || |
| 4920 | mddev->level != info->level || |
| 4921 | /* mddev->layout != info->layout || */ |
| 4922 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4923 | mddev->chunk_size != info->chunk_size || |
| 4924 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4925 | ((state^info->state) & 0xfffffe00) |
| 4926 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | return -EINVAL; |
| 4928 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4929 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4931 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4932 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | if (cnt == 0) return 0; |
| 4934 | if (cnt > 1) return -EINVAL; |
| 4935 | |
| 4936 | if (mddev->layout != info->layout) { |
| 4937 | /* Change layout |
| 4938 | * we don't need to do anything at the md level, the |
| 4939 | * personality will take care of it all. |
| 4940 | */ |
| 4941 | if (mddev->pers->reconfig == NULL) |
| 4942 | return -EINVAL; |
| 4943 | else |
| 4944 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4945 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4946 | if (info->size >= 0 && mddev->size != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4947 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4949 | if (mddev->raid_disks != info->raid_disks) |
| 4950 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4952 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4953 | if (mddev->pers->quiesce == NULL) |
| 4954 | return -EINVAL; |
| 4955 | if (mddev->recovery || mddev->sync_thread) |
| 4956 | return -EBUSY; |
| 4957 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4958 | /* add the bitmap */ |
| 4959 | if (mddev->bitmap) |
| 4960 | return -EEXIST; |
| 4961 | if (mddev->default_bitmap_offset == 0) |
| 4962 | return -EINVAL; |
| 4963 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4964 | mddev->pers->quiesce(mddev, 1); |
| 4965 | rv = bitmap_create(mddev); |
| 4966 | if (rv) |
| 4967 | bitmap_destroy(mddev); |
| 4968 | mddev->pers->quiesce(mddev, 0); |
| 4969 | } else { |
| 4970 | /* remove the bitmap */ |
| 4971 | if (!mddev->bitmap) |
| 4972 | return -ENOENT; |
| 4973 | if (mddev->bitmap->file) |
| 4974 | return -EINVAL; |
| 4975 | mddev->pers->quiesce(mddev, 1); |
| 4976 | bitmap_destroy(mddev); |
| 4977 | mddev->pers->quiesce(mddev, 0); |
| 4978 | mddev->bitmap_offset = 0; |
| 4979 | } |
| 4980 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4981 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | return rv; |
| 4983 | } |
| 4984 | |
| 4985 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4986 | { |
| 4987 | mdk_rdev_t *rdev; |
| 4988 | |
| 4989 | if (mddev->pers == NULL) |
| 4990 | return -ENODEV; |
| 4991 | |
| 4992 | rdev = find_rdev(mddev, dev); |
| 4993 | if (!rdev) |
| 4994 | return -ENODEV; |
| 4995 | |
| 4996 | md_error(mddev, rdev); |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5000 | /* |
| 5001 | * We have a problem here : there is no easy way to give a CHS |
| 5002 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5003 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5004 | * dosfs just mad... ;-) |
| 5005 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5006 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5007 | { |
| 5008 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5009 | |
| 5010 | geo->heads = 2; |
| 5011 | geo->sectors = 4; |
| 5012 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5013 | return 0; |
| 5014 | } |
| 5015 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5016 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | unsigned int cmd, unsigned long arg) |
| 5018 | { |
| 5019 | int err = 0; |
| 5020 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | mddev_t *mddev = NULL; |
| 5022 | |
| 5023 | if (!capable(CAP_SYS_ADMIN)) |
| 5024 | return -EACCES; |
| 5025 | |
| 5026 | /* |
| 5027 | * Commands dealing with the RAID driver but not any |
| 5028 | * particular array: |
| 5029 | */ |
| 5030 | switch (cmd) |
| 5031 | { |
| 5032 | case RAID_VERSION: |
| 5033 | err = get_version(argp); |
| 5034 | goto done; |
| 5035 | |
| 5036 | case PRINT_RAID_DEBUG: |
| 5037 | err = 0; |
| 5038 | md_print_devices(); |
| 5039 | goto done; |
| 5040 | |
| 5041 | #ifndef MODULE |
| 5042 | case RAID_AUTORUN: |
| 5043 | err = 0; |
| 5044 | autostart_arrays(arg); |
| 5045 | goto done; |
| 5046 | #endif |
| 5047 | default:; |
| 5048 | } |
| 5049 | |
| 5050 | /* |
| 5051 | * Commands creating/starting a new array: |
| 5052 | */ |
| 5053 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5054 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | |
| 5056 | if (!mddev) { |
| 5057 | BUG(); |
| 5058 | goto abort; |
| 5059 | } |
| 5060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | err = mddev_lock(mddev); |
| 5062 | if (err) { |
| 5063 | printk(KERN_INFO |
| 5064 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5065 | err, cmd); |
| 5066 | goto abort; |
| 5067 | } |
| 5068 | |
| 5069 | switch (cmd) |
| 5070 | { |
| 5071 | case SET_ARRAY_INFO: |
| 5072 | { |
| 5073 | mdu_array_info_t info; |
| 5074 | if (!arg) |
| 5075 | memset(&info, 0, sizeof(info)); |
| 5076 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5077 | err = -EFAULT; |
| 5078 | goto abort_unlock; |
| 5079 | } |
| 5080 | if (mddev->pers) { |
| 5081 | err = update_array_info(mddev, &info); |
| 5082 | if (err) { |
| 5083 | printk(KERN_WARNING "md: couldn't update" |
| 5084 | " array info. %d\n", err); |
| 5085 | goto abort_unlock; |
| 5086 | } |
| 5087 | goto done_unlock; |
| 5088 | } |
| 5089 | if (!list_empty(&mddev->disks)) { |
| 5090 | printk(KERN_WARNING |
| 5091 | "md: array %s already has disks!\n", |
| 5092 | mdname(mddev)); |
| 5093 | err = -EBUSY; |
| 5094 | goto abort_unlock; |
| 5095 | } |
| 5096 | if (mddev->raid_disks) { |
| 5097 | printk(KERN_WARNING |
| 5098 | "md: array %s already initialised!\n", |
| 5099 | mdname(mddev)); |
| 5100 | err = -EBUSY; |
| 5101 | goto abort_unlock; |
| 5102 | } |
| 5103 | err = set_array_info(mddev, &info); |
| 5104 | if (err) { |
| 5105 | printk(KERN_WARNING "md: couldn't set" |
| 5106 | " array info. %d\n", err); |
| 5107 | goto abort_unlock; |
| 5108 | } |
| 5109 | } |
| 5110 | goto done_unlock; |
| 5111 | |
| 5112 | default:; |
| 5113 | } |
| 5114 | |
| 5115 | /* |
| 5116 | * Commands querying/configuring an existing array: |
| 5117 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5118 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5119 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5120 | if ((!mddev->raid_disks && !mddev->external) |
| 5121 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5122 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5123 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | err = -ENODEV; |
| 5125 | goto abort_unlock; |
| 5126 | } |
| 5127 | |
| 5128 | /* |
| 5129 | * Commands even a read-only array can execute: |
| 5130 | */ |
| 5131 | switch (cmd) |
| 5132 | { |
| 5133 | case GET_ARRAY_INFO: |
| 5134 | err = get_array_info(mddev, argp); |
| 5135 | goto done_unlock; |
| 5136 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5137 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5138 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5139 | goto done_unlock; |
| 5140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | case GET_DISK_INFO: |
| 5142 | err = get_disk_info(mddev, argp); |
| 5143 | goto done_unlock; |
| 5144 | |
| 5145 | case RESTART_ARRAY_RW: |
| 5146 | err = restart_array(mddev); |
| 5147 | goto done_unlock; |
| 5148 | |
| 5149 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5150 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | goto done_unlock; |
| 5152 | |
| 5153 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5154 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | goto done_unlock; |
| 5156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | /* |
| 5160 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5161 | * superblock, so we do not allow them on read-only arrays. |
| 5162 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5163 | * here and hit the 'default' below, so only disallow |
| 5164 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5166 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5167 | if (mddev->ro == 2) { |
| 5168 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5169 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5170 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5171 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5172 | } else { |
| 5173 | err = -EROFS; |
| 5174 | goto abort_unlock; |
| 5175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | switch (cmd) |
| 5179 | { |
| 5180 | case ADD_NEW_DISK: |
| 5181 | { |
| 5182 | mdu_disk_info_t info; |
| 5183 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5184 | err = -EFAULT; |
| 5185 | else |
| 5186 | err = add_new_disk(mddev, &info); |
| 5187 | goto done_unlock; |
| 5188 | } |
| 5189 | |
| 5190 | case HOT_REMOVE_DISK: |
| 5191 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5192 | goto done_unlock; |
| 5193 | |
| 5194 | case HOT_ADD_DISK: |
| 5195 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5196 | goto done_unlock; |
| 5197 | |
| 5198 | case SET_DISK_FAULTY: |
| 5199 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5200 | goto done_unlock; |
| 5201 | |
| 5202 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5203 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | goto done_unlock; |
| 5205 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5206 | case SET_BITMAP_FILE: |
| 5207 | err = set_bitmap_file(mddev, (int)arg); |
| 5208 | goto done_unlock; |
| 5209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | err = -EINVAL; |
| 5212 | goto abort_unlock; |
| 5213 | } |
| 5214 | |
| 5215 | done_unlock: |
| 5216 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5217 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5218 | err != -EINVAL) |
| 5219 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | mddev_unlock(mddev); |
| 5221 | |
| 5222 | return err; |
| 5223 | done: |
| 5224 | if (err) |
| 5225 | MD_BUG(); |
| 5226 | abort: |
| 5227 | return err; |
| 5228 | } |
| 5229 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5230 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | { |
| 5232 | /* |
| 5233 | * Succeed if we can lock the mddev, which confirms that |
| 5234 | * it isn't being stopped right now. |
| 5235 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5236 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | int err; |
| 5238 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5239 | if (mddev->gendisk != bdev->bd_disk) { |
| 5240 | /* we are racing with mddev_put which is discarding this |
| 5241 | * bd_disk. |
| 5242 | */ |
| 5243 | mddev_put(mddev); |
| 5244 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5245 | flush_scheduled_work(); |
| 5246 | /* Then retry the open from the top */ |
| 5247 | return -ERESTARTSYS; |
| 5248 | } |
| 5249 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5250 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5251 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | goto out; |
| 5253 | |
| 5254 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5255 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | mddev_unlock(mddev); |
| 5257 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5258 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | out: |
| 5260 | return err; |
| 5261 | } |
| 5262 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5263 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5265 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5267 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5268 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | mddev_put(mddev); |
| 5270 | |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5274 | static int md_media_changed(struct gendisk *disk) |
| 5275 | { |
| 5276 | mddev_t *mddev = disk->private_data; |
| 5277 | |
| 5278 | return mddev->changed; |
| 5279 | } |
| 5280 | |
| 5281 | static int md_revalidate(struct gendisk *disk) |
| 5282 | { |
| 5283 | mddev_t *mddev = disk->private_data; |
| 5284 | |
| 5285 | mddev->changed = 0; |
| 5286 | return 0; |
| 5287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | static struct block_device_operations md_fops = |
| 5289 | { |
| 5290 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5291 | .open = md_open, |
| 5292 | .release = md_release, |
| 5293 | .locked_ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5294 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5295 | .media_changed = md_media_changed, |
| 5296 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | }; |
| 5298 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5299 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | { |
| 5301 | mdk_thread_t *thread = arg; |
| 5302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | /* |
| 5304 | * md_thread is a 'system-thread', it's priority should be very |
| 5305 | * high. We avoid resource deadlocks individually in each |
| 5306 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5307 | * the very same RT priority as kswapd, thus we will never get |
| 5308 | * into a priority inversion deadlock. |
| 5309 | * |
| 5310 | * we definitely have to have equal or higher priority than |
| 5311 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5312 | * many dirty RAID5 blocks. |
| 5313 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5315 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5316 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5318 | /* We need to wait INTERRUPTIBLE so that |
| 5319 | * we don't add to the load-average. |
| 5320 | * That means we need to be sure no signals are |
| 5321 | * pending |
| 5322 | */ |
| 5323 | if (signal_pending(current)) |
| 5324 | flush_signals(current); |
| 5325 | |
| 5326 | wait_event_interruptible_timeout |
| 5327 | (thread->wqueue, |
| 5328 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5329 | || kthread_should_stop(), |
| 5330 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | |
| 5332 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5333 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5334 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | return 0; |
| 5338 | } |
| 5339 | |
| 5340 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5341 | { |
| 5342 | if (thread) { |
| 5343 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5344 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5345 | wake_up(&thread->wqueue); |
| 5346 | } |
| 5347 | } |
| 5348 | |
| 5349 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5350 | const char *name) |
| 5351 | { |
| 5352 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5354 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | if (!thread) |
| 5356 | return NULL; |
| 5357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | init_waitqueue_head(&thread->wqueue); |
| 5359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | thread->run = run; |
| 5361 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5362 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5363 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5364 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | kfree(thread); |
| 5366 | return NULL; |
| 5367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | return thread; |
| 5369 | } |
| 5370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | void md_unregister_thread(mdk_thread_t *thread) |
| 5372 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5373 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5374 | |
| 5375 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | kfree(thread); |
| 5377 | } |
| 5378 | |
| 5379 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5380 | { |
| 5381 | if (!mddev) { |
| 5382 | MD_BUG(); |
| 5383 | return; |
| 5384 | } |
| 5385 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5386 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5388 | |
| 5389 | if (mddev->external) |
| 5390 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5391 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5393 | mdname(mddev), |
| 5394 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5395 | __builtin_return_address(0),__builtin_return_address(1), |
| 5396 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5397 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5398 | if (!mddev->pers) |
| 5399 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | if (!mddev->pers->error_handler) |
| 5401 | return; |
| 5402 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5403 | if (mddev->degraded) |
| 5404 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5405 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5407 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5408 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5409 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | /* seq_file implementation /proc/mdstat */ |
| 5413 | |
| 5414 | static void status_unused(struct seq_file *seq) |
| 5415 | { |
| 5416 | int i = 0; |
| 5417 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | |
| 5419 | seq_printf(seq, "unused devices: "); |
| 5420 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5421 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | char b[BDEVNAME_SIZE]; |
| 5423 | i++; |
| 5424 | seq_printf(seq, "%s ", |
| 5425 | bdevname(rdev->bdev,b)); |
| 5426 | } |
| 5427 | if (!i) |
| 5428 | seq_printf(seq, "<none>"); |
| 5429 | |
| 5430 | seq_printf(seq, "\n"); |
| 5431 | } |
| 5432 | |
| 5433 | |
| 5434 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5435 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5436 | sector_t max_blocks, resync, res; |
| 5437 | unsigned long dt, db, rt; |
| 5438 | int scale; |
| 5439 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | |
| 5441 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5442 | |
| 5443 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5444 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5445 | else |
| 5446 | max_blocks = mddev->size; |
| 5447 | |
| 5448 | /* |
| 5449 | * Should not happen. |
| 5450 | */ |
| 5451 | if (!max_blocks) { |
| 5452 | MD_BUG(); |
| 5453 | return; |
| 5454 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5455 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5456 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5457 | * u32, as those are the requirements for sector_div. |
| 5458 | * Thus 'scale' must be at least 10 |
| 5459 | */ |
| 5460 | scale = 10; |
| 5461 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5462 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5463 | scale++; |
| 5464 | } |
| 5465 | res = (resync>>scale)*1000; |
| 5466 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5467 | |
| 5468 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5470 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | seq_printf(seq, "["); |
| 5472 | for (i = 0; i < x; i++) |
| 5473 | seq_printf(seq, "="); |
| 5474 | seq_printf(seq, ">"); |
| 5475 | for (i = 0; i < y; i++) |
| 5476 | seq_printf(seq, "."); |
| 5477 | seq_printf(seq, "] "); |
| 5478 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5479 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5480 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5481 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5482 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5483 | "check" : |
| 5484 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5485 | "resync" : "recovery"))), |
| 5486 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5487 | (unsigned long long) resync, |
| 5488 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | |
| 5490 | /* |
| 5491 | * We do not want to overflow, so the order of operands and |
| 5492 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5493 | * safe against division by zero. We only estimate anyway. |
| 5494 | * |
| 5495 | * dt: time from mark until now |
| 5496 | * db: blocks written from mark until now |
| 5497 | * rt: remaining time |
| 5498 | */ |
| 5499 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5500 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5501 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5502 | - mddev->resync_mark_cnt; |
| 5503 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | |
| 5505 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5506 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5507 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5511 | { |
| 5512 | struct list_head *tmp; |
| 5513 | loff_t l = *pos; |
| 5514 | mddev_t *mddev; |
| 5515 | |
| 5516 | if (l >= 0x10000) |
| 5517 | return NULL; |
| 5518 | if (!l--) |
| 5519 | /* header */ |
| 5520 | return (void*)1; |
| 5521 | |
| 5522 | spin_lock(&all_mddevs_lock); |
| 5523 | list_for_each(tmp,&all_mddevs) |
| 5524 | if (!l--) { |
| 5525 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5526 | mddev_get(mddev); |
| 5527 | spin_unlock(&all_mddevs_lock); |
| 5528 | return mddev; |
| 5529 | } |
| 5530 | spin_unlock(&all_mddevs_lock); |
| 5531 | if (!l--) |
| 5532 | return (void*)2;/* tail */ |
| 5533 | return NULL; |
| 5534 | } |
| 5535 | |
| 5536 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5537 | { |
| 5538 | struct list_head *tmp; |
| 5539 | mddev_t *next_mddev, *mddev = v; |
| 5540 | |
| 5541 | ++*pos; |
| 5542 | if (v == (void*)2) |
| 5543 | return NULL; |
| 5544 | |
| 5545 | spin_lock(&all_mddevs_lock); |
| 5546 | if (v == (void*)1) |
| 5547 | tmp = all_mddevs.next; |
| 5548 | else |
| 5549 | tmp = mddev->all_mddevs.next; |
| 5550 | if (tmp != &all_mddevs) |
| 5551 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5552 | else { |
| 5553 | next_mddev = (void*)2; |
| 5554 | *pos = 0x10000; |
| 5555 | } |
| 5556 | spin_unlock(&all_mddevs_lock); |
| 5557 | |
| 5558 | if (v != (void*)1) |
| 5559 | mddev_put(mddev); |
| 5560 | return next_mddev; |
| 5561 | |
| 5562 | } |
| 5563 | |
| 5564 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5565 | { |
| 5566 | mddev_t *mddev = v; |
| 5567 | |
| 5568 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5569 | mddev_put(mddev); |
| 5570 | } |
| 5571 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5572 | struct mdstat_info { |
| 5573 | int event; |
| 5574 | }; |
| 5575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5577 | { |
| 5578 | mddev_t *mddev = v; |
| 5579 | sector_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5581 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5582 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | |
| 5584 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5585 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | seq_printf(seq, "Personalities : "); |
| 5587 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5588 | list_for_each_entry(pers, &pers_list, list) |
| 5589 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | |
| 5591 | spin_unlock(&pers_lock); |
| 5592 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5593 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | return 0; |
| 5595 | } |
| 5596 | if (v == (void*)2) { |
| 5597 | status_unused(seq); |
| 5598 | return 0; |
| 5599 | } |
| 5600 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5601 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5605 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5606 | mddev->pers ? "" : "in"); |
| 5607 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5608 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5610 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5611 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | seq_printf(seq, " %s", mddev->pers->name); |
| 5613 | } |
| 5614 | |
| 5615 | size = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5616 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | char b[BDEVNAME_SIZE]; |
| 5618 | seq_printf(seq, " %s[%d]", |
| 5619 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5620 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5621 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5622 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | seq_printf(seq, "(F)"); |
| 5624 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5625 | } else if (rdev->raid_disk < 0) |
| 5626 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | size += rdev->size; |
| 5628 | } |
| 5629 | |
| 5630 | if (!list_empty(&mddev->disks)) { |
| 5631 | if (mddev->pers) |
| 5632 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5633 | (unsigned long long) |
| 5634 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | else |
| 5636 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5637 | (unsigned long long)size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5639 | if (mddev->persistent) { |
| 5640 | if (mddev->major_version != 0 || |
| 5641 | mddev->minor_version != 90) { |
| 5642 | seq_printf(seq," super %d.%d", |
| 5643 | mddev->major_version, |
| 5644 | mddev->minor_version); |
| 5645 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5646 | } else if (mddev->external) |
| 5647 | seq_printf(seq, " super external:%s", |
| 5648 | mddev->metadata_type); |
| 5649 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5650 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | |
| 5652 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5653 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5655 | if (mddev->pers->sync_request) { |
| 5656 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5657 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5658 | seq_printf(seq, "\n "); |
| 5659 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5660 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5661 | else if (mddev->recovery_cp < MaxSector) |
| 5662 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5663 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5664 | } else |
| 5665 | seq_printf(seq, "\n "); |
| 5666 | |
| 5667 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5668 | unsigned long chunk_kb; |
| 5669 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5670 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5671 | chunk_kb = bitmap->chunksize >> 10; |
| 5672 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5673 | "%lu%s chunk", |
| 5674 | bitmap->pages - bitmap->missing_pages, |
| 5675 | bitmap->pages, |
| 5676 | (bitmap->pages - bitmap->missing_pages) |
| 5677 | << (PAGE_SHIFT - 10), |
| 5678 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5679 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5680 | if (bitmap->file) { |
| 5681 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5682 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5683 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5684 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5685 | seq_printf(seq, "\n"); |
| 5686 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | seq_printf(seq, "\n"); |
| 5690 | } |
| 5691 | mddev_unlock(mddev); |
| 5692 | |
| 5693 | return 0; |
| 5694 | } |
| 5695 | |
| 5696 | static struct seq_operations md_seq_ops = { |
| 5697 | .start = md_seq_start, |
| 5698 | .next = md_seq_next, |
| 5699 | .stop = md_seq_stop, |
| 5700 | .show = md_seq_show, |
| 5701 | }; |
| 5702 | |
| 5703 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5704 | { |
| 5705 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5706 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5707 | if (mi == NULL) |
| 5708 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | |
| 5710 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5711 | if (error) |
| 5712 | kfree(mi); |
| 5713 | else { |
| 5714 | struct seq_file *p = file->private_data; |
| 5715 | p->private = mi; |
| 5716 | mi->event = atomic_read(&md_event_count); |
| 5717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | return error; |
| 5719 | } |
| 5720 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5721 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5722 | { |
| 5723 | struct seq_file *m = filp->private_data; |
| 5724 | struct mdstat_info *mi = m->private; |
| 5725 | int mask; |
| 5726 | |
| 5727 | poll_wait(filp, &md_event_waiters, wait); |
| 5728 | |
| 5729 | /* always allow read */ |
| 5730 | mask = POLLIN | POLLRDNORM; |
| 5731 | |
| 5732 | if (mi->event != atomic_read(&md_event_count)) |
| 5733 | mask |= POLLERR | POLLPRI; |
| 5734 | return mask; |
| 5735 | } |
| 5736 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5737 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5738 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | .open = md_seq_open, |
| 5740 | .read = seq_read, |
| 5741 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5742 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5743 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | }; |
| 5745 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5746 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5749 | list_add_tail(&p->list, &pers_list); |
| 5750 | 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] | 5751 | spin_unlock(&pers_lock); |
| 5752 | return 0; |
| 5753 | } |
| 5754 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5755 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5757 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5759 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | spin_unlock(&pers_lock); |
| 5761 | return 0; |
| 5762 | } |
| 5763 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5764 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | { |
| 5766 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5768 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | |
| 5770 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5771 | rcu_read_lock(); |
| 5772 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5774 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 5775 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 5776 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5777 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5778 | * as sync_io is counted when a request starts, and |
| 5779 | * disk_stats is counted when it completes. |
| 5780 | * So resync activity will cause curr_events to be smaller than |
| 5781 | * when there was no such activity. |
| 5782 | * non-sync IO will cause disk_stat to increase without |
| 5783 | * increasing sync_io so curr_events will (eventually) |
| 5784 | * be larger than it was before. Once it becomes |
| 5785 | * substantially larger, the test below will cause |
| 5786 | * the array to appear non-idle, and resync will slow |
| 5787 | * down. |
| 5788 | * If there is a lot of outstanding resync activity when |
| 5789 | * we set last_event to curr_events, then all that activity |
| 5790 | * completing might cause the array to appear non-idle |
| 5791 | * and resync will be slowed down even though there might |
| 5792 | * not have been non-resync activity. This will only |
| 5793 | * happen once though. 'last_events' will soon reflect |
| 5794 | * the state where there is little or no outstanding |
| 5795 | * resync requests, and further resync activity will |
| 5796 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5797 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5799 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | rdev->last_events = curr_events; |
| 5801 | idle = 0; |
| 5802 | } |
| 5803 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5804 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | return idle; |
| 5806 | } |
| 5807 | |
| 5808 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5809 | { |
| 5810 | /* another "blocks" (512byte) blocks have been synced */ |
| 5811 | atomic_sub(blocks, &mddev->recovery_active); |
| 5812 | wake_up(&mddev->recovery_wait); |
| 5813 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5814 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | md_wakeup_thread(mddev->thread); |
| 5816 | // stop recovery, signal do_sync .... |
| 5817 | } |
| 5818 | } |
| 5819 | |
| 5820 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5821 | /* md_write_start(mddev, bi) |
| 5822 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5823 | * in superblock) before writing, schedule a superblock update |
| 5824 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5825 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5826 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5828 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5829 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5830 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5831 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5832 | BUG_ON(mddev->ro == 1); |
| 5833 | if (mddev->ro == 2) { |
| 5834 | /* need to switch to read/write */ |
| 5835 | mddev->ro = 0; |
| 5836 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5837 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5838 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5839 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5840 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5841 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5842 | if (mddev->safemode == 1) |
| 5843 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5844 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5845 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5846 | if (mddev->in_sync) { |
| 5847 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5848 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5849 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5850 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5851 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5852 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5853 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5854 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5855 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5856 | wait_event(mddev->sb_wait, |
| 5857 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5858 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | void md_write_end(mddev_t *mddev) |
| 5862 | { |
| 5863 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5864 | if (mddev->safemode == 2) |
| 5865 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5866 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5868 | } |
| 5869 | } |
| 5870 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5871 | /* md_allow_write(mddev) |
| 5872 | * Calling this ensures that the array is marked 'active' so that writes |
| 5873 | * may proceed without blocking. It is important to call this before |
| 5874 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5875 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5876 | * |
| 5877 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 5878 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5879 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5880 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5881 | { |
| 5882 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5883 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5884 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5885 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5886 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5887 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5888 | |
| 5889 | spin_lock_irq(&mddev->write_lock); |
| 5890 | if (mddev->in_sync) { |
| 5891 | mddev->in_sync = 0; |
| 5892 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5893 | if (mddev->safemode_delay && |
| 5894 | mddev->safemode == 0) |
| 5895 | mddev->safemode = 1; |
| 5896 | spin_unlock_irq(&mddev->write_lock); |
| 5897 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5898 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5899 | } else |
| 5900 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5901 | |
| 5902 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 5903 | return -EAGAIN; |
| 5904 | else |
| 5905 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5906 | } |
| 5907 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | #define SYNC_MARKS 10 |
| 5910 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5911 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | { |
| 5913 | mddev_t *mddev2; |
| 5914 | unsigned int currspeed = 0, |
| 5915 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5916 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | unsigned long mark[SYNC_MARKS]; |
| 5918 | sector_t mark_cnt[SYNC_MARKS]; |
| 5919 | int last_mark,m; |
| 5920 | struct list_head *tmp; |
| 5921 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5922 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5923 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5924 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | |
| 5926 | /* just incase thread restarts... */ |
| 5927 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5928 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5929 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5930 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5932 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5933 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5934 | desc = "data-check"; |
| 5935 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5936 | desc = "requested-resync"; |
| 5937 | else |
| 5938 | desc = "resync"; |
| 5939 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5940 | desc = "reshape"; |
| 5941 | else |
| 5942 | desc = "recovery"; |
| 5943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | /* we overload curr_resync somewhat here. |
| 5945 | * 0 == not engaged in resync at all |
| 5946 | * 2 == checking that there is no conflict with another sync |
| 5947 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5948 | * commense |
| 5949 | * other == active in resync - this many blocks |
| 5950 | * |
| 5951 | * Before starting a resync we must have set curr_resync to |
| 5952 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5953 | * less than ours. When we find one that is the same or higher |
| 5954 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5955 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5956 | * This will mean we have to start checking from the beginning again. |
| 5957 | * |
| 5958 | */ |
| 5959 | |
| 5960 | do { |
| 5961 | mddev->curr_resync = 2; |
| 5962 | |
| 5963 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5964 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5965 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | goto skip; |
| 5967 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5968 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | if (mddev2 == mddev) |
| 5970 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5971 | if (!mddev->parallel_resync |
| 5972 | && mddev2->curr_resync |
| 5973 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | DEFINE_WAIT(wq); |
| 5975 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5976 | /* arbitrarily yield */ |
| 5977 | mddev->curr_resync = 1; |
| 5978 | wake_up(&resync_wait); |
| 5979 | } |
| 5980 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5981 | /* no need to wait here, we can wait the next |
| 5982 | * time 'round when curr_resync == 2 |
| 5983 | */ |
| 5984 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5985 | /* We need to wait 'interruptible' so as not to |
| 5986 | * contribute to the load average, and not to |
| 5987 | * be caught by 'softlockup' |
| 5988 | */ |
| 5989 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5990 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5991 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5992 | printk(KERN_INFO "md: delaying %s of %s" |
| 5993 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5995 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 5997 | if (signal_pending(current)) |
| 5998 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | schedule(); |
| 6000 | finish_wait(&resync_wait, &wq); |
| 6001 | goto try_again; |
| 6002 | } |
| 6003 | finish_wait(&resync_wait, &wq); |
| 6004 | } |
| 6005 | } |
| 6006 | } while (mddev->curr_resync < 2); |
| 6007 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6008 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6009 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6011 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | */ |
| 6013 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6014 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6015 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6016 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6017 | j = mddev->resync_min; |
| 6018 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6019 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6020 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6021 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6022 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6023 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | /* recovery follows the physical size of devices */ |
| 6025 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6026 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6027 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6028 | if (rdev->raid_disk >= 0 && |
| 6029 | !test_bit(Faulty, &rdev->flags) && |
| 6030 | !test_bit(In_sync, &rdev->flags) && |
| 6031 | rdev->recovery_offset < j) |
| 6032 | j = rdev->recovery_offset; |
| 6033 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6035 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6036 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6037 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6038 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6039 | "(but not more than %d KB/sec) for %s.\n", |
| 6040 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6042 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6043 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6044 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6046 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6047 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | } |
| 6049 | last_mark = 0; |
| 6050 | mddev->resync_mark = mark[last_mark]; |
| 6051 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6052 | |
| 6053 | /* |
| 6054 | * Tune reconstruction: |
| 6055 | */ |
| 6056 | window = 32*(PAGE_SIZE/512); |
| 6057 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6058 | window/2,(unsigned long long) max_sectors/2); |
| 6059 | |
| 6060 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | last_check = 0; |
| 6062 | |
| 6063 | if (j>2) { |
| 6064 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6065 | "md: resuming %s of %s from checkpoint.\n", |
| 6066 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | mddev->curr_resync = j; |
| 6068 | } |
| 6069 | |
| 6070 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6071 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6073 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6074 | if (j >= mddev->resync_max) { |
| 6075 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 6076 | wait_event(mddev->recovery_wait, |
| 6077 | mddev->resync_max > j |
| 6078 | || kthread_should_stop()); |
| 6079 | } |
| 6080 | if (kthread_should_stop()) |
| 6081 | goto interrupted; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6082 | |
| 6083 | if (mddev->curr_resync > mddev->curr_resync_completed && |
| 6084 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6085 | > (max_sectors >> 4)) { |
| 6086 | /* time to update curr_resync_completed */ |
| 6087 | blk_unplug(mddev->queue); |
| 6088 | wait_event(mddev->recovery_wait, |
| 6089 | atomic_read(&mddev->recovery_active) == 0); |
| 6090 | mddev->curr_resync_completed = |
| 6091 | mddev->curr_resync; |
| 6092 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6093 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6094 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6095 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6096 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6097 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | goto out; |
| 6099 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6100 | |
| 6101 | if (!skipped) { /* actual IO requested */ |
| 6102 | io_sectors += sectors; |
| 6103 | atomic_add(sectors, &mddev->recovery_active); |
| 6104 | } |
| 6105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | j += sectors; |
| 6107 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6108 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6109 | if (last_check == 0) |
| 6110 | /* this is the earliers that rebuilt will be |
| 6111 | * visible in /proc/mdstat |
| 6112 | */ |
| 6113 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6114 | |
| 6115 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | continue; |
| 6117 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6118 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6120 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | break; |
| 6122 | |
| 6123 | repeat: |
| 6124 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6125 | /* step marks */ |
| 6126 | int next = (last_mark+1) % SYNC_MARKS; |
| 6127 | |
| 6128 | mddev->resync_mark = mark[next]; |
| 6129 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6130 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6131 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | last_mark = next; |
| 6133 | } |
| 6134 | |
| 6135 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6136 | if (kthread_should_stop()) |
| 6137 | goto interrupted; |
| 6138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | |
| 6140 | /* |
| 6141 | * this loop exits only if either when we are slower than |
| 6142 | * the 'hard' speed limit, or the system was IO-idle for |
| 6143 | * a jiffy. |
| 6144 | * the system might be non-idle CPU-wise, but we only care |
| 6145 | * about not overloading the IO subsystem. (things like an |
| 6146 | * e2fsck being done on the RAID array should execute fast) |
| 6147 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6148 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | cond_resched(); |
| 6150 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6151 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6152 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6154 | if (currspeed > speed_min(mddev)) { |
| 6155 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6156 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6157 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | goto repeat; |
| 6159 | } |
| 6160 | } |
| 6161 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6162 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | /* |
| 6164 | * this also signals 'finished resyncing' to md_stop |
| 6165 | */ |
| 6166 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6167 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | |
| 6169 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6170 | |
| 6171 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6172 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6174 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6175 | mddev->curr_resync > 2) { |
| 6176 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6177 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6178 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6179 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6180 | "md: checkpointing %s of %s.\n", |
| 6181 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6182 | mddev->recovery_cp = mddev->curr_resync; |
| 6183 | } |
| 6184 | } else |
| 6185 | mddev->recovery_cp = MaxSector; |
| 6186 | } else { |
| 6187 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6188 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6189 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6190 | if (rdev->raid_disk >= 0 && |
| 6191 | !test_bit(Faulty, &rdev->flags) && |
| 6192 | !test_bit(In_sync, &rdev->flags) && |
| 6193 | rdev->recovery_offset < mddev->curr_resync) |
| 6194 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6197 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | skip: |
| 6200 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6201 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6202 | mddev->resync_max = MaxSector; |
| 6203 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | wake_up(&resync_wait); |
| 6205 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6206 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6207 | return; |
| 6208 | |
| 6209 | interrupted: |
| 6210 | /* |
| 6211 | * got a signal, exit. |
| 6212 | */ |
| 6213 | printk(KERN_INFO |
| 6214 | "md: md_do_sync() got signal ... exiting\n"); |
| 6215 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6216 | goto out; |
| 6217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6219 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | |
| 6221 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6222 | static int remove_and_add_spares(mddev_t *mddev) |
| 6223 | { |
| 6224 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6225 | int spares = 0; |
| 6226 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6227 | mddev->curr_resync_completed = 0; |
| 6228 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6229 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6230 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6231 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6232 | (test_bit(Faulty, &rdev->flags) || |
| 6233 | ! test_bit(In_sync, &rdev->flags)) && |
| 6234 | atomic_read(&rdev->nr_pending)==0) { |
| 6235 | if (mddev->pers->hot_remove_disk( |
| 6236 | mddev, rdev->raid_disk)==0) { |
| 6237 | char nm[20]; |
| 6238 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6239 | sysfs_remove_link(&mddev->kobj, nm); |
| 6240 | rdev->raid_disk = -1; |
| 6241 | } |
| 6242 | } |
| 6243 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6244 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6245 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6246 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6247 | !test_bit(In_sync, &rdev->flags) && |
| 6248 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6249 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6250 | if (rdev->raid_disk < 0 |
| 6251 | && !test_bit(Faulty, &rdev->flags)) { |
| 6252 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6253 | if (mddev->pers-> |
| 6254 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6255 | char nm[20]; |
| 6256 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6257 | if (sysfs_create_link(&mddev->kobj, |
| 6258 | &rdev->kobj, nm)) |
| 6259 | printk(KERN_WARNING |
| 6260 | "md: cannot register " |
| 6261 | "%s for %s\n", |
| 6262 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6263 | spares++; |
| 6264 | md_new_event(mddev); |
| 6265 | } else |
| 6266 | break; |
| 6267 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6268 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6269 | } |
| 6270 | return spares; |
| 6271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | /* |
| 6273 | * This routine is regularly called by all per-raid-array threads to |
| 6274 | * deal with generic issues like resync and super-block update. |
| 6275 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6276 | * need this as they never do any recovery or update the superblock. |
| 6277 | * |
| 6278 | * It does not do any resync itself, but rather "forks" off other threads |
| 6279 | * to do that as needed. |
| 6280 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6281 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6282 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | * and wakeups up this thread which will reap the thread and finish up. |
| 6284 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6285 | * |
| 6286 | * The overall approach is: |
| 6287 | * 1/ if the superblock needs updating, update it. |
| 6288 | * 2/ If a recovery thread is running, don't do anything else. |
| 6289 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6290 | * 4/ If there are any faulty devices, remove them. |
| 6291 | * 5/ If array is degraded, try to add spares devices |
| 6292 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6293 | */ |
| 6294 | void md_check_recovery(mddev_t *mddev) |
| 6295 | { |
| 6296 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | |
| 6298 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6299 | if (mddev->bitmap) |
| 6300 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | |
| 6302 | if (mddev->ro) |
| 6303 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6304 | |
| 6305 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6306 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6307 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6308 | mdname(mddev)); |
| 6309 | mddev->safemode = 2; |
| 6310 | } |
| 6311 | flush_signals(current); |
| 6312 | } |
| 6313 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6314 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6315 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6317 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6319 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6320 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6321 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6322 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | )) |
| 6324 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6325 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6326 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6327 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6328 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6329 | if (mddev->ro) { |
| 6330 | /* Only thing we do on a ro array is remove |
| 6331 | * failed devices. |
| 6332 | */ |
| 6333 | remove_and_add_spares(mddev); |
| 6334 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6335 | goto unlock; |
| 6336 | } |
| 6337 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6338 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6339 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6340 | spin_lock_irq(&mddev->write_lock); |
| 6341 | if (mddev->safemode && |
| 6342 | !atomic_read(&mddev->writes_pending) && |
| 6343 | !mddev->in_sync && |
| 6344 | mddev->recovery_cp == MaxSector) { |
| 6345 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6346 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6347 | if (mddev->persistent) |
| 6348 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6349 | } |
| 6350 | if (mddev->safemode == 1) |
| 6351 | mddev->safemode = 0; |
| 6352 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6353 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6354 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6355 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6356 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6357 | if (mddev->flags) |
| 6358 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6359 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6360 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6361 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6362 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6363 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6366 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6367 | /* resync/recovery still happening */ |
| 6368 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6369 | goto unlock; |
| 6370 | } |
| 6371 | if (mddev->sync_thread) { |
| 6372 | /* resync has finished, collect result */ |
| 6373 | md_unregister_thread(mddev->sync_thread); |
| 6374 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6375 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6376 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | /* success...*/ |
| 6378 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6379 | if (mddev->pers->spare_active(mddev)) |
| 6380 | sysfs_notify(&mddev->kobj, NULL, |
| 6381 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6383 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6384 | |
| 6385 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6386 | * information must be scrapped |
| 6387 | */ |
| 6388 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6389 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6390 | rdev->saved_raid_disk = -1; |
| 6391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | mddev->recovery = 0; |
| 6393 | /* flag recovery needed just to double check */ |
| 6394 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6395 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6396 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | goto unlock; |
| 6398 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6399 | /* Set RUNNING before clearing NEEDED to avoid |
| 6400 | * any transients in the value of "sync_action". |
| 6401 | */ |
| 6402 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6403 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6404 | /* Clear some bits that don't mean anything, but |
| 6405 | * might be left set |
| 6406 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6407 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6408 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6410 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6411 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | /* no recovery is running. |
| 6413 | * remove any failed drives, then |
| 6414 | * add spares if possible. |
| 6415 | * Spare are also removed and re-added, to allow |
| 6416 | * the personality to fail the re-add. |
| 6417 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6419 | if (mddev->reshape_position != MaxSector) { |
| 6420 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6421 | /* Cannot proceed */ |
| 6422 | goto unlock; |
| 6423 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6424 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6425 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6426 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6427 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6428 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6429 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6430 | } else if (mddev->recovery_cp < MaxSector) { |
| 6431 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6432 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6433 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6434 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6438 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6439 | /* We are adding a device or devices to an array |
| 6440 | * which has the bitmap stored on all devices. |
| 6441 | * So make sure all bitmap pages get written |
| 6442 | */ |
| 6443 | bitmap_write_all(mddev->bitmap); |
| 6444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6446 | mddev, |
| 6447 | "%s_resync"); |
| 6448 | if (!mddev->sync_thread) { |
| 6449 | printk(KERN_ERR "%s: could not start resync" |
| 6450 | " thread...\n", |
| 6451 | mdname(mddev)); |
| 6452 | /* leave the spares where they are, it shouldn't hurt */ |
| 6453 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6454 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6456 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6457 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | } |
| 6459 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6460 | if (!mddev->sync_thread) { |
| 6461 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6462 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6463 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6464 | if (mddev->sysfs_action) |
| 6465 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | mddev_unlock(mddev); |
| 6468 | } |
| 6469 | } |
| 6470 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6471 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6472 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6473 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6474 | wait_event_timeout(rdev->blocked_wait, |
| 6475 | !test_bit(Blocked, &rdev->flags), |
| 6476 | msecs_to_jiffies(5000)); |
| 6477 | rdev_dec_pending(rdev, mddev); |
| 6478 | } |
| 6479 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6480 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6481 | static int md_notify_reboot(struct notifier_block *this, |
| 6482 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | { |
| 6484 | struct list_head *tmp; |
| 6485 | mddev_t *mddev; |
| 6486 | |
| 6487 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6488 | |
| 6489 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6490 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6491 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6492 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6493 | /* Force a switch to readonly even array |
| 6494 | * appears to still be in use. Hence |
| 6495 | * the '100'. |
| 6496 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6497 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6498 | mddev_unlock(mddev); |
| 6499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | /* |
| 6501 | * certain more exotic SCSI devices are known to be |
| 6502 | * volatile wrt too early system reboots. While the |
| 6503 | * right place to handle this issue is the given |
| 6504 | * driver, we do want to have a safe RAID driver ... |
| 6505 | */ |
| 6506 | mdelay(1000*1); |
| 6507 | } |
| 6508 | return NOTIFY_DONE; |
| 6509 | } |
| 6510 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6511 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | .notifier_call = md_notify_reboot, |
| 6513 | .next = NULL, |
| 6514 | .priority = INT_MAX, /* before any real devices */ |
| 6515 | }; |
| 6516 | |
| 6517 | static void md_geninit(void) |
| 6518 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6519 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6520 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6521 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | } |
| 6523 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6524 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6525 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6526 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | return -1; |
| 6528 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6529 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | return -1; |
| 6531 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6532 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6533 | md_probe, NULL, NULL); |
| 6534 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | md_probe, NULL, NULL); |
| 6536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6538 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | |
| 6540 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6541 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | |
| 6545 | #ifndef MODULE |
| 6546 | |
| 6547 | /* |
| 6548 | * Searches all registered partitions for autorun RAID arrays |
| 6549 | * at boot time. |
| 6550 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6551 | |
| 6552 | static LIST_HEAD(all_detected_devices); |
| 6553 | struct detected_devices_node { |
| 6554 | struct list_head list; |
| 6555 | dev_t dev; |
| 6556 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | |
| 6558 | void md_autodetect_dev(dev_t dev) |
| 6559 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6560 | struct detected_devices_node *node_detected_dev; |
| 6561 | |
| 6562 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6563 | if (node_detected_dev) { |
| 6564 | node_detected_dev->dev = dev; |
| 6565 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6566 | } else { |
| 6567 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6568 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | } |
| 6571 | |
| 6572 | |
| 6573 | static void autostart_arrays(int part) |
| 6574 | { |
| 6575 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6576 | struct detected_devices_node *node_detected_dev; |
| 6577 | dev_t dev; |
| 6578 | int i_scanned, i_passed; |
| 6579 | |
| 6580 | i_scanned = 0; |
| 6581 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | |
| 6583 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6584 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6585 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6586 | i_scanned++; |
| 6587 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6588 | struct detected_devices_node, list); |
| 6589 | list_del(&node_detected_dev->list); |
| 6590 | dev = node_detected_dev->dev; |
| 6591 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6592 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | if (IS_ERR(rdev)) |
| 6594 | continue; |
| 6595 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6596 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | MD_BUG(); |
| 6598 | continue; |
| 6599 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6600 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6602 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6604 | |
| 6605 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6606 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | |
| 6608 | autorun_devices(part); |
| 6609 | } |
| 6610 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6611 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | |
| 6613 | static __exit void md_exit(void) |
| 6614 | { |
| 6615 | mddev_t *mddev; |
| 6616 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6617 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6618 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6619 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6621 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | unregister_blkdev(mdp_major, "mdp"); |
| 6623 | unregister_reboot_notifier(&md_notifier); |
| 6624 | unregister_sysctl_table(raid_table_header); |
| 6625 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6626 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6628 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | } |
| 6630 | } |
| 6631 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6632 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | module_exit(md_exit) |
| 6634 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6635 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6636 | { |
| 6637 | return sprintf(buffer, "%d", start_readonly); |
| 6638 | } |
| 6639 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6640 | { |
| 6641 | char *e; |
| 6642 | int num = simple_strtoul(val, &e, 10); |
| 6643 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6644 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6645 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6646 | } |
| 6647 | return -EINVAL; |
| 6648 | } |
| 6649 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6650 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6651 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6652 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6653 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | EXPORT_SYMBOL(register_md_personality); |
| 6656 | EXPORT_SYMBOL(unregister_md_personality); |
| 6657 | EXPORT_SYMBOL(md_error); |
| 6658 | EXPORT_SYMBOL(md_done_sync); |
| 6659 | EXPORT_SYMBOL(md_write_start); |
| 6660 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | EXPORT_SYMBOL(md_register_thread); |
| 6662 | EXPORT_SYMBOL(md_unregister_thread); |
| 6663 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | EXPORT_SYMBOL(md_check_recovery); |
| 6665 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6666 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6667 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |