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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .procname = "speed_limit_min", |
| 102 | .data = &sysctl_speed_limit_min, |
| 103 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 104 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 105 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }, |
| 107 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .procname = "speed_limit_max", |
| 109 | .data = &sysctl_speed_limit_max, |
| 110 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 111 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 112 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 114 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static ctl_table raid_dir_table[] = { |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "raid", |
| 120 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 121 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .child = raid_table, |
| 123 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 124 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static ctl_table raid_root_table[] = { |
| 128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .procname = "dev", |
| 130 | .maxlen = 0, |
| 131 | .mode = 0555, |
| 132 | .child = raid_dir_table, |
| 133 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 134 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 137 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 139 | static int start_readonly; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 142 | * We have a system wide 'event count' that is incremented |
| 143 | * on any 'interesting' event, and readers of /proc/mdstat |
| 144 | * can use 'poll' or 'select' to find out when the event |
| 145 | * count increases. |
| 146 | * |
| 147 | * Events are: |
| 148 | * start array, stop array, error, add device, remove device, |
| 149 | * start build, activate spare |
| 150 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 151 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 152 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 153 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 154 | { |
| 155 | atomic_inc(&md_event_count); |
| 156 | wake_up(&md_event_waiters); |
| 157 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 159 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 160 | /* Alternate version that can be called from interrupts |
| 161 | * when calling sysfs_notify isn't needed. |
| 162 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 163 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 164 | { |
| 165 | atomic_inc(&md_event_count); |
| 166 | wake_up(&md_event_waiters); |
| 167 | } |
| 168 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * Enables to iterate over all existing md arrays |
| 171 | * all_mddevs_lock protects this list. |
| 172 | */ |
| 173 | static LIST_HEAD(all_mddevs); |
| 174 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | * iterates through all used mddevs in the system. |
| 179 | * We take care to grab the all_mddevs_lock whenever navigating |
| 180 | * the list, and to always hold a refcount when unlocked. |
| 181 | * Any code which breaks out of this loop while own |
| 182 | * a reference to the current mddev and must mddev_put it. |
| 183 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 184 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | \ |
| 186 | for (({ spin_lock(&all_mddevs_lock); \ |
| 187 | tmp = all_mddevs.next; \ |
| 188 | mddev = NULL;}); \ |
| 189 | ({ if (tmp != &all_mddevs) \ |
| 190 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 191 | spin_unlock(&all_mddevs_lock); \ |
| 192 | if (mddev) mddev_put(mddev); \ |
| 193 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 194 | tmp != &all_mddevs;}); \ |
| 195 | ({ spin_lock(&all_mddevs_lock); \ |
| 196 | tmp = tmp->next;}) \ |
| 197 | ) |
| 198 | |
| 199 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 200 | /* Rather than calling directly into the personality make_request function, |
| 201 | * IO requests come here first so that we can check if the device is |
| 202 | * being suspended pending a reconfiguration. |
| 203 | * We hold a refcount over the call to ->make_request. By the time that |
| 204 | * call has finished, the bio has been linked into some internal structure |
| 205 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 206 | */ |
| 207 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 209 | mddev_t *mddev = q->queuedata; |
| 210 | int rv; |
| 211 | if (mddev == NULL || mddev->pers == NULL) { |
| 212 | bio_io_error(bio); |
| 213 | return 0; |
| 214 | } |
| 215 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 216 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 217 | DEFINE_WAIT(__wait); |
| 218 | for (;;) { |
| 219 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 220 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 221 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 222 | break; |
| 223 | rcu_read_unlock(); |
| 224 | schedule(); |
| 225 | rcu_read_lock(); |
| 226 | } |
| 227 | finish_wait(&mddev->sb_wait, &__wait); |
| 228 | } |
| 229 | atomic_inc(&mddev->active_io); |
| 230 | rcu_read_unlock(); |
| 231 | rv = mddev->pers->make_request(q, bio); |
| 232 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 233 | wake_up(&mddev->sb_wait); |
| 234 | |
| 235 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 238 | static void mddev_suspend(mddev_t *mddev) |
| 239 | { |
| 240 | BUG_ON(mddev->suspended); |
| 241 | mddev->suspended = 1; |
| 242 | synchronize_rcu(); |
| 243 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 244 | mddev->pers->quiesce(mddev, 1); |
| 245 | md_unregister_thread(mddev->thread); |
| 246 | mddev->thread = NULL; |
| 247 | /* we now know that no code is executing in the personality module, |
| 248 | * except possibly the tail end of a ->bi_end_io function, but that |
| 249 | * is certain to complete before the module has a chance to get |
| 250 | * unloaded |
| 251 | */ |
| 252 | } |
| 253 | |
| 254 | static void mddev_resume(mddev_t *mddev) |
| 255 | { |
| 256 | mddev->suspended = 0; |
| 257 | wake_up(&mddev->sb_wait); |
| 258 | mddev->pers->quiesce(mddev, 0); |
| 259 | } |
| 260 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 261 | int mddev_congested(mddev_t *mddev, int bits) |
| 262 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 263 | if (mddev->barrier) |
| 264 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 265 | return mddev->suspended; |
| 266 | } |
| 267 | EXPORT_SYMBOL(mddev_congested); |
| 268 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 269 | /* |
| 270 | * Generic barrier handling for md |
| 271 | */ |
| 272 | |
| 273 | #define POST_REQUEST_BARRIER ((void*)1) |
| 274 | |
| 275 | static void md_end_barrier(struct bio *bio, int err) |
| 276 | { |
| 277 | mdk_rdev_t *rdev = bio->bi_private; |
| 278 | mddev_t *mddev = rdev->mddev; |
| 279 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 280 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 281 | |
| 282 | rdev_dec_pending(rdev, mddev); |
| 283 | |
| 284 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 285 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 286 | /* This was a post-request barrier */ |
| 287 | mddev->barrier = NULL; |
| 288 | wake_up(&mddev->sb_wait); |
| 289 | } else |
| 290 | /* The pre-request barrier has finished */ |
| 291 | schedule_work(&mddev->barrier_work); |
| 292 | } |
| 293 | bio_put(bio); |
| 294 | } |
| 295 | |
| 296 | static void submit_barriers(mddev_t *mddev) |
| 297 | { |
| 298 | mdk_rdev_t *rdev; |
| 299 | |
| 300 | rcu_read_lock(); |
| 301 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 302 | if (rdev->raid_disk >= 0 && |
| 303 | !test_bit(Faulty, &rdev->flags)) { |
| 304 | /* Take two references, one is dropped |
| 305 | * when request finishes, one after |
| 306 | * we reclaim rcu_read_lock |
| 307 | */ |
| 308 | struct bio *bi; |
| 309 | atomic_inc(&rdev->nr_pending); |
| 310 | atomic_inc(&rdev->nr_pending); |
| 311 | rcu_read_unlock(); |
| 312 | bi = bio_alloc(GFP_KERNEL, 0); |
| 313 | bi->bi_end_io = md_end_barrier; |
| 314 | bi->bi_private = rdev; |
| 315 | bi->bi_bdev = rdev->bdev; |
| 316 | atomic_inc(&mddev->flush_pending); |
| 317 | submit_bio(WRITE_BARRIER, bi); |
| 318 | rcu_read_lock(); |
| 319 | rdev_dec_pending(rdev, mddev); |
| 320 | } |
| 321 | rcu_read_unlock(); |
| 322 | } |
| 323 | |
| 324 | static void md_submit_barrier(struct work_struct *ws) |
| 325 | { |
| 326 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 327 | struct bio *bio = mddev->barrier; |
| 328 | |
| 329 | atomic_set(&mddev->flush_pending, 1); |
| 330 | |
| 331 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 332 | bio_endio(bio, -EOPNOTSUPP); |
| 333 | else if (bio->bi_size == 0) |
| 334 | /* an empty barrier - all done */ |
| 335 | bio_endio(bio, 0); |
| 336 | else { |
| 337 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
| 338 | if (mddev->pers->make_request(mddev->queue, bio)) |
| 339 | generic_make_request(bio); |
| 340 | mddev->barrier = POST_REQUEST_BARRIER; |
| 341 | submit_barriers(mddev); |
| 342 | } |
| 343 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 344 | mddev->barrier = NULL; |
| 345 | wake_up(&mddev->sb_wait); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 350 | { |
| 351 | spin_lock_irq(&mddev->write_lock); |
| 352 | wait_event_lock_irq(mddev->sb_wait, |
| 353 | !mddev->barrier, |
| 354 | mddev->write_lock, /*nothing*/); |
| 355 | mddev->barrier = bio; |
| 356 | spin_unlock_irq(&mddev->write_lock); |
| 357 | |
| 358 | atomic_set(&mddev->flush_pending, 1); |
| 359 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 360 | |
| 361 | submit_barriers(mddev); |
| 362 | |
| 363 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 364 | schedule_work(&mddev->barrier_work); |
| 365 | } |
| 366 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 369 | { |
| 370 | atomic_inc(&mddev->active); |
| 371 | return mddev; |
| 372 | } |
| 373 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 374 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | static void mddev_put(mddev_t *mddev) |
| 377 | { |
| 378 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 379 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 380 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 381 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 383 | if (mddev->gendisk) { |
| 384 | /* we did a probe so need to clean up. |
| 385 | * Call schedule_work inside the spinlock |
| 386 | * so that flush_scheduled_work() after |
| 387 | * mddev_find will succeed in waiting for the |
| 388 | * work to be done. |
| 389 | */ |
| 390 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 391 | schedule_work(&mddev->del_work); |
| 392 | } else |
| 393 | kfree(mddev); |
| 394 | } |
| 395 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static mddev_t * mddev_find(dev_t unit) |
| 399 | { |
| 400 | mddev_t *mddev, *new = NULL; |
| 401 | |
| 402 | retry: |
| 403 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 405 | if (unit) { |
| 406 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 407 | if (mddev->unit == unit) { |
| 408 | mddev_get(mddev); |
| 409 | spin_unlock(&all_mddevs_lock); |
| 410 | kfree(new); |
| 411 | return mddev; |
| 412 | } |
| 413 | |
| 414 | if (new) { |
| 415 | list_add(&new->all_mddevs, &all_mddevs); |
| 416 | spin_unlock(&all_mddevs_lock); |
| 417 | new->hold_active = UNTIL_IOCTL; |
| 418 | return new; |
| 419 | } |
| 420 | } else if (new) { |
| 421 | /* find an unused unit number */ |
| 422 | static int next_minor = 512; |
| 423 | int start = next_minor; |
| 424 | int is_free = 0; |
| 425 | int dev = 0; |
| 426 | while (!is_free) { |
| 427 | dev = MKDEV(MD_MAJOR, next_minor); |
| 428 | next_minor++; |
| 429 | if (next_minor > MINORMASK) |
| 430 | next_minor = 0; |
| 431 | if (next_minor == start) { |
| 432 | /* Oh dear, all in use. */ |
| 433 | spin_unlock(&all_mddevs_lock); |
| 434 | kfree(new); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | is_free = 1; |
| 439 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 440 | if (mddev->unit == dev) { |
| 441 | is_free = 0; |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | new->unit = dev; |
| 446 | new->md_minor = MINOR(dev); |
| 447 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | list_add(&new->all_mddevs, &all_mddevs); |
| 449 | spin_unlock(&all_mddevs_lock); |
| 450 | return new; |
| 451 | } |
| 452 | spin_unlock(&all_mddevs_lock); |
| 453 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 454 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (!new) |
| 456 | return NULL; |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | new->unit = unit; |
| 459 | if (MAJOR(unit) == MD_MAJOR) |
| 460 | new->md_minor = MINOR(unit); |
| 461 | else |
| 462 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 463 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 464 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 465 | mutex_init(&new->reconfig_mutex); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 466 | mutex_init(&new->bitmap_info.mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | INIT_LIST_HEAD(&new->disks); |
| 468 | INIT_LIST_HEAD(&new->all_mddevs); |
| 469 | init_timer(&new->safemode_timer); |
| 470 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 471 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 472 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 473 | spin_lock_init(&new->write_lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | atomic_set(&new->flush_pending, 0); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 475 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 476 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 477 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 478 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 479 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 480 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | goto retry; |
| 483 | } |
| 484 | |
| 485 | static inline int mddev_lock(mddev_t * mddev) |
| 486 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 487 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 490 | static inline int mddev_is_locked(mddev_t *mddev) |
| 491 | { |
| 492 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 493 | } |
| 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | static inline int mddev_trylock(mddev_t * mddev) |
| 496 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 497 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static inline void mddev_unlock(mddev_t * mddev) |
| 501 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 502 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 504 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 507 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 509 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 511 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (rdev->desc_nr == nr) |
| 513 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 519 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | mdk_rdev_t *rdev; |
| 521 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 522 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (rdev->bdev->bd_dev == dev) |
| 524 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
| 528 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 529 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 530 | { |
| 531 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 532 | list_for_each_entry(pers, &pers_list, list) { |
| 533 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 534 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 535 | if (strcmp(pers->name, clevel)==0) |
| 536 | return pers; |
| 537 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 538 | return NULL; |
| 539 | } |
| 540 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 541 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 542 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 544 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 545 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 549 | { |
| 550 | if (rdev->sb_page) |
| 551 | MD_BUG(); |
| 552 | |
| 553 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 554 | if (!rdev->sb_page) { |
| 555 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 556 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 563 | { |
| 564 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 565 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | rdev->sb_loaded = 0; |
| 567 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 568 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 569 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 574 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 575 | { |
| 576 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 577 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 578 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 579 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 580 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 581 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 582 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 583 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 584 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 585 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 586 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 587 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 588 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 589 | } |
| 590 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 591 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 592 | { |
| 593 | struct bio *bio2 = bio->bi_private; |
| 594 | mdk_rdev_t *rdev = bio2->bi_private; |
| 595 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 596 | |
| 597 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 598 | error == -EOPNOTSUPP) { |
| 599 | unsigned long flags; |
| 600 | /* barriers don't appear to be supported :-( */ |
| 601 | set_bit(BarriersNotsupp, &rdev->flags); |
| 602 | mddev->barriers_work = 0; |
| 603 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 604 | bio2->bi_next = mddev->biolist; |
| 605 | mddev->biolist = bio2; |
| 606 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 607 | wake_up(&mddev->sb_wait); |
| 608 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 609 | } else { |
| 610 | bio_put(bio2); |
| 611 | bio->bi_private = rdev; |
| 612 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 613 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 614 | } |
| 615 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 616 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 617 | sector_t sector, int size, struct page *page) |
| 618 | { |
| 619 | /* write first size bytes of page to sector of rdev |
| 620 | * Increment mddev->pending_writes before returning |
| 621 | * and decrement it on completion, waking up sb_wait |
| 622 | * if zero is reached. |
| 623 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 624 | * |
| 625 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 626 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 627 | */ |
| 628 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 629 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 630 | |
| 631 | bio->bi_bdev = rdev->bdev; |
| 632 | bio->bi_sector = sector; |
| 633 | bio_add_page(bio, page, size, 0); |
| 634 | bio->bi_private = rdev; |
| 635 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 636 | bio->bi_rw = rw; |
| 637 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 638 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 639 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 640 | struct bio *rbio; |
| 641 | rw |= (1<<BIO_RW_BARRIER); |
| 642 | rbio = bio_clone(bio, GFP_NOIO); |
| 643 | rbio->bi_private = bio; |
| 644 | rbio->bi_end_io = super_written_barrier; |
| 645 | submit_bio(rw, rbio); |
| 646 | } else |
| 647 | submit_bio(rw, bio); |
| 648 | } |
| 649 | |
| 650 | void md_super_wait(mddev_t *mddev) |
| 651 | { |
| 652 | /* wait for all superblock writes that were scheduled to complete. |
| 653 | * if any had to be retried (due to BARRIER problems), retry them |
| 654 | */ |
| 655 | DEFINE_WAIT(wq); |
| 656 | for(;;) { |
| 657 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 658 | if (atomic_read(&mddev->pending_writes)==0) |
| 659 | break; |
| 660 | while (mddev->biolist) { |
| 661 | struct bio *bio; |
| 662 | spin_lock_irq(&mddev->write_lock); |
| 663 | bio = mddev->biolist; |
| 664 | mddev->biolist = bio->bi_next ; |
| 665 | bio->bi_next = NULL; |
| 666 | spin_unlock_irq(&mddev->write_lock); |
| 667 | submit_bio(bio->bi_rw, bio); |
| 668 | } |
| 669 | schedule(); |
| 670 | } |
| 671 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 672 | } |
| 673 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 674 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 679 | 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] | 680 | struct page *page, int rw) |
| 681 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 682 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | struct completion event; |
| 684 | int ret; |
| 685 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 686 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | bio->bi_bdev = bdev; |
| 689 | bio->bi_sector = sector; |
| 690 | bio_add_page(bio, page, size, 0); |
| 691 | init_completion(&event); |
| 692 | bio->bi_private = &event; |
| 693 | bio->bi_end_io = bi_complete; |
| 694 | submit_bio(rw, bio); |
| 695 | wait_for_completion(&event); |
| 696 | |
| 697 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 698 | bio_put(bio); |
| 699 | return ret; |
| 700 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 701 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 703 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
| 705 | char b[BDEVNAME_SIZE]; |
| 706 | if (!rdev->sb_page) { |
| 707 | MD_BUG(); |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | if (rdev->sb_loaded) |
| 711 | return 0; |
| 712 | |
| 713 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 714 | 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] | 715 | goto fail; |
| 716 | rdev->sb_loaded = 1; |
| 717 | return 0; |
| 718 | |
| 719 | fail: |
| 720 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 721 | bdevname(rdev->bdev,b)); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 726 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 727 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 728 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 729 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 730 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 734 | { |
| 735 | int ret; |
| 736 | mdp_super_t *tmp1, *tmp2; |
| 737 | |
| 738 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 739 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 740 | |
| 741 | if (!tmp1 || !tmp2) { |
| 742 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 743 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | goto abort; |
| 745 | } |
| 746 | |
| 747 | *tmp1 = *sb1; |
| 748 | *tmp2 = *sb2; |
| 749 | |
| 750 | /* |
| 751 | * nr_disks is not constant |
| 752 | */ |
| 753 | tmp1->nr_disks = 0; |
| 754 | tmp2->nr_disks = 0; |
| 755 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 756 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 758 | kfree(tmp1); |
| 759 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 763 | |
| 764 | static u32 md_csum_fold(u32 csum) |
| 765 | { |
| 766 | csum = (csum & 0xffff) + (csum >> 16); |
| 767 | return (csum & 0xffff) + (csum >> 16); |
| 768 | } |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 771 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 772 | u64 newcsum = 0; |
| 773 | u32 *sb32 = (u32*)sb; |
| 774 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | unsigned int disk_csum, csum; |
| 776 | |
| 777 | disk_csum = sb->sb_csum; |
| 778 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 779 | |
| 780 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 781 | newcsum += sb32[i]; |
| 782 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 783 | |
| 784 | |
| 785 | #ifdef CONFIG_ALPHA |
| 786 | /* This used to use csum_partial, which was wrong for several |
| 787 | * reasons including that different results are returned on |
| 788 | * different architectures. It isn't critical that we get exactly |
| 789 | * the same return value as before (we always csum_fold before |
| 790 | * testing, and that removes any differences). However as we |
| 791 | * know that csum_partial always returned a 16bit value on |
| 792 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 793 | */ |
| 794 | sb->sb_csum = md_csum_fold(disk_csum); |
| 795 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 797 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return csum; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | /* |
| 803 | * Handle superblock details. |
| 804 | * We want to be able to handle multiple superblock formats |
| 805 | * so we have a common interface to them all, and an array of |
| 806 | * different handlers. |
| 807 | * We rely on user-space to write the initial superblock, and support |
| 808 | * reading and updating of superblocks. |
| 809 | * Interface methods are: |
| 810 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 811 | * loads and validates a superblock on dev. |
| 812 | * if refdev != NULL, compare superblocks on both devices |
| 813 | * Return: |
| 814 | * 0 - dev has a superblock that is compatible with refdev |
| 815 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 816 | * so dev should be used as the refdev in future |
| 817 | * -EINVAL superblock incompatible or invalid |
| 818 | * -othererror e.g. -EIO |
| 819 | * |
| 820 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 821 | * Verify that dev is acceptable into mddev. |
| 822 | * The first time, mddev->raid_disks will be 0, and data from |
| 823 | * dev should be merged in. Subsequent calls check that dev |
| 824 | * is new enough. Return 0 or -EINVAL |
| 825 | * |
| 826 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 827 | * Update the superblock for rdev with data in mddev |
| 828 | * This does not write to disc. |
| 829 | * |
| 830 | */ |
| 831 | |
| 832 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 833 | char *name; |
| 834 | struct module *owner; |
| 835 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 836 | int minor_version); |
| 837 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 838 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 839 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 840 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | }; |
| 842 | |
| 843 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 844 | * Check that the given mddev has no bitmap. |
| 845 | * |
| 846 | * This function is called from the run method of all personalities that do not |
| 847 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 848 | * has a bitmap. Otherwise, it returns 0. |
| 849 | * |
| 850 | */ |
| 851 | int md_check_no_bitmap(mddev_t *mddev) |
| 852 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 853 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 854 | return 0; |
| 855 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 856 | mdname(mddev), mddev->pers->name); |
| 857 | return 1; |
| 858 | } |
| 859 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 860 | |
| 861 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | * load_super for 0.90.0 |
| 863 | */ |
| 864 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 865 | { |
| 866 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 867 | mdp_super_t *sb; |
| 868 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
| 870 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 871 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | * it's at the end of the disk. |
| 873 | * |
| 874 | * It also happens to be a multiple of 4Kb. |
| 875 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 876 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 878 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | if (ret) return ret; |
| 880 | |
| 881 | ret = -EINVAL; |
| 882 | |
| 883 | bdevname(rdev->bdev, b); |
| 884 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 885 | |
| 886 | if (sb->md_magic != MD_SB_MAGIC) { |
| 887 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 888 | b); |
| 889 | goto abort; |
| 890 | } |
| 891 | |
| 892 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 893 | sb->minor_version < 90 || |
| 894 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 896 | sb->major_version, sb->minor_version, |
| 897 | b); |
| 898 | goto abort; |
| 899 | } |
| 900 | |
| 901 | if (sb->raid_disks <= 0) |
| 902 | goto abort; |
| 903 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 904 | 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] | 905 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 906 | b); |
| 907 | goto abort; |
| 908 | } |
| 909 | |
| 910 | rdev->preferred_minor = sb->md_minor; |
| 911 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 912 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | if (sb->level == LEVEL_MULTIPATH) |
| 915 | rdev->desc_nr = -1; |
| 916 | else |
| 917 | rdev->desc_nr = sb->this_disk.number; |
| 918 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 919 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 921 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | __u64 ev1, ev2; |
| 923 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 924 | if (!uuid_equal(refsb, sb)) { |
| 925 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 926 | b, bdevname(refdev->bdev,b2)); |
| 927 | goto abort; |
| 928 | } |
| 929 | if (!sb_equal(refsb, sb)) { |
| 930 | printk(KERN_WARNING "md: %s has same UUID" |
| 931 | " but different superblock to %s\n", |
| 932 | b, bdevname(refdev->bdev, b2)); |
| 933 | goto abort; |
| 934 | } |
| 935 | ev1 = md_event(sb); |
| 936 | ev2 = md_event(refsb); |
| 937 | if (ev1 > ev2) |
| 938 | ret = 1; |
| 939 | else |
| 940 | ret = 0; |
| 941 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 942 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 944 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 945 | /* "this cannot possibly happen" ... */ |
| 946 | ret = -EINVAL; |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | abort: |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * validate_super for 0.90.0 |
| 954 | */ |
| 955 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 956 | { |
| 957 | mdp_disk_t *desc; |
| 958 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 959 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 961 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 962 | clear_bit(Faulty, &rdev->flags); |
| 963 | clear_bit(In_sync, &rdev->flags); |
| 964 | clear_bit(WriteMostly, &rdev->flags); |
| 965 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | if (mddev->raid_disks == 0) { |
| 968 | mddev->major_version = 0; |
| 969 | mddev->minor_version = sb->minor_version; |
| 970 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 971 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 972 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | mddev->ctime = sb->ctime; |
| 974 | mddev->utime = sb->utime; |
| 975 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 976 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | mddev->layout = sb->layout; |
| 978 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 979 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 980 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 981 | mddev->bitmap_info.offset = 0; |
| 982 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 984 | if (mddev->minor_version >= 91) { |
| 985 | mddev->reshape_position = sb->reshape_position; |
| 986 | mddev->delta_disks = sb->delta_disks; |
| 987 | mddev->new_level = sb->new_level; |
| 988 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 989 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 990 | } else { |
| 991 | mddev->reshape_position = MaxSector; |
| 992 | mddev->delta_disks = 0; |
| 993 | mddev->new_level = mddev->level; |
| 994 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 995 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 999 | mddev->recovery_cp = MaxSector; |
| 1000 | else { |
| 1001 | if (sb->events_hi == sb->cp_events_hi && |
| 1002 | sb->events_lo == sb->cp_events_lo) { |
| 1003 | mddev->recovery_cp = sb->recovery_cp; |
| 1004 | } else |
| 1005 | mddev->recovery_cp = 0; |
| 1006 | } |
| 1007 | |
| 1008 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1009 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1010 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1011 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1012 | |
| 1013 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1014 | |
| 1015 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1016 | mddev->bitmap_info.file == NULL) |
| 1017 | mddev->bitmap_info.offset = |
| 1018 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1019 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1020 | } else if (mddev->pers == NULL) { |
| 1021 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | ++ev1; |
| 1023 | if (ev1 < mddev->events) |
| 1024 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1025 | } else if (mddev->bitmap) { |
| 1026 | /* if adding to array with a bitmap, then we can accept an |
| 1027 | * older device ... but not too old. |
| 1028 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1029 | if (ev1 < mddev->bitmap->events_cleared) |
| 1030 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1031 | } else { |
| 1032 | if (ev1 < mddev->events) |
| 1033 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1034 | return 0; |
| 1035 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | desc = sb->disks + rdev->desc_nr; |
| 1039 | |
| 1040 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1041 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1042 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1043 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1044 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1046 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1047 | /* active but not in sync implies recovery up to |
| 1048 | * reshape position. We don't know exactly where |
| 1049 | * that is, so set to zero for now */ |
| 1050 | if (mddev->minor_version >= 91) { |
| 1051 | rdev->recovery_offset = 0; |
| 1052 | rdev->raid_disk = desc->raid_disk; |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1055 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1056 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1057 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1058 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * sync_super for 0.90.0 |
| 1064 | */ |
| 1065 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1066 | { |
| 1067 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | mdk_rdev_t *rdev2; |
| 1069 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
| 1072 | /* make rdev->sb match mddev data.. |
| 1073 | * |
| 1074 | * 1/ zero out disks |
| 1075 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1076 | * 3/ any empty disks < next_spare become removed |
| 1077 | * |
| 1078 | * disks[0] gets initialised to REMOVED because |
| 1079 | * we cannot be sure from other fields if it has |
| 1080 | * been initialised or not. |
| 1081 | */ |
| 1082 | int i; |
| 1083 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1084 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1085 | rdev->sb_size = MD_SB_BYTES; |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1088 | |
| 1089 | memset(sb, 0, sizeof(*sb)); |
| 1090 | |
| 1091 | sb->md_magic = MD_SB_MAGIC; |
| 1092 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | sb->patch_version = mddev->patch_version; |
| 1094 | sb->gvalid_words = 0; /* ignored */ |
| 1095 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1096 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1097 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1098 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1099 | |
| 1100 | sb->ctime = mddev->ctime; |
| 1101 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1102 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | sb->raid_disks = mddev->raid_disks; |
| 1104 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1105 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | sb->utime = mddev->utime; |
| 1107 | sb->state = 0; |
| 1108 | sb->events_hi = (mddev->events>>32); |
| 1109 | sb->events_lo = (u32)mddev->events; |
| 1110 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1111 | if (mddev->reshape_position == MaxSector) |
| 1112 | sb->minor_version = 90; |
| 1113 | else { |
| 1114 | sb->minor_version = 91; |
| 1115 | sb->reshape_position = mddev->reshape_position; |
| 1116 | sb->new_level = mddev->new_level; |
| 1117 | sb->delta_disks = mddev->delta_disks; |
| 1118 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1119 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1120 | } |
| 1121 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | if (mddev->in_sync) |
| 1123 | { |
| 1124 | sb->recovery_cp = mddev->recovery_cp; |
| 1125 | sb->cp_events_hi = (mddev->events>>32); |
| 1126 | sb->cp_events_lo = (u32)mddev->events; |
| 1127 | if (mddev->recovery_cp == MaxSector) |
| 1128 | sb->state = (1<< MD_SB_CLEAN); |
| 1129 | } else |
| 1130 | sb->recovery_cp = 0; |
| 1131 | |
| 1132 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1133 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1135 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1136 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1139 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1141 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1142 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1143 | |
| 1144 | if (rdev2->raid_disk >= 0 && |
| 1145 | sb->minor_version >= 91) |
| 1146 | /* we have nowhere to store the recovery_offset, |
| 1147 | * but if it is not below the reshape_position, |
| 1148 | * we can piggy-back on that. |
| 1149 | */ |
| 1150 | is_active = 1; |
| 1151 | if (rdev2->raid_disk < 0 || |
| 1152 | test_bit(Faulty, &rdev2->flags)) |
| 1153 | is_active = 0; |
| 1154 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1155 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1157 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1158 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | d = &sb->disks[rdev2->desc_nr]; |
| 1160 | nr_disks++; |
| 1161 | d->number = rdev2->desc_nr; |
| 1162 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1163 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1164 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | d->raid_disk = rdev2->raid_disk; |
| 1166 | else |
| 1167 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1168 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1170 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1172 | if (test_bit(In_sync, &rdev2->flags)) |
| 1173 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | active++; |
| 1175 | working++; |
| 1176 | } else { |
| 1177 | d->state = 0; |
| 1178 | spare++; |
| 1179 | working++; |
| 1180 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1181 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1182 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1185 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1186 | mdp_disk_t *d = &sb->disks[i]; |
| 1187 | if (d->state == 0 && d->number == 0) { |
| 1188 | d->number = i; |
| 1189 | d->raid_disk = i; |
| 1190 | d->state = (1<<MD_DISK_REMOVED); |
| 1191 | d->state |= (1<<MD_DISK_FAULTY); |
| 1192 | failed++; |
| 1193 | } |
| 1194 | } |
| 1195 | sb->nr_disks = nr_disks; |
| 1196 | sb->active_disks = active; |
| 1197 | sb->working_disks = working; |
| 1198 | sb->failed_disks = failed; |
| 1199 | sb->spare_disks = spare; |
| 1200 | |
| 1201 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1202 | sb->sb_csum = calc_sb_csum(sb); |
| 1203 | } |
| 1204 | |
| 1205 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1206 | * rdev_size_change for 0.90.0 |
| 1207 | */ |
| 1208 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1209 | 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] | 1210 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1211 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1212 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1213 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1214 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1215 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1216 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1217 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1218 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1219 | rdev->sb_page); |
| 1220 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1221 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | * version 1 superblock |
| 1227 | */ |
| 1228 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1229 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1231 | __le32 disk_csum; |
| 1232 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | unsigned long long newcsum; |
| 1234 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1235 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | int i; |
| 1237 | |
| 1238 | disk_csum = sb->sb_csum; |
| 1239 | sb->sb_csum = 0; |
| 1240 | newcsum = 0; |
| 1241 | for (i=0; size>=4; size -= 4 ) |
| 1242 | newcsum += le32_to_cpu(*isuper++); |
| 1243 | |
| 1244 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1245 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
| 1247 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1248 | sb->sb_csum = disk_csum; |
| 1249 | return cpu_to_le32(csum); |
| 1250 | } |
| 1251 | |
| 1252 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1253 | { |
| 1254 | struct mdp_superblock_1 *sb; |
| 1255 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1256 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1258 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1261 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | * It is always aligned to a 4K boundary and |
| 1263 | * depeding on minor_version, it can be: |
| 1264 | * 0: At least 8K, but less than 12K, from end of device |
| 1265 | * 1: At start of device |
| 1266 | * 2: 4K from start of device. |
| 1267 | */ |
| 1268 | switch(minor_version) { |
| 1269 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1270 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1271 | sb_start -= 8*2; |
| 1272 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | break; |
| 1274 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1275 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | break; |
| 1277 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1278 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | break; |
| 1280 | default: |
| 1281 | return -EINVAL; |
| 1282 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1283 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1285 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1286 | * and it is safe to read 4k, so we do that |
| 1287 | */ |
| 1288 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (ret) return ret; |
| 1290 | |
| 1291 | |
| 1292 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1293 | |
| 1294 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1295 | sb->major_version != cpu_to_le32(1) || |
| 1296 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1297 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1298 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | return -EINVAL; |
| 1300 | |
| 1301 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1302 | printk("md: invalid superblock checksum on %s\n", |
| 1303 | bdevname(rdev->bdev,b)); |
| 1304 | return -EINVAL; |
| 1305 | } |
| 1306 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1307 | printk("md: data_size too small on %s\n", |
| 1308 | bdevname(rdev->bdev,b)); |
| 1309 | return -EINVAL; |
| 1310 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | rdev->preferred_minor = 0xffff; |
| 1313 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1314 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1316 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1317 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1318 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1319 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1320 | |
| 1321 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1322 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1323 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1324 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1325 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1326 | rdev->desc_nr = -1; |
| 1327 | else |
| 1328 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1329 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1330 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1331 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1332 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | __u64 ev1, ev2; |
| 1334 | struct mdp_superblock_1 *refsb = |
| 1335 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1336 | |
| 1337 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1338 | sb->level != refsb->level || |
| 1339 | sb->layout != refsb->layout || |
| 1340 | sb->chunksize != refsb->chunksize) { |
| 1341 | printk(KERN_WARNING "md: %s has strangely different" |
| 1342 | " superblock to %s\n", |
| 1343 | bdevname(rdev->bdev,b), |
| 1344 | bdevname(refdev->bdev,b2)); |
| 1345 | return -EINVAL; |
| 1346 | } |
| 1347 | ev1 = le64_to_cpu(sb->events); |
| 1348 | ev2 = le64_to_cpu(refsb->events); |
| 1349 | |
| 1350 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1351 | ret = 1; |
| 1352 | else |
| 1353 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1355 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1356 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1357 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1359 | rdev->sectors = rdev->sb_start; |
| 1360 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1362 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1363 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1364 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1365 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1369 | { |
| 1370 | 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] | 1371 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1373 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1374 | clear_bit(Faulty, &rdev->flags); |
| 1375 | clear_bit(In_sync, &rdev->flags); |
| 1376 | clear_bit(WriteMostly, &rdev->flags); |
| 1377 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | if (mddev->raid_disks == 0) { |
| 1380 | mddev->major_version = 1; |
| 1381 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1382 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1383 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1385 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1386 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1387 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | mddev->layout = le32_to_cpu(sb->layout); |
| 1389 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1390 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1391 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1392 | mddev->bitmap_info.offset = 0; |
| 1393 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1396 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1397 | |
| 1398 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1399 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1400 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1401 | mddev->bitmap_info.file == NULL ) |
| 1402 | mddev->bitmap_info.offset = |
| 1403 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1404 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1405 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1406 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1407 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1408 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1409 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1410 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1411 | } else { |
| 1412 | mddev->reshape_position = MaxSector; |
| 1413 | mddev->delta_disks = 0; |
| 1414 | mddev->new_level = mddev->level; |
| 1415 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1416 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1419 | } else if (mddev->pers == NULL) { |
| 1420 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | ++ev1; |
| 1422 | if (ev1 < mddev->events) |
| 1423 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1424 | } else if (mddev->bitmap) { |
| 1425 | /* If adding to array with a bitmap, then we can accept an |
| 1426 | * older device, but not too old. |
| 1427 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1428 | if (ev1 < mddev->bitmap->events_cleared) |
| 1429 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1430 | } else { |
| 1431 | if (ev1 < mddev->events) |
| 1432 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1433 | return 0; |
| 1434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1436 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1437 | if (rdev->desc_nr < 0 || |
| 1438 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1439 | role = 0xffff; |
| 1440 | rdev->desc_nr = -1; |
| 1441 | } else |
| 1442 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | switch(role) { |
| 1444 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | break; |
| 1446 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1447 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | break; |
| 1449 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1450 | if ((le32_to_cpu(sb->feature_map) & |
| 1451 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1452 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1453 | else |
| 1454 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | rdev->raid_disk = role; |
| 1456 | break; |
| 1457 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1458 | if (sb->devflags & WriteMostly1) |
| 1459 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1460 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1461 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1467 | { |
| 1468 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | mdk_rdev_t *rdev2; |
| 1470 | int max_dev, i; |
| 1471 | /* make rdev->sb match mddev and rdev data. */ |
| 1472 | |
| 1473 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1474 | |
| 1475 | sb->feature_map = 0; |
| 1476 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1477 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1479 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1480 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1481 | |
| 1482 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1483 | sb->events = cpu_to_le64(mddev->events); |
| 1484 | if (mddev->in_sync) |
| 1485 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1486 | else |
| 1487 | sb->resync_offset = cpu_to_le64(0); |
| 1488 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1489 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1490 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1491 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1492 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1493 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1494 | sb->level = cpu_to_le32(mddev->level); |
| 1495 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1496 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1497 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1498 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1499 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1500 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1503 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1504 | if (rdev->recovery_offset > 0) { |
| 1505 | sb->feature_map |= |
| 1506 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1507 | sb->recovery_offset = |
| 1508 | cpu_to_le64(rdev->recovery_offset); |
| 1509 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1512 | if (mddev->reshape_position != MaxSector) { |
| 1513 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1514 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1515 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1516 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1517 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1518 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1519 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1522 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | if (rdev2->desc_nr+1 > max_dev) |
| 1524 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1525 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1526 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1527 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1528 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1529 | rdev->sb_size = max_dev * 2 + 256; |
| 1530 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1531 | if (rdev->sb_size & bmask) |
| 1532 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | for (i=0; i<max_dev;i++) |
| 1535 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1536 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1537 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1539 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1541 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1543 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1544 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | else |
| 1546 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1547 | } |
| 1548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1550 | } |
| 1551 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1552 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1553 | 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] | 1554 | { |
| 1555 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1556 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1557 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1558 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1559 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1560 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1561 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1562 | max_sectors -= rdev->data_offset; |
| 1563 | if (!num_sectors || num_sectors > max_sectors) |
| 1564 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1565 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1566 | /* minor version 0 with bitmap we can't move */ |
| 1567 | return 0; |
| 1568 | } else { |
| 1569 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1570 | sector_t sb_start; |
| 1571 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1572 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1573 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1574 | if (!num_sectors || num_sectors > max_sectors) |
| 1575 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1576 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1577 | } |
| 1578 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1579 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1580 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1581 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1582 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1583 | rdev->sb_page); |
| 1584 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1585 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1588 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | [0] = { |
| 1590 | .name = "0.90.0", |
| 1591 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1592 | .load_super = super_90_load, |
| 1593 | .validate_super = super_90_validate, |
| 1594 | .sync_super = super_90_sync, |
| 1595 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | }, |
| 1597 | [1] = { |
| 1598 | .name = "md-1", |
| 1599 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1600 | .load_super = super_1_load, |
| 1601 | .validate_super = super_1_validate, |
| 1602 | .sync_super = super_1_sync, |
| 1603 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | }, |
| 1605 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
| 1607 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1608 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1609 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1611 | rcu_read_lock(); |
| 1612 | rdev_for_each_rcu(rdev, mddev1) |
| 1613 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1614 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1615 | rdev2->bdev->bd_contains) { |
| 1616 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1617 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1618 | } |
| 1619 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | static LIST_HEAD(pending_raid_disks); |
| 1624 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1625 | /* |
| 1626 | * Try to register data integrity profile for an mddev |
| 1627 | * |
| 1628 | * This is called when an array is started and after a disk has been kicked |
| 1629 | * from the array. It only succeeds if all working and active component devices |
| 1630 | * are integrity capable with matching profiles. |
| 1631 | */ |
| 1632 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1633 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1634 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1635 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1636 | if (list_empty(&mddev->disks)) |
| 1637 | return 0; /* nothing to do */ |
| 1638 | if (blk_get_integrity(mddev->gendisk)) |
| 1639 | return 0; /* already registered */ |
| 1640 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1641 | /* skip spares and non-functional disks */ |
| 1642 | if (test_bit(Faulty, &rdev->flags)) |
| 1643 | continue; |
| 1644 | if (rdev->raid_disk < 0) |
| 1645 | continue; |
| 1646 | /* |
| 1647 | * If at least one rdev is not integrity capable, we can not |
| 1648 | * enable data integrity for the md device. |
| 1649 | */ |
| 1650 | if (!bdev_get_integrity(rdev->bdev)) |
| 1651 | return -EINVAL; |
| 1652 | if (!reference) { |
| 1653 | /* Use the first rdev as the reference */ |
| 1654 | reference = rdev; |
| 1655 | continue; |
| 1656 | } |
| 1657 | /* does this rdev's profile match the reference profile? */ |
| 1658 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1659 | rdev->bdev->bd_disk) < 0) |
| 1660 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1661 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1662 | /* |
| 1663 | * All component devices are integrity capable and have matching |
| 1664 | * profiles, register the common profile for the md device. |
| 1665 | */ |
| 1666 | if (blk_integrity_register(mddev->gendisk, |
| 1667 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1668 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1669 | mdname(mddev)); |
| 1670 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1671 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1672 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1673 | mdname(mddev)); |
| 1674 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1675 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1676 | EXPORT_SYMBOL(md_integrity_register); |
| 1677 | |
| 1678 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1679 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1680 | { |
| 1681 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1682 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1683 | |
| 1684 | if (!bi_mddev) /* nothing to do */ |
| 1685 | return; |
| 1686 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1687 | return; |
| 1688 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1689 | rdev->bdev->bd_disk) >= 0) |
| 1690 | return; |
| 1691 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1692 | blk_integrity_unregister(mddev->gendisk); |
| 1693 | } |
| 1694 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1697 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1698 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1699 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1700 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1701 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | |
| 1703 | if (rdev->mddev) { |
| 1704 | MD_BUG(); |
| 1705 | return -EINVAL; |
| 1706 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1707 | |
| 1708 | /* prevent duplicates */ |
| 1709 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1710 | return -EEXIST; |
| 1711 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1712 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1713 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1714 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1715 | if (mddev->pers) { |
| 1716 | /* Cannot change size, so fail |
| 1717 | * If mddev->level <= 0, then we don't care |
| 1718 | * about aligning sizes (e.g. linear) |
| 1719 | */ |
| 1720 | if (mddev->level > 0) |
| 1721 | return -ENOSPC; |
| 1722 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1723 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | /* Verify rdev->desc_nr is unique. |
| 1727 | * If it is -1, assign a free number, else |
| 1728 | * check number is not in use |
| 1729 | */ |
| 1730 | if (rdev->desc_nr < 0) { |
| 1731 | int choice = 0; |
| 1732 | if (mddev->pers) choice = mddev->raid_disks; |
| 1733 | while (find_rdev_nr(mddev, choice)) |
| 1734 | choice++; |
| 1735 | rdev->desc_nr = choice; |
| 1736 | } else { |
| 1737 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1738 | return -EBUSY; |
| 1739 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1740 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1741 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1742 | mdname(mddev), mddev->max_disks); |
| 1743 | return -EBUSY; |
| 1744 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1745 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1746 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1747 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1750 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1751 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1752 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1753 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1754 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1755 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1756 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1757 | kobject_del(&rdev->kobj); |
| 1758 | goto fail; |
| 1759 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1760 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1761 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1762 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1763 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1764 | |
| 1765 | /* May as well allow recovery to be retried once */ |
| 1766 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1769 | |
| 1770 | fail: |
| 1771 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1772 | b, mdname(mddev)); |
| 1773 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1776 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1777 | { |
| 1778 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1779 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1780 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1784 | { |
| 1785 | char b[BDEVNAME_SIZE]; |
| 1786 | if (!rdev->mddev) { |
| 1787 | MD_BUG(); |
| 1788 | return; |
| 1789 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1790 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1791 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1793 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1794 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1795 | sysfs_put(rdev->sysfs_state); |
| 1796 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1797 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1798 | * writing to 'remove' to "dev/state". We also need |
| 1799 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1800 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1801 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1802 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1803 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1804 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * prevent the device from being mounted, repartitioned or |
| 1809 | * otherwise reused by a RAID array (or any other kernel |
| 1810 | * subsystem), by bd_claiming the device. |
| 1811 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1812 | 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] | 1813 | { |
| 1814 | int err = 0; |
| 1815 | struct block_device *bdev; |
| 1816 | char b[BDEVNAME_SIZE]; |
| 1817 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1818 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | if (IS_ERR(bdev)) { |
| 1820 | printk(KERN_ERR "md: could not open %s.\n", |
| 1821 | __bdevname(dev, b)); |
| 1822 | return PTR_ERR(bdev); |
| 1823 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1824 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | if (err) { |
| 1826 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1827 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1828 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | return err; |
| 1830 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1831 | if (!shared) |
| 1832 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | rdev->bdev = bdev; |
| 1834 | return err; |
| 1835 | } |
| 1836 | |
| 1837 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1838 | { |
| 1839 | struct block_device *bdev = rdev->bdev; |
| 1840 | rdev->bdev = NULL; |
| 1841 | if (!bdev) |
| 1842 | MD_BUG(); |
| 1843 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1844 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | void md_autodetect_dev(dev_t dev); |
| 1848 | |
| 1849 | static void export_rdev(mdk_rdev_t * rdev) |
| 1850 | { |
| 1851 | char b[BDEVNAME_SIZE]; |
| 1852 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1853 | bdevname(rdev->bdev,b)); |
| 1854 | if (rdev->mddev) |
| 1855 | MD_BUG(); |
| 1856 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1858 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1859 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | #endif |
| 1861 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1862 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1866 | { |
| 1867 | unbind_rdev_from_array(rdev); |
| 1868 | export_rdev(rdev); |
| 1869 | } |
| 1870 | |
| 1871 | static void export_array(mddev_t *mddev) |
| 1872 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1873 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1875 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | if (!rdev->mddev) { |
| 1877 | MD_BUG(); |
| 1878 | continue; |
| 1879 | } |
| 1880 | kick_rdev_from_array(rdev); |
| 1881 | } |
| 1882 | if (!list_empty(&mddev->disks)) |
| 1883 | MD_BUG(); |
| 1884 | mddev->raid_disks = 0; |
| 1885 | mddev->major_version = 0; |
| 1886 | } |
| 1887 | |
| 1888 | static void print_desc(mdp_disk_t *desc) |
| 1889 | { |
| 1890 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1891 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1892 | } |
| 1893 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1894 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | { |
| 1896 | int i; |
| 1897 | |
| 1898 | printk(KERN_INFO |
| 1899 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1900 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1901 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1902 | sb->ctime); |
| 1903 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1904 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1905 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1906 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1907 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1908 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1909 | sb->failed_disks, sb->spare_disks, |
| 1910 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1911 | |
| 1912 | printk(KERN_INFO); |
| 1913 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1914 | mdp_disk_t *desc; |
| 1915 | |
| 1916 | desc = sb->disks + i; |
| 1917 | if (desc->number || desc->major || desc->minor || |
| 1918 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1919 | printk(" D %2d: ", i); |
| 1920 | print_desc(desc); |
| 1921 | } |
| 1922 | } |
| 1923 | printk(KERN_INFO "md: THIS: "); |
| 1924 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1927 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1928 | { |
| 1929 | __u8 *uuid; |
| 1930 | |
| 1931 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1932 | printk(KERN_INFO |
| 1933 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1934 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1935 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1936 | le32_to_cpu(sb->major_version), |
| 1937 | le32_to_cpu(sb->feature_map), |
| 1938 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1939 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1940 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1941 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1942 | sb->set_name, |
| 1943 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1944 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1945 | |
| 1946 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1947 | printk(KERN_INFO |
| 1948 | "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1949 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1950 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1951 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1952 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1953 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1954 | le32_to_cpu(sb->level), |
| 1955 | (unsigned long long)le64_to_cpu(sb->size), |
| 1956 | le32_to_cpu(sb->raid_disks), |
| 1957 | le32_to_cpu(sb->layout), |
| 1958 | le32_to_cpu(sb->chunksize), |
| 1959 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1960 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1961 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1962 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1963 | le32_to_cpu(sb->dev_number), |
| 1964 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1965 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1966 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1967 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1968 | sb->devflags, |
| 1969 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1970 | (unsigned long long)le64_to_cpu(sb->events), |
| 1971 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1972 | le32_to_cpu(sb->sb_csum), |
| 1973 | le32_to_cpu(sb->max_dev) |
| 1974 | ); |
| 1975 | } |
| 1976 | |
| 1977 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | { |
| 1979 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1980 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1981 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1982 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1983 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1985 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1986 | switch (major_version) { |
| 1987 | case 0: |
| 1988 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1989 | break; |
| 1990 | case 1: |
| 1991 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1992 | break; |
| 1993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | } else |
| 1995 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1996 | } |
| 1997 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1998 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2000 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | mdk_rdev_t *rdev; |
| 2002 | mddev_t *mddev; |
| 2003 | char b[BDEVNAME_SIZE]; |
| 2004 | |
| 2005 | printk("\n"); |
| 2006 | printk("md: **********************************\n"); |
| 2007 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2008 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2009 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2011 | if (mddev->bitmap) |
| 2012 | bitmap_print_sb(mddev->bitmap); |
| 2013 | else |
| 2014 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2015 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2017 | printk("\n"); |
| 2018 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2019 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2020 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
| 2022 | printk("md: **********************************\n"); |
| 2023 | printk("\n"); |
| 2024 | } |
| 2025 | |
| 2026 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2027 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2029 | /* Update each superblock (in-memory image), but |
| 2030 | * if we are allowed to, skip spares which already |
| 2031 | * have the right event counter, or have one earlier |
| 2032 | * (which would mean they aren't being marked as dirty |
| 2033 | * with the rest of the array) |
| 2034 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2037 | /* First make sure individual recovery_offsets are correct */ |
| 2038 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2039 | if (rdev->raid_disk >= 0 && |
| 2040 | !test_bit(In_sync, &rdev->flags) && |
| 2041 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2042 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2043 | |
| 2044 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2045 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2046 | if (rdev->sb_events == mddev->events || |
| 2047 | (nospares && |
| 2048 | rdev->raid_disk < 0 && |
| 2049 | (rdev->sb_events&1)==0 && |
| 2050 | rdev->sb_events+1 == mddev->events)) { |
| 2051 | /* Don't update this superblock */ |
| 2052 | rdev->sb_loaded = 2; |
| 2053 | } else { |
| 2054 | super_types[mddev->major_version]. |
| 2055 | sync_super(mddev, rdev); |
| 2056 | rdev->sb_loaded = 1; |
| 2057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2061 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2064 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2065 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2067 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2068 | if (mddev->external) |
| 2069 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2071 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2072 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2073 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2074 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2075 | force_change = 1; |
| 2076 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2077 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2078 | * though if events isn't the right even/odd, we will have to do |
| 2079 | * spares after all |
| 2080 | */ |
| 2081 | nospares = 1; |
| 2082 | if (force_change) |
| 2083 | nospares = 0; |
| 2084 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2085 | /* If the array is degraded, then skipping spares is both |
| 2086 | * dangerous and fairly pointless. |
| 2087 | * Dangerous because a device that was removed from the array |
| 2088 | * might have a event_count that still looks up-to-date, |
| 2089 | * so it can be re-added without a resync. |
| 2090 | * Pointless because if there are any spares to skip, |
| 2091 | * then a recovery will happen and soon that array won't |
| 2092 | * be degraded any more and the spare can go back to sleep then. |
| 2093 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2094 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2095 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2096 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2097 | |
| 2098 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2099 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2100 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2101 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2102 | && (mddev->events & 1) |
| 2103 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2104 | mddev->events--; |
| 2105 | else { |
| 2106 | /* otherwise we have to go forward and ... */ |
| 2107 | mddev->events ++; |
| 2108 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2109 | /* .. if the array isn't clean, an 'even' event must also go |
| 2110 | * to spares. */ |
| 2111 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2112 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2113 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2114 | /* otherwise an 'odd' event must go to spares */ |
| 2115 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2116 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | if (!mddev->events) { |
| 2121 | /* |
| 2122 | * oops, this 64-bit counter should never wrap. |
| 2123 | * Either we are in around ~1 trillion A.C., assuming |
| 2124 | * 1 reboot per second, or we have a bug: |
| 2125 | */ |
| 2126 | MD_BUG(); |
| 2127 | mddev->events --; |
| 2128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | /* |
| 2131 | * do not write anything to disk if using |
| 2132 | * nonpersistent superblocks |
| 2133 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2134 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2135 | if (!mddev->external) |
| 2136 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2137 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2138 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2139 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2141 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2142 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2143 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | |
| 2145 | dprintk(KERN_INFO |
| 2146 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2147 | mdname(mddev),mddev->in_sync); |
| 2148 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2149 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2150 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | char b[BDEVNAME_SIZE]; |
| 2152 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2153 | if (rdev->sb_loaded != 1) |
| 2154 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2155 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | dprintk("(skipping faulty "); |
| 2157 | |
| 2158 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2159 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2160 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2161 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2162 | rdev->sb_page); |
| 2163 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2164 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2165 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2166 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } else |
| 2169 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2170 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | /* only need to write one superblock... */ |
| 2172 | break; |
| 2173 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2174 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2175 | /* 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] | 2176 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2177 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2178 | if (mddev->in_sync != sync_req || |
| 2179 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2180 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2181 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2182 | goto repeat; |
| 2183 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2184 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2185 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2186 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2187 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2188 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2192 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2193 | * We want to accept with case. For this we use cmd_match. |
| 2194 | */ |
| 2195 | static int cmd_match(const char *cmd, const char *str) |
| 2196 | { |
| 2197 | /* See if cmd, written into a sysfs file, matches |
| 2198 | * str. They must either be the same, or cmd can |
| 2199 | * have a trailing newline |
| 2200 | */ |
| 2201 | while (*cmd && *str && *cmd == *str) { |
| 2202 | cmd++; |
| 2203 | str++; |
| 2204 | } |
| 2205 | if (*cmd == '\n') |
| 2206 | cmd++; |
| 2207 | if (*str || *cmd) |
| 2208 | return 0; |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2212 | struct rdev_sysfs_entry { |
| 2213 | struct attribute attr; |
| 2214 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2215 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2216 | }; |
| 2217 | |
| 2218 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2219 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2220 | { |
| 2221 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2222 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2223 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2224 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2225 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2226 | sep = ","; |
| 2227 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2228 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2229 | len += sprintf(page+len, "%sin_sync",sep); |
| 2230 | sep = ","; |
| 2231 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2232 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2233 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2234 | sep = ","; |
| 2235 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2236 | if (test_bit(Blocked, &rdev->flags)) { |
| 2237 | len += sprintf(page+len, "%sblocked", sep); |
| 2238 | sep = ","; |
| 2239 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2240 | if (!test_bit(Faulty, &rdev->flags) && |
| 2241 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2242 | len += sprintf(page+len, "%sspare", sep); |
| 2243 | sep = ","; |
| 2244 | } |
| 2245 | return len+sprintf(page+len, "\n"); |
| 2246 | } |
| 2247 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2248 | static ssize_t |
| 2249 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2250 | { |
| 2251 | /* can write |
| 2252 | * faulty - simulates and error |
| 2253 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2254 | * writemostly - sets write_mostly |
| 2255 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2256 | * blocked - sets the Blocked flag |
| 2257 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2258 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2259 | */ |
| 2260 | int err = -EINVAL; |
| 2261 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2262 | md_error(rdev->mddev, rdev); |
| 2263 | err = 0; |
| 2264 | } else if (cmd_match(buf, "remove")) { |
| 2265 | if (rdev->raid_disk >= 0) |
| 2266 | err = -EBUSY; |
| 2267 | else { |
| 2268 | mddev_t *mddev = rdev->mddev; |
| 2269 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2270 | if (mddev->pers) |
| 2271 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2272 | md_new_event(mddev); |
| 2273 | err = 0; |
| 2274 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2275 | } else if (cmd_match(buf, "writemostly")) { |
| 2276 | set_bit(WriteMostly, &rdev->flags); |
| 2277 | err = 0; |
| 2278 | } else if (cmd_match(buf, "-writemostly")) { |
| 2279 | clear_bit(WriteMostly, &rdev->flags); |
| 2280 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2281 | } else if (cmd_match(buf, "blocked")) { |
| 2282 | set_bit(Blocked, &rdev->flags); |
| 2283 | err = 0; |
| 2284 | } else if (cmd_match(buf, "-blocked")) { |
| 2285 | clear_bit(Blocked, &rdev->flags); |
| 2286 | wake_up(&rdev->blocked_wait); |
| 2287 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2288 | md_wakeup_thread(rdev->mddev->thread); |
| 2289 | |
| 2290 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2291 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2292 | set_bit(In_sync, &rdev->flags); |
| 2293 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2294 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2295 | if (!err && rdev->sysfs_state) |
| 2296 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2297 | return err ? err : len; |
| 2298 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2299 | static struct rdev_sysfs_entry rdev_state = |
| 2300 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2301 | |
| 2302 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2303 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2304 | { |
| 2305 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2306 | } |
| 2307 | |
| 2308 | static ssize_t |
| 2309 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2310 | { |
| 2311 | char *e; |
| 2312 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2313 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2314 | atomic_set(&rdev->corrected_errors, n); |
| 2315 | return len; |
| 2316 | } |
| 2317 | return -EINVAL; |
| 2318 | } |
| 2319 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2320 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2321 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2322 | static ssize_t |
| 2323 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2324 | { |
| 2325 | if (rdev->raid_disk < 0) |
| 2326 | return sprintf(page, "none\n"); |
| 2327 | else |
| 2328 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2329 | } |
| 2330 | |
| 2331 | static ssize_t |
| 2332 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2333 | { |
| 2334 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2335 | int err; |
| 2336 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2337 | int slot = simple_strtoul(buf, &e, 10); |
| 2338 | if (strncmp(buf, "none", 4)==0) |
| 2339 | slot = -1; |
| 2340 | else if (e==buf || (*e && *e!= '\n')) |
| 2341 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2342 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2343 | /* Setting 'slot' on an active array requires also |
| 2344 | * updating the 'rd%d' link, and communicating |
| 2345 | * with the personality with ->hot_*_disk. |
| 2346 | * For now we only support removing |
| 2347 | * failed/spare devices. This normally happens automatically, |
| 2348 | * but not when the metadata is externally managed. |
| 2349 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2350 | if (rdev->raid_disk == -1) |
| 2351 | return -EEXIST; |
| 2352 | /* personality does all needed checks */ |
| 2353 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2354 | return -EINVAL; |
| 2355 | err = rdev->mddev->pers-> |
| 2356 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2357 | if (err) |
| 2358 | return err; |
| 2359 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2360 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2361 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2362 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2363 | } else if (rdev->mddev->pers) { |
| 2364 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2365 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2366 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2367 | */ |
| 2368 | |
| 2369 | if (rdev->raid_disk != -1) |
| 2370 | return -EBUSY; |
| 2371 | |
| 2372 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2373 | return -EINVAL; |
| 2374 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2375 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2376 | if (rdev2->raid_disk == slot) |
| 2377 | return -EEXIST; |
| 2378 | |
| 2379 | rdev->raid_disk = slot; |
| 2380 | if (test_bit(In_sync, &rdev->flags)) |
| 2381 | rdev->saved_raid_disk = slot; |
| 2382 | else |
| 2383 | rdev->saved_raid_disk = -1; |
| 2384 | err = rdev->mddev->pers-> |
| 2385 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2386 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2387 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2388 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2389 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2390 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2391 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2392 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2393 | printk(KERN_WARNING |
| 2394 | "md: cannot register " |
| 2395 | "%s for %s\n", |
| 2396 | nm, mdname(rdev->mddev)); |
| 2397 | |
| 2398 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2399 | } else { |
| 2400 | if (slot >= rdev->mddev->raid_disks) |
| 2401 | return -ENOSPC; |
| 2402 | rdev->raid_disk = slot; |
| 2403 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2404 | clear_bit(Faulty, &rdev->flags); |
| 2405 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2406 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2407 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2408 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2409 | return len; |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2414 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2415 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2416 | static ssize_t |
| 2417 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2418 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2419 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | static ssize_t |
| 2423 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2424 | { |
| 2425 | char *e; |
| 2426 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2427 | if (e==buf || (*e && *e != '\n')) |
| 2428 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2429 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2430 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2431 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2432 | /* Must set offset before size, so overlap checks |
| 2433 | * can be sane */ |
| 2434 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2435 | rdev->data_offset = offset; |
| 2436 | return len; |
| 2437 | } |
| 2438 | |
| 2439 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2440 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2441 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2442 | static ssize_t |
| 2443 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2444 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2445 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2446 | } |
| 2447 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2448 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2449 | { |
| 2450 | /* check if two start/length pairs overlap */ |
| 2451 | if (s1+l1 <= s2) |
| 2452 | return 0; |
| 2453 | if (s2+l2 <= s1) |
| 2454 | return 0; |
| 2455 | return 1; |
| 2456 | } |
| 2457 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2458 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2459 | { |
| 2460 | unsigned long long blocks; |
| 2461 | sector_t new; |
| 2462 | |
| 2463 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2464 | return -EINVAL; |
| 2465 | |
| 2466 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2467 | return -EINVAL; /* sector conversion overflow */ |
| 2468 | |
| 2469 | new = blocks * 2; |
| 2470 | if (new != blocks * 2) |
| 2471 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2472 | |
| 2473 | *sectors = new; |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2477 | static ssize_t |
| 2478 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2479 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2480 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2481 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2482 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2483 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2484 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2485 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2486 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2487 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2488 | sectors = super_types[my_mddev->major_version]. |
| 2489 | rdev_size_change(rdev, sectors); |
| 2490 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2491 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2492 | } else if (!sectors) |
| 2493 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2494 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2495 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2496 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2497 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2498 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2499 | rdev->sectors = sectors; |
| 2500 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2501 | /* need to check that all other rdevs with the same ->bdev |
| 2502 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2503 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2504 | * we have to change it back, we will have the lock again. |
| 2505 | */ |
| 2506 | mddev_t *mddev; |
| 2507 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2508 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2509 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2510 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2511 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2512 | mdk_rdev_t *rdev2; |
| 2513 | |
| 2514 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2515 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2516 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2517 | (rdev->bdev == rdev2->bdev && |
| 2518 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2519 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2520 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2521 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2522 | overlap = 1; |
| 2523 | break; |
| 2524 | } |
| 2525 | mddev_unlock(mddev); |
| 2526 | if (overlap) { |
| 2527 | mddev_put(mddev); |
| 2528 | break; |
| 2529 | } |
| 2530 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2531 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2532 | if (overlap) { |
| 2533 | /* Someone else could have slipped in a size |
| 2534 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2535 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2536 | * safe, and trust userspace not to race with |
| 2537 | * itself |
| 2538 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2539 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2540 | return -EBUSY; |
| 2541 | } |
| 2542 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2543 | return len; |
| 2544 | } |
| 2545 | |
| 2546 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2547 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2548 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2549 | static struct attribute *rdev_default_attrs[] = { |
| 2550 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2551 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2552 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2553 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2554 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2555 | NULL, |
| 2556 | }; |
| 2557 | static ssize_t |
| 2558 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2559 | { |
| 2560 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2561 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2562 | mddev_t *mddev = rdev->mddev; |
| 2563 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2564 | |
| 2565 | if (!entry->show) |
| 2566 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2567 | |
| 2568 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2569 | if (!rv) { |
| 2570 | if (rdev->mddev == NULL) |
| 2571 | rv = -EBUSY; |
| 2572 | else |
| 2573 | rv = entry->show(rdev, page); |
| 2574 | mddev_unlock(mddev); |
| 2575 | } |
| 2576 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | static ssize_t |
| 2580 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2581 | const char *page, size_t length) |
| 2582 | { |
| 2583 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2584 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2585 | ssize_t rv; |
| 2586 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2587 | |
| 2588 | if (!entry->store) |
| 2589 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2590 | if (!capable(CAP_SYS_ADMIN)) |
| 2591 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2592 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2593 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2594 | if (rdev->mddev == NULL) |
| 2595 | rv = -EBUSY; |
| 2596 | else |
| 2597 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2598 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2599 | } |
| 2600 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | static void rdev_free(struct kobject *ko) |
| 2604 | { |
| 2605 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2606 | kfree(rdev); |
| 2607 | } |
| 2608 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2609 | .show = rdev_attr_show, |
| 2610 | .store = rdev_attr_store, |
| 2611 | }; |
| 2612 | static struct kobj_type rdev_ktype = { |
| 2613 | .release = rdev_free, |
| 2614 | .sysfs_ops = &rdev_sysfs_ops, |
| 2615 | .default_attrs = rdev_default_attrs, |
| 2616 | }; |
| 2617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | /* |
| 2619 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2620 | * |
| 2621 | * mark the device faulty if: |
| 2622 | * |
| 2623 | * - the device is nonexistent (zero size) |
| 2624 | * - the device has no valid superblock |
| 2625 | * |
| 2626 | * a faulty rdev _never_ has rdev->sb set. |
| 2627 | */ |
| 2628 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2629 | { |
| 2630 | char b[BDEVNAME_SIZE]; |
| 2631 | int err; |
| 2632 | mdk_rdev_t *rdev; |
| 2633 | sector_t size; |
| 2634 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2635 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | if (!rdev) { |
| 2637 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2638 | return ERR_PTR(-ENOMEM); |
| 2639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | |
| 2641 | if ((err = alloc_disk_sb(rdev))) |
| 2642 | goto abort_free; |
| 2643 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2644 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | if (err) |
| 2646 | goto abort_free; |
| 2647 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2648 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2651 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2652 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2653 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2655 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2657 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2658 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | |
| 2660 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2661 | if (!size) { |
| 2662 | printk(KERN_WARNING |
| 2663 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2664 | bdevname(rdev->bdev,b)); |
| 2665 | err = -EINVAL; |
| 2666 | goto abort_free; |
| 2667 | } |
| 2668 | |
| 2669 | if (super_format >= 0) { |
| 2670 | err = super_types[super_format]. |
| 2671 | load_super(rdev, NULL, super_minor); |
| 2672 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2673 | printk(KERN_WARNING |
| 2674 | "md: %s does not have a valid v%d.%d " |
| 2675 | "superblock, not importing!\n", |
| 2676 | bdevname(rdev->bdev,b), |
| 2677 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | goto abort_free; |
| 2679 | } |
| 2680 | if (err < 0) { |
| 2681 | printk(KERN_WARNING |
| 2682 | "md: could not read %s's sb, not importing!\n", |
| 2683 | bdevname(rdev->bdev,b)); |
| 2684 | goto abort_free; |
| 2685 | } |
| 2686 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2689 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
| 2691 | return rdev; |
| 2692 | |
| 2693 | abort_free: |
| 2694 | if (rdev->sb_page) { |
| 2695 | if (rdev->bdev) |
| 2696 | unlock_rdev(rdev); |
| 2697 | free_disk_sb(rdev); |
| 2698 | } |
| 2699 | kfree(rdev); |
| 2700 | return ERR_PTR(err); |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * Check a full RAID array for plausibility |
| 2705 | */ |
| 2706 | |
| 2707 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2708 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | { |
| 2710 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2711 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | char b[BDEVNAME_SIZE]; |
| 2713 | |
| 2714 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2715 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | switch (super_types[mddev->major_version]. |
| 2717 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2718 | case 1: |
| 2719 | freshest = rdev; |
| 2720 | break; |
| 2721 | case 0: |
| 2722 | break; |
| 2723 | default: |
| 2724 | printk( KERN_ERR \ |
| 2725 | "md: fatal superblock inconsistency in %s" |
| 2726 | " -- removing from array\n", |
| 2727 | bdevname(rdev->bdev,b)); |
| 2728 | kick_rdev_from_array(rdev); |
| 2729 | } |
| 2730 | |
| 2731 | |
| 2732 | super_types[mddev->major_version]. |
| 2733 | validate_super(mddev, freshest); |
| 2734 | |
| 2735 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2736 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2737 | if (rdev->desc_nr >= mddev->max_disks || |
| 2738 | i > mddev->max_disks) { |
| 2739 | printk(KERN_WARNING |
| 2740 | "md: %s: %s: only %d devices permitted\n", |
| 2741 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2742 | mddev->max_disks); |
| 2743 | kick_rdev_from_array(rdev); |
| 2744 | continue; |
| 2745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | if (rdev != freshest) |
| 2747 | if (super_types[mddev->major_version]. |
| 2748 | validate_super(mddev, rdev)) { |
| 2749 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2750 | " from array!\n", |
| 2751 | bdevname(rdev->bdev,b)); |
| 2752 | kick_rdev_from_array(rdev); |
| 2753 | continue; |
| 2754 | } |
| 2755 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2756 | rdev->desc_nr = i++; |
| 2757 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2758 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2759 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2760 | rdev->raid_disk = -1; |
| 2761 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
| 2763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2766 | static void md_safemode_timeout(unsigned long data); |
| 2767 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2768 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2769 | safe_delay_show(mddev_t *mddev, char *page) |
| 2770 | { |
| 2771 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2772 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2773 | } |
| 2774 | static ssize_t |
| 2775 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2776 | { |
| 2777 | int scale=1; |
| 2778 | int dot=0; |
| 2779 | int i; |
| 2780 | unsigned long msec; |
| 2781 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2782 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2783 | /* remove a period, and count digits after it */ |
| 2784 | if (len >= sizeof(buf)) |
| 2785 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2786 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2787 | for (i=0; i<len; i++) { |
| 2788 | if (dot) { |
| 2789 | if (isdigit(buf[i])) { |
| 2790 | buf[i-1] = buf[i]; |
| 2791 | scale *= 10; |
| 2792 | } |
| 2793 | buf[i] = 0; |
| 2794 | } else if (buf[i] == '.') { |
| 2795 | dot=1; |
| 2796 | buf[i] = 0; |
| 2797 | } |
| 2798 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2799 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2800 | return -EINVAL; |
| 2801 | msec = (msec * 1000) / scale; |
| 2802 | if (msec == 0) |
| 2803 | mddev->safemode_delay = 0; |
| 2804 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2805 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2806 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2807 | if (mddev->safemode_delay == 0) |
| 2808 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2809 | if (mddev->safemode_delay < old_delay) |
| 2810 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2811 | } |
| 2812 | return len; |
| 2813 | } |
| 2814 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2815 | __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] | 2816 | |
| 2817 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2818 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2819 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2820 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2821 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2822 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2823 | else if (mddev->clevel[0]) |
| 2824 | return sprintf(page, "%s\n", mddev->clevel); |
| 2825 | else if (mddev->level != LEVEL_NONE) |
| 2826 | return sprintf(page, "%d\n", mddev->level); |
| 2827 | else |
| 2828 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2829 | } |
| 2830 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2831 | static ssize_t |
| 2832 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2833 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2834 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2835 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2836 | struct mdk_personality *pers; |
| 2837 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2838 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2839 | |
| 2840 | if (mddev->pers == NULL) { |
| 2841 | if (len == 0) |
| 2842 | return 0; |
| 2843 | if (len >= sizeof(mddev->clevel)) |
| 2844 | return -ENOSPC; |
| 2845 | strncpy(mddev->clevel, buf, len); |
| 2846 | if (mddev->clevel[len-1] == '\n') |
| 2847 | len--; |
| 2848 | mddev->clevel[len] = 0; |
| 2849 | mddev->level = LEVEL_NONE; |
| 2850 | return rv; |
| 2851 | } |
| 2852 | |
| 2853 | /* request to change the personality. Need to ensure: |
| 2854 | * - array is not engaged in resync/recovery/reshape |
| 2855 | * - old personality can be suspended |
| 2856 | * - new personality will access other array. |
| 2857 | */ |
| 2858 | |
| 2859 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2860 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2861 | |
| 2862 | if (!mddev->pers->quiesce) { |
| 2863 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2864 | mdname(mddev), mddev->pers->name); |
| 2865 | return -EINVAL; |
| 2866 | } |
| 2867 | |
| 2868 | /* Now find the new personality */ |
| 2869 | if (len == 0 || len >= sizeof(level)) |
| 2870 | return -EINVAL; |
| 2871 | strncpy(level, buf, len); |
| 2872 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2873 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2874 | level[len] = 0; |
| 2875 | |
| 2876 | request_module("md-%s", level); |
| 2877 | spin_lock(&pers_lock); |
| 2878 | pers = find_pers(LEVEL_NONE, level); |
| 2879 | if (!pers || !try_module_get(pers->owner)) { |
| 2880 | spin_unlock(&pers_lock); |
| 2881 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2882 | return -EINVAL; |
| 2883 | } |
| 2884 | spin_unlock(&pers_lock); |
| 2885 | |
| 2886 | if (pers == mddev->pers) { |
| 2887 | /* Nothing to do! */ |
| 2888 | module_put(pers->owner); |
| 2889 | return rv; |
| 2890 | } |
| 2891 | if (!pers->takeover) { |
| 2892 | module_put(pers->owner); |
| 2893 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2894 | mdname(mddev), level); |
| 2895 | return -EINVAL; |
| 2896 | } |
| 2897 | |
| 2898 | /* ->takeover must set new_* and/or delta_disks |
| 2899 | * if it succeeds, and may set them when it fails. |
| 2900 | */ |
| 2901 | priv = pers->takeover(mddev); |
| 2902 | if (IS_ERR(priv)) { |
| 2903 | mddev->new_level = mddev->level; |
| 2904 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2905 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2906 | mddev->raid_disks -= mddev->delta_disks; |
| 2907 | mddev->delta_disks = 0; |
| 2908 | module_put(pers->owner); |
| 2909 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2910 | mdname(mddev), level); |
| 2911 | return PTR_ERR(priv); |
| 2912 | } |
| 2913 | |
| 2914 | /* Looks like we have a winner */ |
| 2915 | mddev_suspend(mddev); |
| 2916 | mddev->pers->stop(mddev); |
| 2917 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2918 | /* Invalidate devices that are now superfluous */ |
| 2919 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2920 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2921 | rdev->raid_disk = -1; |
| 2922 | clear_bit(In_sync, &rdev->flags); |
| 2923 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2924 | mddev->pers = pers; |
| 2925 | mddev->private = priv; |
| 2926 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2927 | mddev->level = mddev->new_level; |
| 2928 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2929 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2930 | mddev->delta_disks = 0; |
| 2931 | pers->run(mddev); |
| 2932 | mddev_resume(mddev); |
| 2933 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2934 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2935 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2936 | return rv; |
| 2937 | } |
| 2938 | |
| 2939 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2940 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2941 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2942 | |
| 2943 | static ssize_t |
| 2944 | layout_show(mddev_t *mddev, char *page) |
| 2945 | { |
| 2946 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2947 | if (mddev->reshape_position != MaxSector && |
| 2948 | mddev->layout != mddev->new_layout) |
| 2949 | return sprintf(page, "%d (%d)\n", |
| 2950 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2951 | return sprintf(page, "%d\n", mddev->layout); |
| 2952 | } |
| 2953 | |
| 2954 | static ssize_t |
| 2955 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2956 | { |
| 2957 | char *e; |
| 2958 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2959 | |
| 2960 | if (!*buf || (*e && *e != '\n')) |
| 2961 | return -EINVAL; |
| 2962 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2963 | if (mddev->pers) { |
| 2964 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2965 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2966 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2967 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2968 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2969 | if (err) { |
| 2970 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2971 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2972 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2973 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2974 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2975 | if (mddev->reshape_position == MaxSector) |
| 2976 | mddev->layout = n; |
| 2977 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2978 | return len; |
| 2979 | } |
| 2980 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2981 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2982 | |
| 2983 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2984 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2985 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2986 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2987 | if (mddev->raid_disks == 0) |
| 2988 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2989 | if (mddev->reshape_position != MaxSector && |
| 2990 | mddev->delta_disks != 0) |
| 2991 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2992 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2993 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2994 | } |
| 2995 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2996 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2997 | |
| 2998 | static ssize_t |
| 2999 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3000 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3001 | char *e; |
| 3002 | int rv = 0; |
| 3003 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3004 | |
| 3005 | if (!*buf || (*e && *e != '\n')) |
| 3006 | return -EINVAL; |
| 3007 | |
| 3008 | if (mddev->pers) |
| 3009 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3010 | else if (mddev->reshape_position != MaxSector) { |
| 3011 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3012 | mddev->delta_disks = n - olddisks; |
| 3013 | mddev->raid_disks = n; |
| 3014 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3015 | mddev->raid_disks = n; |
| 3016 | return rv ? rv : len; |
| 3017 | } |
| 3018 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3019 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3020 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3021 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3022 | chunk_size_show(mddev_t *mddev, char *page) |
| 3023 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3024 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3025 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3026 | return sprintf(page, "%d (%d)\n", |
| 3027 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3028 | mddev->chunk_sectors << 9); |
| 3029 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | static ssize_t |
| 3033 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3034 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3035 | char *e; |
| 3036 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3037 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3038 | if (!*buf || (*e && *e != '\n')) |
| 3039 | return -EINVAL; |
| 3040 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3041 | if (mddev->pers) { |
| 3042 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3043 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3044 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3045 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3046 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3047 | if (err) { |
| 3048 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3049 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3050 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3051 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3052 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3053 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3054 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3055 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3056 | return len; |
| 3057 | } |
| 3058 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3059 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3060 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3061 | static ssize_t |
| 3062 | resync_start_show(mddev_t *mddev, char *page) |
| 3063 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3064 | if (mddev->recovery_cp == MaxSector) |
| 3065 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3066 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3067 | } |
| 3068 | |
| 3069 | static ssize_t |
| 3070 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3071 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3072 | char *e; |
| 3073 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3074 | |
| 3075 | if (mddev->pers) |
| 3076 | return -EBUSY; |
| 3077 | if (!*buf || (*e && *e != '\n')) |
| 3078 | return -EINVAL; |
| 3079 | |
| 3080 | mddev->recovery_cp = n; |
| 3081 | return len; |
| 3082 | } |
| 3083 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3084 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3085 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3086 | /* |
| 3087 | * The array state can be: |
| 3088 | * |
| 3089 | * clear |
| 3090 | * No devices, no size, no level |
| 3091 | * Equivalent to STOP_ARRAY ioctl |
| 3092 | * inactive |
| 3093 | * May have some settings, but array is not active |
| 3094 | * all IO results in error |
| 3095 | * When written, doesn't tear down array, but just stops it |
| 3096 | * suspended (not supported yet) |
| 3097 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3098 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3099 | * readonly |
| 3100 | * no resync can happen. no superblocks get written. |
| 3101 | * write requests fail |
| 3102 | * read-auto |
| 3103 | * like readonly, but behaves like 'clean' on a write request. |
| 3104 | * |
| 3105 | * clean - no pending writes, but otherwise active. |
| 3106 | * When written to inactive array, starts without resync |
| 3107 | * If a write request arrives then |
| 3108 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3109 | * if not known, block and switch to write-pending |
| 3110 | * If written to an active array that has pending writes, then fails. |
| 3111 | * active |
| 3112 | * fully active: IO and resync can be happening. |
| 3113 | * When written to inactive array, starts with resync |
| 3114 | * |
| 3115 | * write-pending |
| 3116 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3117 | * |
| 3118 | * active-idle |
| 3119 | * like active, but no writes have been seen for a while (100msec). |
| 3120 | * |
| 3121 | */ |
| 3122 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3123 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3124 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3125 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3126 | "write-pending", "active-idle", NULL }; |
| 3127 | |
| 3128 | static int match_word(const char *word, char **list) |
| 3129 | { |
| 3130 | int n; |
| 3131 | for (n=0; list[n]; n++) |
| 3132 | if (cmd_match(word, list[n])) |
| 3133 | break; |
| 3134 | return n; |
| 3135 | } |
| 3136 | |
| 3137 | static ssize_t |
| 3138 | array_state_show(mddev_t *mddev, char *page) |
| 3139 | { |
| 3140 | enum array_state st = inactive; |
| 3141 | |
| 3142 | if (mddev->pers) |
| 3143 | switch(mddev->ro) { |
| 3144 | case 1: |
| 3145 | st = readonly; |
| 3146 | break; |
| 3147 | case 2: |
| 3148 | st = read_auto; |
| 3149 | break; |
| 3150 | case 0: |
| 3151 | if (mddev->in_sync) |
| 3152 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3153 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3154 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3155 | else if (mddev->safemode) |
| 3156 | st = active_idle; |
| 3157 | else |
| 3158 | st = active; |
| 3159 | } |
| 3160 | else { |
| 3161 | if (list_empty(&mddev->disks) && |
| 3162 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3163 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3164 | st = clear; |
| 3165 | else |
| 3166 | st = inactive; |
| 3167 | } |
| 3168 | return sprintf(page, "%s\n", array_states[st]); |
| 3169 | } |
| 3170 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3171 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3172 | static int do_md_run(mddev_t * mddev); |
| 3173 | static int restart_array(mddev_t *mddev); |
| 3174 | |
| 3175 | static ssize_t |
| 3176 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3177 | { |
| 3178 | int err = -EINVAL; |
| 3179 | enum array_state st = match_word(buf, array_states); |
| 3180 | switch(st) { |
| 3181 | case bad_word: |
| 3182 | break; |
| 3183 | case clear: |
| 3184 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3185 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3186 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3187 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3188 | break; |
| 3189 | case inactive: |
| 3190 | /* stopping an active array */ |
| 3191 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3192 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3193 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3194 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3195 | } else |
| 3196 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3197 | break; |
| 3198 | case suspended: |
| 3199 | break; /* not supported yet */ |
| 3200 | case readonly: |
| 3201 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3202 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3203 | else { |
| 3204 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3205 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3206 | err = do_md_run(mddev); |
| 3207 | } |
| 3208 | break; |
| 3209 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3210 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3211 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3212 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3213 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3214 | err = restart_array(mddev); |
| 3215 | if (err == 0) { |
| 3216 | mddev->ro = 2; |
| 3217 | set_disk_ro(mddev->gendisk, 0); |
| 3218 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3219 | } else { |
| 3220 | mddev->ro = 2; |
| 3221 | err = do_md_run(mddev); |
| 3222 | } |
| 3223 | break; |
| 3224 | case clean: |
| 3225 | if (mddev->pers) { |
| 3226 | restart_array(mddev); |
| 3227 | spin_lock_irq(&mddev->write_lock); |
| 3228 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3229 | if (mddev->in_sync == 0) { |
| 3230 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3231 | if (mddev->safemode == 1) |
| 3232 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3233 | if (mddev->persistent) |
| 3234 | set_bit(MD_CHANGE_CLEAN, |
| 3235 | &mddev->flags); |
| 3236 | } |
| 3237 | err = 0; |
| 3238 | } else |
| 3239 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3240 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3241 | } else |
| 3242 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3243 | break; |
| 3244 | case active: |
| 3245 | if (mddev->pers) { |
| 3246 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3247 | if (mddev->external) |
| 3248 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3249 | wake_up(&mddev->sb_wait); |
| 3250 | err = 0; |
| 3251 | } else { |
| 3252 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3253 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3254 | err = do_md_run(mddev); |
| 3255 | } |
| 3256 | break; |
| 3257 | case write_pending: |
| 3258 | case active_idle: |
| 3259 | /* these cannot be set */ |
| 3260 | break; |
| 3261 | } |
| 3262 | if (err) |
| 3263 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3264 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3265 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3266 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3267 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3268 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3269 | static struct md_sysfs_entry md_array_state = |
| 3270 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3271 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3272 | static ssize_t |
| 3273 | null_show(mddev_t *mddev, char *page) |
| 3274 | { |
| 3275 | return -EINVAL; |
| 3276 | } |
| 3277 | |
| 3278 | static ssize_t |
| 3279 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3280 | { |
| 3281 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3282 | /* The new device is added to the array. |
| 3283 | * If the array has a persistent superblock, we read the |
| 3284 | * superblock to initialise info and check validity. |
| 3285 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3286 | * which mainly checks size. |
| 3287 | */ |
| 3288 | char *e; |
| 3289 | int major = simple_strtoul(buf, &e, 10); |
| 3290 | int minor; |
| 3291 | dev_t dev; |
| 3292 | mdk_rdev_t *rdev; |
| 3293 | int err; |
| 3294 | |
| 3295 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3296 | return -EINVAL; |
| 3297 | minor = simple_strtoul(e+1, &e, 10); |
| 3298 | if (*e && *e != '\n') |
| 3299 | return -EINVAL; |
| 3300 | dev = MKDEV(major, minor); |
| 3301 | if (major != MAJOR(dev) || |
| 3302 | minor != MINOR(dev)) |
| 3303 | return -EOVERFLOW; |
| 3304 | |
| 3305 | |
| 3306 | if (mddev->persistent) { |
| 3307 | rdev = md_import_device(dev, mddev->major_version, |
| 3308 | mddev->minor_version); |
| 3309 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3310 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3311 | mdk_rdev_t, same_set); |
| 3312 | err = super_types[mddev->major_version] |
| 3313 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3314 | if (err < 0) |
| 3315 | goto out; |
| 3316 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3317 | } else if (mddev->external) |
| 3318 | rdev = md_import_device(dev, -2, -1); |
| 3319 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3320 | rdev = md_import_device(dev, -1, -1); |
| 3321 | |
| 3322 | if (IS_ERR(rdev)) |
| 3323 | return PTR_ERR(rdev); |
| 3324 | err = bind_rdev_to_array(rdev, mddev); |
| 3325 | out: |
| 3326 | if (err) |
| 3327 | export_rdev(rdev); |
| 3328 | return err ? err : len; |
| 3329 | } |
| 3330 | |
| 3331 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3332 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3333 | |
| 3334 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3335 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3336 | { |
| 3337 | char *end; |
| 3338 | unsigned long chunk, end_chunk; |
| 3339 | |
| 3340 | if (!mddev->bitmap) |
| 3341 | goto out; |
| 3342 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3343 | while (*buf) { |
| 3344 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3345 | if (buf == end) break; |
| 3346 | if (*end == '-') { /* range */ |
| 3347 | buf = end + 1; |
| 3348 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3349 | if (buf == end) break; |
| 3350 | } |
| 3351 | if (*end && !isspace(*end)) break; |
| 3352 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3353 | buf = end; |
| 3354 | while (isspace(*buf)) buf++; |
| 3355 | } |
| 3356 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3357 | out: |
| 3358 | return len; |
| 3359 | } |
| 3360 | |
| 3361 | static struct md_sysfs_entry md_bitmap = |
| 3362 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3363 | |
| 3364 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3365 | size_show(mddev_t *mddev, char *page) |
| 3366 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3367 | return sprintf(page, "%llu\n", |
| 3368 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3371 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3372 | |
| 3373 | static ssize_t |
| 3374 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3375 | { |
| 3376 | /* If array is inactive, we can reduce the component size, but |
| 3377 | * not increase it (except from 0). |
| 3378 | * If array is active, we can try an on-line resize |
| 3379 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3380 | sector_t sectors; |
| 3381 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3382 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3383 | if (err < 0) |
| 3384 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3385 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3386 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3387 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3388 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3389 | if (mddev->dev_sectors == 0 || |
| 3390 | mddev->dev_sectors > sectors) |
| 3391 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3392 | else |
| 3393 | err = -ENOSPC; |
| 3394 | } |
| 3395 | return err ? err : len; |
| 3396 | } |
| 3397 | |
| 3398 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3399 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3400 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3401 | |
| 3402 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3403 | * This is one of |
| 3404 | * 'none' for arrays with no metadata (good luck...) |
| 3405 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3406 | * or N.M for internally known formats |
| 3407 | */ |
| 3408 | static ssize_t |
| 3409 | metadata_show(mddev_t *mddev, char *page) |
| 3410 | { |
| 3411 | if (mddev->persistent) |
| 3412 | return sprintf(page, "%d.%d\n", |
| 3413 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3414 | else if (mddev->external) |
| 3415 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3416 | else |
| 3417 | return sprintf(page, "none\n"); |
| 3418 | } |
| 3419 | |
| 3420 | static ssize_t |
| 3421 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3422 | { |
| 3423 | int major, minor; |
| 3424 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3425 | /* Changing the details of 'external' metadata is |
| 3426 | * always permitted. Otherwise there must be |
| 3427 | * no devices attached to the array. |
| 3428 | */ |
| 3429 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3430 | ; |
| 3431 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3432 | return -EBUSY; |
| 3433 | |
| 3434 | if (cmd_match(buf, "none")) { |
| 3435 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3436 | mddev->external = 0; |
| 3437 | mddev->major_version = 0; |
| 3438 | mddev->minor_version = 90; |
| 3439 | return len; |
| 3440 | } |
| 3441 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3442 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3443 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3444 | namelen = sizeof(mddev->metadata_type)-1; |
| 3445 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3446 | mddev->metadata_type[namelen] = 0; |
| 3447 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3448 | mddev->metadata_type[--namelen] = 0; |
| 3449 | mddev->persistent = 0; |
| 3450 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3451 | mddev->major_version = 0; |
| 3452 | mddev->minor_version = 90; |
| 3453 | return len; |
| 3454 | } |
| 3455 | major = simple_strtoul(buf, &e, 10); |
| 3456 | if (e==buf || *e != '.') |
| 3457 | return -EINVAL; |
| 3458 | buf = e+1; |
| 3459 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3460 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3461 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3462 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3463 | return -ENOENT; |
| 3464 | mddev->major_version = major; |
| 3465 | mddev->minor_version = minor; |
| 3466 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3467 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3468 | return len; |
| 3469 | } |
| 3470 | |
| 3471 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3472 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3473 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3474 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3475 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3476 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3477 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3478 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3479 | type = "frozen"; |
| 3480 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3481 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3482 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3483 | type = "reshape"; |
| 3484 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3485 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3486 | type = "resync"; |
| 3487 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3488 | type = "check"; |
| 3489 | else |
| 3490 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3491 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3492 | type = "recover"; |
| 3493 | } |
| 3494 | return sprintf(page, "%s\n", type); |
| 3495 | } |
| 3496 | |
| 3497 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3498 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3499 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3500 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3501 | return -EINVAL; |
| 3502 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3503 | if (cmd_match(page, "frozen")) |
| 3504 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3505 | else |
| 3506 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3507 | |
| 3508 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3509 | if (mddev->sync_thread) { |
| 3510 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3511 | md_unregister_thread(mddev->sync_thread); |
| 3512 | mddev->sync_thread = NULL; |
| 3513 | mddev->recovery = 0; |
| 3514 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3515 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3516 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3517 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3518 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3519 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3520 | else if (cmd_match(page, "recover")) { |
| 3521 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3522 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3523 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3524 | int err; |
| 3525 | if (mddev->pers->start_reshape == NULL) |
| 3526 | return -EINVAL; |
| 3527 | err = mddev->pers->start_reshape(mddev); |
| 3528 | if (err) |
| 3529 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3530 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3531 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3532 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3533 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3534 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3535 | return -EINVAL; |
| 3536 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3537 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3538 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3539 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3540 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3541 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3542 | return len; |
| 3543 | } |
| 3544 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3545 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3546 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3547 | { |
| 3548 | return sprintf(page, "%llu\n", |
| 3549 | (unsigned long long) mddev->resync_mismatches); |
| 3550 | } |
| 3551 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3552 | static struct md_sysfs_entry md_scan_mode = |
| 3553 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3554 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3555 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3556 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3557 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3558 | static ssize_t |
| 3559 | sync_min_show(mddev_t *mddev, char *page) |
| 3560 | { |
| 3561 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3562 | mddev->sync_speed_min ? "local": "system"); |
| 3563 | } |
| 3564 | |
| 3565 | static ssize_t |
| 3566 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3567 | { |
| 3568 | int min; |
| 3569 | char *e; |
| 3570 | if (strncmp(buf, "system", 6)==0) { |
| 3571 | mddev->sync_speed_min = 0; |
| 3572 | return len; |
| 3573 | } |
| 3574 | min = simple_strtoul(buf, &e, 10); |
| 3575 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3576 | return -EINVAL; |
| 3577 | mddev->sync_speed_min = min; |
| 3578 | return len; |
| 3579 | } |
| 3580 | |
| 3581 | static struct md_sysfs_entry md_sync_min = |
| 3582 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3583 | |
| 3584 | static ssize_t |
| 3585 | sync_max_show(mddev_t *mddev, char *page) |
| 3586 | { |
| 3587 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3588 | mddev->sync_speed_max ? "local": "system"); |
| 3589 | } |
| 3590 | |
| 3591 | static ssize_t |
| 3592 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3593 | { |
| 3594 | int max; |
| 3595 | char *e; |
| 3596 | if (strncmp(buf, "system", 6)==0) { |
| 3597 | mddev->sync_speed_max = 0; |
| 3598 | return len; |
| 3599 | } |
| 3600 | max = simple_strtoul(buf, &e, 10); |
| 3601 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3602 | return -EINVAL; |
| 3603 | mddev->sync_speed_max = max; |
| 3604 | return len; |
| 3605 | } |
| 3606 | |
| 3607 | static struct md_sysfs_entry md_sync_max = |
| 3608 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3609 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3610 | static ssize_t |
| 3611 | degraded_show(mddev_t *mddev, char *page) |
| 3612 | { |
| 3613 | return sprintf(page, "%d\n", mddev->degraded); |
| 3614 | } |
| 3615 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3616 | |
| 3617 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3618 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3619 | { |
| 3620 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3621 | } |
| 3622 | |
| 3623 | static ssize_t |
| 3624 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3625 | { |
| 3626 | long n; |
| 3627 | |
| 3628 | if (strict_strtol(buf, 10, &n)) |
| 3629 | return -EINVAL; |
| 3630 | |
| 3631 | if (n != 0 && n != 1) |
| 3632 | return -EINVAL; |
| 3633 | |
| 3634 | mddev->parallel_resync = n; |
| 3635 | |
| 3636 | if (mddev->sync_thread) |
| 3637 | wake_up(&resync_wait); |
| 3638 | |
| 3639 | return len; |
| 3640 | } |
| 3641 | |
| 3642 | /* force parallel resync, even with shared block devices */ |
| 3643 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3644 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3645 | sync_force_parallel_show, sync_force_parallel_store); |
| 3646 | |
| 3647 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3648 | sync_speed_show(mddev_t *mddev, char *page) |
| 3649 | { |
| 3650 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3651 | if (mddev->curr_resync == 0) |
| 3652 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3653 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3654 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3655 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3656 | db = resync - mddev->resync_mark_cnt; |
| 3657 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3658 | } |
| 3659 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3660 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3661 | |
| 3662 | static ssize_t |
| 3663 | sync_completed_show(mddev_t *mddev, char *page) |
| 3664 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3665 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3666 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3667 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3668 | return sprintf(page, "none\n"); |
| 3669 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3670 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3671 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3672 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3673 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3674 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3675 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3676 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3677 | } |
| 3678 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3679 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3680 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3681 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3682 | min_sync_show(mddev_t *mddev, char *page) |
| 3683 | { |
| 3684 | return sprintf(page, "%llu\n", |
| 3685 | (unsigned long long)mddev->resync_min); |
| 3686 | } |
| 3687 | static ssize_t |
| 3688 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3689 | { |
| 3690 | unsigned long long min; |
| 3691 | if (strict_strtoull(buf, 10, &min)) |
| 3692 | return -EINVAL; |
| 3693 | if (min > mddev->resync_max) |
| 3694 | return -EINVAL; |
| 3695 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3696 | return -EBUSY; |
| 3697 | |
| 3698 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3699 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3700 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3701 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3702 | return -EINVAL; |
| 3703 | } |
| 3704 | mddev->resync_min = min; |
| 3705 | |
| 3706 | return len; |
| 3707 | } |
| 3708 | |
| 3709 | static struct md_sysfs_entry md_min_sync = |
| 3710 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3711 | |
| 3712 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3713 | max_sync_show(mddev_t *mddev, char *page) |
| 3714 | { |
| 3715 | if (mddev->resync_max == MaxSector) |
| 3716 | return sprintf(page, "max\n"); |
| 3717 | else |
| 3718 | return sprintf(page, "%llu\n", |
| 3719 | (unsigned long long)mddev->resync_max); |
| 3720 | } |
| 3721 | static ssize_t |
| 3722 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3723 | { |
| 3724 | if (strncmp(buf, "max", 3) == 0) |
| 3725 | mddev->resync_max = MaxSector; |
| 3726 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3727 | unsigned long long max; |
| 3728 | if (strict_strtoull(buf, 10, &max)) |
| 3729 | return -EINVAL; |
| 3730 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3731 | return -EINVAL; |
| 3732 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3733 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3734 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3735 | return -EBUSY; |
| 3736 | |
| 3737 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3738 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3739 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3740 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3741 | return -EINVAL; |
| 3742 | } |
| 3743 | mddev->resync_max = max; |
| 3744 | } |
| 3745 | wake_up(&mddev->recovery_wait); |
| 3746 | return len; |
| 3747 | } |
| 3748 | |
| 3749 | static struct md_sysfs_entry md_max_sync = |
| 3750 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3751 | |
| 3752 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3753 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3754 | { |
| 3755 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3756 | } |
| 3757 | |
| 3758 | static ssize_t |
| 3759 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3760 | { |
| 3761 | char *e; |
| 3762 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3763 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3764 | if (mddev->pers == NULL || |
| 3765 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3766 | return -EINVAL; |
| 3767 | if (buf == e || (*e && *e != '\n')) |
| 3768 | return -EINVAL; |
| 3769 | if (new >= mddev->suspend_hi || |
| 3770 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3771 | mddev->suspend_lo = new; |
| 3772 | mddev->pers->quiesce(mddev, 2); |
| 3773 | return len; |
| 3774 | } else |
| 3775 | return -EINVAL; |
| 3776 | } |
| 3777 | static struct md_sysfs_entry md_suspend_lo = |
| 3778 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3779 | |
| 3780 | |
| 3781 | static ssize_t |
| 3782 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3783 | { |
| 3784 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3785 | } |
| 3786 | |
| 3787 | static ssize_t |
| 3788 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3789 | { |
| 3790 | char *e; |
| 3791 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3792 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3793 | if (mddev->pers == NULL || |
| 3794 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3795 | return -EINVAL; |
| 3796 | if (buf == e || (*e && *e != '\n')) |
| 3797 | return -EINVAL; |
| 3798 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3799 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3800 | mddev->suspend_hi = new; |
| 3801 | mddev->pers->quiesce(mddev, 1); |
| 3802 | mddev->pers->quiesce(mddev, 0); |
| 3803 | return len; |
| 3804 | } else |
| 3805 | return -EINVAL; |
| 3806 | } |
| 3807 | static struct md_sysfs_entry md_suspend_hi = |
| 3808 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3809 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3810 | static ssize_t |
| 3811 | reshape_position_show(mddev_t *mddev, char *page) |
| 3812 | { |
| 3813 | if (mddev->reshape_position != MaxSector) |
| 3814 | return sprintf(page, "%llu\n", |
| 3815 | (unsigned long long)mddev->reshape_position); |
| 3816 | strcpy(page, "none\n"); |
| 3817 | return 5; |
| 3818 | } |
| 3819 | |
| 3820 | static ssize_t |
| 3821 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3822 | { |
| 3823 | char *e; |
| 3824 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3825 | if (mddev->pers) |
| 3826 | return -EBUSY; |
| 3827 | if (buf == e || (*e && *e != '\n')) |
| 3828 | return -EINVAL; |
| 3829 | mddev->reshape_position = new; |
| 3830 | mddev->delta_disks = 0; |
| 3831 | mddev->new_level = mddev->level; |
| 3832 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3833 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3834 | return len; |
| 3835 | } |
| 3836 | |
| 3837 | static struct md_sysfs_entry md_reshape_position = |
| 3838 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3839 | reshape_position_store); |
| 3840 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3841 | static ssize_t |
| 3842 | array_size_show(mddev_t *mddev, char *page) |
| 3843 | { |
| 3844 | if (mddev->external_size) |
| 3845 | return sprintf(page, "%llu\n", |
| 3846 | (unsigned long long)mddev->array_sectors/2); |
| 3847 | else |
| 3848 | return sprintf(page, "default\n"); |
| 3849 | } |
| 3850 | |
| 3851 | static ssize_t |
| 3852 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3853 | { |
| 3854 | sector_t sectors; |
| 3855 | |
| 3856 | if (strncmp(buf, "default", 7) == 0) { |
| 3857 | if (mddev->pers) |
| 3858 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3859 | else |
| 3860 | sectors = mddev->array_sectors; |
| 3861 | |
| 3862 | mddev->external_size = 0; |
| 3863 | } else { |
| 3864 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3865 | return -EINVAL; |
| 3866 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3867 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3868 | |
| 3869 | mddev->external_size = 1; |
| 3870 | } |
| 3871 | |
| 3872 | mddev->array_sectors = sectors; |
| 3873 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3874 | if (mddev->pers) |
| 3875 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3876 | |
| 3877 | return len; |
| 3878 | } |
| 3879 | |
| 3880 | static struct md_sysfs_entry md_array_size = |
| 3881 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3882 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3883 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3884 | static struct attribute *md_default_attrs[] = { |
| 3885 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3886 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3887 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3888 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3889 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3890 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3891 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3892 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3893 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3894 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3895 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3896 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3897 | NULL, |
| 3898 | }; |
| 3899 | |
| 3900 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3901 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3902 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3903 | &md_sync_min.attr, |
| 3904 | &md_sync_max.attr, |
| 3905 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3906 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3907 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3908 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3909 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3910 | &md_suspend_lo.attr, |
| 3911 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3912 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3913 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3914 | NULL, |
| 3915 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3916 | static struct attribute_group md_redundancy_group = { |
| 3917 | .name = NULL, |
| 3918 | .attrs = md_redundancy_attrs, |
| 3919 | }; |
| 3920 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3921 | |
| 3922 | static ssize_t |
| 3923 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3924 | { |
| 3925 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3926 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3927 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3928 | |
| 3929 | if (!entry->show) |
| 3930 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3931 | rv = mddev_lock(mddev); |
| 3932 | if (!rv) { |
| 3933 | rv = entry->show(mddev, page); |
| 3934 | mddev_unlock(mddev); |
| 3935 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3936 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | static ssize_t |
| 3940 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3941 | const char *page, size_t length) |
| 3942 | { |
| 3943 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3944 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3945 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3946 | |
| 3947 | if (!entry->store) |
| 3948 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3949 | if (!capable(CAP_SYS_ADMIN)) |
| 3950 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3951 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3952 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3953 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3954 | if (!rv) { |
| 3955 | rv = entry->store(mddev, page, length); |
| 3956 | mddev_unlock(mddev); |
| 3957 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3958 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | static void md_free(struct kobject *ko) |
| 3962 | { |
| 3963 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3964 | |
| 3965 | if (mddev->sysfs_state) |
| 3966 | sysfs_put(mddev->sysfs_state); |
| 3967 | |
| 3968 | if (mddev->gendisk) { |
| 3969 | del_gendisk(mddev->gendisk); |
| 3970 | put_disk(mddev->gendisk); |
| 3971 | } |
| 3972 | if (mddev->queue) |
| 3973 | blk_cleanup_queue(mddev->queue); |
| 3974 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3975 | kfree(mddev); |
| 3976 | } |
| 3977 | |
| 3978 | static struct sysfs_ops md_sysfs_ops = { |
| 3979 | .show = md_attr_show, |
| 3980 | .store = md_attr_store, |
| 3981 | }; |
| 3982 | static struct kobj_type md_ktype = { |
| 3983 | .release = md_free, |
| 3984 | .sysfs_ops = &md_sysfs_ops, |
| 3985 | .default_attrs = md_default_attrs, |
| 3986 | }; |
| 3987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | int mdp_major = 0; |
| 3989 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3990 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3991 | { |
| 3992 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3993 | |
| 3994 | if (mddev->private == &md_redundancy_group) { |
| 3995 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3996 | if (mddev->sysfs_action) |
| 3997 | sysfs_put(mddev->sysfs_action); |
| 3998 | mddev->sysfs_action = NULL; |
| 3999 | mddev->private = NULL; |
| 4000 | } |
| 4001 | kobject_del(&mddev->kobj); |
| 4002 | kobject_put(&mddev->kobj); |
| 4003 | } |
| 4004 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4005 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4007 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | mddev_t *mddev = mddev_find(dev); |
| 4009 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4010 | int partitioned; |
| 4011 | int shift; |
| 4012 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4013 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
| 4015 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4016 | return -ENODEV; |
| 4017 | |
| 4018 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4019 | shift = partitioned ? MdpMinorShift : 0; |
| 4020 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4022 | /* wait for any previous instance if this device |
| 4023 | * to be completed removed (mddev_delayed_delete). |
| 4024 | */ |
| 4025 | flush_scheduled_work(); |
| 4026 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4027 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4028 | error = -EEXIST; |
| 4029 | if (mddev->gendisk) |
| 4030 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4031 | |
| 4032 | if (name) { |
| 4033 | /* Need to ensure that 'name' is not a duplicate. |
| 4034 | */ |
| 4035 | mddev_t *mddev2; |
| 4036 | spin_lock(&all_mddevs_lock); |
| 4037 | |
| 4038 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4039 | if (mddev2->gendisk && |
| 4040 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4041 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4042 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4043 | } |
| 4044 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4046 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4047 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4048 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4049 | if (!mddev->queue) |
| 4050 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4051 | mddev->queue->queuedata = mddev; |
| 4052 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4053 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4054 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4055 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4056 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | disk = alloc_disk(1 << shift); |
| 4059 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4060 | blk_cleanup_queue(mddev->queue); |
| 4061 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4062 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4064 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4066 | if (name) |
| 4067 | strcpy(disk->disk_name, name); |
| 4068 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4070 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | disk->fops = &md_fops; |
| 4073 | disk->private_data = mddev; |
| 4074 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4075 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4076 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4077 | * remove it now. |
| 4078 | */ |
| 4079 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | add_disk(disk); |
| 4081 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4082 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4083 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4084 | if (error) { |
| 4085 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4086 | * __must_check, we must do something with the result |
| 4087 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4088 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4089 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4090 | error = 0; |
| 4091 | } |
| 4092 | abort: |
| 4093 | mutex_unlock(&disks_mutex); |
| 4094 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4095 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4096 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4097 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4098 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4099 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4103 | { |
| 4104 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | return NULL; |
| 4106 | } |
| 4107 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4108 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4109 | { |
| 4110 | /* val must be "md_*" where * is not all digits. |
| 4111 | * We allocate an array with a large free minor number, and |
| 4112 | * set the name to val. val must not already be an active name. |
| 4113 | */ |
| 4114 | int len = strlen(val); |
| 4115 | char buf[DISK_NAME_LEN]; |
| 4116 | |
| 4117 | while (len && val[len-1] == '\n') |
| 4118 | len--; |
| 4119 | if (len >= DISK_NAME_LEN) |
| 4120 | return -E2BIG; |
| 4121 | strlcpy(buf, val, len+1); |
| 4122 | if (strncmp(buf, "md_", 3) != 0) |
| 4123 | return -EINVAL; |
| 4124 | return md_alloc(0, buf); |
| 4125 | } |
| 4126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | static void md_safemode_timeout(unsigned long data) |
| 4128 | { |
| 4129 | mddev_t *mddev = (mddev_t *) data; |
| 4130 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4131 | if (!atomic_read(&mddev->writes_pending)) { |
| 4132 | mddev->safemode = 1; |
| 4133 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4134 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | md_wakeup_thread(mddev->thread); |
| 4137 | } |
| 4138 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4139 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | |
| 4141 | static int do_md_run(mddev_t * mddev) |
| 4142 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4143 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | mdk_rdev_t *rdev; |
| 4145 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4146 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4148 | if (list_empty(&mddev->disks)) |
| 4149 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | |
| 4152 | if (mddev->pers) |
| 4153 | return -EBUSY; |
| 4154 | |
| 4155 | /* |
| 4156 | * Analyze all RAID superblock(s) |
| 4157 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4158 | if (!mddev->raid_disks) { |
| 4159 | if (!mddev->persistent) |
| 4160 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4161 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4164 | if (mddev->level != LEVEL_NONE) |
| 4165 | request_module("md-level-%d", mddev->level); |
| 4166 | else if (mddev->clevel[0]) |
| 4167 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * Drop all container device buffers, from now on |
| 4171 | * the only valid external interface is through the md |
| 4172 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4174 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4175 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | continue; |
| 4177 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4178 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4179 | |
| 4180 | /* perform some consistency tests on the device. |
| 4181 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4182 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4183 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4184 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4185 | if (mddev->dev_sectors && |
| 4186 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4187 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4188 | printk("md: %s: data overlaps metadata\n", |
| 4189 | mdname(mddev)); |
| 4190 | return -EINVAL; |
| 4191 | } |
| 4192 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4193 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4194 | > rdev->data_offset) { |
| 4195 | printk("md: %s: metadata overlaps data\n", |
| 4196 | mdname(mddev)); |
| 4197 | return -EINVAL; |
| 4198 | } |
| 4199 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4200 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | md_probe(mddev->unit, NULL, NULL); |
| 4204 | disk = mddev->gendisk; |
| 4205 | if (!disk) |
| 4206 | return -ENOMEM; |
| 4207 | |
| 4208 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4209 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4210 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4212 | if (mddev->level != LEVEL_NONE) |
| 4213 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4214 | mddev->level); |
| 4215 | else |
| 4216 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4217 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | return -EINVAL; |
| 4219 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4220 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4222 | if (mddev->level != pers->level) { |
| 4223 | mddev->level = pers->level; |
| 4224 | mddev->new_level = pers->level; |
| 4225 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4226 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4228 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4229 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4230 | /* This personality cannot handle reshaping... */ |
| 4231 | mddev->pers = NULL; |
| 4232 | module_put(pers->owner); |
| 4233 | return -EINVAL; |
| 4234 | } |
| 4235 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4236 | if (pers->sync_request) { |
| 4237 | /* Warn if this is a potentially silly |
| 4238 | * configuration. |
| 4239 | */ |
| 4240 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4241 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4242 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4243 | |
| 4244 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4245 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4246 | if (rdev < rdev2 && |
| 4247 | rdev->bdev->bd_contains == |
| 4248 | rdev2->bdev->bd_contains) { |
| 4249 | printk(KERN_WARNING |
| 4250 | "%s: WARNING: %s appears to be" |
| 4251 | " on the same physical disk as" |
| 4252 | " %s.\n", |
| 4253 | mdname(mddev), |
| 4254 | bdevname(rdev->bdev,b), |
| 4255 | bdevname(rdev2->bdev,b2)); |
| 4256 | warned = 1; |
| 4257 | } |
| 4258 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4259 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4260 | if (warned) |
| 4261 | printk(KERN_WARNING |
| 4262 | "True protection against single-disk" |
| 4263 | " failure might be compromised.\n"); |
| 4264 | } |
| 4265 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4266 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4267 | /* may be over-ridden by personality */ |
| 4268 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4269 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4270 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4271 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4273 | if (start_readonly) |
| 4274 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4275 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4276 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4277 | if (err) |
| 4278 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4279 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4280 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4281 | " but 'external_size' not in effect?\n", __func__); |
| 4282 | printk(KERN_ERR |
| 4283 | "md: invalid array_size %llu > default size %llu\n", |
| 4284 | (unsigned long long)mddev->array_sectors / 2, |
| 4285 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4286 | err = -EINVAL; |
| 4287 | mddev->pers->stop(mddev); |
| 4288 | } |
| 4289 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4290 | err = bitmap_create(mddev); |
| 4291 | if (err) { |
| 4292 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4293 | mdname(mddev), err); |
| 4294 | mddev->pers->stop(mddev); |
| 4295 | } |
| 4296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | module_put(mddev->pers->owner); |
| 4299 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4300 | bitmap_destroy(mddev); |
| 4301 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4303 | if (mddev->pers->sync_request) { |
| 4304 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4305 | printk(KERN_WARNING |
| 4306 | "md: cannot register extra attributes for %s\n", |
| 4307 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4308 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4309 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4310 | mddev->ro = 0; |
| 4311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | atomic_set(&mddev->writes_pending,0); |
| 4313 | mddev->safemode = 0; |
| 4314 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4315 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4316 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4318 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4319 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4320 | if (rdev->raid_disk >= 0) { |
| 4321 | char nm[20]; |
| 4322 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4323 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4324 | printk("md: cannot register %s for %s\n", |
| 4325 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
| 4328 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4329 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4330 | if (mddev->flags) |
| 4331 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4333 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4335 | /* If there is a partially-recovered drive we need to |
| 4336 | * start recovery here. If we leave it to md_check_recovery, |
| 4337 | * it will remove the drives and not do the right thing |
| 4338 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4339 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4340 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4341 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4342 | if (rdev->raid_disk >= 0 && |
| 4343 | !test_bit(In_sync, &rdev->flags) && |
| 4344 | !test_bit(Faulty, &rdev->flags)) |
| 4345 | /* complete an interrupted recovery */ |
| 4346 | spares++; |
| 4347 | if (spares && mddev->pers->sync_request) { |
| 4348 | mddev->recovery = 0; |
| 4349 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4350 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4351 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4352 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4353 | if (!mddev->sync_thread) { |
| 4354 | printk(KERN_ERR "%s: could not start resync" |
| 4355 | " thread...\n", |
| 4356 | mdname(mddev)); |
| 4357 | /* leave the spares where they are, it shouldn't hurt */ |
| 4358 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4359 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4360 | } |
| 4361 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4362 | md_wakeup_thread(mddev->thread); |
| 4363 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4364 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4365 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4366 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4367 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4368 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4369 | if (mddev->sysfs_action) |
| 4370 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4371 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4372 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | return 0; |
| 4374 | } |
| 4375 | |
| 4376 | static int restart_array(mddev_t *mddev) |
| 4377 | { |
| 4378 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4380 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4382 | return -ENXIO; |
| 4383 | if (!mddev->pers) |
| 4384 | return -EINVAL; |
| 4385 | if (!mddev->ro) |
| 4386 | return -EBUSY; |
| 4387 | mddev->safemode = 0; |
| 4388 | mddev->ro = 0; |
| 4389 | set_disk_ro(disk, 0); |
| 4390 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4391 | mdname(mddev)); |
| 4392 | /* Kick recovery or resync if necessary */ |
| 4393 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4394 | md_wakeup_thread(mddev->thread); |
| 4395 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4396 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4397 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | } |
| 4399 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4400 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4401 | * to the file ourselves */ |
| 4402 | static int deny_bitmap_write_access(struct file * file) |
| 4403 | { |
| 4404 | struct inode *inode = file->f_mapping->host; |
| 4405 | |
| 4406 | spin_lock(&inode->i_lock); |
| 4407 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4408 | spin_unlock(&inode->i_lock); |
| 4409 | return -ETXTBSY; |
| 4410 | } |
| 4411 | atomic_set(&inode->i_writecount, -1); |
| 4412 | spin_unlock(&inode->i_lock); |
| 4413 | |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
| 4417 | static void restore_bitmap_write_access(struct file *file) |
| 4418 | { |
| 4419 | struct inode *inode = file->f_mapping->host; |
| 4420 | |
| 4421 | spin_lock(&inode->i_lock); |
| 4422 | atomic_set(&inode->i_writecount, 1); |
| 4423 | spin_unlock(&inode->i_lock); |
| 4424 | } |
| 4425 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4426 | /* mode: |
| 4427 | * 0 - completely stop and dis-assemble array |
| 4428 | * 1 - switch to readonly |
| 4429 | * 2 - stop but do not disassemble array |
| 4430 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4431 | 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] | 4432 | { |
| 4433 | int err = 0; |
| 4434 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4435 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4437 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4438 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4439 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4440 | err = -EBUSY; |
| 4441 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | |
| 4443 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4444 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4446 | md_unregister_thread(mddev->sync_thread); |
| 4447 | mddev->sync_thread = NULL; |
| 4448 | } |
| 4449 | |
| 4450 | del_timer_sync(&mddev->safemode_timer); |
| 4451 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4452 | switch(mode) { |
| 4453 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4455 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | goto out; |
| 4457 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4458 | break; |
| 4459 | case 0: /* disassemble */ |
| 4460 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4461 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4462 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (mddev->ro) |
| 4464 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4467 | mddev->queue->merge_bvec_fn = NULL; |
| 4468 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4469 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4471 | if (mddev->pers->sync_request) |
| 4472 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4474 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4475 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4476 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4477 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4478 | if (rdev->raid_disk >= 0) { |
| 4479 | char nm[20]; |
| 4480 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4481 | sysfs_remove_link(&mddev->kobj, nm); |
| 4482 | } |
| 4483 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4484 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4485 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | if (mddev->ro) |
| 4488 | mddev->ro = 0; |
| 4489 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4490 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | /* mark array as shutdown cleanly */ |
| 4492 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4493 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4495 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4497 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4498 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4500 | out: |
| 4501 | mutex_unlock(&mddev->open_mutex); |
| 4502 | if (err) |
| 4503 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | /* |
| 4505 | * Free resources if final stop |
| 4506 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4507 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4510 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4511 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4512 | if (mddev->bitmap_info.file) { |
| 4513 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4514 | fput(mddev->bitmap_info.file); |
| 4515 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4516 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4517 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4518 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4519 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4520 | flush_scheduled_work(); |
| 4521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | export_array(mddev); |
| 4523 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4524 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4525 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4526 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4527 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4528 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4529 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4530 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4531 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4532 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4533 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4534 | mddev->level = LEVEL_NONE; |
| 4535 | mddev->clevel[0] = 0; |
| 4536 | mddev->flags = 0; |
| 4537 | mddev->ro = 0; |
| 4538 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4539 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4540 | mddev->ctime = mddev->utime = 0; |
| 4541 | mddev->layout = 0; |
| 4542 | mddev->max_disks = 0; |
| 4543 | mddev->events = 0; |
| 4544 | mddev->delta_disks = 0; |
| 4545 | mddev->new_level = LEVEL_NONE; |
| 4546 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4547 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4548 | mddev->curr_resync = 0; |
| 4549 | mddev->resync_mismatches = 0; |
| 4550 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4551 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4552 | mddev->recovery = 0; |
| 4553 | mddev->in_sync = 0; |
| 4554 | mddev->changed = 0; |
| 4555 | mddev->degraded = 0; |
| 4556 | mddev->barriers_work = 0; |
| 4557 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4558 | mddev->bitmap_info.offset = 0; |
| 4559 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame^] | 4560 | mddev->bitmap_info.chunksize = 0; |
| 4561 | mddev->bitmap_info.daemon_sleep = 0; |
| 4562 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4563 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4564 | if (mddev->hold_active == UNTIL_STOP) |
| 4565 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4566 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4567 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4569 | mdname(mddev)); |
| 4570 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4571 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4572 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4573 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | return err; |
| 4575 | } |
| 4576 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4577 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | static void autorun_array(mddev_t *mddev) |
| 4579 | { |
| 4580 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | int err; |
| 4582 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4583 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | |
| 4586 | printk(KERN_INFO "md: running: "); |
| 4587 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4588 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | char b[BDEVNAME_SIZE]; |
| 4590 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4591 | } |
| 4592 | printk("\n"); |
| 4593 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4594 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | if (err) { |
| 4596 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4597 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | /* |
| 4602 | * lets try to run arrays based on all disks that have arrived |
| 4603 | * until now. (those are in pending_raid_disks) |
| 4604 | * |
| 4605 | * the method: pick the first pending disk, collect all disks with |
| 4606 | * the same UUID, remove all from the pending list and put them into |
| 4607 | * the 'same_array' list. Then order this list based on superblock |
| 4608 | * update time (freshest comes first), kick out 'old' disks and |
| 4609 | * compare superblocks. If everything's fine then run it. |
| 4610 | * |
| 4611 | * If "unit" is allocated, then bump its reference count |
| 4612 | */ |
| 4613 | static void autorun_devices(int part) |
| 4614 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4615 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | mddev_t *mddev; |
| 4617 | char b[BDEVNAME_SIZE]; |
| 4618 | |
| 4619 | printk(KERN_INFO "md: autorun ...\n"); |
| 4620 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4621 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4623 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | rdev0 = list_entry(pending_raid_disks.next, |
| 4625 | mdk_rdev_t, same_set); |
| 4626 | |
| 4627 | printk(KERN_INFO "md: considering %s ...\n", |
| 4628 | bdevname(rdev0->bdev,b)); |
| 4629 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4630 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4632 | printk(KERN_INFO "md: adding %s ...\n", |
| 4633 | bdevname(rdev->bdev,b)); |
| 4634 | list_move(&rdev->same_set, &candidates); |
| 4635 | } |
| 4636 | /* |
| 4637 | * now we have a set of devices, with all of them having |
| 4638 | * mostly sane superblocks. It's time to allocate the |
| 4639 | * mddev. |
| 4640 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4641 | if (part) { |
| 4642 | dev = MKDEV(mdp_major, |
| 4643 | rdev0->preferred_minor << MdpMinorShift); |
| 4644 | unit = MINOR(dev) >> MdpMinorShift; |
| 4645 | } else { |
| 4646 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4647 | unit = MINOR(dev); |
| 4648 | } |
| 4649 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4651 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4652 | break; |
| 4653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
| 4655 | md_probe(dev, NULL, NULL); |
| 4656 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4657 | if (!mddev || !mddev->gendisk) { |
| 4658 | if (mddev) |
| 4659 | mddev_put(mddev); |
| 4660 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | "md: cannot allocate memory for md drive.\n"); |
| 4662 | break; |
| 4663 | } |
| 4664 | if (mddev_lock(mddev)) |
| 4665 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4666 | mdname(mddev)); |
| 4667 | else if (mddev->raid_disks || mddev->major_version |
| 4668 | || !list_empty(&mddev->disks)) { |
| 4669 | printk(KERN_WARNING |
| 4670 | "md: %s already running, cannot run %s\n", |
| 4671 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4672 | mddev_unlock(mddev); |
| 4673 | } else { |
| 4674 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4675 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4676 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | list_del_init(&rdev->same_set); |
| 4678 | if (bind_rdev_to_array(rdev, mddev)) |
| 4679 | export_rdev(rdev); |
| 4680 | } |
| 4681 | autorun_array(mddev); |
| 4682 | mddev_unlock(mddev); |
| 4683 | } |
| 4684 | /* on success, candidates will be empty, on error |
| 4685 | * it won't... |
| 4686 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4687 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4688 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | mddev_put(mddev); |
| 4692 | } |
| 4693 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4694 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4695 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | static int get_version(void __user * arg) |
| 4698 | { |
| 4699 | mdu_version_t ver; |
| 4700 | |
| 4701 | ver.major = MD_MAJOR_VERSION; |
| 4702 | ver.minor = MD_MINOR_VERSION; |
| 4703 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4704 | |
| 4705 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4706 | return -EFAULT; |
| 4707 | |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
| 4711 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4712 | { |
| 4713 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4714 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4717 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4718 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4720 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | failed++; |
| 4722 | else { |
| 4723 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4724 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4725 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | else |
| 4727 | spare++; |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | info.major_version = mddev->major_version; |
| 4732 | info.minor_version = mddev->minor_version; |
| 4733 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4734 | info.ctime = mddev->ctime; |
| 4735 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4736 | info.size = mddev->dev_sectors / 2; |
| 4737 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4738 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | info.nr_disks = nr; |
| 4740 | info.raid_disks = mddev->raid_disks; |
| 4741 | info.md_minor = mddev->md_minor; |
| 4742 | info.not_persistent= !mddev->persistent; |
| 4743 | |
| 4744 | info.utime = mddev->utime; |
| 4745 | info.state = 0; |
| 4746 | if (mddev->in_sync) |
| 4747 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4748 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4749 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4750 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | info.working_disks = working; |
| 4752 | info.failed_disks = failed; |
| 4753 | info.spare_disks = spare; |
| 4754 | |
| 4755 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4756 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | |
| 4758 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4759 | return -EFAULT; |
| 4760 | |
| 4761 | return 0; |
| 4762 | } |
| 4763 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4764 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4765 | { |
| 4766 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4767 | char *ptr, *buf = NULL; |
| 4768 | int err = -ENOMEM; |
| 4769 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4770 | if (md_allow_write(mddev)) |
| 4771 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4772 | else |
| 4773 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4774 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4775 | if (!file) |
| 4776 | goto out; |
| 4777 | |
| 4778 | /* bitmap disabled, zero the first byte and copy out */ |
| 4779 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4780 | file->pathname[0] = '\0'; |
| 4781 | goto copy_out; |
| 4782 | } |
| 4783 | |
| 4784 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4785 | if (!buf) |
| 4786 | goto out; |
| 4787 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4788 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4789 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4790 | goto out; |
| 4791 | |
| 4792 | strcpy(file->pathname, ptr); |
| 4793 | |
| 4794 | copy_out: |
| 4795 | err = 0; |
| 4796 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4797 | err = -EFAULT; |
| 4798 | out: |
| 4799 | kfree(buf); |
| 4800 | kfree(file); |
| 4801 | return err; |
| 4802 | } |
| 4803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4805 | { |
| 4806 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | mdk_rdev_t *rdev; |
| 4808 | |
| 4809 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4810 | return -EFAULT; |
| 4811 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4812 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | if (rdev) { |
| 4814 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4815 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4816 | info.raid_disk = rdev->raid_disk; |
| 4817 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4818 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4820 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4822 | info.state |= (1<<MD_DISK_SYNC); |
| 4823 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4824 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4825 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | } else { |
| 4827 | info.major = info.minor = 0; |
| 4828 | info.raid_disk = -1; |
| 4829 | info.state = (1<<MD_DISK_REMOVED); |
| 4830 | } |
| 4831 | |
| 4832 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4833 | return -EFAULT; |
| 4834 | |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
| 4838 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4839 | { |
| 4840 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4841 | mdk_rdev_t *rdev; |
| 4842 | dev_t dev = MKDEV(info->major,info->minor); |
| 4843 | |
| 4844 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4845 | return -EOVERFLOW; |
| 4846 | |
| 4847 | if (!mddev->raid_disks) { |
| 4848 | int err; |
| 4849 | /* expecting a device which has a superblock */ |
| 4850 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4851 | if (IS_ERR(rdev)) { |
| 4852 | printk(KERN_WARNING |
| 4853 | "md: md_import_device returned %ld\n", |
| 4854 | PTR_ERR(rdev)); |
| 4855 | return PTR_ERR(rdev); |
| 4856 | } |
| 4857 | if (!list_empty(&mddev->disks)) { |
| 4858 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4859 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4860 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4862 | if (err < 0) { |
| 4863 | printk(KERN_WARNING |
| 4864 | "md: %s has different UUID to %s\n", |
| 4865 | bdevname(rdev->bdev,b), |
| 4866 | bdevname(rdev0->bdev,b2)); |
| 4867 | export_rdev(rdev); |
| 4868 | return -EINVAL; |
| 4869 | } |
| 4870 | } |
| 4871 | err = bind_rdev_to_array(rdev, mddev); |
| 4872 | if (err) |
| 4873 | export_rdev(rdev); |
| 4874 | return err; |
| 4875 | } |
| 4876 | |
| 4877 | /* |
| 4878 | * add_new_disk can be used once the array is assembled |
| 4879 | * to add "hot spares". They must already have a superblock |
| 4880 | * written |
| 4881 | */ |
| 4882 | if (mddev->pers) { |
| 4883 | int err; |
| 4884 | if (!mddev->pers->hot_add_disk) { |
| 4885 | printk(KERN_WARNING |
| 4886 | "%s: personality does not support diskops!\n", |
| 4887 | mdname(mddev)); |
| 4888 | return -EINVAL; |
| 4889 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4890 | if (mddev->persistent) |
| 4891 | rdev = md_import_device(dev, mddev->major_version, |
| 4892 | mddev->minor_version); |
| 4893 | else |
| 4894 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | if (IS_ERR(rdev)) { |
| 4896 | printk(KERN_WARNING |
| 4897 | "md: md_import_device returned %ld\n", |
| 4898 | PTR_ERR(rdev)); |
| 4899 | return PTR_ERR(rdev); |
| 4900 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4901 | /* set save_raid_disk if appropriate */ |
| 4902 | if (!mddev->persistent) { |
| 4903 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4904 | info->raid_disk < mddev->raid_disks) |
| 4905 | rdev->raid_disk = info->raid_disk; |
| 4906 | else |
| 4907 | rdev->raid_disk = -1; |
| 4908 | } else |
| 4909 | super_types[mddev->major_version]. |
| 4910 | validate_super(mddev, rdev); |
| 4911 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4912 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4913 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4914 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4915 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4916 | else |
| 4917 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | rdev->raid_disk = -1; |
| 4920 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4921 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4922 | /* If there is hot_add_disk but no hot_remove_disk |
| 4923 | * then added disks for geometry changes, |
| 4924 | * and should be added immediately. |
| 4925 | */ |
| 4926 | super_types[mddev->major_version]. |
| 4927 | validate_super(mddev, rdev); |
| 4928 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4929 | if (err) |
| 4930 | unbind_rdev_from_array(rdev); |
| 4931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | if (err) |
| 4933 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4934 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4935 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4936 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4937 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4938 | if (mddev->degraded) |
| 4939 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4940 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4941 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | return err; |
| 4943 | } |
| 4944 | |
| 4945 | /* otherwise, add_new_disk is only allowed |
| 4946 | * for major_version==0 superblocks |
| 4947 | */ |
| 4948 | if (mddev->major_version != 0) { |
| 4949 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4950 | mdname(mddev)); |
| 4951 | return -EINVAL; |
| 4952 | } |
| 4953 | |
| 4954 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4955 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4956 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | if (IS_ERR(rdev)) { |
| 4958 | printk(KERN_WARNING |
| 4959 | "md: error, md_import_device() returned %ld\n", |
| 4960 | PTR_ERR(rdev)); |
| 4961 | return PTR_ERR(rdev); |
| 4962 | } |
| 4963 | rdev->desc_nr = info->number; |
| 4964 | if (info->raid_disk < mddev->raid_disks) |
| 4965 | rdev->raid_disk = info->raid_disk; |
| 4966 | else |
| 4967 | rdev->raid_disk = -1; |
| 4968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4970 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4971 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4973 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4974 | set_bit(WriteMostly, &rdev->flags); |
| 4975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | if (!mddev->persistent) { |
| 4977 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4978 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4980 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4981 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4983 | err = bind_rdev_to_array(rdev, mddev); |
| 4984 | if (err) { |
| 4985 | export_rdev(rdev); |
| 4986 | return err; |
| 4987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | return 0; |
| 4991 | } |
| 4992 | |
| 4993 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4994 | { |
| 4995 | char b[BDEVNAME_SIZE]; |
| 4996 | mdk_rdev_t *rdev; |
| 4997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | rdev = find_rdev(mddev, dev); |
| 4999 | if (!rdev) |
| 5000 | return -ENXIO; |
| 5001 | |
| 5002 | if (rdev->raid_disk >= 0) |
| 5003 | goto busy; |
| 5004 | |
| 5005 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5006 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5007 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
| 5009 | return 0; |
| 5010 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5011 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5013 | return -EBUSY; |
| 5014 | } |
| 5015 | |
| 5016 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5017 | { |
| 5018 | char b[BDEVNAME_SIZE]; |
| 5019 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | mdk_rdev_t *rdev; |
| 5021 | |
| 5022 | if (!mddev->pers) |
| 5023 | return -ENODEV; |
| 5024 | |
| 5025 | if (mddev->major_version != 0) { |
| 5026 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5027 | " version-0 superblocks.\n", |
| 5028 | mdname(mddev)); |
| 5029 | return -EINVAL; |
| 5030 | } |
| 5031 | if (!mddev->pers->hot_add_disk) { |
| 5032 | printk(KERN_WARNING |
| 5033 | "%s: personality does not support diskops!\n", |
| 5034 | mdname(mddev)); |
| 5035 | return -EINVAL; |
| 5036 | } |
| 5037 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5038 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | if (IS_ERR(rdev)) { |
| 5040 | printk(KERN_WARNING |
| 5041 | "md: error, md_import_device() returned %ld\n", |
| 5042 | PTR_ERR(rdev)); |
| 5043 | return -EINVAL; |
| 5044 | } |
| 5045 | |
| 5046 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5047 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5048 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5049 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5051 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5053 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | printk(KERN_WARNING |
| 5055 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5056 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5057 | err = -EINVAL; |
| 5058 | goto abort_export; |
| 5059 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5060 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5062 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5063 | err = bind_rdev_to_array(rdev, mddev); |
| 5064 | if (err) |
| 5065 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
| 5067 | /* |
| 5068 | * The rest should better be atomic, we can have disk failures |
| 5069 | * noticed in interrupt contexts ... |
| 5070 | */ |
| 5071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | rdev->raid_disk = -1; |
| 5073 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5074 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | |
| 5076 | /* |
| 5077 | * Kick recovery, maybe this spare has to be added to the |
| 5078 | * array immediately. |
| 5079 | */ |
| 5080 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5081 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5082 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | return 0; |
| 5084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | abort_export: |
| 5086 | export_rdev(rdev); |
| 5087 | return err; |
| 5088 | } |
| 5089 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5090 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5091 | { |
| 5092 | int err; |
| 5093 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5094 | if (mddev->pers) { |
| 5095 | if (!mddev->pers->quiesce) |
| 5096 | return -EBUSY; |
| 5097 | if (mddev->recovery || mddev->sync_thread) |
| 5098 | return -EBUSY; |
| 5099 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5100 | } |
| 5101 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5102 | |
| 5103 | if (fd >= 0) { |
| 5104 | if (mddev->bitmap) |
| 5105 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5106 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5107 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5108 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5109 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5110 | mdname(mddev)); |
| 5111 | return -EBADF; |
| 5112 | } |
| 5113 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5114 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5115 | if (err) { |
| 5116 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5117 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5118 | fput(mddev->bitmap_info.file); |
| 5119 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5120 | return err; |
| 5121 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5122 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5123 | } else if (mddev->bitmap == NULL) |
| 5124 | return -ENOENT; /* cannot remove what isn't there */ |
| 5125 | err = 0; |
| 5126 | if (mddev->pers) { |
| 5127 | mddev->pers->quiesce(mddev, 1); |
| 5128 | if (fd >= 0) |
| 5129 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5130 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5131 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5132 | fd = -1; /* make sure to put the file */ |
| 5133 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5134 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5135 | } |
| 5136 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5137 | if (mddev->bitmap_info.file) { |
| 5138 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5139 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5140 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5141 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5142 | } |
| 5143 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5144 | return err; |
| 5145 | } |
| 5146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | /* |
| 5148 | * set_array_info is used two different ways |
| 5149 | * The original usage is when creating a new array. |
| 5150 | * In this usage, raid_disks is > 0 and it together with |
| 5151 | * level, size, not_persistent,layout,chunksize determine the |
| 5152 | * shape of the array. |
| 5153 | * This will always create an array with a type-0.90.0 superblock. |
| 5154 | * The newer usage is when assembling an array. |
| 5155 | * In this case raid_disks will be 0, and the major_version field is |
| 5156 | * use to determine which style super-blocks are to be found on the devices. |
| 5157 | * The minor and patch _version numbers are also kept incase the |
| 5158 | * super_block handler wishes to interpret them. |
| 5159 | */ |
| 5160 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5161 | { |
| 5162 | |
| 5163 | if (info->raid_disks == 0) { |
| 5164 | /* just setting version number for superblock loading */ |
| 5165 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5166 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | super_types[info->major_version].name == NULL) { |
| 5168 | /* maybe try to auto-load a module? */ |
| 5169 | printk(KERN_INFO |
| 5170 | "md: superblock version %d not known\n", |
| 5171 | info->major_version); |
| 5172 | return -EINVAL; |
| 5173 | } |
| 5174 | mddev->major_version = info->major_version; |
| 5175 | mddev->minor_version = info->minor_version; |
| 5176 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5177 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | return 0; |
| 5179 | } |
| 5180 | mddev->major_version = MD_MAJOR_VERSION; |
| 5181 | mddev->minor_version = MD_MINOR_VERSION; |
| 5182 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5183 | mddev->ctime = get_seconds(); |
| 5184 | |
| 5185 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5186 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5187 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | mddev->raid_disks = info->raid_disks; |
| 5189 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5190 | * openned |
| 5191 | */ |
| 5192 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5193 | mddev->recovery_cp = MaxSector; |
| 5194 | else |
| 5195 | mddev->recovery_cp = 0; |
| 5196 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5197 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | |
| 5199 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5200 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | |
| 5202 | mddev->max_disks = MD_SB_DISKS; |
| 5203 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5204 | if (mddev->persistent) |
| 5205 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5206 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5208 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5209 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5210 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5211 | mddev->reshape_position = MaxSector; |
| 5212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | /* |
| 5214 | * Generate a 128 bit UUID |
| 5215 | */ |
| 5216 | get_random_bytes(mddev->uuid, 16); |
| 5217 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5218 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5219 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5220 | mddev->new_layout = mddev->layout; |
| 5221 | mddev->delta_disks = 0; |
| 5222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | return 0; |
| 5224 | } |
| 5225 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5226 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5227 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5228 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5229 | |
| 5230 | if (mddev->external_size) |
| 5231 | return; |
| 5232 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5233 | mddev->array_sectors = array_sectors; |
| 5234 | } |
| 5235 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5236 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5237 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5238 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5239 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5240 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5241 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5242 | |
| 5243 | if (mddev->pers->resize == NULL) |
| 5244 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5245 | /* The "num_sectors" is the number of sectors of each device that |
| 5246 | * is used. This can only make sense for arrays with redundancy. |
| 5247 | * linear and raid0 always use whatever space is available. We can only |
| 5248 | * consider changing this number if no resync or reconstruction is |
| 5249 | * 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] | 5250 | * 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] | 5251 | * of each device. If num_sectors is zero, we find the largest size |
| 5252 | * that fits. |
| 5253 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5254 | */ |
| 5255 | if (mddev->sync_thread) |
| 5256 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5257 | if (mddev->bitmap) |
| 5258 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5259 | * grow, and re-add. |
| 5260 | */ |
| 5261 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5262 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5263 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5264 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5265 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5266 | num_sectors = avail; |
| 5267 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5268 | return -ENOSPC; |
| 5269 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5270 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5271 | if (!rv) |
| 5272 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5273 | return rv; |
| 5274 | } |
| 5275 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5276 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5277 | { |
| 5278 | int rv; |
| 5279 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5280 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5281 | return -EINVAL; |
| 5282 | if (raid_disks <= 0 || |
| 5283 | raid_disks >= mddev->max_disks) |
| 5284 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5285 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5286 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5287 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5288 | |
| 5289 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5290 | return rv; |
| 5291 | } |
| 5292 | |
| 5293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | /* |
| 5295 | * update_array_info is used to change the configuration of an |
| 5296 | * on-line array. |
| 5297 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5298 | * fields in the info are checked against the array. |
| 5299 | * Any differences that cannot be handled will cause an error. |
| 5300 | * Normally, only one change can be managed at a time. |
| 5301 | */ |
| 5302 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5303 | { |
| 5304 | int rv = 0; |
| 5305 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5306 | int state = 0; |
| 5307 | |
| 5308 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5309 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5310 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
| 5312 | if (mddev->major_version != info->major_version || |
| 5313 | mddev->minor_version != info->minor_version || |
| 5314 | /* mddev->patch_version != info->patch_version || */ |
| 5315 | mddev->ctime != info->ctime || |
| 5316 | mddev->level != info->level || |
| 5317 | /* mddev->layout != info->layout || */ |
| 5318 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5319 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5320 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5321 | ((state^info->state) & 0xfffffe00) |
| 5322 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | return -EINVAL; |
| 5324 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5325 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5326 | cnt++; |
| 5327 | if (mddev->raid_disks != info->raid_disks) |
| 5328 | cnt++; |
| 5329 | if (mddev->layout != info->layout) |
| 5330 | cnt++; |
| 5331 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5332 | cnt++; |
| 5333 | if (cnt == 0) |
| 5334 | return 0; |
| 5335 | if (cnt > 1) |
| 5336 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | |
| 5338 | if (mddev->layout != info->layout) { |
| 5339 | /* Change layout |
| 5340 | * we don't need to do anything at the md level, the |
| 5341 | * personality will take care of it all. |
| 5342 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5343 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5345 | else { |
| 5346 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5347 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5348 | if (rv) |
| 5349 | mddev->new_layout = mddev->layout; |
| 5350 | return rv; |
| 5351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5353 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5354 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5356 | if (mddev->raid_disks != info->raid_disks) |
| 5357 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5359 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5360 | if (mddev->pers->quiesce == NULL) |
| 5361 | return -EINVAL; |
| 5362 | if (mddev->recovery || mddev->sync_thread) |
| 5363 | return -EBUSY; |
| 5364 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5365 | /* add the bitmap */ |
| 5366 | if (mddev->bitmap) |
| 5367 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5368 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5369 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5370 | mddev->bitmap_info.offset = |
| 5371 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5372 | mddev->pers->quiesce(mddev, 1); |
| 5373 | rv = bitmap_create(mddev); |
| 5374 | if (rv) |
| 5375 | bitmap_destroy(mddev); |
| 5376 | mddev->pers->quiesce(mddev, 0); |
| 5377 | } else { |
| 5378 | /* remove the bitmap */ |
| 5379 | if (!mddev->bitmap) |
| 5380 | return -ENOENT; |
| 5381 | if (mddev->bitmap->file) |
| 5382 | return -EINVAL; |
| 5383 | mddev->pers->quiesce(mddev, 1); |
| 5384 | bitmap_destroy(mddev); |
| 5385 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5386 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5387 | } |
| 5388 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5389 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | return rv; |
| 5391 | } |
| 5392 | |
| 5393 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5394 | { |
| 5395 | mdk_rdev_t *rdev; |
| 5396 | |
| 5397 | if (mddev->pers == NULL) |
| 5398 | return -ENODEV; |
| 5399 | |
| 5400 | rdev = find_rdev(mddev, dev); |
| 5401 | if (!rdev) |
| 5402 | return -ENODEV; |
| 5403 | |
| 5404 | md_error(mddev, rdev); |
| 5405 | return 0; |
| 5406 | } |
| 5407 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5408 | /* |
| 5409 | * We have a problem here : there is no easy way to give a CHS |
| 5410 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5411 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5412 | * dosfs just mad... ;-) |
| 5413 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5414 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5415 | { |
| 5416 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5417 | |
| 5418 | geo->heads = 2; |
| 5419 | geo->sectors = 4; |
| 5420 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5421 | return 0; |
| 5422 | } |
| 5423 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5424 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | unsigned int cmd, unsigned long arg) |
| 5426 | { |
| 5427 | int err = 0; |
| 5428 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | mddev_t *mddev = NULL; |
| 5430 | |
| 5431 | if (!capable(CAP_SYS_ADMIN)) |
| 5432 | return -EACCES; |
| 5433 | |
| 5434 | /* |
| 5435 | * Commands dealing with the RAID driver but not any |
| 5436 | * particular array: |
| 5437 | */ |
| 5438 | switch (cmd) |
| 5439 | { |
| 5440 | case RAID_VERSION: |
| 5441 | err = get_version(argp); |
| 5442 | goto done; |
| 5443 | |
| 5444 | case PRINT_RAID_DEBUG: |
| 5445 | err = 0; |
| 5446 | md_print_devices(); |
| 5447 | goto done; |
| 5448 | |
| 5449 | #ifndef MODULE |
| 5450 | case RAID_AUTORUN: |
| 5451 | err = 0; |
| 5452 | autostart_arrays(arg); |
| 5453 | goto done; |
| 5454 | #endif |
| 5455 | default:; |
| 5456 | } |
| 5457 | |
| 5458 | /* |
| 5459 | * Commands creating/starting a new array: |
| 5460 | */ |
| 5461 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5462 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | |
| 5464 | if (!mddev) { |
| 5465 | BUG(); |
| 5466 | goto abort; |
| 5467 | } |
| 5468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | err = mddev_lock(mddev); |
| 5470 | if (err) { |
| 5471 | printk(KERN_INFO |
| 5472 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5473 | err, cmd); |
| 5474 | goto abort; |
| 5475 | } |
| 5476 | |
| 5477 | switch (cmd) |
| 5478 | { |
| 5479 | case SET_ARRAY_INFO: |
| 5480 | { |
| 5481 | mdu_array_info_t info; |
| 5482 | if (!arg) |
| 5483 | memset(&info, 0, sizeof(info)); |
| 5484 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5485 | err = -EFAULT; |
| 5486 | goto abort_unlock; |
| 5487 | } |
| 5488 | if (mddev->pers) { |
| 5489 | err = update_array_info(mddev, &info); |
| 5490 | if (err) { |
| 5491 | printk(KERN_WARNING "md: couldn't update" |
| 5492 | " array info. %d\n", err); |
| 5493 | goto abort_unlock; |
| 5494 | } |
| 5495 | goto done_unlock; |
| 5496 | } |
| 5497 | if (!list_empty(&mddev->disks)) { |
| 5498 | printk(KERN_WARNING |
| 5499 | "md: array %s already has disks!\n", |
| 5500 | mdname(mddev)); |
| 5501 | err = -EBUSY; |
| 5502 | goto abort_unlock; |
| 5503 | } |
| 5504 | if (mddev->raid_disks) { |
| 5505 | printk(KERN_WARNING |
| 5506 | "md: array %s already initialised!\n", |
| 5507 | mdname(mddev)); |
| 5508 | err = -EBUSY; |
| 5509 | goto abort_unlock; |
| 5510 | } |
| 5511 | err = set_array_info(mddev, &info); |
| 5512 | if (err) { |
| 5513 | printk(KERN_WARNING "md: couldn't set" |
| 5514 | " array info. %d\n", err); |
| 5515 | goto abort_unlock; |
| 5516 | } |
| 5517 | } |
| 5518 | goto done_unlock; |
| 5519 | |
| 5520 | default:; |
| 5521 | } |
| 5522 | |
| 5523 | /* |
| 5524 | * Commands querying/configuring an existing array: |
| 5525 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5526 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5527 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5528 | if ((!mddev->raid_disks && !mddev->external) |
| 5529 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5530 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5531 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | err = -ENODEV; |
| 5533 | goto abort_unlock; |
| 5534 | } |
| 5535 | |
| 5536 | /* |
| 5537 | * Commands even a read-only array can execute: |
| 5538 | */ |
| 5539 | switch (cmd) |
| 5540 | { |
| 5541 | case GET_ARRAY_INFO: |
| 5542 | err = get_array_info(mddev, argp); |
| 5543 | goto done_unlock; |
| 5544 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5545 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5546 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5547 | goto done_unlock; |
| 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | case GET_DISK_INFO: |
| 5550 | err = get_disk_info(mddev, argp); |
| 5551 | goto done_unlock; |
| 5552 | |
| 5553 | case RESTART_ARRAY_RW: |
| 5554 | err = restart_array(mddev); |
| 5555 | goto done_unlock; |
| 5556 | |
| 5557 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5558 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | goto done_unlock; |
| 5560 | |
| 5561 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5562 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | goto done_unlock; |
| 5564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | /* |
| 5568 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5569 | * superblock, so we do not allow them on read-only arrays. |
| 5570 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5571 | * here and hit the 'default' below, so only disallow |
| 5572 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5574 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5575 | if (mddev->ro == 2) { |
| 5576 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5577 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5578 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5579 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5580 | } else { |
| 5581 | err = -EROFS; |
| 5582 | goto abort_unlock; |
| 5583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | switch (cmd) |
| 5587 | { |
| 5588 | case ADD_NEW_DISK: |
| 5589 | { |
| 5590 | mdu_disk_info_t info; |
| 5591 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5592 | err = -EFAULT; |
| 5593 | else |
| 5594 | err = add_new_disk(mddev, &info); |
| 5595 | goto done_unlock; |
| 5596 | } |
| 5597 | |
| 5598 | case HOT_REMOVE_DISK: |
| 5599 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5600 | goto done_unlock; |
| 5601 | |
| 5602 | case HOT_ADD_DISK: |
| 5603 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5604 | goto done_unlock; |
| 5605 | |
| 5606 | case SET_DISK_FAULTY: |
| 5607 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5608 | goto done_unlock; |
| 5609 | |
| 5610 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5611 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | goto done_unlock; |
| 5613 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5614 | case SET_BITMAP_FILE: |
| 5615 | err = set_bitmap_file(mddev, (int)arg); |
| 5616 | goto done_unlock; |
| 5617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | err = -EINVAL; |
| 5620 | goto abort_unlock; |
| 5621 | } |
| 5622 | |
| 5623 | done_unlock: |
| 5624 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5625 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5626 | err != -EINVAL) |
| 5627 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | mddev_unlock(mddev); |
| 5629 | |
| 5630 | return err; |
| 5631 | done: |
| 5632 | if (err) |
| 5633 | MD_BUG(); |
| 5634 | abort: |
| 5635 | return err; |
| 5636 | } |
| 5637 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5638 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | { |
| 5640 | /* |
| 5641 | * Succeed if we can lock the mddev, which confirms that |
| 5642 | * it isn't being stopped right now. |
| 5643 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5644 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | int err; |
| 5646 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5647 | if (mddev->gendisk != bdev->bd_disk) { |
| 5648 | /* we are racing with mddev_put which is discarding this |
| 5649 | * bd_disk. |
| 5650 | */ |
| 5651 | mddev_put(mddev); |
| 5652 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5653 | flush_scheduled_work(); |
| 5654 | /* Then retry the open from the top */ |
| 5655 | return -ERESTARTSYS; |
| 5656 | } |
| 5657 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5658 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5659 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | goto out; |
| 5661 | |
| 5662 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5663 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5664 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5666 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | out: |
| 5668 | return err; |
| 5669 | } |
| 5670 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5671 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5673 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5675 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5676 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | mddev_put(mddev); |
| 5678 | |
| 5679 | return 0; |
| 5680 | } |
| 5681 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5682 | static int md_media_changed(struct gendisk *disk) |
| 5683 | { |
| 5684 | mddev_t *mddev = disk->private_data; |
| 5685 | |
| 5686 | return mddev->changed; |
| 5687 | } |
| 5688 | |
| 5689 | static int md_revalidate(struct gendisk *disk) |
| 5690 | { |
| 5691 | mddev_t *mddev = disk->private_data; |
| 5692 | |
| 5693 | mddev->changed = 0; |
| 5694 | return 0; |
| 5695 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5696 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | { |
| 5698 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5699 | .open = md_open, |
| 5700 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5701 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5702 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5703 | .media_changed = md_media_changed, |
| 5704 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | }; |
| 5706 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5707 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | { |
| 5709 | mdk_thread_t *thread = arg; |
| 5710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | /* |
| 5712 | * md_thread is a 'system-thread', it's priority should be very |
| 5713 | * high. We avoid resource deadlocks individually in each |
| 5714 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5715 | * the very same RT priority as kswapd, thus we will never get |
| 5716 | * into a priority inversion deadlock. |
| 5717 | * |
| 5718 | * we definitely have to have equal or higher priority than |
| 5719 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5720 | * many dirty RAID5 blocks. |
| 5721 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5723 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5724 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5726 | /* We need to wait INTERRUPTIBLE so that |
| 5727 | * we don't add to the load-average. |
| 5728 | * That means we need to be sure no signals are |
| 5729 | * pending |
| 5730 | */ |
| 5731 | if (signal_pending(current)) |
| 5732 | flush_signals(current); |
| 5733 | |
| 5734 | wait_event_interruptible_timeout |
| 5735 | (thread->wqueue, |
| 5736 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5737 | || kthread_should_stop(), |
| 5738 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | |
| 5740 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5741 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5742 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | return 0; |
| 5746 | } |
| 5747 | |
| 5748 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5749 | { |
| 5750 | if (thread) { |
| 5751 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5752 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5753 | wake_up(&thread->wqueue); |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5758 | const char *name) |
| 5759 | { |
| 5760 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5762 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | if (!thread) |
| 5764 | return NULL; |
| 5765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | init_waitqueue_head(&thread->wqueue); |
| 5767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | thread->run = run; |
| 5769 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5770 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5771 | thread->tsk = kthread_run(md_thread, thread, |
| 5772 | "%s_%s", |
| 5773 | mdname(thread->mddev), |
| 5774 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5775 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | kfree(thread); |
| 5777 | return NULL; |
| 5778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | return thread; |
| 5780 | } |
| 5781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | void md_unregister_thread(mdk_thread_t *thread) |
| 5783 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5784 | if (!thread) |
| 5785 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5786 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5787 | |
| 5788 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | kfree(thread); |
| 5790 | } |
| 5791 | |
| 5792 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5793 | { |
| 5794 | if (!mddev) { |
| 5795 | MD_BUG(); |
| 5796 | return; |
| 5797 | } |
| 5798 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5799 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5801 | |
| 5802 | if (mddev->external) |
| 5803 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5804 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5806 | mdname(mddev), |
| 5807 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5808 | __builtin_return_address(0),__builtin_return_address(1), |
| 5809 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5810 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5811 | if (!mddev->pers) |
| 5812 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | if (!mddev->pers->error_handler) |
| 5814 | return; |
| 5815 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5816 | if (mddev->degraded) |
| 5817 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5818 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5820 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5821 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5822 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | /* seq_file implementation /proc/mdstat */ |
| 5826 | |
| 5827 | static void status_unused(struct seq_file *seq) |
| 5828 | { |
| 5829 | int i = 0; |
| 5830 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | |
| 5832 | seq_printf(seq, "unused devices: "); |
| 5833 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5834 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | char b[BDEVNAME_SIZE]; |
| 5836 | i++; |
| 5837 | seq_printf(seq, "%s ", |
| 5838 | bdevname(rdev->bdev,b)); |
| 5839 | } |
| 5840 | if (!i) |
| 5841 | seq_printf(seq, "<none>"); |
| 5842 | |
| 5843 | seq_printf(seq, "\n"); |
| 5844 | } |
| 5845 | |
| 5846 | |
| 5847 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5848 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5849 | sector_t max_sectors, resync, res; |
| 5850 | unsigned long dt, db; |
| 5851 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5852 | int scale; |
| 5853 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5855 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
| 5857 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5858 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5860 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | |
| 5862 | /* |
| 5863 | * Should not happen. |
| 5864 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5865 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | MD_BUG(); |
| 5867 | return; |
| 5868 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5869 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5870 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5871 | * u32, as those are the requirements for sector_div. |
| 5872 | * Thus 'scale' must be at least 10 |
| 5873 | */ |
| 5874 | scale = 10; |
| 5875 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5876 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5877 | scale++; |
| 5878 | } |
| 5879 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5880 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5881 | |
| 5882 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5884 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | seq_printf(seq, "["); |
| 5886 | for (i = 0; i < x; i++) |
| 5887 | seq_printf(seq, "="); |
| 5888 | seq_printf(seq, ">"); |
| 5889 | for (i = 0; i < y; i++) |
| 5890 | seq_printf(seq, "."); |
| 5891 | seq_printf(seq, "] "); |
| 5892 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5893 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5894 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5895 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5896 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5897 | "check" : |
| 5898 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5899 | "resync" : "recovery"))), |
| 5900 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5901 | (unsigned long long) resync/2, |
| 5902 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | |
| 5904 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | * dt: time from mark until now |
| 5906 | * db: blocks written from mark until now |
| 5907 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5908 | * |
| 5909 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5910 | * So we divide before multiply in case it is 32bit and close |
| 5911 | * to the limit. |
| 5912 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5913 | * near the end of resync when the number of remaining sectors |
| 5914 | * is close to 'db'. |
| 5915 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5916 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | */ |
| 5918 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5919 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5920 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5921 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5923 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5924 | sector_div(rt, db/32+1); |
| 5925 | rt *= dt; |
| 5926 | rt >>= 5; |
| 5927 | |
| 5928 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5929 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5931 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5935 | { |
| 5936 | struct list_head *tmp; |
| 5937 | loff_t l = *pos; |
| 5938 | mddev_t *mddev; |
| 5939 | |
| 5940 | if (l >= 0x10000) |
| 5941 | return NULL; |
| 5942 | if (!l--) |
| 5943 | /* header */ |
| 5944 | return (void*)1; |
| 5945 | |
| 5946 | spin_lock(&all_mddevs_lock); |
| 5947 | list_for_each(tmp,&all_mddevs) |
| 5948 | if (!l--) { |
| 5949 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5950 | mddev_get(mddev); |
| 5951 | spin_unlock(&all_mddevs_lock); |
| 5952 | return mddev; |
| 5953 | } |
| 5954 | spin_unlock(&all_mddevs_lock); |
| 5955 | if (!l--) |
| 5956 | return (void*)2;/* tail */ |
| 5957 | return NULL; |
| 5958 | } |
| 5959 | |
| 5960 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5961 | { |
| 5962 | struct list_head *tmp; |
| 5963 | mddev_t *next_mddev, *mddev = v; |
| 5964 | |
| 5965 | ++*pos; |
| 5966 | if (v == (void*)2) |
| 5967 | return NULL; |
| 5968 | |
| 5969 | spin_lock(&all_mddevs_lock); |
| 5970 | if (v == (void*)1) |
| 5971 | tmp = all_mddevs.next; |
| 5972 | else |
| 5973 | tmp = mddev->all_mddevs.next; |
| 5974 | if (tmp != &all_mddevs) |
| 5975 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5976 | else { |
| 5977 | next_mddev = (void*)2; |
| 5978 | *pos = 0x10000; |
| 5979 | } |
| 5980 | spin_unlock(&all_mddevs_lock); |
| 5981 | |
| 5982 | if (v != (void*)1) |
| 5983 | mddev_put(mddev); |
| 5984 | return next_mddev; |
| 5985 | |
| 5986 | } |
| 5987 | |
| 5988 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5989 | { |
| 5990 | mddev_t *mddev = v; |
| 5991 | |
| 5992 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5993 | mddev_put(mddev); |
| 5994 | } |
| 5995 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5996 | struct mdstat_info { |
| 5997 | int event; |
| 5998 | }; |
| 5999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6001 | { |
| 6002 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6003 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6005 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6006 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | |
| 6008 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6009 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | seq_printf(seq, "Personalities : "); |
| 6011 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6012 | list_for_each_entry(pers, &pers_list, list) |
| 6013 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | |
| 6015 | spin_unlock(&pers_lock); |
| 6016 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6017 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | return 0; |
| 6019 | } |
| 6020 | if (v == (void*)2) { |
| 6021 | status_unused(seq); |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6025 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6029 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6030 | mddev->pers ? "" : "in"); |
| 6031 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6032 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6034 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6035 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | seq_printf(seq, " %s", mddev->pers->name); |
| 6037 | } |
| 6038 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6039 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6040 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | char b[BDEVNAME_SIZE]; |
| 6042 | seq_printf(seq, " %s[%d]", |
| 6043 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6044 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6045 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6046 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | seq_printf(seq, "(F)"); |
| 6048 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6049 | } else if (rdev->raid_disk < 0) |
| 6050 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6051 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | if (!list_empty(&mddev->disks)) { |
| 6055 | if (mddev->pers) |
| 6056 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6057 | (unsigned long long) |
| 6058 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | else |
| 6060 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6061 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6063 | if (mddev->persistent) { |
| 6064 | if (mddev->major_version != 0 || |
| 6065 | mddev->minor_version != 90) { |
| 6066 | seq_printf(seq," super %d.%d", |
| 6067 | mddev->major_version, |
| 6068 | mddev->minor_version); |
| 6069 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6070 | } else if (mddev->external) |
| 6071 | seq_printf(seq, " super external:%s", |
| 6072 | mddev->metadata_type); |
| 6073 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6074 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | |
| 6076 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6077 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6079 | if (mddev->pers->sync_request) { |
| 6080 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6081 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6082 | seq_printf(seq, "\n "); |
| 6083 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6084 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6085 | else if (mddev->recovery_cp < MaxSector) |
| 6086 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6087 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6088 | } else |
| 6089 | seq_printf(seq, "\n "); |
| 6090 | |
| 6091 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6092 | unsigned long chunk_kb; |
| 6093 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6094 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame^] | 6095 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6096 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6097 | "%lu%s chunk", |
| 6098 | bitmap->pages - bitmap->missing_pages, |
| 6099 | bitmap->pages, |
| 6100 | (bitmap->pages - bitmap->missing_pages) |
| 6101 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame^] | 6102 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6103 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6104 | if (bitmap->file) { |
| 6105 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6106 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6107 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6108 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6109 | seq_printf(seq, "\n"); |
| 6110 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | } |
| 6112 | |
| 6113 | seq_printf(seq, "\n"); |
| 6114 | } |
| 6115 | mddev_unlock(mddev); |
| 6116 | |
| 6117 | return 0; |
| 6118 | } |
| 6119 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6120 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | .start = md_seq_start, |
| 6122 | .next = md_seq_next, |
| 6123 | .stop = md_seq_stop, |
| 6124 | .show = md_seq_show, |
| 6125 | }; |
| 6126 | |
| 6127 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6128 | { |
| 6129 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6130 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6131 | if (mi == NULL) |
| 6132 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | |
| 6134 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6135 | if (error) |
| 6136 | kfree(mi); |
| 6137 | else { |
| 6138 | struct seq_file *p = file->private_data; |
| 6139 | p->private = mi; |
| 6140 | mi->event = atomic_read(&md_event_count); |
| 6141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | return error; |
| 6143 | } |
| 6144 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6145 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6146 | { |
| 6147 | struct seq_file *m = filp->private_data; |
| 6148 | struct mdstat_info *mi = m->private; |
| 6149 | int mask; |
| 6150 | |
| 6151 | poll_wait(filp, &md_event_waiters, wait); |
| 6152 | |
| 6153 | /* always allow read */ |
| 6154 | mask = POLLIN | POLLRDNORM; |
| 6155 | |
| 6156 | if (mi->event != atomic_read(&md_event_count)) |
| 6157 | mask |= POLLERR | POLLPRI; |
| 6158 | return mask; |
| 6159 | } |
| 6160 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6161 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6162 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | .open = md_seq_open, |
| 6164 | .read = seq_read, |
| 6165 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6166 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6167 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | }; |
| 6169 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6170 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6173 | list_add_tail(&p->list, &pers_list); |
| 6174 | 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] | 6175 | spin_unlock(&pers_lock); |
| 6176 | return 0; |
| 6177 | } |
| 6178 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6179 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6181 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6183 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | spin_unlock(&pers_lock); |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6188 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | { |
| 6190 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6192 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | |
| 6194 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6195 | rcu_read_lock(); |
| 6196 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6198 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6199 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6200 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6201 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6202 | * as sync_io is counted when a request starts, and |
| 6203 | * disk_stats is counted when it completes. |
| 6204 | * So resync activity will cause curr_events to be smaller than |
| 6205 | * when there was no such activity. |
| 6206 | * non-sync IO will cause disk_stat to increase without |
| 6207 | * increasing sync_io so curr_events will (eventually) |
| 6208 | * be larger than it was before. Once it becomes |
| 6209 | * substantially larger, the test below will cause |
| 6210 | * the array to appear non-idle, and resync will slow |
| 6211 | * down. |
| 6212 | * If there is a lot of outstanding resync activity when |
| 6213 | * we set last_event to curr_events, then all that activity |
| 6214 | * completing might cause the array to appear non-idle |
| 6215 | * and resync will be slowed down even though there might |
| 6216 | * not have been non-resync activity. This will only |
| 6217 | * happen once though. 'last_events' will soon reflect |
| 6218 | * the state where there is little or no outstanding |
| 6219 | * resync requests, and further resync activity will |
| 6220 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6221 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6223 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | rdev->last_events = curr_events; |
| 6225 | idle = 0; |
| 6226 | } |
| 6227 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6228 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | return idle; |
| 6230 | } |
| 6231 | |
| 6232 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6233 | { |
| 6234 | /* another "blocks" (512byte) blocks have been synced */ |
| 6235 | atomic_sub(blocks, &mddev->recovery_active); |
| 6236 | wake_up(&mddev->recovery_wait); |
| 6237 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6238 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | md_wakeup_thread(mddev->thread); |
| 6240 | // stop recovery, signal do_sync .... |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6245 | /* md_write_start(mddev, bi) |
| 6246 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6247 | * in superblock) before writing, schedule a superblock update |
| 6248 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6249 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6250 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6252 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6253 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6254 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6255 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6256 | BUG_ON(mddev->ro == 1); |
| 6257 | if (mddev->ro == 2) { |
| 6258 | /* need to switch to read/write */ |
| 6259 | mddev->ro = 0; |
| 6260 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6261 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6262 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6263 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6264 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6265 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6266 | if (mddev->safemode == 1) |
| 6267 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6268 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6269 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6270 | if (mddev->in_sync) { |
| 6271 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6272 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6273 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6274 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6275 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6276 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6277 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6278 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6279 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6280 | wait_event(mddev->sb_wait, |
| 6281 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6282 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | void md_write_end(mddev_t *mddev) |
| 6286 | { |
| 6287 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6288 | if (mddev->safemode == 2) |
| 6289 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6290 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6292 | } |
| 6293 | } |
| 6294 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6295 | /* md_allow_write(mddev) |
| 6296 | * Calling this ensures that the array is marked 'active' so that writes |
| 6297 | * may proceed without blocking. It is important to call this before |
| 6298 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6299 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6300 | * |
| 6301 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6302 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6303 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6304 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6305 | { |
| 6306 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6307 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6308 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6309 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6310 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6311 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6312 | |
| 6313 | spin_lock_irq(&mddev->write_lock); |
| 6314 | if (mddev->in_sync) { |
| 6315 | mddev->in_sync = 0; |
| 6316 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6317 | if (mddev->safemode_delay && |
| 6318 | mddev->safemode == 0) |
| 6319 | mddev->safemode = 1; |
| 6320 | spin_unlock_irq(&mddev->write_lock); |
| 6321 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6322 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6323 | } else |
| 6324 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6325 | |
| 6326 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6327 | return -EAGAIN; |
| 6328 | else |
| 6329 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6330 | } |
| 6331 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | #define SYNC_MARKS 10 |
| 6334 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6335 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | { |
| 6337 | mddev_t *mddev2; |
| 6338 | unsigned int currspeed = 0, |
| 6339 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6340 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | unsigned long mark[SYNC_MARKS]; |
| 6342 | sector_t mark_cnt[SYNC_MARKS]; |
| 6343 | int last_mark,m; |
| 6344 | struct list_head *tmp; |
| 6345 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6346 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6347 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6348 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6349 | |
| 6350 | /* just incase thread restarts... */ |
| 6351 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6352 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6353 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6354 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6355 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6356 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6357 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6358 | desc = "data-check"; |
| 6359 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6360 | desc = "requested-resync"; |
| 6361 | else |
| 6362 | desc = "resync"; |
| 6363 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6364 | desc = "reshape"; |
| 6365 | else |
| 6366 | desc = "recovery"; |
| 6367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | /* we overload curr_resync somewhat here. |
| 6369 | * 0 == not engaged in resync at all |
| 6370 | * 2 == checking that there is no conflict with another sync |
| 6371 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6372 | * commense |
| 6373 | * other == active in resync - this many blocks |
| 6374 | * |
| 6375 | * Before starting a resync we must have set curr_resync to |
| 6376 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6377 | * less than ours. When we find one that is the same or higher |
| 6378 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6379 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6380 | * This will mean we have to start checking from the beginning again. |
| 6381 | * |
| 6382 | */ |
| 6383 | |
| 6384 | do { |
| 6385 | mddev->curr_resync = 2; |
| 6386 | |
| 6387 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6388 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6389 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | goto skip; |
| 6391 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6392 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | if (mddev2 == mddev) |
| 6394 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6395 | if (!mddev->parallel_resync |
| 6396 | && mddev2->curr_resync |
| 6397 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | DEFINE_WAIT(wq); |
| 6399 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6400 | /* arbitrarily yield */ |
| 6401 | mddev->curr_resync = 1; |
| 6402 | wake_up(&resync_wait); |
| 6403 | } |
| 6404 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6405 | /* no need to wait here, we can wait the next |
| 6406 | * time 'round when curr_resync == 2 |
| 6407 | */ |
| 6408 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6409 | /* We need to wait 'interruptible' so as not to |
| 6410 | * contribute to the load average, and not to |
| 6411 | * be caught by 'softlockup' |
| 6412 | */ |
| 6413 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6414 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6415 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6416 | printk(KERN_INFO "md: delaying %s of %s" |
| 6417 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6419 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6421 | if (signal_pending(current)) |
| 6422 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | schedule(); |
| 6424 | finish_wait(&resync_wait, &wq); |
| 6425 | goto try_again; |
| 6426 | } |
| 6427 | finish_wait(&resync_wait, &wq); |
| 6428 | } |
| 6429 | } |
| 6430 | } while (mddev->curr_resync < 2); |
| 6431 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6432 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6433 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6435 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | */ |
| 6437 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6438 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6439 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6440 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6441 | j = mddev->resync_min; |
| 6442 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6443 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6444 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6445 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6446 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6447 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6449 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6450 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6451 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6452 | if (rdev->raid_disk >= 0 && |
| 6453 | !test_bit(Faulty, &rdev->flags) && |
| 6454 | !test_bit(In_sync, &rdev->flags) && |
| 6455 | rdev->recovery_offset < j) |
| 6456 | j = rdev->recovery_offset; |
| 6457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6459 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6460 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6461 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6462 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6463 | "(but not more than %d KB/sec) for %s.\n", |
| 6464 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6466 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6467 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6468 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6470 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6471 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | } |
| 6473 | last_mark = 0; |
| 6474 | mddev->resync_mark = mark[last_mark]; |
| 6475 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6476 | |
| 6477 | /* |
| 6478 | * Tune reconstruction: |
| 6479 | */ |
| 6480 | window = 32*(PAGE_SIZE/512); |
| 6481 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6482 | window/2,(unsigned long long) max_sectors/2); |
| 6483 | |
| 6484 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | last_check = 0; |
| 6486 | |
| 6487 | if (j>2) { |
| 6488 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6489 | "md: resuming %s of %s from checkpoint.\n", |
| 6490 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | mddev->curr_resync = j; |
| 6492 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6493 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
| 6495 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6496 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6498 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6499 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6500 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6501 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6502 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6503 | > (max_sectors >> 4)) || |
| 6504 | (j - mddev->curr_resync_completed)*2 |
| 6505 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6506 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6507 | /* time to update curr_resync_completed */ |
| 6508 | blk_unplug(mddev->queue); |
| 6509 | wait_event(mddev->recovery_wait, |
| 6510 | atomic_read(&mddev->recovery_active) == 0); |
| 6511 | mddev->curr_resync_completed = |
| 6512 | mddev->curr_resync; |
| 6513 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6514 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6515 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6516 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6517 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6518 | /* As this condition is controlled by user-space, |
| 6519 | * we can block indefinitely, so use '_interruptible' |
| 6520 | * to avoid triggering warnings. |
| 6521 | */ |
| 6522 | flush_signals(current); /* just in case */ |
| 6523 | wait_event_interruptible(mddev->recovery_wait, |
| 6524 | mddev->resync_max > j |
| 6525 | || kthread_should_stop()); |
| 6526 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6527 | |
| 6528 | if (kthread_should_stop()) |
| 6529 | goto interrupted; |
| 6530 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6531 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6532 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6533 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6534 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | goto out; |
| 6536 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6537 | |
| 6538 | if (!skipped) { /* actual IO requested */ |
| 6539 | io_sectors += sectors; |
| 6540 | atomic_add(sectors, &mddev->recovery_active); |
| 6541 | } |
| 6542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | j += sectors; |
| 6544 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6545 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6546 | if (last_check == 0) |
| 6547 | /* this is the earliers that rebuilt will be |
| 6548 | * visible in /proc/mdstat |
| 6549 | */ |
| 6550 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6551 | |
| 6552 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | continue; |
| 6554 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6555 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6557 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | break; |
| 6559 | |
| 6560 | repeat: |
| 6561 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6562 | /* step marks */ |
| 6563 | int next = (last_mark+1) % SYNC_MARKS; |
| 6564 | |
| 6565 | mddev->resync_mark = mark[next]; |
| 6566 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6567 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6568 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6569 | last_mark = next; |
| 6570 | } |
| 6571 | |
| 6572 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6573 | if (kthread_should_stop()) |
| 6574 | goto interrupted; |
| 6575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | |
| 6577 | /* |
| 6578 | * this loop exits only if either when we are slower than |
| 6579 | * the 'hard' speed limit, or the system was IO-idle for |
| 6580 | * a jiffy. |
| 6581 | * the system might be non-idle CPU-wise, but we only care |
| 6582 | * about not overloading the IO subsystem. (things like an |
| 6583 | * e2fsck being done on the RAID array should execute fast) |
| 6584 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6585 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | cond_resched(); |
| 6587 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6588 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6589 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6591 | if (currspeed > speed_min(mddev)) { |
| 6592 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6593 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6594 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | goto repeat; |
| 6596 | } |
| 6597 | } |
| 6598 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6599 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | /* |
| 6601 | * this also signals 'finished resyncing' to md_stop |
| 6602 | */ |
| 6603 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6604 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | |
| 6606 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6607 | |
| 6608 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6609 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6611 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6612 | mddev->curr_resync > 2) { |
| 6613 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6614 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6615 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6616 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6617 | "md: checkpointing %s of %s.\n", |
| 6618 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6619 | mddev->recovery_cp = mddev->curr_resync; |
| 6620 | } |
| 6621 | } else |
| 6622 | mddev->recovery_cp = MaxSector; |
| 6623 | } else { |
| 6624 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6625 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6626 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6627 | if (rdev->raid_disk >= 0 && |
| 6628 | !test_bit(Faulty, &rdev->flags) && |
| 6629 | !test_bit(In_sync, &rdev->flags) && |
| 6630 | rdev->recovery_offset < mddev->curr_resync) |
| 6631 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6634 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6637 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6638 | /* We completed so min/max setting can be forgotten if used. */ |
| 6639 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6640 | mddev->resync_min = 0; |
| 6641 | mddev->resync_max = MaxSector; |
| 6642 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6643 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6645 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6646 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6647 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | wake_up(&resync_wait); |
| 6649 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6650 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6651 | return; |
| 6652 | |
| 6653 | interrupted: |
| 6654 | /* |
| 6655 | * got a signal, exit. |
| 6656 | */ |
| 6657 | printk(KERN_INFO |
| 6658 | "md: md_do_sync() got signal ... exiting\n"); |
| 6659 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6660 | goto out; |
| 6661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6663 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | |
| 6665 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6666 | static int remove_and_add_spares(mddev_t *mddev) |
| 6667 | { |
| 6668 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6669 | int spares = 0; |
| 6670 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6671 | mddev->curr_resync_completed = 0; |
| 6672 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6673 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6674 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6675 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6676 | (test_bit(Faulty, &rdev->flags) || |
| 6677 | ! test_bit(In_sync, &rdev->flags)) && |
| 6678 | atomic_read(&rdev->nr_pending)==0) { |
| 6679 | if (mddev->pers->hot_remove_disk( |
| 6680 | mddev, rdev->raid_disk)==0) { |
| 6681 | char nm[20]; |
| 6682 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6683 | sysfs_remove_link(&mddev->kobj, nm); |
| 6684 | rdev->raid_disk = -1; |
| 6685 | } |
| 6686 | } |
| 6687 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6688 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6689 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6690 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6691 | !test_bit(In_sync, &rdev->flags) && |
| 6692 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6693 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6694 | if (rdev->raid_disk < 0 |
| 6695 | && !test_bit(Faulty, &rdev->flags)) { |
| 6696 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6697 | if (mddev->pers-> |
| 6698 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6699 | char nm[20]; |
| 6700 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6701 | if (sysfs_create_link(&mddev->kobj, |
| 6702 | &rdev->kobj, nm)) |
| 6703 | printk(KERN_WARNING |
| 6704 | "md: cannot register " |
| 6705 | "%s for %s\n", |
| 6706 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6707 | spares++; |
| 6708 | md_new_event(mddev); |
| 6709 | } else |
| 6710 | break; |
| 6711 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6712 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6713 | } |
| 6714 | return spares; |
| 6715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | /* |
| 6717 | * This routine is regularly called by all per-raid-array threads to |
| 6718 | * deal with generic issues like resync and super-block update. |
| 6719 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6720 | * need this as they never do any recovery or update the superblock. |
| 6721 | * |
| 6722 | * It does not do any resync itself, but rather "forks" off other threads |
| 6723 | * to do that as needed. |
| 6724 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6725 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6726 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | * and wakeups up this thread which will reap the thread and finish up. |
| 6728 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6729 | * |
| 6730 | * The overall approach is: |
| 6731 | * 1/ if the superblock needs updating, update it. |
| 6732 | * 2/ If a recovery thread is running, don't do anything else. |
| 6733 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6734 | * 4/ If there are any faulty devices, remove them. |
| 6735 | * 5/ If array is degraded, try to add spares devices |
| 6736 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6737 | */ |
| 6738 | void md_check_recovery(mddev_t *mddev) |
| 6739 | { |
| 6740 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | |
| 6742 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6743 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6744 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6745 | |
| 6746 | if (mddev->ro) |
| 6747 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6748 | |
| 6749 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6750 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6751 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6752 | mdname(mddev)); |
| 6753 | mddev->safemode = 2; |
| 6754 | } |
| 6755 | flush_signals(current); |
| 6756 | } |
| 6757 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6758 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6759 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6761 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6763 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6764 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6765 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6766 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | )) |
| 6768 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6769 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6770 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6771 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6772 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6773 | if (mddev->ro) { |
| 6774 | /* Only thing we do on a ro array is remove |
| 6775 | * failed devices. |
| 6776 | */ |
| 6777 | remove_and_add_spares(mddev); |
| 6778 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6779 | goto unlock; |
| 6780 | } |
| 6781 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6782 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6783 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6784 | spin_lock_irq(&mddev->write_lock); |
| 6785 | if (mddev->safemode && |
| 6786 | !atomic_read(&mddev->writes_pending) && |
| 6787 | !mddev->in_sync && |
| 6788 | mddev->recovery_cp == MaxSector) { |
| 6789 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6790 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6791 | if (mddev->persistent) |
| 6792 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6793 | } |
| 6794 | if (mddev->safemode == 1) |
| 6795 | mddev->safemode = 0; |
| 6796 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6797 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6798 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6799 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6800 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6801 | if (mddev->flags) |
| 6802 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6803 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6804 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6805 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6806 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6807 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6810 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6811 | /* resync/recovery still happening */ |
| 6812 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6813 | goto unlock; |
| 6814 | } |
| 6815 | if (mddev->sync_thread) { |
| 6816 | /* resync has finished, collect result */ |
| 6817 | md_unregister_thread(mddev->sync_thread); |
| 6818 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6819 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6820 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | /* success...*/ |
| 6822 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6823 | if (mddev->pers->spare_active(mddev)) |
| 6824 | sysfs_notify(&mddev->kobj, NULL, |
| 6825 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6827 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6828 | mddev->pers->finish_reshape) |
| 6829 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6830 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6831 | |
| 6832 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6833 | * information must be scrapped |
| 6834 | */ |
| 6835 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6836 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6837 | rdev->saved_raid_disk = -1; |
| 6838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | mddev->recovery = 0; |
| 6840 | /* flag recovery needed just to double check */ |
| 6841 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6842 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6843 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | goto unlock; |
| 6845 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6846 | /* Set RUNNING before clearing NEEDED to avoid |
| 6847 | * any transients in the value of "sync_action". |
| 6848 | */ |
| 6849 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6850 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6851 | /* Clear some bits that don't mean anything, but |
| 6852 | * might be left set |
| 6853 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6854 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6855 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6857 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6858 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6859 | /* no recovery is running. |
| 6860 | * remove any failed drives, then |
| 6861 | * add spares if possible. |
| 6862 | * Spare are also removed and re-added, to allow |
| 6863 | * the personality to fail the re-add. |
| 6864 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6866 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6867 | if (mddev->pers->check_reshape == NULL || |
| 6868 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6869 | /* Cannot proceed */ |
| 6870 | goto unlock; |
| 6871 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6872 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6873 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6874 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6875 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6876 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6877 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6878 | } else if (mddev->recovery_cp < MaxSector) { |
| 6879 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6880 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6881 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6882 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6886 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6887 | /* We are adding a device or devices to an array |
| 6888 | * which has the bitmap stored on all devices. |
| 6889 | * So make sure all bitmap pages get written |
| 6890 | */ |
| 6891 | bitmap_write_all(mddev->bitmap); |
| 6892 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6894 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6895 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | if (!mddev->sync_thread) { |
| 6897 | printk(KERN_ERR "%s: could not start resync" |
| 6898 | " thread...\n", |
| 6899 | mdname(mddev)); |
| 6900 | /* leave the spares where they are, it shouldn't hurt */ |
| 6901 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6902 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6904 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6905 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | } |
| 6907 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6908 | if (!mddev->sync_thread) { |
| 6909 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6910 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6911 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6912 | if (mddev->sysfs_action) |
| 6913 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | mddev_unlock(mddev); |
| 6916 | } |
| 6917 | } |
| 6918 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6919 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6920 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6921 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6922 | wait_event_timeout(rdev->blocked_wait, |
| 6923 | !test_bit(Blocked, &rdev->flags), |
| 6924 | msecs_to_jiffies(5000)); |
| 6925 | rdev_dec_pending(rdev, mddev); |
| 6926 | } |
| 6927 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6928 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6929 | static int md_notify_reboot(struct notifier_block *this, |
| 6930 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | { |
| 6932 | struct list_head *tmp; |
| 6933 | mddev_t *mddev; |
| 6934 | |
| 6935 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6936 | |
| 6937 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6938 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6939 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6940 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6941 | /* Force a switch to readonly even array |
| 6942 | * appears to still be in use. Hence |
| 6943 | * the '100'. |
| 6944 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6945 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6946 | mddev_unlock(mddev); |
| 6947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | /* |
| 6949 | * certain more exotic SCSI devices are known to be |
| 6950 | * volatile wrt too early system reboots. While the |
| 6951 | * right place to handle this issue is the given |
| 6952 | * driver, we do want to have a safe RAID driver ... |
| 6953 | */ |
| 6954 | mdelay(1000*1); |
| 6955 | } |
| 6956 | return NOTIFY_DONE; |
| 6957 | } |
| 6958 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6959 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | .notifier_call = md_notify_reboot, |
| 6961 | .next = NULL, |
| 6962 | .priority = INT_MAX, /* before any real devices */ |
| 6963 | }; |
| 6964 | |
| 6965 | static void md_geninit(void) |
| 6966 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6968 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6969 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | } |
| 6971 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6972 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6974 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | return -1; |
| 6976 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6977 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | return -1; |
| 6979 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6980 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6981 | md_probe, NULL, NULL); |
| 6982 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | md_probe, NULL, NULL); |
| 6984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6986 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | |
| 6988 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6989 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | |
| 6993 | #ifndef MODULE |
| 6994 | |
| 6995 | /* |
| 6996 | * Searches all registered partitions for autorun RAID arrays |
| 6997 | * at boot time. |
| 6998 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6999 | |
| 7000 | static LIST_HEAD(all_detected_devices); |
| 7001 | struct detected_devices_node { |
| 7002 | struct list_head list; |
| 7003 | dev_t dev; |
| 7004 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | |
| 7006 | void md_autodetect_dev(dev_t dev) |
| 7007 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7008 | struct detected_devices_node *node_detected_dev; |
| 7009 | |
| 7010 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7011 | if (node_detected_dev) { |
| 7012 | node_detected_dev->dev = dev; |
| 7013 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7014 | } else { |
| 7015 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7016 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | |
| 7021 | static void autostart_arrays(int part) |
| 7022 | { |
| 7023 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7024 | struct detected_devices_node *node_detected_dev; |
| 7025 | dev_t dev; |
| 7026 | int i_scanned, i_passed; |
| 7027 | |
| 7028 | i_scanned = 0; |
| 7029 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | |
| 7031 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7032 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7033 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7034 | i_scanned++; |
| 7035 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7036 | struct detected_devices_node, list); |
| 7037 | list_del(&node_detected_dev->list); |
| 7038 | dev = node_detected_dev->dev; |
| 7039 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7040 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | if (IS_ERR(rdev)) |
| 7042 | continue; |
| 7043 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7044 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | MD_BUG(); |
| 7046 | continue; |
| 7047 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7048 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7050 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7052 | |
| 7053 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7054 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | |
| 7056 | autorun_devices(part); |
| 7057 | } |
| 7058 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7059 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | |
| 7061 | static __exit void md_exit(void) |
| 7062 | { |
| 7063 | mddev_t *mddev; |
| 7064 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7065 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7066 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7067 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7069 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | unregister_blkdev(mdp_major, "mdp"); |
| 7071 | unregister_reboot_notifier(&md_notifier); |
| 7072 | unregister_sysctl_table(raid_table_header); |
| 7073 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7074 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7076 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | } |
| 7078 | } |
| 7079 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7080 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | module_exit(md_exit) |
| 7082 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7083 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7084 | { |
| 7085 | return sprintf(buffer, "%d", start_readonly); |
| 7086 | } |
| 7087 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7088 | { |
| 7089 | char *e; |
| 7090 | int num = simple_strtoul(val, &e, 10); |
| 7091 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7092 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7093 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7094 | } |
| 7095 | return -EINVAL; |
| 7096 | } |
| 7097 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7098 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7099 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7100 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7101 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | EXPORT_SYMBOL(register_md_personality); |
| 7104 | EXPORT_SYMBOL(unregister_md_personality); |
| 7105 | EXPORT_SYMBOL(md_error); |
| 7106 | EXPORT_SYMBOL(md_done_sync); |
| 7107 | EXPORT_SYMBOL(md_write_start); |
| 7108 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | EXPORT_SYMBOL(md_register_thread); |
| 7110 | EXPORT_SYMBOL(md_unregister_thread); |
| 7111 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | EXPORT_SYMBOL(md_check_recovery); |
| 7113 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7114 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7115 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |