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 | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 466 | mutex_init(&new->bitmap_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 | { |
| 853 | if (!mddev->bitmap_file && !mddev->bitmap_offset) |
| 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 | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 981 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 982 | mddev->default_bitmap_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 | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1016 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1017 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1018 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1019 | } else if (mddev->pers == NULL) { |
| 1020 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | ++ev1; |
| 1022 | if (ev1 < mddev->events) |
| 1023 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1024 | } else if (mddev->bitmap) { |
| 1025 | /* if adding to array with a bitmap, then we can accept an |
| 1026 | * older device ... but not too old. |
| 1027 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1028 | if (ev1 < mddev->bitmap->events_cleared) |
| 1029 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1030 | } else { |
| 1031 | if (ev1 < mddev->events) |
| 1032 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1033 | return 0; |
| 1034 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | desc = sb->disks + rdev->desc_nr; |
| 1038 | |
| 1039 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1040 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1041 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1042 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1043 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1045 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1046 | /* active but not in sync implies recovery up to |
| 1047 | * reshape position. We don't know exactly where |
| 1048 | * that is, so set to zero for now */ |
| 1049 | if (mddev->minor_version >= 91) { |
| 1050 | rdev->recovery_offset = 0; |
| 1051 | rdev->raid_disk = desc->raid_disk; |
| 1052 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1054 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1055 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1056 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1057 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * sync_super for 0.90.0 |
| 1063 | */ |
| 1064 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1065 | { |
| 1066 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | mdk_rdev_t *rdev2; |
| 1068 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* make rdev->sb match mddev data.. |
| 1072 | * |
| 1073 | * 1/ zero out disks |
| 1074 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1075 | * 3/ any empty disks < next_spare become removed |
| 1076 | * |
| 1077 | * disks[0] gets initialised to REMOVED because |
| 1078 | * we cannot be sure from other fields if it has |
| 1079 | * been initialised or not. |
| 1080 | */ |
| 1081 | int i; |
| 1082 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1083 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1084 | rdev->sb_size = MD_SB_BYTES; |
| 1085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1087 | |
| 1088 | memset(sb, 0, sizeof(*sb)); |
| 1089 | |
| 1090 | sb->md_magic = MD_SB_MAGIC; |
| 1091 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | sb->patch_version = mddev->patch_version; |
| 1093 | sb->gvalid_words = 0; /* ignored */ |
| 1094 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1095 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1096 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1097 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1098 | |
| 1099 | sb->ctime = mddev->ctime; |
| 1100 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1101 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | sb->raid_disks = mddev->raid_disks; |
| 1103 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1104 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | sb->utime = mddev->utime; |
| 1106 | sb->state = 0; |
| 1107 | sb->events_hi = (mddev->events>>32); |
| 1108 | sb->events_lo = (u32)mddev->events; |
| 1109 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1110 | if (mddev->reshape_position == MaxSector) |
| 1111 | sb->minor_version = 90; |
| 1112 | else { |
| 1113 | sb->minor_version = 91; |
| 1114 | sb->reshape_position = mddev->reshape_position; |
| 1115 | sb->new_level = mddev->new_level; |
| 1116 | sb->delta_disks = mddev->delta_disks; |
| 1117 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1118 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1119 | } |
| 1120 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | if (mddev->in_sync) |
| 1122 | { |
| 1123 | sb->recovery_cp = mddev->recovery_cp; |
| 1124 | sb->cp_events_hi = (mddev->events>>32); |
| 1125 | sb->cp_events_lo = (u32)mddev->events; |
| 1126 | if (mddev->recovery_cp == MaxSector) |
| 1127 | sb->state = (1<< MD_SB_CLEAN); |
| 1128 | } else |
| 1129 | sb->recovery_cp = 0; |
| 1130 | |
| 1131 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1132 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1134 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1135 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1138 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1140 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1141 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1142 | |
| 1143 | if (rdev2->raid_disk >= 0 && |
| 1144 | sb->minor_version >= 91) |
| 1145 | /* we have nowhere to store the recovery_offset, |
| 1146 | * but if it is not below the reshape_position, |
| 1147 | * we can piggy-back on that. |
| 1148 | */ |
| 1149 | is_active = 1; |
| 1150 | if (rdev2->raid_disk < 0 || |
| 1151 | test_bit(Faulty, &rdev2->flags)) |
| 1152 | is_active = 0; |
| 1153 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1154 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1156 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1157 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | d = &sb->disks[rdev2->desc_nr]; |
| 1159 | nr_disks++; |
| 1160 | d->number = rdev2->desc_nr; |
| 1161 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1162 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1163 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | d->raid_disk = rdev2->raid_disk; |
| 1165 | else |
| 1166 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1167 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1169 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1171 | if (test_bit(In_sync, &rdev2->flags)) |
| 1172 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | active++; |
| 1174 | working++; |
| 1175 | } else { |
| 1176 | d->state = 0; |
| 1177 | spare++; |
| 1178 | working++; |
| 1179 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1180 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1181 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1184 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1185 | mdp_disk_t *d = &sb->disks[i]; |
| 1186 | if (d->state == 0 && d->number == 0) { |
| 1187 | d->number = i; |
| 1188 | d->raid_disk = i; |
| 1189 | d->state = (1<<MD_DISK_REMOVED); |
| 1190 | d->state |= (1<<MD_DISK_FAULTY); |
| 1191 | failed++; |
| 1192 | } |
| 1193 | } |
| 1194 | sb->nr_disks = nr_disks; |
| 1195 | sb->active_disks = active; |
| 1196 | sb->working_disks = working; |
| 1197 | sb->failed_disks = failed; |
| 1198 | sb->spare_disks = spare; |
| 1199 | |
| 1200 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1201 | sb->sb_csum = calc_sb_csum(sb); |
| 1202 | } |
| 1203 | |
| 1204 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1205 | * rdev_size_change for 0.90.0 |
| 1206 | */ |
| 1207 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1208 | 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] | 1209 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1210 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1211 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1212 | if (rdev->mddev->bitmap_offset) |
| 1213 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1214 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1215 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1216 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1217 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1218 | rdev->sb_page); |
| 1219 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1220 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | * version 1 superblock |
| 1226 | */ |
| 1227 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1228 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1230 | __le32 disk_csum; |
| 1231 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | unsigned long long newcsum; |
| 1233 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1234 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | int i; |
| 1236 | |
| 1237 | disk_csum = sb->sb_csum; |
| 1238 | sb->sb_csum = 0; |
| 1239 | newcsum = 0; |
| 1240 | for (i=0; size>=4; size -= 4 ) |
| 1241 | newcsum += le32_to_cpu(*isuper++); |
| 1242 | |
| 1243 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1244 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1247 | sb->sb_csum = disk_csum; |
| 1248 | return cpu_to_le32(csum); |
| 1249 | } |
| 1250 | |
| 1251 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1252 | { |
| 1253 | struct mdp_superblock_1 *sb; |
| 1254 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1255 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1257 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1260 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | * It is always aligned to a 4K boundary and |
| 1262 | * depeding on minor_version, it can be: |
| 1263 | * 0: At least 8K, but less than 12K, from end of device |
| 1264 | * 1: At start of device |
| 1265 | * 2: 4K from start of device. |
| 1266 | */ |
| 1267 | switch(minor_version) { |
| 1268 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1269 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1270 | sb_start -= 8*2; |
| 1271 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1274 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | break; |
| 1276 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1277 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | break; |
| 1279 | default: |
| 1280 | return -EINVAL; |
| 1281 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1282 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1284 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1285 | * and it is safe to read 4k, so we do that |
| 1286 | */ |
| 1287 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | if (ret) return ret; |
| 1289 | |
| 1290 | |
| 1291 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1292 | |
| 1293 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1294 | sb->major_version != cpu_to_le32(1) || |
| 1295 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1296 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1297 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | return -EINVAL; |
| 1299 | |
| 1300 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1301 | printk("md: invalid superblock checksum on %s\n", |
| 1302 | bdevname(rdev->bdev,b)); |
| 1303 | return -EINVAL; |
| 1304 | } |
| 1305 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1306 | printk("md: data_size too small on %s\n", |
| 1307 | bdevname(rdev->bdev,b)); |
| 1308 | return -EINVAL; |
| 1309 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | rdev->preferred_minor = 0xffff; |
| 1312 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1313 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1315 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1316 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1317 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1318 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1319 | |
| 1320 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1321 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1322 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1323 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1324 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1325 | rdev->desc_nr = -1; |
| 1326 | else |
| 1327 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1328 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1329 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1330 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1331 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | __u64 ev1, ev2; |
| 1333 | struct mdp_superblock_1 *refsb = |
| 1334 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1335 | |
| 1336 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1337 | sb->level != refsb->level || |
| 1338 | sb->layout != refsb->layout || |
| 1339 | sb->chunksize != refsb->chunksize) { |
| 1340 | printk(KERN_WARNING "md: %s has strangely different" |
| 1341 | " superblock to %s\n", |
| 1342 | bdevname(rdev->bdev,b), |
| 1343 | bdevname(refdev->bdev,b2)); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | ev1 = le64_to_cpu(sb->events); |
| 1347 | ev2 = le64_to_cpu(refsb->events); |
| 1348 | |
| 1349 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1350 | ret = 1; |
| 1351 | else |
| 1352 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1354 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1355 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1356 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1358 | rdev->sectors = rdev->sb_start; |
| 1359 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1361 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1362 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1363 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1364 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1368 | { |
| 1369 | 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] | 1370 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1372 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1373 | clear_bit(Faulty, &rdev->flags); |
| 1374 | clear_bit(In_sync, &rdev->flags); |
| 1375 | clear_bit(WriteMostly, &rdev->flags); |
| 1376 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | if (mddev->raid_disks == 0) { |
| 1379 | mddev->major_version = 1; |
| 1380 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1381 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1382 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1384 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1385 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1386 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | mddev->layout = le32_to_cpu(sb->layout); |
| 1388 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1389 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1390 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1391 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1392 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1395 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1396 | |
| 1397 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1398 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1399 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1400 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1401 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1402 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1403 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1404 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1405 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1406 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1407 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1408 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1409 | } else { |
| 1410 | mddev->reshape_position = MaxSector; |
| 1411 | mddev->delta_disks = 0; |
| 1412 | mddev->new_level = mddev->level; |
| 1413 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1414 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1417 | } else if (mddev->pers == NULL) { |
| 1418 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | ++ev1; |
| 1420 | if (ev1 < mddev->events) |
| 1421 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1422 | } else if (mddev->bitmap) { |
| 1423 | /* If adding to array with a bitmap, then we can accept an |
| 1424 | * older device, but not too old. |
| 1425 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1426 | if (ev1 < mddev->bitmap->events_cleared) |
| 1427 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1428 | } else { |
| 1429 | if (ev1 < mddev->events) |
| 1430 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1431 | return 0; |
| 1432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1434 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1435 | if (rdev->desc_nr < 0 || |
| 1436 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1437 | role = 0xffff; |
| 1438 | rdev->desc_nr = -1; |
| 1439 | } else |
| 1440 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | switch(role) { |
| 1442 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | break; |
| 1444 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1445 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | break; |
| 1447 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1448 | if ((le32_to_cpu(sb->feature_map) & |
| 1449 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1450 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1451 | else |
| 1452 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | rdev->raid_disk = role; |
| 1454 | break; |
| 1455 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1456 | if (sb->devflags & WriteMostly1) |
| 1457 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1458 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1459 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1465 | { |
| 1466 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | mdk_rdev_t *rdev2; |
| 1468 | int max_dev, i; |
| 1469 | /* make rdev->sb match mddev and rdev data. */ |
| 1470 | |
| 1471 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1472 | |
| 1473 | sb->feature_map = 0; |
| 1474 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1475 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1477 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1478 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1479 | |
| 1480 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1481 | sb->events = cpu_to_le64(mddev->events); |
| 1482 | if (mddev->in_sync) |
| 1483 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1484 | else |
| 1485 | sb->resync_offset = cpu_to_le64(0); |
| 1486 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1487 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1488 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1489 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1490 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1491 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1492 | sb->level = cpu_to_le32(mddev->level); |
| 1493 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1494 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1495 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1496 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1497 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1498 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1499 | |
| 1500 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1501 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1502 | if (rdev->recovery_offset > 0) { |
| 1503 | sb->feature_map |= |
| 1504 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1505 | sb->recovery_offset = |
| 1506 | cpu_to_le64(rdev->recovery_offset); |
| 1507 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1510 | if (mddev->reshape_position != MaxSector) { |
| 1511 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1512 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1513 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1514 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1515 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1516 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1517 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1520 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | if (rdev2->desc_nr+1 > max_dev) |
| 1522 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1523 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1524 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1525 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1526 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1527 | rdev->sb_size = max_dev * 2 + 256; |
| 1528 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1529 | if (rdev->sb_size & bmask) |
| 1530 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | for (i=0; i<max_dev;i++) |
| 1533 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1534 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1535 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1537 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1539 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1541 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1542 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | else |
| 1544 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1545 | } |
| 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1548 | } |
| 1549 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1550 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1551 | 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] | 1552 | { |
| 1553 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1554 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1555 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1556 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1557 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1558 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1559 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1560 | max_sectors -= rdev->data_offset; |
| 1561 | if (!num_sectors || num_sectors > max_sectors) |
| 1562 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1563 | } else if (rdev->mddev->bitmap_offset) { |
| 1564 | /* minor version 0 with bitmap we can't move */ |
| 1565 | return 0; |
| 1566 | } else { |
| 1567 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1568 | sector_t sb_start; |
| 1569 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1570 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1571 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1572 | if (!num_sectors || num_sectors > max_sectors) |
| 1573 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1574 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1575 | } |
| 1576 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1577 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1578 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1579 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1580 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1581 | rdev->sb_page); |
| 1582 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1583 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1586 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | [0] = { |
| 1588 | .name = "0.90.0", |
| 1589 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1590 | .load_super = super_90_load, |
| 1591 | .validate_super = super_90_validate, |
| 1592 | .sync_super = super_90_sync, |
| 1593 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | }, |
| 1595 | [1] = { |
| 1596 | .name = "md-1", |
| 1597 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1598 | .load_super = super_1_load, |
| 1599 | .validate_super = super_1_validate, |
| 1600 | .sync_super = super_1_sync, |
| 1601 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | }, |
| 1603 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
| 1605 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1606 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1607 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1609 | rcu_read_lock(); |
| 1610 | rdev_for_each_rcu(rdev, mddev1) |
| 1611 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1612 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1613 | rdev2->bdev->bd_contains) { |
| 1614 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1615 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1616 | } |
| 1617 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | static LIST_HEAD(pending_raid_disks); |
| 1622 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1623 | /* |
| 1624 | * Try to register data integrity profile for an mddev |
| 1625 | * |
| 1626 | * This is called when an array is started and after a disk has been kicked |
| 1627 | * from the array. It only succeeds if all working and active component devices |
| 1628 | * are integrity capable with matching profiles. |
| 1629 | */ |
| 1630 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1631 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1632 | mdk_rdev_t *rdev, *reference = NULL; |
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 | if (list_empty(&mddev->disks)) |
| 1635 | return 0; /* nothing to do */ |
| 1636 | if (blk_get_integrity(mddev->gendisk)) |
| 1637 | return 0; /* already registered */ |
| 1638 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1639 | /* skip spares and non-functional disks */ |
| 1640 | if (test_bit(Faulty, &rdev->flags)) |
| 1641 | continue; |
| 1642 | if (rdev->raid_disk < 0) |
| 1643 | continue; |
| 1644 | /* |
| 1645 | * If at least one rdev is not integrity capable, we can not |
| 1646 | * enable data integrity for the md device. |
| 1647 | */ |
| 1648 | if (!bdev_get_integrity(rdev->bdev)) |
| 1649 | return -EINVAL; |
| 1650 | if (!reference) { |
| 1651 | /* Use the first rdev as the reference */ |
| 1652 | reference = rdev; |
| 1653 | continue; |
| 1654 | } |
| 1655 | /* does this rdev's profile match the reference profile? */ |
| 1656 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1657 | rdev->bdev->bd_disk) < 0) |
| 1658 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1659 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1660 | /* |
| 1661 | * All component devices are integrity capable and have matching |
| 1662 | * profiles, register the common profile for the md device. |
| 1663 | */ |
| 1664 | if (blk_integrity_register(mddev->gendisk, |
| 1665 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1666 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1667 | mdname(mddev)); |
| 1668 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1669 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1670 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1671 | mdname(mddev)); |
| 1672 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1673 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1674 | EXPORT_SYMBOL(md_integrity_register); |
| 1675 | |
| 1676 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1677 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1678 | { |
| 1679 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1680 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1681 | |
| 1682 | if (!bi_mddev) /* nothing to do */ |
| 1683 | return; |
| 1684 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1685 | return; |
| 1686 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1687 | rdev->bdev->bd_disk) >= 0) |
| 1688 | return; |
| 1689 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1690 | blk_integrity_unregister(mddev->gendisk); |
| 1691 | } |
| 1692 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1695 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1696 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1697 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1698 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1699 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | if (rdev->mddev) { |
| 1702 | MD_BUG(); |
| 1703 | return -EINVAL; |
| 1704 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1705 | |
| 1706 | /* prevent duplicates */ |
| 1707 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1708 | return -EEXIST; |
| 1709 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1710 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1711 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1712 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1713 | if (mddev->pers) { |
| 1714 | /* Cannot change size, so fail |
| 1715 | * If mddev->level <= 0, then we don't care |
| 1716 | * about aligning sizes (e.g. linear) |
| 1717 | */ |
| 1718 | if (mddev->level > 0) |
| 1719 | return -ENOSPC; |
| 1720 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1721 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | |
| 1724 | /* Verify rdev->desc_nr is unique. |
| 1725 | * If it is -1, assign a free number, else |
| 1726 | * check number is not in use |
| 1727 | */ |
| 1728 | if (rdev->desc_nr < 0) { |
| 1729 | int choice = 0; |
| 1730 | if (mddev->pers) choice = mddev->raid_disks; |
| 1731 | while (find_rdev_nr(mddev, choice)) |
| 1732 | choice++; |
| 1733 | rdev->desc_nr = choice; |
| 1734 | } else { |
| 1735 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1736 | return -EBUSY; |
| 1737 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1738 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1739 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1740 | mdname(mddev), mddev->max_disks); |
| 1741 | return -EBUSY; |
| 1742 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1743 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1744 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1745 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1748 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1749 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1750 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1751 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1752 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1753 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1754 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1755 | kobject_del(&rdev->kobj); |
| 1756 | goto fail; |
| 1757 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1758 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1759 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1760 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1761 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1762 | |
| 1763 | /* May as well allow recovery to be retried once */ |
| 1764 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1767 | |
| 1768 | fail: |
| 1769 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1770 | b, mdname(mddev)); |
| 1771 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1774 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1775 | { |
| 1776 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1777 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1778 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1782 | { |
| 1783 | char b[BDEVNAME_SIZE]; |
| 1784 | if (!rdev->mddev) { |
| 1785 | MD_BUG(); |
| 1786 | return; |
| 1787 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1788 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1789 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1791 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1792 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1793 | sysfs_put(rdev->sysfs_state); |
| 1794 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1795 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1796 | * writing to 'remove' to "dev/state". We also need |
| 1797 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1798 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1799 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1800 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1801 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1802 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * prevent the device from being mounted, repartitioned or |
| 1807 | * otherwise reused by a RAID array (or any other kernel |
| 1808 | * subsystem), by bd_claiming the device. |
| 1809 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1810 | 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] | 1811 | { |
| 1812 | int err = 0; |
| 1813 | struct block_device *bdev; |
| 1814 | char b[BDEVNAME_SIZE]; |
| 1815 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1816 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | if (IS_ERR(bdev)) { |
| 1818 | printk(KERN_ERR "md: could not open %s.\n", |
| 1819 | __bdevname(dev, b)); |
| 1820 | return PTR_ERR(bdev); |
| 1821 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1822 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | if (err) { |
| 1824 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1825 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1826 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | return err; |
| 1828 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1829 | if (!shared) |
| 1830 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | rdev->bdev = bdev; |
| 1832 | return err; |
| 1833 | } |
| 1834 | |
| 1835 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1836 | { |
| 1837 | struct block_device *bdev = rdev->bdev; |
| 1838 | rdev->bdev = NULL; |
| 1839 | if (!bdev) |
| 1840 | MD_BUG(); |
| 1841 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1842 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | void md_autodetect_dev(dev_t dev); |
| 1846 | |
| 1847 | static void export_rdev(mdk_rdev_t * rdev) |
| 1848 | { |
| 1849 | char b[BDEVNAME_SIZE]; |
| 1850 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1851 | bdevname(rdev->bdev,b)); |
| 1852 | if (rdev->mddev) |
| 1853 | MD_BUG(); |
| 1854 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1856 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1857 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | #endif |
| 1859 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1860 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1864 | { |
| 1865 | unbind_rdev_from_array(rdev); |
| 1866 | export_rdev(rdev); |
| 1867 | } |
| 1868 | |
| 1869 | static void export_array(mddev_t *mddev) |
| 1870 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1871 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1873 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | if (!rdev->mddev) { |
| 1875 | MD_BUG(); |
| 1876 | continue; |
| 1877 | } |
| 1878 | kick_rdev_from_array(rdev); |
| 1879 | } |
| 1880 | if (!list_empty(&mddev->disks)) |
| 1881 | MD_BUG(); |
| 1882 | mddev->raid_disks = 0; |
| 1883 | mddev->major_version = 0; |
| 1884 | } |
| 1885 | |
| 1886 | static void print_desc(mdp_disk_t *desc) |
| 1887 | { |
| 1888 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1889 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1890 | } |
| 1891 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1892 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | { |
| 1894 | int i; |
| 1895 | |
| 1896 | printk(KERN_INFO |
| 1897 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1898 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1899 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1900 | sb->ctime); |
| 1901 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1902 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1903 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1904 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1905 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1906 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1907 | sb->failed_disks, sb->spare_disks, |
| 1908 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1909 | |
| 1910 | printk(KERN_INFO); |
| 1911 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1912 | mdp_disk_t *desc; |
| 1913 | |
| 1914 | desc = sb->disks + i; |
| 1915 | if (desc->number || desc->major || desc->minor || |
| 1916 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1917 | printk(" D %2d: ", i); |
| 1918 | print_desc(desc); |
| 1919 | } |
| 1920 | } |
| 1921 | printk(KERN_INFO "md: THIS: "); |
| 1922 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1925 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1926 | { |
| 1927 | __u8 *uuid; |
| 1928 | |
| 1929 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1930 | printk(KERN_INFO |
| 1931 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1932 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1933 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1934 | le32_to_cpu(sb->major_version), |
| 1935 | le32_to_cpu(sb->feature_map), |
| 1936 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1937 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1938 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1939 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1940 | sb->set_name, |
| 1941 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1942 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1943 | |
| 1944 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1945 | printk(KERN_INFO |
| 1946 | "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] | 1947 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1948 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1949 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1950 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1951 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1952 | le32_to_cpu(sb->level), |
| 1953 | (unsigned long long)le64_to_cpu(sb->size), |
| 1954 | le32_to_cpu(sb->raid_disks), |
| 1955 | le32_to_cpu(sb->layout), |
| 1956 | le32_to_cpu(sb->chunksize), |
| 1957 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1958 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1959 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1960 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1961 | le32_to_cpu(sb->dev_number), |
| 1962 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1963 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1964 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1965 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1966 | sb->devflags, |
| 1967 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1968 | (unsigned long long)le64_to_cpu(sb->events), |
| 1969 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1970 | le32_to_cpu(sb->sb_csum), |
| 1971 | le32_to_cpu(sb->max_dev) |
| 1972 | ); |
| 1973 | } |
| 1974 | |
| 1975 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | { |
| 1977 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1978 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1979 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1980 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1981 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1983 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1984 | switch (major_version) { |
| 1985 | case 0: |
| 1986 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1987 | break; |
| 1988 | case 1: |
| 1989 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1990 | break; |
| 1991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | } else |
| 1993 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1994 | } |
| 1995 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1996 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1998 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | mdk_rdev_t *rdev; |
| 2000 | mddev_t *mddev; |
| 2001 | char b[BDEVNAME_SIZE]; |
| 2002 | |
| 2003 | printk("\n"); |
| 2004 | printk("md: **********************************\n"); |
| 2005 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2006 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2007 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2009 | if (mddev->bitmap) |
| 2010 | bitmap_print_sb(mddev->bitmap); |
| 2011 | else |
| 2012 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2013 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2015 | printk("\n"); |
| 2016 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2017 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2018 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | } |
| 2020 | printk("md: **********************************\n"); |
| 2021 | printk("\n"); |
| 2022 | } |
| 2023 | |
| 2024 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2025 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2027 | /* Update each superblock (in-memory image), but |
| 2028 | * if we are allowed to, skip spares which already |
| 2029 | * have the right event counter, or have one earlier |
| 2030 | * (which would mean they aren't being marked as dirty |
| 2031 | * with the rest of the array) |
| 2032 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2035 | /* First make sure individual recovery_offsets are correct */ |
| 2036 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2037 | if (rdev->raid_disk >= 0 && |
| 2038 | !test_bit(In_sync, &rdev->flags) && |
| 2039 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2040 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2041 | |
| 2042 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2043 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2044 | if (rdev->sb_events == mddev->events || |
| 2045 | (nospares && |
| 2046 | rdev->raid_disk < 0 && |
| 2047 | (rdev->sb_events&1)==0 && |
| 2048 | rdev->sb_events+1 == mddev->events)) { |
| 2049 | /* Don't update this superblock */ |
| 2050 | rdev->sb_loaded = 2; |
| 2051 | } else { |
| 2052 | super_types[mddev->major_version]. |
| 2053 | sync_super(mddev, rdev); |
| 2054 | rdev->sb_loaded = 1; |
| 2055 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2059 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2062 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2063 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2065 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2066 | if (mddev->external) |
| 2067 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2069 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2070 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2071 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2072 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2073 | force_change = 1; |
| 2074 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2075 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2076 | * though if events isn't the right even/odd, we will have to do |
| 2077 | * spares after all |
| 2078 | */ |
| 2079 | nospares = 1; |
| 2080 | if (force_change) |
| 2081 | nospares = 0; |
| 2082 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2083 | /* If the array is degraded, then skipping spares is both |
| 2084 | * dangerous and fairly pointless. |
| 2085 | * Dangerous because a device that was removed from the array |
| 2086 | * might have a event_count that still looks up-to-date, |
| 2087 | * so it can be re-added without a resync. |
| 2088 | * Pointless because if there are any spares to skip, |
| 2089 | * then a recovery will happen and soon that array won't |
| 2090 | * be degraded any more and the spare can go back to sleep then. |
| 2091 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2092 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2093 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2094 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2095 | |
| 2096 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2097 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2098 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2099 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2100 | && (mddev->events & 1) |
| 2101 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2102 | mddev->events--; |
| 2103 | else { |
| 2104 | /* otherwise we have to go forward and ... */ |
| 2105 | mddev->events ++; |
| 2106 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2107 | /* .. if the array isn't clean, an 'even' event must also go |
| 2108 | * to spares. */ |
| 2109 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2110 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2111 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2112 | /* otherwise an 'odd' event must go to spares */ |
| 2113 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2114 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2115 | } |
| 2116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
| 2118 | if (!mddev->events) { |
| 2119 | /* |
| 2120 | * oops, this 64-bit counter should never wrap. |
| 2121 | * Either we are in around ~1 trillion A.C., assuming |
| 2122 | * 1 reboot per second, or we have a bug: |
| 2123 | */ |
| 2124 | MD_BUG(); |
| 2125 | mddev->events --; |
| 2126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
| 2128 | /* |
| 2129 | * do not write anything to disk if using |
| 2130 | * nonpersistent superblocks |
| 2131 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2132 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2133 | if (!mddev->external) |
| 2134 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2135 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2136 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2137 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2139 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2140 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2141 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | |
| 2143 | dprintk(KERN_INFO |
| 2144 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2145 | mdname(mddev),mddev->in_sync); |
| 2146 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2147 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2148 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | char b[BDEVNAME_SIZE]; |
| 2150 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2151 | if (rdev->sb_loaded != 1) |
| 2152 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2153 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | dprintk("(skipping faulty "); |
| 2155 | |
| 2156 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2157 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2158 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2159 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2160 | rdev->sb_page); |
| 2161 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2162 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2163 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2164 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | } else |
| 2167 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2168 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | /* only need to write one superblock... */ |
| 2170 | break; |
| 2171 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2172 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2173 | /* 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] | 2174 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2175 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2176 | if (mddev->in_sync != sync_req || |
| 2177 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2178 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2179 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2180 | goto repeat; |
| 2181 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2182 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2183 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2184 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2185 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2186 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2190 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2191 | * We want to accept with case. For this we use cmd_match. |
| 2192 | */ |
| 2193 | static int cmd_match(const char *cmd, const char *str) |
| 2194 | { |
| 2195 | /* See if cmd, written into a sysfs file, matches |
| 2196 | * str. They must either be the same, or cmd can |
| 2197 | * have a trailing newline |
| 2198 | */ |
| 2199 | while (*cmd && *str && *cmd == *str) { |
| 2200 | cmd++; |
| 2201 | str++; |
| 2202 | } |
| 2203 | if (*cmd == '\n') |
| 2204 | cmd++; |
| 2205 | if (*str || *cmd) |
| 2206 | return 0; |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2210 | struct rdev_sysfs_entry { |
| 2211 | struct attribute attr; |
| 2212 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2213 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2214 | }; |
| 2215 | |
| 2216 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2217 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2218 | { |
| 2219 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2220 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2221 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2222 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2223 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2224 | sep = ","; |
| 2225 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2226 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2227 | len += sprintf(page+len, "%sin_sync",sep); |
| 2228 | sep = ","; |
| 2229 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2230 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2231 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2232 | sep = ","; |
| 2233 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2234 | if (test_bit(Blocked, &rdev->flags)) { |
| 2235 | len += sprintf(page+len, "%sblocked", sep); |
| 2236 | sep = ","; |
| 2237 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2238 | if (!test_bit(Faulty, &rdev->flags) && |
| 2239 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2240 | len += sprintf(page+len, "%sspare", sep); |
| 2241 | sep = ","; |
| 2242 | } |
| 2243 | return len+sprintf(page+len, "\n"); |
| 2244 | } |
| 2245 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2246 | static ssize_t |
| 2247 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2248 | { |
| 2249 | /* can write |
| 2250 | * faulty - simulates and error |
| 2251 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2252 | * writemostly - sets write_mostly |
| 2253 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2254 | * blocked - sets the Blocked flag |
| 2255 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2256 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2257 | */ |
| 2258 | int err = -EINVAL; |
| 2259 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2260 | md_error(rdev->mddev, rdev); |
| 2261 | err = 0; |
| 2262 | } else if (cmd_match(buf, "remove")) { |
| 2263 | if (rdev->raid_disk >= 0) |
| 2264 | err = -EBUSY; |
| 2265 | else { |
| 2266 | mddev_t *mddev = rdev->mddev; |
| 2267 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2268 | if (mddev->pers) |
| 2269 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2270 | md_new_event(mddev); |
| 2271 | err = 0; |
| 2272 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2273 | } else if (cmd_match(buf, "writemostly")) { |
| 2274 | set_bit(WriteMostly, &rdev->flags); |
| 2275 | err = 0; |
| 2276 | } else if (cmd_match(buf, "-writemostly")) { |
| 2277 | clear_bit(WriteMostly, &rdev->flags); |
| 2278 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2279 | } else if (cmd_match(buf, "blocked")) { |
| 2280 | set_bit(Blocked, &rdev->flags); |
| 2281 | err = 0; |
| 2282 | } else if (cmd_match(buf, "-blocked")) { |
| 2283 | clear_bit(Blocked, &rdev->flags); |
| 2284 | wake_up(&rdev->blocked_wait); |
| 2285 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2286 | md_wakeup_thread(rdev->mddev->thread); |
| 2287 | |
| 2288 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2289 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2290 | set_bit(In_sync, &rdev->flags); |
| 2291 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2292 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2293 | if (!err && rdev->sysfs_state) |
| 2294 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2295 | return err ? err : len; |
| 2296 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2297 | static struct rdev_sysfs_entry rdev_state = |
| 2298 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2299 | |
| 2300 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2301 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2302 | { |
| 2303 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2304 | } |
| 2305 | |
| 2306 | static ssize_t |
| 2307 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2308 | { |
| 2309 | char *e; |
| 2310 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2311 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2312 | atomic_set(&rdev->corrected_errors, n); |
| 2313 | return len; |
| 2314 | } |
| 2315 | return -EINVAL; |
| 2316 | } |
| 2317 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2318 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2319 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2320 | static ssize_t |
| 2321 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2322 | { |
| 2323 | if (rdev->raid_disk < 0) |
| 2324 | return sprintf(page, "none\n"); |
| 2325 | else |
| 2326 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2327 | } |
| 2328 | |
| 2329 | static ssize_t |
| 2330 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2331 | { |
| 2332 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2333 | int err; |
| 2334 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2335 | int slot = simple_strtoul(buf, &e, 10); |
| 2336 | if (strncmp(buf, "none", 4)==0) |
| 2337 | slot = -1; |
| 2338 | else if (e==buf || (*e && *e!= '\n')) |
| 2339 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2340 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2341 | /* Setting 'slot' on an active array requires also |
| 2342 | * updating the 'rd%d' link, and communicating |
| 2343 | * with the personality with ->hot_*_disk. |
| 2344 | * For now we only support removing |
| 2345 | * failed/spare devices. This normally happens automatically, |
| 2346 | * but not when the metadata is externally managed. |
| 2347 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2348 | if (rdev->raid_disk == -1) |
| 2349 | return -EEXIST; |
| 2350 | /* personality does all needed checks */ |
| 2351 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2352 | return -EINVAL; |
| 2353 | err = rdev->mddev->pers-> |
| 2354 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2355 | if (err) |
| 2356 | return err; |
| 2357 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2358 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2359 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2360 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2361 | } else if (rdev->mddev->pers) { |
| 2362 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2363 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2364 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2365 | */ |
| 2366 | |
| 2367 | if (rdev->raid_disk != -1) |
| 2368 | return -EBUSY; |
| 2369 | |
| 2370 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2371 | return -EINVAL; |
| 2372 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2373 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2374 | if (rdev2->raid_disk == slot) |
| 2375 | return -EEXIST; |
| 2376 | |
| 2377 | rdev->raid_disk = slot; |
| 2378 | if (test_bit(In_sync, &rdev->flags)) |
| 2379 | rdev->saved_raid_disk = slot; |
| 2380 | else |
| 2381 | rdev->saved_raid_disk = -1; |
| 2382 | err = rdev->mddev->pers-> |
| 2383 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2384 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2385 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2386 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2387 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2388 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2389 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2390 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2391 | printk(KERN_WARNING |
| 2392 | "md: cannot register " |
| 2393 | "%s for %s\n", |
| 2394 | nm, mdname(rdev->mddev)); |
| 2395 | |
| 2396 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2397 | } else { |
| 2398 | if (slot >= rdev->mddev->raid_disks) |
| 2399 | return -ENOSPC; |
| 2400 | rdev->raid_disk = slot; |
| 2401 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2402 | clear_bit(Faulty, &rdev->flags); |
| 2403 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2404 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2405 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2406 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2407 | return len; |
| 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2412 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2413 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2414 | static ssize_t |
| 2415 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2416 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2417 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | static ssize_t |
| 2421 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2422 | { |
| 2423 | char *e; |
| 2424 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2425 | if (e==buf || (*e && *e != '\n')) |
| 2426 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2427 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2428 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2429 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2430 | /* Must set offset before size, so overlap checks |
| 2431 | * can be sane */ |
| 2432 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2433 | rdev->data_offset = offset; |
| 2434 | return len; |
| 2435 | } |
| 2436 | |
| 2437 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2438 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2439 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2440 | static ssize_t |
| 2441 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2442 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2443 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2446 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2447 | { |
| 2448 | /* check if two start/length pairs overlap */ |
| 2449 | if (s1+l1 <= s2) |
| 2450 | return 0; |
| 2451 | if (s2+l2 <= s1) |
| 2452 | return 0; |
| 2453 | return 1; |
| 2454 | } |
| 2455 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2456 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2457 | { |
| 2458 | unsigned long long blocks; |
| 2459 | sector_t new; |
| 2460 | |
| 2461 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2462 | return -EINVAL; |
| 2463 | |
| 2464 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2465 | return -EINVAL; /* sector conversion overflow */ |
| 2466 | |
| 2467 | new = blocks * 2; |
| 2468 | if (new != blocks * 2) |
| 2469 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2470 | |
| 2471 | *sectors = new; |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2475 | static ssize_t |
| 2476 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2477 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2478 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2479 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2480 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2481 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2482 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2483 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2484 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2485 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2486 | sectors = super_types[my_mddev->major_version]. |
| 2487 | rdev_size_change(rdev, sectors); |
| 2488 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2489 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2490 | } else if (!sectors) |
| 2491 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2492 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2493 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2494 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2495 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2496 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2497 | rdev->sectors = sectors; |
| 2498 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2499 | /* need to check that all other rdevs with the same ->bdev |
| 2500 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2501 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2502 | * we have to change it back, we will have the lock again. |
| 2503 | */ |
| 2504 | mddev_t *mddev; |
| 2505 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2506 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2507 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2508 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2509 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2510 | mdk_rdev_t *rdev2; |
| 2511 | |
| 2512 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2513 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2514 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2515 | (rdev->bdev == rdev2->bdev && |
| 2516 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2517 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2518 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2519 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2520 | overlap = 1; |
| 2521 | break; |
| 2522 | } |
| 2523 | mddev_unlock(mddev); |
| 2524 | if (overlap) { |
| 2525 | mddev_put(mddev); |
| 2526 | break; |
| 2527 | } |
| 2528 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2529 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2530 | if (overlap) { |
| 2531 | /* Someone else could have slipped in a size |
| 2532 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2533 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2534 | * safe, and trust userspace not to race with |
| 2535 | * itself |
| 2536 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2537 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2538 | return -EBUSY; |
| 2539 | } |
| 2540 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2541 | return len; |
| 2542 | } |
| 2543 | |
| 2544 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2545 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2546 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2547 | static struct attribute *rdev_default_attrs[] = { |
| 2548 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2549 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2550 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2551 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2552 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2553 | NULL, |
| 2554 | }; |
| 2555 | static ssize_t |
| 2556 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2557 | { |
| 2558 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2559 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2560 | mddev_t *mddev = rdev->mddev; |
| 2561 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2562 | |
| 2563 | if (!entry->show) |
| 2564 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2565 | |
| 2566 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2567 | if (!rv) { |
| 2568 | if (rdev->mddev == NULL) |
| 2569 | rv = -EBUSY; |
| 2570 | else |
| 2571 | rv = entry->show(rdev, page); |
| 2572 | mddev_unlock(mddev); |
| 2573 | } |
| 2574 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | static ssize_t |
| 2578 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2579 | const char *page, size_t length) |
| 2580 | { |
| 2581 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2582 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2583 | ssize_t rv; |
| 2584 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2585 | |
| 2586 | if (!entry->store) |
| 2587 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2588 | if (!capable(CAP_SYS_ADMIN)) |
| 2589 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2590 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2591 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2592 | if (rdev->mddev == NULL) |
| 2593 | rv = -EBUSY; |
| 2594 | else |
| 2595 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2596 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2597 | } |
| 2598 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | static void rdev_free(struct kobject *ko) |
| 2602 | { |
| 2603 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2604 | kfree(rdev); |
| 2605 | } |
| 2606 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2607 | .show = rdev_attr_show, |
| 2608 | .store = rdev_attr_store, |
| 2609 | }; |
| 2610 | static struct kobj_type rdev_ktype = { |
| 2611 | .release = rdev_free, |
| 2612 | .sysfs_ops = &rdev_sysfs_ops, |
| 2613 | .default_attrs = rdev_default_attrs, |
| 2614 | }; |
| 2615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | /* |
| 2617 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2618 | * |
| 2619 | * mark the device faulty if: |
| 2620 | * |
| 2621 | * - the device is nonexistent (zero size) |
| 2622 | * - the device has no valid superblock |
| 2623 | * |
| 2624 | * a faulty rdev _never_ has rdev->sb set. |
| 2625 | */ |
| 2626 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2627 | { |
| 2628 | char b[BDEVNAME_SIZE]; |
| 2629 | int err; |
| 2630 | mdk_rdev_t *rdev; |
| 2631 | sector_t size; |
| 2632 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2633 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | if (!rdev) { |
| 2635 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2636 | return ERR_PTR(-ENOMEM); |
| 2637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | |
| 2639 | if ((err = alloc_disk_sb(rdev))) |
| 2640 | goto abort_free; |
| 2641 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2642 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | if (err) |
| 2644 | goto abort_free; |
| 2645 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2646 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2649 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2650 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2651 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2653 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2655 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2656 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | |
| 2658 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2659 | if (!size) { |
| 2660 | printk(KERN_WARNING |
| 2661 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2662 | bdevname(rdev->bdev,b)); |
| 2663 | err = -EINVAL; |
| 2664 | goto abort_free; |
| 2665 | } |
| 2666 | |
| 2667 | if (super_format >= 0) { |
| 2668 | err = super_types[super_format]. |
| 2669 | load_super(rdev, NULL, super_minor); |
| 2670 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2671 | printk(KERN_WARNING |
| 2672 | "md: %s does not have a valid v%d.%d " |
| 2673 | "superblock, not importing!\n", |
| 2674 | bdevname(rdev->bdev,b), |
| 2675 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | goto abort_free; |
| 2677 | } |
| 2678 | if (err < 0) { |
| 2679 | printk(KERN_WARNING |
| 2680 | "md: could not read %s's sb, not importing!\n", |
| 2681 | bdevname(rdev->bdev,b)); |
| 2682 | goto abort_free; |
| 2683 | } |
| 2684 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2687 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | |
| 2689 | return rdev; |
| 2690 | |
| 2691 | abort_free: |
| 2692 | if (rdev->sb_page) { |
| 2693 | if (rdev->bdev) |
| 2694 | unlock_rdev(rdev); |
| 2695 | free_disk_sb(rdev); |
| 2696 | } |
| 2697 | kfree(rdev); |
| 2698 | return ERR_PTR(err); |
| 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * Check a full RAID array for plausibility |
| 2703 | */ |
| 2704 | |
| 2705 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2706 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | { |
| 2708 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2709 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | char b[BDEVNAME_SIZE]; |
| 2711 | |
| 2712 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2713 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | switch (super_types[mddev->major_version]. |
| 2715 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2716 | case 1: |
| 2717 | freshest = rdev; |
| 2718 | break; |
| 2719 | case 0: |
| 2720 | break; |
| 2721 | default: |
| 2722 | printk( KERN_ERR \ |
| 2723 | "md: fatal superblock inconsistency in %s" |
| 2724 | " -- removing from array\n", |
| 2725 | bdevname(rdev->bdev,b)); |
| 2726 | kick_rdev_from_array(rdev); |
| 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | super_types[mddev->major_version]. |
| 2731 | validate_super(mddev, freshest); |
| 2732 | |
| 2733 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2734 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2735 | if (rdev->desc_nr >= mddev->max_disks || |
| 2736 | i > mddev->max_disks) { |
| 2737 | printk(KERN_WARNING |
| 2738 | "md: %s: %s: only %d devices permitted\n", |
| 2739 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2740 | mddev->max_disks); |
| 2741 | kick_rdev_from_array(rdev); |
| 2742 | continue; |
| 2743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | if (rdev != freshest) |
| 2745 | if (super_types[mddev->major_version]. |
| 2746 | validate_super(mddev, rdev)) { |
| 2747 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2748 | " from array!\n", |
| 2749 | bdevname(rdev->bdev,b)); |
| 2750 | kick_rdev_from_array(rdev); |
| 2751 | continue; |
| 2752 | } |
| 2753 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2754 | rdev->desc_nr = i++; |
| 2755 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2756 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2757 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2758 | rdev->raid_disk = -1; |
| 2759 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | } |
| 2761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2764 | static void md_safemode_timeout(unsigned long data); |
| 2765 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2766 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2767 | safe_delay_show(mddev_t *mddev, char *page) |
| 2768 | { |
| 2769 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2770 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2771 | } |
| 2772 | static ssize_t |
| 2773 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2774 | { |
| 2775 | int scale=1; |
| 2776 | int dot=0; |
| 2777 | int i; |
| 2778 | unsigned long msec; |
| 2779 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2780 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2781 | /* remove a period, and count digits after it */ |
| 2782 | if (len >= sizeof(buf)) |
| 2783 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2784 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2785 | for (i=0; i<len; i++) { |
| 2786 | if (dot) { |
| 2787 | if (isdigit(buf[i])) { |
| 2788 | buf[i-1] = buf[i]; |
| 2789 | scale *= 10; |
| 2790 | } |
| 2791 | buf[i] = 0; |
| 2792 | } else if (buf[i] == '.') { |
| 2793 | dot=1; |
| 2794 | buf[i] = 0; |
| 2795 | } |
| 2796 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2797 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2798 | return -EINVAL; |
| 2799 | msec = (msec * 1000) / scale; |
| 2800 | if (msec == 0) |
| 2801 | mddev->safemode_delay = 0; |
| 2802 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2803 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2804 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2805 | if (mddev->safemode_delay == 0) |
| 2806 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2807 | if (mddev->safemode_delay < old_delay) |
| 2808 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2809 | } |
| 2810 | return len; |
| 2811 | } |
| 2812 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2813 | __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] | 2814 | |
| 2815 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2816 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2817 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2818 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2819 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2820 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2821 | else if (mddev->clevel[0]) |
| 2822 | return sprintf(page, "%s\n", mddev->clevel); |
| 2823 | else if (mddev->level != LEVEL_NONE) |
| 2824 | return sprintf(page, "%d\n", mddev->level); |
| 2825 | else |
| 2826 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2827 | } |
| 2828 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2829 | static ssize_t |
| 2830 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2831 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2832 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2833 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2834 | struct mdk_personality *pers; |
| 2835 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2836 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2837 | |
| 2838 | if (mddev->pers == NULL) { |
| 2839 | if (len == 0) |
| 2840 | return 0; |
| 2841 | if (len >= sizeof(mddev->clevel)) |
| 2842 | return -ENOSPC; |
| 2843 | strncpy(mddev->clevel, buf, len); |
| 2844 | if (mddev->clevel[len-1] == '\n') |
| 2845 | len--; |
| 2846 | mddev->clevel[len] = 0; |
| 2847 | mddev->level = LEVEL_NONE; |
| 2848 | return rv; |
| 2849 | } |
| 2850 | |
| 2851 | /* request to change the personality. Need to ensure: |
| 2852 | * - array is not engaged in resync/recovery/reshape |
| 2853 | * - old personality can be suspended |
| 2854 | * - new personality will access other array. |
| 2855 | */ |
| 2856 | |
| 2857 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2858 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2859 | |
| 2860 | if (!mddev->pers->quiesce) { |
| 2861 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2862 | mdname(mddev), mddev->pers->name); |
| 2863 | return -EINVAL; |
| 2864 | } |
| 2865 | |
| 2866 | /* Now find the new personality */ |
| 2867 | if (len == 0 || len >= sizeof(level)) |
| 2868 | return -EINVAL; |
| 2869 | strncpy(level, buf, len); |
| 2870 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2871 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2872 | level[len] = 0; |
| 2873 | |
| 2874 | request_module("md-%s", level); |
| 2875 | spin_lock(&pers_lock); |
| 2876 | pers = find_pers(LEVEL_NONE, level); |
| 2877 | if (!pers || !try_module_get(pers->owner)) { |
| 2878 | spin_unlock(&pers_lock); |
| 2879 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2880 | return -EINVAL; |
| 2881 | } |
| 2882 | spin_unlock(&pers_lock); |
| 2883 | |
| 2884 | if (pers == mddev->pers) { |
| 2885 | /* Nothing to do! */ |
| 2886 | module_put(pers->owner); |
| 2887 | return rv; |
| 2888 | } |
| 2889 | if (!pers->takeover) { |
| 2890 | module_put(pers->owner); |
| 2891 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2892 | mdname(mddev), level); |
| 2893 | return -EINVAL; |
| 2894 | } |
| 2895 | |
| 2896 | /* ->takeover must set new_* and/or delta_disks |
| 2897 | * if it succeeds, and may set them when it fails. |
| 2898 | */ |
| 2899 | priv = pers->takeover(mddev); |
| 2900 | if (IS_ERR(priv)) { |
| 2901 | mddev->new_level = mddev->level; |
| 2902 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2903 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2904 | mddev->raid_disks -= mddev->delta_disks; |
| 2905 | mddev->delta_disks = 0; |
| 2906 | module_put(pers->owner); |
| 2907 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2908 | mdname(mddev), level); |
| 2909 | return PTR_ERR(priv); |
| 2910 | } |
| 2911 | |
| 2912 | /* Looks like we have a winner */ |
| 2913 | mddev_suspend(mddev); |
| 2914 | mddev->pers->stop(mddev); |
| 2915 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2916 | /* Invalidate devices that are now superfluous */ |
| 2917 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2918 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2919 | rdev->raid_disk = -1; |
| 2920 | clear_bit(In_sync, &rdev->flags); |
| 2921 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2922 | mddev->pers = pers; |
| 2923 | mddev->private = priv; |
| 2924 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2925 | mddev->level = mddev->new_level; |
| 2926 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2927 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2928 | mddev->delta_disks = 0; |
| 2929 | pers->run(mddev); |
| 2930 | mddev_resume(mddev); |
| 2931 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2932 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2933 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2934 | return rv; |
| 2935 | } |
| 2936 | |
| 2937 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2938 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2939 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2940 | |
| 2941 | static ssize_t |
| 2942 | layout_show(mddev_t *mddev, char *page) |
| 2943 | { |
| 2944 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2945 | if (mddev->reshape_position != MaxSector && |
| 2946 | mddev->layout != mddev->new_layout) |
| 2947 | return sprintf(page, "%d (%d)\n", |
| 2948 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2949 | return sprintf(page, "%d\n", mddev->layout); |
| 2950 | } |
| 2951 | |
| 2952 | static ssize_t |
| 2953 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2954 | { |
| 2955 | char *e; |
| 2956 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2957 | |
| 2958 | if (!*buf || (*e && *e != '\n')) |
| 2959 | return -EINVAL; |
| 2960 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2961 | if (mddev->pers) { |
| 2962 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2963 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2964 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2965 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2966 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2967 | if (err) { |
| 2968 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2969 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2970 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2971 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2972 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2973 | if (mddev->reshape_position == MaxSector) |
| 2974 | mddev->layout = n; |
| 2975 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2976 | return len; |
| 2977 | } |
| 2978 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2979 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2980 | |
| 2981 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2982 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2983 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2984 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2985 | if (mddev->raid_disks == 0) |
| 2986 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2987 | if (mddev->reshape_position != MaxSector && |
| 2988 | mddev->delta_disks != 0) |
| 2989 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2990 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2991 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2992 | } |
| 2993 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2994 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2995 | |
| 2996 | static ssize_t |
| 2997 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2998 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2999 | char *e; |
| 3000 | int rv = 0; |
| 3001 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3002 | |
| 3003 | if (!*buf || (*e && *e != '\n')) |
| 3004 | return -EINVAL; |
| 3005 | |
| 3006 | if (mddev->pers) |
| 3007 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3008 | else if (mddev->reshape_position != MaxSector) { |
| 3009 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3010 | mddev->delta_disks = n - olddisks; |
| 3011 | mddev->raid_disks = n; |
| 3012 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3013 | mddev->raid_disks = n; |
| 3014 | return rv ? rv : len; |
| 3015 | } |
| 3016 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3017 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3018 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3019 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3020 | chunk_size_show(mddev_t *mddev, char *page) |
| 3021 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3022 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3023 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3024 | return sprintf(page, "%d (%d)\n", |
| 3025 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3026 | mddev->chunk_sectors << 9); |
| 3027 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | static ssize_t |
| 3031 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3032 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3033 | char *e; |
| 3034 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3035 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3036 | if (!*buf || (*e && *e != '\n')) |
| 3037 | return -EINVAL; |
| 3038 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3039 | if (mddev->pers) { |
| 3040 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3041 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3042 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3043 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3044 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3045 | if (err) { |
| 3046 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3047 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3048 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3049 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3050 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3051 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3052 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3053 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3054 | return len; |
| 3055 | } |
| 3056 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3057 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3058 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3059 | static ssize_t |
| 3060 | resync_start_show(mddev_t *mddev, char *page) |
| 3061 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3062 | if (mddev->recovery_cp == MaxSector) |
| 3063 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3064 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3065 | } |
| 3066 | |
| 3067 | static ssize_t |
| 3068 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3069 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3070 | char *e; |
| 3071 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3072 | |
| 3073 | if (mddev->pers) |
| 3074 | return -EBUSY; |
| 3075 | if (!*buf || (*e && *e != '\n')) |
| 3076 | return -EINVAL; |
| 3077 | |
| 3078 | mddev->recovery_cp = n; |
| 3079 | return len; |
| 3080 | } |
| 3081 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3082 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3083 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3084 | /* |
| 3085 | * The array state can be: |
| 3086 | * |
| 3087 | * clear |
| 3088 | * No devices, no size, no level |
| 3089 | * Equivalent to STOP_ARRAY ioctl |
| 3090 | * inactive |
| 3091 | * May have some settings, but array is not active |
| 3092 | * all IO results in error |
| 3093 | * When written, doesn't tear down array, but just stops it |
| 3094 | * suspended (not supported yet) |
| 3095 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3096 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3097 | * readonly |
| 3098 | * no resync can happen. no superblocks get written. |
| 3099 | * write requests fail |
| 3100 | * read-auto |
| 3101 | * like readonly, but behaves like 'clean' on a write request. |
| 3102 | * |
| 3103 | * clean - no pending writes, but otherwise active. |
| 3104 | * When written to inactive array, starts without resync |
| 3105 | * If a write request arrives then |
| 3106 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3107 | * if not known, block and switch to write-pending |
| 3108 | * If written to an active array that has pending writes, then fails. |
| 3109 | * active |
| 3110 | * fully active: IO and resync can be happening. |
| 3111 | * When written to inactive array, starts with resync |
| 3112 | * |
| 3113 | * write-pending |
| 3114 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3115 | * |
| 3116 | * active-idle |
| 3117 | * like active, but no writes have been seen for a while (100msec). |
| 3118 | * |
| 3119 | */ |
| 3120 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3121 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3122 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3123 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3124 | "write-pending", "active-idle", NULL }; |
| 3125 | |
| 3126 | static int match_word(const char *word, char **list) |
| 3127 | { |
| 3128 | int n; |
| 3129 | for (n=0; list[n]; n++) |
| 3130 | if (cmd_match(word, list[n])) |
| 3131 | break; |
| 3132 | return n; |
| 3133 | } |
| 3134 | |
| 3135 | static ssize_t |
| 3136 | array_state_show(mddev_t *mddev, char *page) |
| 3137 | { |
| 3138 | enum array_state st = inactive; |
| 3139 | |
| 3140 | if (mddev->pers) |
| 3141 | switch(mddev->ro) { |
| 3142 | case 1: |
| 3143 | st = readonly; |
| 3144 | break; |
| 3145 | case 2: |
| 3146 | st = read_auto; |
| 3147 | break; |
| 3148 | case 0: |
| 3149 | if (mddev->in_sync) |
| 3150 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3151 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3152 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3153 | else if (mddev->safemode) |
| 3154 | st = active_idle; |
| 3155 | else |
| 3156 | st = active; |
| 3157 | } |
| 3158 | else { |
| 3159 | if (list_empty(&mddev->disks) && |
| 3160 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3161 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3162 | st = clear; |
| 3163 | else |
| 3164 | st = inactive; |
| 3165 | } |
| 3166 | return sprintf(page, "%s\n", array_states[st]); |
| 3167 | } |
| 3168 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3169 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3170 | static int do_md_run(mddev_t * mddev); |
| 3171 | static int restart_array(mddev_t *mddev); |
| 3172 | |
| 3173 | static ssize_t |
| 3174 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3175 | { |
| 3176 | int err = -EINVAL; |
| 3177 | enum array_state st = match_word(buf, array_states); |
| 3178 | switch(st) { |
| 3179 | case bad_word: |
| 3180 | break; |
| 3181 | case clear: |
| 3182 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3183 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3184 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3185 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3186 | break; |
| 3187 | case inactive: |
| 3188 | /* stopping an active array */ |
| 3189 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3190 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3191 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3192 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3193 | } else |
| 3194 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3195 | break; |
| 3196 | case suspended: |
| 3197 | break; /* not supported yet */ |
| 3198 | case readonly: |
| 3199 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3200 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3201 | else { |
| 3202 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3203 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3204 | err = do_md_run(mddev); |
| 3205 | } |
| 3206 | break; |
| 3207 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3208 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3209 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3210 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3211 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3212 | err = restart_array(mddev); |
| 3213 | if (err == 0) { |
| 3214 | mddev->ro = 2; |
| 3215 | set_disk_ro(mddev->gendisk, 0); |
| 3216 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3217 | } else { |
| 3218 | mddev->ro = 2; |
| 3219 | err = do_md_run(mddev); |
| 3220 | } |
| 3221 | break; |
| 3222 | case clean: |
| 3223 | if (mddev->pers) { |
| 3224 | restart_array(mddev); |
| 3225 | spin_lock_irq(&mddev->write_lock); |
| 3226 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3227 | if (mddev->in_sync == 0) { |
| 3228 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3229 | if (mddev->safemode == 1) |
| 3230 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3231 | if (mddev->persistent) |
| 3232 | set_bit(MD_CHANGE_CLEAN, |
| 3233 | &mddev->flags); |
| 3234 | } |
| 3235 | err = 0; |
| 3236 | } else |
| 3237 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3238 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3239 | } else |
| 3240 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3241 | break; |
| 3242 | case active: |
| 3243 | if (mddev->pers) { |
| 3244 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3245 | if (mddev->external) |
| 3246 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3247 | wake_up(&mddev->sb_wait); |
| 3248 | err = 0; |
| 3249 | } else { |
| 3250 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3251 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3252 | err = do_md_run(mddev); |
| 3253 | } |
| 3254 | break; |
| 3255 | case write_pending: |
| 3256 | case active_idle: |
| 3257 | /* these cannot be set */ |
| 3258 | break; |
| 3259 | } |
| 3260 | if (err) |
| 3261 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3262 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3263 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3264 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3265 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3266 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3267 | static struct md_sysfs_entry md_array_state = |
| 3268 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3269 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3270 | static ssize_t |
| 3271 | null_show(mddev_t *mddev, char *page) |
| 3272 | { |
| 3273 | return -EINVAL; |
| 3274 | } |
| 3275 | |
| 3276 | static ssize_t |
| 3277 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3278 | { |
| 3279 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3280 | /* The new device is added to the array. |
| 3281 | * If the array has a persistent superblock, we read the |
| 3282 | * superblock to initialise info and check validity. |
| 3283 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3284 | * which mainly checks size. |
| 3285 | */ |
| 3286 | char *e; |
| 3287 | int major = simple_strtoul(buf, &e, 10); |
| 3288 | int minor; |
| 3289 | dev_t dev; |
| 3290 | mdk_rdev_t *rdev; |
| 3291 | int err; |
| 3292 | |
| 3293 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3294 | return -EINVAL; |
| 3295 | minor = simple_strtoul(e+1, &e, 10); |
| 3296 | if (*e && *e != '\n') |
| 3297 | return -EINVAL; |
| 3298 | dev = MKDEV(major, minor); |
| 3299 | if (major != MAJOR(dev) || |
| 3300 | minor != MINOR(dev)) |
| 3301 | return -EOVERFLOW; |
| 3302 | |
| 3303 | |
| 3304 | if (mddev->persistent) { |
| 3305 | rdev = md_import_device(dev, mddev->major_version, |
| 3306 | mddev->minor_version); |
| 3307 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3308 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3309 | mdk_rdev_t, same_set); |
| 3310 | err = super_types[mddev->major_version] |
| 3311 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3312 | if (err < 0) |
| 3313 | goto out; |
| 3314 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3315 | } else if (mddev->external) |
| 3316 | rdev = md_import_device(dev, -2, -1); |
| 3317 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3318 | rdev = md_import_device(dev, -1, -1); |
| 3319 | |
| 3320 | if (IS_ERR(rdev)) |
| 3321 | return PTR_ERR(rdev); |
| 3322 | err = bind_rdev_to_array(rdev, mddev); |
| 3323 | out: |
| 3324 | if (err) |
| 3325 | export_rdev(rdev); |
| 3326 | return err ? err : len; |
| 3327 | } |
| 3328 | |
| 3329 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3330 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3331 | |
| 3332 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3333 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3334 | { |
| 3335 | char *end; |
| 3336 | unsigned long chunk, end_chunk; |
| 3337 | |
| 3338 | if (!mddev->bitmap) |
| 3339 | goto out; |
| 3340 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3341 | while (*buf) { |
| 3342 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3343 | if (buf == end) break; |
| 3344 | if (*end == '-') { /* range */ |
| 3345 | buf = end + 1; |
| 3346 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3347 | if (buf == end) break; |
| 3348 | } |
| 3349 | if (*end && !isspace(*end)) break; |
| 3350 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3351 | buf = end; |
| 3352 | while (isspace(*buf)) buf++; |
| 3353 | } |
| 3354 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3355 | out: |
| 3356 | return len; |
| 3357 | } |
| 3358 | |
| 3359 | static struct md_sysfs_entry md_bitmap = |
| 3360 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3361 | |
| 3362 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3363 | size_show(mddev_t *mddev, char *page) |
| 3364 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3365 | return sprintf(page, "%llu\n", |
| 3366 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3367 | } |
| 3368 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3369 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3370 | |
| 3371 | static ssize_t |
| 3372 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3373 | { |
| 3374 | /* If array is inactive, we can reduce the component size, but |
| 3375 | * not increase it (except from 0). |
| 3376 | * If array is active, we can try an on-line resize |
| 3377 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3378 | sector_t sectors; |
| 3379 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3380 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3381 | if (err < 0) |
| 3382 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3383 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3384 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3385 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3386 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3387 | if (mddev->dev_sectors == 0 || |
| 3388 | mddev->dev_sectors > sectors) |
| 3389 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3390 | else |
| 3391 | err = -ENOSPC; |
| 3392 | } |
| 3393 | return err ? err : len; |
| 3394 | } |
| 3395 | |
| 3396 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3397 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3398 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3399 | |
| 3400 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3401 | * This is one of |
| 3402 | * 'none' for arrays with no metadata (good luck...) |
| 3403 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3404 | * or N.M for internally known formats |
| 3405 | */ |
| 3406 | static ssize_t |
| 3407 | metadata_show(mddev_t *mddev, char *page) |
| 3408 | { |
| 3409 | if (mddev->persistent) |
| 3410 | return sprintf(page, "%d.%d\n", |
| 3411 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3412 | else if (mddev->external) |
| 3413 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3414 | else |
| 3415 | return sprintf(page, "none\n"); |
| 3416 | } |
| 3417 | |
| 3418 | static ssize_t |
| 3419 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3420 | { |
| 3421 | int major, minor; |
| 3422 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3423 | /* Changing the details of 'external' metadata is |
| 3424 | * always permitted. Otherwise there must be |
| 3425 | * no devices attached to the array. |
| 3426 | */ |
| 3427 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3428 | ; |
| 3429 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3430 | return -EBUSY; |
| 3431 | |
| 3432 | if (cmd_match(buf, "none")) { |
| 3433 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3434 | mddev->external = 0; |
| 3435 | mddev->major_version = 0; |
| 3436 | mddev->minor_version = 90; |
| 3437 | return len; |
| 3438 | } |
| 3439 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3440 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3441 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3442 | namelen = sizeof(mddev->metadata_type)-1; |
| 3443 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3444 | mddev->metadata_type[namelen] = 0; |
| 3445 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3446 | mddev->metadata_type[--namelen] = 0; |
| 3447 | mddev->persistent = 0; |
| 3448 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3449 | mddev->major_version = 0; |
| 3450 | mddev->minor_version = 90; |
| 3451 | return len; |
| 3452 | } |
| 3453 | major = simple_strtoul(buf, &e, 10); |
| 3454 | if (e==buf || *e != '.') |
| 3455 | return -EINVAL; |
| 3456 | buf = e+1; |
| 3457 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3458 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3459 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3460 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3461 | return -ENOENT; |
| 3462 | mddev->major_version = major; |
| 3463 | mddev->minor_version = minor; |
| 3464 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3465 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3466 | return len; |
| 3467 | } |
| 3468 | |
| 3469 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3470 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3471 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3472 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3473 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3474 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3475 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3476 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3477 | type = "frozen"; |
| 3478 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3479 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3480 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3481 | type = "reshape"; |
| 3482 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3483 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3484 | type = "resync"; |
| 3485 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3486 | type = "check"; |
| 3487 | else |
| 3488 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3489 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3490 | type = "recover"; |
| 3491 | } |
| 3492 | return sprintf(page, "%s\n", type); |
| 3493 | } |
| 3494 | |
| 3495 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3496 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3497 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3498 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3499 | return -EINVAL; |
| 3500 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3501 | if (cmd_match(page, "frozen")) |
| 3502 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3503 | else |
| 3504 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3505 | |
| 3506 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3507 | if (mddev->sync_thread) { |
| 3508 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3509 | md_unregister_thread(mddev->sync_thread); |
| 3510 | mddev->sync_thread = NULL; |
| 3511 | mddev->recovery = 0; |
| 3512 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3513 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3514 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3515 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3516 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3517 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3518 | else if (cmd_match(page, "recover")) { |
| 3519 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3520 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3521 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3522 | int err; |
| 3523 | if (mddev->pers->start_reshape == NULL) |
| 3524 | return -EINVAL; |
| 3525 | err = mddev->pers->start_reshape(mddev); |
| 3526 | if (err) |
| 3527 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3528 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3529 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3530 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3531 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3532 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3533 | return -EINVAL; |
| 3534 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3535 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3536 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3537 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3538 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3539 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3540 | return len; |
| 3541 | } |
| 3542 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3543 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3544 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3545 | { |
| 3546 | return sprintf(page, "%llu\n", |
| 3547 | (unsigned long long) mddev->resync_mismatches); |
| 3548 | } |
| 3549 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3550 | static struct md_sysfs_entry md_scan_mode = |
| 3551 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3552 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3553 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3554 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3555 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3556 | static ssize_t |
| 3557 | sync_min_show(mddev_t *mddev, char *page) |
| 3558 | { |
| 3559 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3560 | mddev->sync_speed_min ? "local": "system"); |
| 3561 | } |
| 3562 | |
| 3563 | static ssize_t |
| 3564 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3565 | { |
| 3566 | int min; |
| 3567 | char *e; |
| 3568 | if (strncmp(buf, "system", 6)==0) { |
| 3569 | mddev->sync_speed_min = 0; |
| 3570 | return len; |
| 3571 | } |
| 3572 | min = simple_strtoul(buf, &e, 10); |
| 3573 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3574 | return -EINVAL; |
| 3575 | mddev->sync_speed_min = min; |
| 3576 | return len; |
| 3577 | } |
| 3578 | |
| 3579 | static struct md_sysfs_entry md_sync_min = |
| 3580 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3581 | |
| 3582 | static ssize_t |
| 3583 | sync_max_show(mddev_t *mddev, char *page) |
| 3584 | { |
| 3585 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3586 | mddev->sync_speed_max ? "local": "system"); |
| 3587 | } |
| 3588 | |
| 3589 | static ssize_t |
| 3590 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3591 | { |
| 3592 | int max; |
| 3593 | char *e; |
| 3594 | if (strncmp(buf, "system", 6)==0) { |
| 3595 | mddev->sync_speed_max = 0; |
| 3596 | return len; |
| 3597 | } |
| 3598 | max = simple_strtoul(buf, &e, 10); |
| 3599 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3600 | return -EINVAL; |
| 3601 | mddev->sync_speed_max = max; |
| 3602 | return len; |
| 3603 | } |
| 3604 | |
| 3605 | static struct md_sysfs_entry md_sync_max = |
| 3606 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3607 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3608 | static ssize_t |
| 3609 | degraded_show(mddev_t *mddev, char *page) |
| 3610 | { |
| 3611 | return sprintf(page, "%d\n", mddev->degraded); |
| 3612 | } |
| 3613 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3614 | |
| 3615 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3616 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3617 | { |
| 3618 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3619 | } |
| 3620 | |
| 3621 | static ssize_t |
| 3622 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3623 | { |
| 3624 | long n; |
| 3625 | |
| 3626 | if (strict_strtol(buf, 10, &n)) |
| 3627 | return -EINVAL; |
| 3628 | |
| 3629 | if (n != 0 && n != 1) |
| 3630 | return -EINVAL; |
| 3631 | |
| 3632 | mddev->parallel_resync = n; |
| 3633 | |
| 3634 | if (mddev->sync_thread) |
| 3635 | wake_up(&resync_wait); |
| 3636 | |
| 3637 | return len; |
| 3638 | } |
| 3639 | |
| 3640 | /* force parallel resync, even with shared block devices */ |
| 3641 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3642 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3643 | sync_force_parallel_show, sync_force_parallel_store); |
| 3644 | |
| 3645 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3646 | sync_speed_show(mddev_t *mddev, char *page) |
| 3647 | { |
| 3648 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3649 | if (mddev->curr_resync == 0) |
| 3650 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3651 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3652 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3653 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3654 | db = resync - mddev->resync_mark_cnt; |
| 3655 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3656 | } |
| 3657 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3658 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3659 | |
| 3660 | static ssize_t |
| 3661 | sync_completed_show(mddev_t *mddev, char *page) |
| 3662 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3663 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3664 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3665 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3666 | return sprintf(page, "none\n"); |
| 3667 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3668 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3669 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3670 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3671 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3672 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3673 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3674 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3675 | } |
| 3676 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3677 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3678 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3679 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3680 | min_sync_show(mddev_t *mddev, char *page) |
| 3681 | { |
| 3682 | return sprintf(page, "%llu\n", |
| 3683 | (unsigned long long)mddev->resync_min); |
| 3684 | } |
| 3685 | static ssize_t |
| 3686 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3687 | { |
| 3688 | unsigned long long min; |
| 3689 | if (strict_strtoull(buf, 10, &min)) |
| 3690 | return -EINVAL; |
| 3691 | if (min > mddev->resync_max) |
| 3692 | return -EINVAL; |
| 3693 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3694 | return -EBUSY; |
| 3695 | |
| 3696 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3697 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3698 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3699 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3700 | return -EINVAL; |
| 3701 | } |
| 3702 | mddev->resync_min = min; |
| 3703 | |
| 3704 | return len; |
| 3705 | } |
| 3706 | |
| 3707 | static struct md_sysfs_entry md_min_sync = |
| 3708 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3709 | |
| 3710 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3711 | max_sync_show(mddev_t *mddev, char *page) |
| 3712 | { |
| 3713 | if (mddev->resync_max == MaxSector) |
| 3714 | return sprintf(page, "max\n"); |
| 3715 | else |
| 3716 | return sprintf(page, "%llu\n", |
| 3717 | (unsigned long long)mddev->resync_max); |
| 3718 | } |
| 3719 | static ssize_t |
| 3720 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3721 | { |
| 3722 | if (strncmp(buf, "max", 3) == 0) |
| 3723 | mddev->resync_max = MaxSector; |
| 3724 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3725 | unsigned long long max; |
| 3726 | if (strict_strtoull(buf, 10, &max)) |
| 3727 | return -EINVAL; |
| 3728 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3729 | return -EINVAL; |
| 3730 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3731 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3732 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3733 | return -EBUSY; |
| 3734 | |
| 3735 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3736 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3737 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3738 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3739 | return -EINVAL; |
| 3740 | } |
| 3741 | mddev->resync_max = max; |
| 3742 | } |
| 3743 | wake_up(&mddev->recovery_wait); |
| 3744 | return len; |
| 3745 | } |
| 3746 | |
| 3747 | static struct md_sysfs_entry md_max_sync = |
| 3748 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3749 | |
| 3750 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3751 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3752 | { |
| 3753 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3754 | } |
| 3755 | |
| 3756 | static ssize_t |
| 3757 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3758 | { |
| 3759 | char *e; |
| 3760 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3761 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3762 | if (mddev->pers == NULL || |
| 3763 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3764 | return -EINVAL; |
| 3765 | if (buf == e || (*e && *e != '\n')) |
| 3766 | return -EINVAL; |
| 3767 | if (new >= mddev->suspend_hi || |
| 3768 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3769 | mddev->suspend_lo = new; |
| 3770 | mddev->pers->quiesce(mddev, 2); |
| 3771 | return len; |
| 3772 | } else |
| 3773 | return -EINVAL; |
| 3774 | } |
| 3775 | static struct md_sysfs_entry md_suspend_lo = |
| 3776 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3777 | |
| 3778 | |
| 3779 | static ssize_t |
| 3780 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3781 | { |
| 3782 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3783 | } |
| 3784 | |
| 3785 | static ssize_t |
| 3786 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3787 | { |
| 3788 | char *e; |
| 3789 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3790 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3791 | if (mddev->pers == NULL || |
| 3792 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3793 | return -EINVAL; |
| 3794 | if (buf == e || (*e && *e != '\n')) |
| 3795 | return -EINVAL; |
| 3796 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3797 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3798 | mddev->suspend_hi = new; |
| 3799 | mddev->pers->quiesce(mddev, 1); |
| 3800 | mddev->pers->quiesce(mddev, 0); |
| 3801 | return len; |
| 3802 | } else |
| 3803 | return -EINVAL; |
| 3804 | } |
| 3805 | static struct md_sysfs_entry md_suspend_hi = |
| 3806 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3807 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3808 | static ssize_t |
| 3809 | reshape_position_show(mddev_t *mddev, char *page) |
| 3810 | { |
| 3811 | if (mddev->reshape_position != MaxSector) |
| 3812 | return sprintf(page, "%llu\n", |
| 3813 | (unsigned long long)mddev->reshape_position); |
| 3814 | strcpy(page, "none\n"); |
| 3815 | return 5; |
| 3816 | } |
| 3817 | |
| 3818 | static ssize_t |
| 3819 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3820 | { |
| 3821 | char *e; |
| 3822 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3823 | if (mddev->pers) |
| 3824 | return -EBUSY; |
| 3825 | if (buf == e || (*e && *e != '\n')) |
| 3826 | return -EINVAL; |
| 3827 | mddev->reshape_position = new; |
| 3828 | mddev->delta_disks = 0; |
| 3829 | mddev->new_level = mddev->level; |
| 3830 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3831 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3832 | return len; |
| 3833 | } |
| 3834 | |
| 3835 | static struct md_sysfs_entry md_reshape_position = |
| 3836 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3837 | reshape_position_store); |
| 3838 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3839 | static ssize_t |
| 3840 | array_size_show(mddev_t *mddev, char *page) |
| 3841 | { |
| 3842 | if (mddev->external_size) |
| 3843 | return sprintf(page, "%llu\n", |
| 3844 | (unsigned long long)mddev->array_sectors/2); |
| 3845 | else |
| 3846 | return sprintf(page, "default\n"); |
| 3847 | } |
| 3848 | |
| 3849 | static ssize_t |
| 3850 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3851 | { |
| 3852 | sector_t sectors; |
| 3853 | |
| 3854 | if (strncmp(buf, "default", 7) == 0) { |
| 3855 | if (mddev->pers) |
| 3856 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3857 | else |
| 3858 | sectors = mddev->array_sectors; |
| 3859 | |
| 3860 | mddev->external_size = 0; |
| 3861 | } else { |
| 3862 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3863 | return -EINVAL; |
| 3864 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3865 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3866 | |
| 3867 | mddev->external_size = 1; |
| 3868 | } |
| 3869 | |
| 3870 | mddev->array_sectors = sectors; |
| 3871 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3872 | if (mddev->pers) |
| 3873 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3874 | |
| 3875 | return len; |
| 3876 | } |
| 3877 | |
| 3878 | static struct md_sysfs_entry md_array_size = |
| 3879 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3880 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3881 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3882 | static struct attribute *md_default_attrs[] = { |
| 3883 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3884 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3885 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3886 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3887 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3888 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3889 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3890 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3891 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3892 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3893 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3894 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3895 | NULL, |
| 3896 | }; |
| 3897 | |
| 3898 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3899 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3900 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3901 | &md_sync_min.attr, |
| 3902 | &md_sync_max.attr, |
| 3903 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3904 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3905 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3906 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3907 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3908 | &md_suspend_lo.attr, |
| 3909 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3910 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3911 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3912 | NULL, |
| 3913 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3914 | static struct attribute_group md_redundancy_group = { |
| 3915 | .name = NULL, |
| 3916 | .attrs = md_redundancy_attrs, |
| 3917 | }; |
| 3918 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3919 | |
| 3920 | static ssize_t |
| 3921 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3922 | { |
| 3923 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3924 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3925 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3926 | |
| 3927 | if (!entry->show) |
| 3928 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3929 | rv = mddev_lock(mddev); |
| 3930 | if (!rv) { |
| 3931 | rv = entry->show(mddev, page); |
| 3932 | mddev_unlock(mddev); |
| 3933 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3934 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | static ssize_t |
| 3938 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3939 | const char *page, size_t length) |
| 3940 | { |
| 3941 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3942 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3943 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3944 | |
| 3945 | if (!entry->store) |
| 3946 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3947 | if (!capable(CAP_SYS_ADMIN)) |
| 3948 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3949 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3950 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3951 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3952 | if (!rv) { |
| 3953 | rv = entry->store(mddev, page, length); |
| 3954 | mddev_unlock(mddev); |
| 3955 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3956 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | static void md_free(struct kobject *ko) |
| 3960 | { |
| 3961 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3962 | |
| 3963 | if (mddev->sysfs_state) |
| 3964 | sysfs_put(mddev->sysfs_state); |
| 3965 | |
| 3966 | if (mddev->gendisk) { |
| 3967 | del_gendisk(mddev->gendisk); |
| 3968 | put_disk(mddev->gendisk); |
| 3969 | } |
| 3970 | if (mddev->queue) |
| 3971 | blk_cleanup_queue(mddev->queue); |
| 3972 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3973 | kfree(mddev); |
| 3974 | } |
| 3975 | |
| 3976 | static struct sysfs_ops md_sysfs_ops = { |
| 3977 | .show = md_attr_show, |
| 3978 | .store = md_attr_store, |
| 3979 | }; |
| 3980 | static struct kobj_type md_ktype = { |
| 3981 | .release = md_free, |
| 3982 | .sysfs_ops = &md_sysfs_ops, |
| 3983 | .default_attrs = md_default_attrs, |
| 3984 | }; |
| 3985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | int mdp_major = 0; |
| 3987 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3988 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3989 | { |
| 3990 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3991 | |
| 3992 | if (mddev->private == &md_redundancy_group) { |
| 3993 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3994 | if (mddev->sysfs_action) |
| 3995 | sysfs_put(mddev->sysfs_action); |
| 3996 | mddev->sysfs_action = NULL; |
| 3997 | mddev->private = NULL; |
| 3998 | } |
| 3999 | kobject_del(&mddev->kobj); |
| 4000 | kobject_put(&mddev->kobj); |
| 4001 | } |
| 4002 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4003 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4005 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | mddev_t *mddev = mddev_find(dev); |
| 4007 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4008 | int partitioned; |
| 4009 | int shift; |
| 4010 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4011 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
| 4013 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4014 | return -ENODEV; |
| 4015 | |
| 4016 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4017 | shift = partitioned ? MdpMinorShift : 0; |
| 4018 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4020 | /* wait for any previous instance if this device |
| 4021 | * to be completed removed (mddev_delayed_delete). |
| 4022 | */ |
| 4023 | flush_scheduled_work(); |
| 4024 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4025 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4026 | error = -EEXIST; |
| 4027 | if (mddev->gendisk) |
| 4028 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4029 | |
| 4030 | if (name) { |
| 4031 | /* Need to ensure that 'name' is not a duplicate. |
| 4032 | */ |
| 4033 | mddev_t *mddev2; |
| 4034 | spin_lock(&all_mddevs_lock); |
| 4035 | |
| 4036 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4037 | if (mddev2->gendisk && |
| 4038 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4039 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4040 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4041 | } |
| 4042 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4044 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4045 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4046 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4047 | if (!mddev->queue) |
| 4048 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4049 | mddev->queue->queuedata = mddev; |
| 4050 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4051 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4052 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4053 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4054 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | disk = alloc_disk(1 << shift); |
| 4057 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4058 | blk_cleanup_queue(mddev->queue); |
| 4059 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4060 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4062 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4064 | if (name) |
| 4065 | strcpy(disk->disk_name, name); |
| 4066 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4068 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | disk->fops = &md_fops; |
| 4071 | disk->private_data = mddev; |
| 4072 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4073 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4074 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4075 | * remove it now. |
| 4076 | */ |
| 4077 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | add_disk(disk); |
| 4079 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4080 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4081 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4082 | if (error) { |
| 4083 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4084 | * __must_check, we must do something with the result |
| 4085 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4086 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4087 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4088 | error = 0; |
| 4089 | } |
| 4090 | abort: |
| 4091 | mutex_unlock(&disks_mutex); |
| 4092 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4093 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4094 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4095 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4096 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4097 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4101 | { |
| 4102 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | return NULL; |
| 4104 | } |
| 4105 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4106 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4107 | { |
| 4108 | /* val must be "md_*" where * is not all digits. |
| 4109 | * We allocate an array with a large free minor number, and |
| 4110 | * set the name to val. val must not already be an active name. |
| 4111 | */ |
| 4112 | int len = strlen(val); |
| 4113 | char buf[DISK_NAME_LEN]; |
| 4114 | |
| 4115 | while (len && val[len-1] == '\n') |
| 4116 | len--; |
| 4117 | if (len >= DISK_NAME_LEN) |
| 4118 | return -E2BIG; |
| 4119 | strlcpy(buf, val, len+1); |
| 4120 | if (strncmp(buf, "md_", 3) != 0) |
| 4121 | return -EINVAL; |
| 4122 | return md_alloc(0, buf); |
| 4123 | } |
| 4124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | static void md_safemode_timeout(unsigned long data) |
| 4126 | { |
| 4127 | mddev_t *mddev = (mddev_t *) data; |
| 4128 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4129 | if (!atomic_read(&mddev->writes_pending)) { |
| 4130 | mddev->safemode = 1; |
| 4131 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4132 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | md_wakeup_thread(mddev->thread); |
| 4135 | } |
| 4136 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4137 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | |
| 4139 | static int do_md_run(mddev_t * mddev) |
| 4140 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4141 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | mdk_rdev_t *rdev; |
| 4143 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4144 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4146 | if (list_empty(&mddev->disks)) |
| 4147 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | |
| 4150 | if (mddev->pers) |
| 4151 | return -EBUSY; |
| 4152 | |
| 4153 | /* |
| 4154 | * Analyze all RAID superblock(s) |
| 4155 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4156 | if (!mddev->raid_disks) { |
| 4157 | if (!mddev->persistent) |
| 4158 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4159 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4162 | if (mddev->level != LEVEL_NONE) |
| 4163 | request_module("md-level-%d", mddev->level); |
| 4164 | else if (mddev->clevel[0]) |
| 4165 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | |
| 4167 | /* |
| 4168 | * Drop all container device buffers, from now on |
| 4169 | * the only valid external interface is through the md |
| 4170 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4172 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4173 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | continue; |
| 4175 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4176 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4177 | |
| 4178 | /* perform some consistency tests on the device. |
| 4179 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4180 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4181 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4182 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4183 | if (mddev->dev_sectors && |
| 4184 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4185 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4186 | printk("md: %s: data overlaps metadata\n", |
| 4187 | mdname(mddev)); |
| 4188 | return -EINVAL; |
| 4189 | } |
| 4190 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4191 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4192 | > rdev->data_offset) { |
| 4193 | printk("md: %s: metadata overlaps data\n", |
| 4194 | mdname(mddev)); |
| 4195 | return -EINVAL; |
| 4196 | } |
| 4197 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4198 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | md_probe(mddev->unit, NULL, NULL); |
| 4202 | disk = mddev->gendisk; |
| 4203 | if (!disk) |
| 4204 | return -ENOMEM; |
| 4205 | |
| 4206 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4207 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4208 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4210 | if (mddev->level != LEVEL_NONE) |
| 4211 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4212 | mddev->level); |
| 4213 | else |
| 4214 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4215 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | return -EINVAL; |
| 4217 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4218 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4220 | if (mddev->level != pers->level) { |
| 4221 | mddev->level = pers->level; |
| 4222 | mddev->new_level = pers->level; |
| 4223 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4224 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4226 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4227 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4228 | /* This personality cannot handle reshaping... */ |
| 4229 | mddev->pers = NULL; |
| 4230 | module_put(pers->owner); |
| 4231 | return -EINVAL; |
| 4232 | } |
| 4233 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4234 | if (pers->sync_request) { |
| 4235 | /* Warn if this is a potentially silly |
| 4236 | * configuration. |
| 4237 | */ |
| 4238 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4239 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4240 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4241 | |
| 4242 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4243 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4244 | if (rdev < rdev2 && |
| 4245 | rdev->bdev->bd_contains == |
| 4246 | rdev2->bdev->bd_contains) { |
| 4247 | printk(KERN_WARNING |
| 4248 | "%s: WARNING: %s appears to be" |
| 4249 | " on the same physical disk as" |
| 4250 | " %s.\n", |
| 4251 | mdname(mddev), |
| 4252 | bdevname(rdev->bdev,b), |
| 4253 | bdevname(rdev2->bdev,b2)); |
| 4254 | warned = 1; |
| 4255 | } |
| 4256 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4257 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4258 | if (warned) |
| 4259 | printk(KERN_WARNING |
| 4260 | "True protection against single-disk" |
| 4261 | " failure might be compromised.\n"); |
| 4262 | } |
| 4263 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4264 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4265 | /* may be over-ridden by personality */ |
| 4266 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4267 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4268 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4269 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4271 | if (start_readonly) |
| 4272 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4273 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4274 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4275 | if (err) |
| 4276 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4277 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4278 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4279 | " but 'external_size' not in effect?\n", __func__); |
| 4280 | printk(KERN_ERR |
| 4281 | "md: invalid array_size %llu > default size %llu\n", |
| 4282 | (unsigned long long)mddev->array_sectors / 2, |
| 4283 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4284 | err = -EINVAL; |
| 4285 | mddev->pers->stop(mddev); |
| 4286 | } |
| 4287 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4288 | err = bitmap_create(mddev); |
| 4289 | if (err) { |
| 4290 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4291 | mdname(mddev), err); |
| 4292 | mddev->pers->stop(mddev); |
| 4293 | } |
| 4294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | module_put(mddev->pers->owner); |
| 4297 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4298 | bitmap_destroy(mddev); |
| 4299 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4301 | if (mddev->pers->sync_request) { |
| 4302 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4303 | printk(KERN_WARNING |
| 4304 | "md: cannot register extra attributes for %s\n", |
| 4305 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4306 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4307 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4308 | mddev->ro = 0; |
| 4309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | atomic_set(&mddev->writes_pending,0); |
| 4311 | mddev->safemode = 0; |
| 4312 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4313 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4314 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4316 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4317 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4318 | if (rdev->raid_disk >= 0) { |
| 4319 | char nm[20]; |
| 4320 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4321 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4322 | printk("md: cannot register %s for %s\n", |
| 4323 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | |
| 4326 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4327 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4328 | if (mddev->flags) |
| 4329 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4331 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4333 | /* If there is a partially-recovered drive we need to |
| 4334 | * start recovery here. If we leave it to md_check_recovery, |
| 4335 | * it will remove the drives and not do the right thing |
| 4336 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4337 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4338 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4339 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4340 | if (rdev->raid_disk >= 0 && |
| 4341 | !test_bit(In_sync, &rdev->flags) && |
| 4342 | !test_bit(Faulty, &rdev->flags)) |
| 4343 | /* complete an interrupted recovery */ |
| 4344 | spares++; |
| 4345 | if (spares && mddev->pers->sync_request) { |
| 4346 | mddev->recovery = 0; |
| 4347 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4348 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4349 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4350 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4351 | if (!mddev->sync_thread) { |
| 4352 | printk(KERN_ERR "%s: could not start resync" |
| 4353 | " thread...\n", |
| 4354 | mdname(mddev)); |
| 4355 | /* leave the spares where they are, it shouldn't hurt */ |
| 4356 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4357 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4358 | } |
| 4359 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4360 | md_wakeup_thread(mddev->thread); |
| 4361 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4362 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4363 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4364 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4365 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4366 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4367 | if (mddev->sysfs_action) |
| 4368 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4369 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4370 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | return 0; |
| 4372 | } |
| 4373 | |
| 4374 | static int restart_array(mddev_t *mddev) |
| 4375 | { |
| 4376 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4378 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4380 | return -ENXIO; |
| 4381 | if (!mddev->pers) |
| 4382 | return -EINVAL; |
| 4383 | if (!mddev->ro) |
| 4384 | return -EBUSY; |
| 4385 | mddev->safemode = 0; |
| 4386 | mddev->ro = 0; |
| 4387 | set_disk_ro(disk, 0); |
| 4388 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4389 | mdname(mddev)); |
| 4390 | /* Kick recovery or resync if necessary */ |
| 4391 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4392 | md_wakeup_thread(mddev->thread); |
| 4393 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4394 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4395 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | } |
| 4397 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4398 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4399 | * to the file ourselves */ |
| 4400 | static int deny_bitmap_write_access(struct file * file) |
| 4401 | { |
| 4402 | struct inode *inode = file->f_mapping->host; |
| 4403 | |
| 4404 | spin_lock(&inode->i_lock); |
| 4405 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4406 | spin_unlock(&inode->i_lock); |
| 4407 | return -ETXTBSY; |
| 4408 | } |
| 4409 | atomic_set(&inode->i_writecount, -1); |
| 4410 | spin_unlock(&inode->i_lock); |
| 4411 | |
| 4412 | return 0; |
| 4413 | } |
| 4414 | |
| 4415 | static void restore_bitmap_write_access(struct file *file) |
| 4416 | { |
| 4417 | struct inode *inode = file->f_mapping->host; |
| 4418 | |
| 4419 | spin_lock(&inode->i_lock); |
| 4420 | atomic_set(&inode->i_writecount, 1); |
| 4421 | spin_unlock(&inode->i_lock); |
| 4422 | } |
| 4423 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4424 | /* mode: |
| 4425 | * 0 - completely stop and dis-assemble array |
| 4426 | * 1 - switch to readonly |
| 4427 | * 2 - stop but do not disassemble array |
| 4428 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4429 | 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] | 4430 | { |
| 4431 | int err = 0; |
| 4432 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4433 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4435 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4436 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4437 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4438 | err = -EBUSY; |
| 4439 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | |
| 4441 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4442 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4444 | md_unregister_thread(mddev->sync_thread); |
| 4445 | mddev->sync_thread = NULL; |
| 4446 | } |
| 4447 | |
| 4448 | del_timer_sync(&mddev->safemode_timer); |
| 4449 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4450 | switch(mode) { |
| 4451 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4453 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | goto out; |
| 4455 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4456 | break; |
| 4457 | case 0: /* disassemble */ |
| 4458 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4459 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4460 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | if (mddev->ro) |
| 4462 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4465 | mddev->queue->merge_bvec_fn = NULL; |
| 4466 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4467 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4469 | if (mddev->pers->sync_request) |
| 4470 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4472 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4473 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4474 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4475 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4476 | if (rdev->raid_disk >= 0) { |
| 4477 | char nm[20]; |
| 4478 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4479 | sysfs_remove_link(&mddev->kobj, nm); |
| 4480 | } |
| 4481 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4482 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4483 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | if (mddev->ro) |
| 4486 | mddev->ro = 0; |
| 4487 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4488 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | /* mark array as shutdown cleanly */ |
| 4490 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4491 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4493 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4495 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4496 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4498 | out: |
| 4499 | mutex_unlock(&mddev->open_mutex); |
| 4500 | if (err) |
| 4501 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | /* |
| 4503 | * Free resources if final stop |
| 4504 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4505 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4508 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4509 | bitmap_destroy(mddev); |
| 4510 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4511 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4512 | fput(mddev->bitmap_file); |
| 4513 | mddev->bitmap_file = NULL; |
| 4514 | } |
| 4515 | mddev->bitmap_offset = 0; |
| 4516 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4517 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4518 | flush_scheduled_work(); |
| 4519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | export_array(mddev); |
| 4521 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4522 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4523 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4524 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4525 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4526 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4527 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4528 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4529 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4530 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4531 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4532 | mddev->level = LEVEL_NONE; |
| 4533 | mddev->clevel[0] = 0; |
| 4534 | mddev->flags = 0; |
| 4535 | mddev->ro = 0; |
| 4536 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4537 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4538 | mddev->ctime = mddev->utime = 0; |
| 4539 | mddev->layout = 0; |
| 4540 | mddev->max_disks = 0; |
| 4541 | mddev->events = 0; |
| 4542 | mddev->delta_disks = 0; |
| 4543 | mddev->new_level = LEVEL_NONE; |
| 4544 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4545 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4546 | mddev->curr_resync = 0; |
| 4547 | mddev->resync_mismatches = 0; |
| 4548 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4549 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4550 | mddev->recovery = 0; |
| 4551 | mddev->in_sync = 0; |
| 4552 | mddev->changed = 0; |
| 4553 | mddev->degraded = 0; |
| 4554 | mddev->barriers_work = 0; |
| 4555 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4556 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4557 | if (mddev->hold_active == UNTIL_STOP) |
| 4558 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4559 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4560 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4562 | mdname(mddev)); |
| 4563 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4564 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4565 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4566 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | return err; |
| 4568 | } |
| 4569 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4570 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | static void autorun_array(mddev_t *mddev) |
| 4572 | { |
| 4573 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | int err; |
| 4575 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4576 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
| 4579 | printk(KERN_INFO "md: running: "); |
| 4580 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4581 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | char b[BDEVNAME_SIZE]; |
| 4583 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4584 | } |
| 4585 | printk("\n"); |
| 4586 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4587 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | if (err) { |
| 4589 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4590 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | /* |
| 4595 | * lets try to run arrays based on all disks that have arrived |
| 4596 | * until now. (those are in pending_raid_disks) |
| 4597 | * |
| 4598 | * the method: pick the first pending disk, collect all disks with |
| 4599 | * the same UUID, remove all from the pending list and put them into |
| 4600 | * the 'same_array' list. Then order this list based on superblock |
| 4601 | * update time (freshest comes first), kick out 'old' disks and |
| 4602 | * compare superblocks. If everything's fine then run it. |
| 4603 | * |
| 4604 | * If "unit" is allocated, then bump its reference count |
| 4605 | */ |
| 4606 | static void autorun_devices(int part) |
| 4607 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4608 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | mddev_t *mddev; |
| 4610 | char b[BDEVNAME_SIZE]; |
| 4611 | |
| 4612 | printk(KERN_INFO "md: autorun ...\n"); |
| 4613 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4614 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4616 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | rdev0 = list_entry(pending_raid_disks.next, |
| 4618 | mdk_rdev_t, same_set); |
| 4619 | |
| 4620 | printk(KERN_INFO "md: considering %s ...\n", |
| 4621 | bdevname(rdev0->bdev,b)); |
| 4622 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4623 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4625 | printk(KERN_INFO "md: adding %s ...\n", |
| 4626 | bdevname(rdev->bdev,b)); |
| 4627 | list_move(&rdev->same_set, &candidates); |
| 4628 | } |
| 4629 | /* |
| 4630 | * now we have a set of devices, with all of them having |
| 4631 | * mostly sane superblocks. It's time to allocate the |
| 4632 | * mddev. |
| 4633 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4634 | if (part) { |
| 4635 | dev = MKDEV(mdp_major, |
| 4636 | rdev0->preferred_minor << MdpMinorShift); |
| 4637 | unit = MINOR(dev) >> MdpMinorShift; |
| 4638 | } else { |
| 4639 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4640 | unit = MINOR(dev); |
| 4641 | } |
| 4642 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4644 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4645 | break; |
| 4646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | |
| 4648 | md_probe(dev, NULL, NULL); |
| 4649 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4650 | if (!mddev || !mddev->gendisk) { |
| 4651 | if (mddev) |
| 4652 | mddev_put(mddev); |
| 4653 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | "md: cannot allocate memory for md drive.\n"); |
| 4655 | break; |
| 4656 | } |
| 4657 | if (mddev_lock(mddev)) |
| 4658 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4659 | mdname(mddev)); |
| 4660 | else if (mddev->raid_disks || mddev->major_version |
| 4661 | || !list_empty(&mddev->disks)) { |
| 4662 | printk(KERN_WARNING |
| 4663 | "md: %s already running, cannot run %s\n", |
| 4664 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4665 | mddev_unlock(mddev); |
| 4666 | } else { |
| 4667 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4668 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4669 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | list_del_init(&rdev->same_set); |
| 4671 | if (bind_rdev_to_array(rdev, mddev)) |
| 4672 | export_rdev(rdev); |
| 4673 | } |
| 4674 | autorun_array(mddev); |
| 4675 | mddev_unlock(mddev); |
| 4676 | } |
| 4677 | /* on success, candidates will be empty, on error |
| 4678 | * it won't... |
| 4679 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4680 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4681 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | mddev_put(mddev); |
| 4685 | } |
| 4686 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4687 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4688 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | static int get_version(void __user * arg) |
| 4691 | { |
| 4692 | mdu_version_t ver; |
| 4693 | |
| 4694 | ver.major = MD_MAJOR_VERSION; |
| 4695 | ver.minor = MD_MINOR_VERSION; |
| 4696 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4697 | |
| 4698 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4699 | return -EFAULT; |
| 4700 | |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
| 4704 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4705 | { |
| 4706 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4707 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4710 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4711 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4713 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | failed++; |
| 4715 | else { |
| 4716 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4717 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4718 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | else |
| 4720 | spare++; |
| 4721 | } |
| 4722 | } |
| 4723 | |
| 4724 | info.major_version = mddev->major_version; |
| 4725 | info.minor_version = mddev->minor_version; |
| 4726 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4727 | info.ctime = mddev->ctime; |
| 4728 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4729 | info.size = mddev->dev_sectors / 2; |
| 4730 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4731 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | info.nr_disks = nr; |
| 4733 | info.raid_disks = mddev->raid_disks; |
| 4734 | info.md_minor = mddev->md_minor; |
| 4735 | info.not_persistent= !mddev->persistent; |
| 4736 | |
| 4737 | info.utime = mddev->utime; |
| 4738 | info.state = 0; |
| 4739 | if (mddev->in_sync) |
| 4740 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4741 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4742 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4743 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | info.working_disks = working; |
| 4745 | info.failed_disks = failed; |
| 4746 | info.spare_disks = spare; |
| 4747 | |
| 4748 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4749 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | |
| 4751 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4752 | return -EFAULT; |
| 4753 | |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4757 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4758 | { |
| 4759 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4760 | char *ptr, *buf = NULL; |
| 4761 | int err = -ENOMEM; |
| 4762 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4763 | if (md_allow_write(mddev)) |
| 4764 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4765 | else |
| 4766 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4767 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4768 | if (!file) |
| 4769 | goto out; |
| 4770 | |
| 4771 | /* bitmap disabled, zero the first byte and copy out */ |
| 4772 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4773 | file->pathname[0] = '\0'; |
| 4774 | goto copy_out; |
| 4775 | } |
| 4776 | |
| 4777 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4778 | if (!buf) |
| 4779 | goto out; |
| 4780 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4781 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4782 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4783 | goto out; |
| 4784 | |
| 4785 | strcpy(file->pathname, ptr); |
| 4786 | |
| 4787 | copy_out: |
| 4788 | err = 0; |
| 4789 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4790 | err = -EFAULT; |
| 4791 | out: |
| 4792 | kfree(buf); |
| 4793 | kfree(file); |
| 4794 | return err; |
| 4795 | } |
| 4796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4798 | { |
| 4799 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | mdk_rdev_t *rdev; |
| 4801 | |
| 4802 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4803 | return -EFAULT; |
| 4804 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4805 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | if (rdev) { |
| 4807 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4808 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4809 | info.raid_disk = rdev->raid_disk; |
| 4810 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4811 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4813 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4815 | info.state |= (1<<MD_DISK_SYNC); |
| 4816 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4817 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4818 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | } else { |
| 4820 | info.major = info.minor = 0; |
| 4821 | info.raid_disk = -1; |
| 4822 | info.state = (1<<MD_DISK_REMOVED); |
| 4823 | } |
| 4824 | |
| 4825 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4826 | return -EFAULT; |
| 4827 | |
| 4828 | return 0; |
| 4829 | } |
| 4830 | |
| 4831 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4832 | { |
| 4833 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4834 | mdk_rdev_t *rdev; |
| 4835 | dev_t dev = MKDEV(info->major,info->minor); |
| 4836 | |
| 4837 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4838 | return -EOVERFLOW; |
| 4839 | |
| 4840 | if (!mddev->raid_disks) { |
| 4841 | int err; |
| 4842 | /* expecting a device which has a superblock */ |
| 4843 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4844 | if (IS_ERR(rdev)) { |
| 4845 | printk(KERN_WARNING |
| 4846 | "md: md_import_device returned %ld\n", |
| 4847 | PTR_ERR(rdev)); |
| 4848 | return PTR_ERR(rdev); |
| 4849 | } |
| 4850 | if (!list_empty(&mddev->disks)) { |
| 4851 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4852 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4853 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4855 | if (err < 0) { |
| 4856 | printk(KERN_WARNING |
| 4857 | "md: %s has different UUID to %s\n", |
| 4858 | bdevname(rdev->bdev,b), |
| 4859 | bdevname(rdev0->bdev,b2)); |
| 4860 | export_rdev(rdev); |
| 4861 | return -EINVAL; |
| 4862 | } |
| 4863 | } |
| 4864 | err = bind_rdev_to_array(rdev, mddev); |
| 4865 | if (err) |
| 4866 | export_rdev(rdev); |
| 4867 | return err; |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * add_new_disk can be used once the array is assembled |
| 4872 | * to add "hot spares". They must already have a superblock |
| 4873 | * written |
| 4874 | */ |
| 4875 | if (mddev->pers) { |
| 4876 | int err; |
| 4877 | if (!mddev->pers->hot_add_disk) { |
| 4878 | printk(KERN_WARNING |
| 4879 | "%s: personality does not support diskops!\n", |
| 4880 | mdname(mddev)); |
| 4881 | return -EINVAL; |
| 4882 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4883 | if (mddev->persistent) |
| 4884 | rdev = md_import_device(dev, mddev->major_version, |
| 4885 | mddev->minor_version); |
| 4886 | else |
| 4887 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | if (IS_ERR(rdev)) { |
| 4889 | printk(KERN_WARNING |
| 4890 | "md: md_import_device returned %ld\n", |
| 4891 | PTR_ERR(rdev)); |
| 4892 | return PTR_ERR(rdev); |
| 4893 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4894 | /* set save_raid_disk if appropriate */ |
| 4895 | if (!mddev->persistent) { |
| 4896 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4897 | info->raid_disk < mddev->raid_disks) |
| 4898 | rdev->raid_disk = info->raid_disk; |
| 4899 | else |
| 4900 | rdev->raid_disk = -1; |
| 4901 | } else |
| 4902 | super_types[mddev->major_version]. |
| 4903 | validate_super(mddev, rdev); |
| 4904 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4905 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4906 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4907 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4908 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4909 | else |
| 4910 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | rdev->raid_disk = -1; |
| 4913 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4914 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4915 | /* If there is hot_add_disk but no hot_remove_disk |
| 4916 | * then added disks for geometry changes, |
| 4917 | * and should be added immediately. |
| 4918 | */ |
| 4919 | super_types[mddev->major_version]. |
| 4920 | validate_super(mddev, rdev); |
| 4921 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4922 | if (err) |
| 4923 | unbind_rdev_from_array(rdev); |
| 4924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | if (err) |
| 4926 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4927 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4928 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4929 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4930 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4931 | if (mddev->degraded) |
| 4932 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4933 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4934 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | return err; |
| 4936 | } |
| 4937 | |
| 4938 | /* otherwise, add_new_disk is only allowed |
| 4939 | * for major_version==0 superblocks |
| 4940 | */ |
| 4941 | if (mddev->major_version != 0) { |
| 4942 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4943 | mdname(mddev)); |
| 4944 | return -EINVAL; |
| 4945 | } |
| 4946 | |
| 4947 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4948 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4949 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | if (IS_ERR(rdev)) { |
| 4951 | printk(KERN_WARNING |
| 4952 | "md: error, md_import_device() returned %ld\n", |
| 4953 | PTR_ERR(rdev)); |
| 4954 | return PTR_ERR(rdev); |
| 4955 | } |
| 4956 | rdev->desc_nr = info->number; |
| 4957 | if (info->raid_disk < mddev->raid_disks) |
| 4958 | rdev->raid_disk = info->raid_disk; |
| 4959 | else |
| 4960 | rdev->raid_disk = -1; |
| 4961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4963 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4964 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4966 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4967 | set_bit(WriteMostly, &rdev->flags); |
| 4968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | if (!mddev->persistent) { |
| 4970 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4971 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4973 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4974 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4976 | err = bind_rdev_to_array(rdev, mddev); |
| 4977 | if (err) { |
| 4978 | export_rdev(rdev); |
| 4979 | return err; |
| 4980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | return 0; |
| 4984 | } |
| 4985 | |
| 4986 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4987 | { |
| 4988 | char b[BDEVNAME_SIZE]; |
| 4989 | mdk_rdev_t *rdev; |
| 4990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | rdev = find_rdev(mddev, dev); |
| 4992 | if (!rdev) |
| 4993 | return -ENXIO; |
| 4994 | |
| 4995 | if (rdev->raid_disk >= 0) |
| 4996 | goto busy; |
| 4997 | |
| 4998 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4999 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5000 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | |
| 5002 | return 0; |
| 5003 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5004 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5006 | return -EBUSY; |
| 5007 | } |
| 5008 | |
| 5009 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5010 | { |
| 5011 | char b[BDEVNAME_SIZE]; |
| 5012 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | mdk_rdev_t *rdev; |
| 5014 | |
| 5015 | if (!mddev->pers) |
| 5016 | return -ENODEV; |
| 5017 | |
| 5018 | if (mddev->major_version != 0) { |
| 5019 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5020 | " version-0 superblocks.\n", |
| 5021 | mdname(mddev)); |
| 5022 | return -EINVAL; |
| 5023 | } |
| 5024 | if (!mddev->pers->hot_add_disk) { |
| 5025 | printk(KERN_WARNING |
| 5026 | "%s: personality does not support diskops!\n", |
| 5027 | mdname(mddev)); |
| 5028 | return -EINVAL; |
| 5029 | } |
| 5030 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5031 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | if (IS_ERR(rdev)) { |
| 5033 | printk(KERN_WARNING |
| 5034 | "md: error, md_import_device() returned %ld\n", |
| 5035 | PTR_ERR(rdev)); |
| 5036 | return -EINVAL; |
| 5037 | } |
| 5038 | |
| 5039 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5040 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5042 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5044 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5046 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | printk(KERN_WARNING |
| 5048 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5049 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5050 | err = -EINVAL; |
| 5051 | goto abort_export; |
| 5052 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5053 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5055 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5056 | err = bind_rdev_to_array(rdev, mddev); |
| 5057 | if (err) |
| 5058 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | |
| 5060 | /* |
| 5061 | * The rest should better be atomic, we can have disk failures |
| 5062 | * noticed in interrupt contexts ... |
| 5063 | */ |
| 5064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | rdev->raid_disk = -1; |
| 5066 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5067 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | |
| 5069 | /* |
| 5070 | * Kick recovery, maybe this spare has to be added to the |
| 5071 | * array immediately. |
| 5072 | */ |
| 5073 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5074 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5075 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | return 0; |
| 5077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | abort_export: |
| 5079 | export_rdev(rdev); |
| 5080 | return err; |
| 5081 | } |
| 5082 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5083 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5084 | { |
| 5085 | int err; |
| 5086 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5087 | if (mddev->pers) { |
| 5088 | if (!mddev->pers->quiesce) |
| 5089 | return -EBUSY; |
| 5090 | if (mddev->recovery || mddev->sync_thread) |
| 5091 | return -EBUSY; |
| 5092 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5093 | } |
| 5094 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5095 | |
| 5096 | if (fd >= 0) { |
| 5097 | if (mddev->bitmap) |
| 5098 | return -EEXIST; /* cannot add when bitmap is present */ |
| 5099 | mddev->bitmap_file = fget(fd); |
| 5100 | |
| 5101 | if (mddev->bitmap_file == NULL) { |
| 5102 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5103 | mdname(mddev)); |
| 5104 | return -EBADF; |
| 5105 | } |
| 5106 | |
| 5107 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 5108 | if (err) { |
| 5109 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5110 | mdname(mddev)); |
| 5111 | fput(mddev->bitmap_file); |
| 5112 | mddev->bitmap_file = NULL; |
| 5113 | return err; |
| 5114 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 5115 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5116 | } else if (mddev->bitmap == NULL) |
| 5117 | return -ENOENT; /* cannot remove what isn't there */ |
| 5118 | err = 0; |
| 5119 | if (mddev->pers) { |
| 5120 | mddev->pers->quiesce(mddev, 1); |
| 5121 | if (fd >= 0) |
| 5122 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5123 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5124 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5125 | fd = -1; /* make sure to put the file */ |
| 5126 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5127 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5128 | } |
| 5129 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5130 | if (mddev->bitmap_file) { |
| 5131 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5132 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5133 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5134 | mddev->bitmap_file = NULL; |
| 5135 | } |
| 5136 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5137 | return err; |
| 5138 | } |
| 5139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | /* |
| 5141 | * set_array_info is used two different ways |
| 5142 | * The original usage is when creating a new array. |
| 5143 | * In this usage, raid_disks is > 0 and it together with |
| 5144 | * level, size, not_persistent,layout,chunksize determine the |
| 5145 | * shape of the array. |
| 5146 | * This will always create an array with a type-0.90.0 superblock. |
| 5147 | * The newer usage is when assembling an array. |
| 5148 | * In this case raid_disks will be 0, and the major_version field is |
| 5149 | * use to determine which style super-blocks are to be found on the devices. |
| 5150 | * The minor and patch _version numbers are also kept incase the |
| 5151 | * super_block handler wishes to interpret them. |
| 5152 | */ |
| 5153 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5154 | { |
| 5155 | |
| 5156 | if (info->raid_disks == 0) { |
| 5157 | /* just setting version number for superblock loading */ |
| 5158 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5159 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | super_types[info->major_version].name == NULL) { |
| 5161 | /* maybe try to auto-load a module? */ |
| 5162 | printk(KERN_INFO |
| 5163 | "md: superblock version %d not known\n", |
| 5164 | info->major_version); |
| 5165 | return -EINVAL; |
| 5166 | } |
| 5167 | mddev->major_version = info->major_version; |
| 5168 | mddev->minor_version = info->minor_version; |
| 5169 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5170 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | return 0; |
| 5172 | } |
| 5173 | mddev->major_version = MD_MAJOR_VERSION; |
| 5174 | mddev->minor_version = MD_MINOR_VERSION; |
| 5175 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5176 | mddev->ctime = get_seconds(); |
| 5177 | |
| 5178 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5179 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5180 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | mddev->raid_disks = info->raid_disks; |
| 5182 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5183 | * openned |
| 5184 | */ |
| 5185 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5186 | mddev->recovery_cp = MaxSector; |
| 5187 | else |
| 5188 | mddev->recovery_cp = 0; |
| 5189 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5190 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
| 5192 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5193 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | |
| 5195 | mddev->max_disks = MD_SB_DISKS; |
| 5196 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5197 | if (mddev->persistent) |
| 5198 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5199 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5201 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5202 | mddev->bitmap_offset = 0; |
| 5203 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5204 | mddev->reshape_position = MaxSector; |
| 5205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | /* |
| 5207 | * Generate a 128 bit UUID |
| 5208 | */ |
| 5209 | get_random_bytes(mddev->uuid, 16); |
| 5210 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5211 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5212 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5213 | mddev->new_layout = mddev->layout; |
| 5214 | mddev->delta_disks = 0; |
| 5215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | return 0; |
| 5217 | } |
| 5218 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5219 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5220 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5221 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5222 | |
| 5223 | if (mddev->external_size) |
| 5224 | return; |
| 5225 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5226 | mddev->array_sectors = array_sectors; |
| 5227 | } |
| 5228 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5229 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5230 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5231 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5232 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5233 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5234 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5235 | |
| 5236 | if (mddev->pers->resize == NULL) |
| 5237 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5238 | /* The "num_sectors" is the number of sectors of each device that |
| 5239 | * is used. This can only make sense for arrays with redundancy. |
| 5240 | * linear and raid0 always use whatever space is available. We can only |
| 5241 | * consider changing this number if no resync or reconstruction is |
| 5242 | * 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] | 5243 | * 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] | 5244 | * of each device. If num_sectors is zero, we find the largest size |
| 5245 | * that fits. |
| 5246 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5247 | */ |
| 5248 | if (mddev->sync_thread) |
| 5249 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5250 | if (mddev->bitmap) |
| 5251 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5252 | * grow, and re-add. |
| 5253 | */ |
| 5254 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5255 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5256 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5257 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5258 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5259 | num_sectors = avail; |
| 5260 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5261 | return -ENOSPC; |
| 5262 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5263 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5264 | if (!rv) |
| 5265 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5266 | return rv; |
| 5267 | } |
| 5268 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5269 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5270 | { |
| 5271 | int rv; |
| 5272 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5273 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5274 | return -EINVAL; |
| 5275 | if (raid_disks <= 0 || |
| 5276 | raid_disks >= mddev->max_disks) |
| 5277 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5278 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5279 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5280 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5281 | |
| 5282 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5283 | return rv; |
| 5284 | } |
| 5285 | |
| 5286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | /* |
| 5288 | * update_array_info is used to change the configuration of an |
| 5289 | * on-line array. |
| 5290 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5291 | * fields in the info are checked against the array. |
| 5292 | * Any differences that cannot be handled will cause an error. |
| 5293 | * Normally, only one change can be managed at a time. |
| 5294 | */ |
| 5295 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5296 | { |
| 5297 | int rv = 0; |
| 5298 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5299 | int state = 0; |
| 5300 | |
| 5301 | /* calculate expected state,ignoring low bits */ |
| 5302 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5303 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | |
| 5305 | if (mddev->major_version != info->major_version || |
| 5306 | mddev->minor_version != info->minor_version || |
| 5307 | /* mddev->patch_version != info->patch_version || */ |
| 5308 | mddev->ctime != info->ctime || |
| 5309 | mddev->level != info->level || |
| 5310 | /* mddev->layout != info->layout || */ |
| 5311 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5312 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5313 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5314 | ((state^info->state) & 0xfffffe00) |
| 5315 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | return -EINVAL; |
| 5317 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5318 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5319 | cnt++; |
| 5320 | if (mddev->raid_disks != info->raid_disks) |
| 5321 | cnt++; |
| 5322 | if (mddev->layout != info->layout) |
| 5323 | cnt++; |
| 5324 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5325 | cnt++; |
| 5326 | if (cnt == 0) |
| 5327 | return 0; |
| 5328 | if (cnt > 1) |
| 5329 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | |
| 5331 | if (mddev->layout != info->layout) { |
| 5332 | /* Change layout |
| 5333 | * we don't need to do anything at the md level, the |
| 5334 | * personality will take care of it all. |
| 5335 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5336 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5338 | else { |
| 5339 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5340 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5341 | if (rv) |
| 5342 | mddev->new_layout = mddev->layout; |
| 5343 | return rv; |
| 5344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5346 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5347 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5349 | if (mddev->raid_disks != info->raid_disks) |
| 5350 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5352 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5353 | if (mddev->pers->quiesce == NULL) |
| 5354 | return -EINVAL; |
| 5355 | if (mddev->recovery || mddev->sync_thread) |
| 5356 | return -EBUSY; |
| 5357 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5358 | /* add the bitmap */ |
| 5359 | if (mddev->bitmap) |
| 5360 | return -EEXIST; |
| 5361 | if (mddev->default_bitmap_offset == 0) |
| 5362 | return -EINVAL; |
| 5363 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5364 | mddev->pers->quiesce(mddev, 1); |
| 5365 | rv = bitmap_create(mddev); |
| 5366 | if (rv) |
| 5367 | bitmap_destroy(mddev); |
| 5368 | mddev->pers->quiesce(mddev, 0); |
| 5369 | } else { |
| 5370 | /* remove the bitmap */ |
| 5371 | if (!mddev->bitmap) |
| 5372 | return -ENOENT; |
| 5373 | if (mddev->bitmap->file) |
| 5374 | return -EINVAL; |
| 5375 | mddev->pers->quiesce(mddev, 1); |
| 5376 | bitmap_destroy(mddev); |
| 5377 | mddev->pers->quiesce(mddev, 0); |
| 5378 | mddev->bitmap_offset = 0; |
| 5379 | } |
| 5380 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5381 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | return rv; |
| 5383 | } |
| 5384 | |
| 5385 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5386 | { |
| 5387 | mdk_rdev_t *rdev; |
| 5388 | |
| 5389 | if (mddev->pers == NULL) |
| 5390 | return -ENODEV; |
| 5391 | |
| 5392 | rdev = find_rdev(mddev, dev); |
| 5393 | if (!rdev) |
| 5394 | return -ENODEV; |
| 5395 | |
| 5396 | md_error(mddev, rdev); |
| 5397 | return 0; |
| 5398 | } |
| 5399 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5400 | /* |
| 5401 | * We have a problem here : there is no easy way to give a CHS |
| 5402 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5403 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5404 | * dosfs just mad... ;-) |
| 5405 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5406 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5407 | { |
| 5408 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5409 | |
| 5410 | geo->heads = 2; |
| 5411 | geo->sectors = 4; |
| 5412 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5416 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | unsigned int cmd, unsigned long arg) |
| 5418 | { |
| 5419 | int err = 0; |
| 5420 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | mddev_t *mddev = NULL; |
| 5422 | |
| 5423 | if (!capable(CAP_SYS_ADMIN)) |
| 5424 | return -EACCES; |
| 5425 | |
| 5426 | /* |
| 5427 | * Commands dealing with the RAID driver but not any |
| 5428 | * particular array: |
| 5429 | */ |
| 5430 | switch (cmd) |
| 5431 | { |
| 5432 | case RAID_VERSION: |
| 5433 | err = get_version(argp); |
| 5434 | goto done; |
| 5435 | |
| 5436 | case PRINT_RAID_DEBUG: |
| 5437 | err = 0; |
| 5438 | md_print_devices(); |
| 5439 | goto done; |
| 5440 | |
| 5441 | #ifndef MODULE |
| 5442 | case RAID_AUTORUN: |
| 5443 | err = 0; |
| 5444 | autostart_arrays(arg); |
| 5445 | goto done; |
| 5446 | #endif |
| 5447 | default:; |
| 5448 | } |
| 5449 | |
| 5450 | /* |
| 5451 | * Commands creating/starting a new array: |
| 5452 | */ |
| 5453 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5454 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | |
| 5456 | if (!mddev) { |
| 5457 | BUG(); |
| 5458 | goto abort; |
| 5459 | } |
| 5460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | err = mddev_lock(mddev); |
| 5462 | if (err) { |
| 5463 | printk(KERN_INFO |
| 5464 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5465 | err, cmd); |
| 5466 | goto abort; |
| 5467 | } |
| 5468 | |
| 5469 | switch (cmd) |
| 5470 | { |
| 5471 | case SET_ARRAY_INFO: |
| 5472 | { |
| 5473 | mdu_array_info_t info; |
| 5474 | if (!arg) |
| 5475 | memset(&info, 0, sizeof(info)); |
| 5476 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5477 | err = -EFAULT; |
| 5478 | goto abort_unlock; |
| 5479 | } |
| 5480 | if (mddev->pers) { |
| 5481 | err = update_array_info(mddev, &info); |
| 5482 | if (err) { |
| 5483 | printk(KERN_WARNING "md: couldn't update" |
| 5484 | " array info. %d\n", err); |
| 5485 | goto abort_unlock; |
| 5486 | } |
| 5487 | goto done_unlock; |
| 5488 | } |
| 5489 | if (!list_empty(&mddev->disks)) { |
| 5490 | printk(KERN_WARNING |
| 5491 | "md: array %s already has disks!\n", |
| 5492 | mdname(mddev)); |
| 5493 | err = -EBUSY; |
| 5494 | goto abort_unlock; |
| 5495 | } |
| 5496 | if (mddev->raid_disks) { |
| 5497 | printk(KERN_WARNING |
| 5498 | "md: array %s already initialised!\n", |
| 5499 | mdname(mddev)); |
| 5500 | err = -EBUSY; |
| 5501 | goto abort_unlock; |
| 5502 | } |
| 5503 | err = set_array_info(mddev, &info); |
| 5504 | if (err) { |
| 5505 | printk(KERN_WARNING "md: couldn't set" |
| 5506 | " array info. %d\n", err); |
| 5507 | goto abort_unlock; |
| 5508 | } |
| 5509 | } |
| 5510 | goto done_unlock; |
| 5511 | |
| 5512 | default:; |
| 5513 | } |
| 5514 | |
| 5515 | /* |
| 5516 | * Commands querying/configuring an existing array: |
| 5517 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5518 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5519 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5520 | if ((!mddev->raid_disks && !mddev->external) |
| 5521 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5522 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5523 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | err = -ENODEV; |
| 5525 | goto abort_unlock; |
| 5526 | } |
| 5527 | |
| 5528 | /* |
| 5529 | * Commands even a read-only array can execute: |
| 5530 | */ |
| 5531 | switch (cmd) |
| 5532 | { |
| 5533 | case GET_ARRAY_INFO: |
| 5534 | err = get_array_info(mddev, argp); |
| 5535 | goto done_unlock; |
| 5536 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5537 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5538 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5539 | goto done_unlock; |
| 5540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | case GET_DISK_INFO: |
| 5542 | err = get_disk_info(mddev, argp); |
| 5543 | goto done_unlock; |
| 5544 | |
| 5545 | case RESTART_ARRAY_RW: |
| 5546 | err = restart_array(mddev); |
| 5547 | goto done_unlock; |
| 5548 | |
| 5549 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5550 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | goto done_unlock; |
| 5552 | |
| 5553 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5554 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | goto done_unlock; |
| 5556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | /* |
| 5560 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5561 | * superblock, so we do not allow them on read-only arrays. |
| 5562 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5563 | * here and hit the 'default' below, so only disallow |
| 5564 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5566 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5567 | if (mddev->ro == 2) { |
| 5568 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5569 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5570 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5571 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5572 | } else { |
| 5573 | err = -EROFS; |
| 5574 | goto abort_unlock; |
| 5575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | switch (cmd) |
| 5579 | { |
| 5580 | case ADD_NEW_DISK: |
| 5581 | { |
| 5582 | mdu_disk_info_t info; |
| 5583 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5584 | err = -EFAULT; |
| 5585 | else |
| 5586 | err = add_new_disk(mddev, &info); |
| 5587 | goto done_unlock; |
| 5588 | } |
| 5589 | |
| 5590 | case HOT_REMOVE_DISK: |
| 5591 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5592 | goto done_unlock; |
| 5593 | |
| 5594 | case HOT_ADD_DISK: |
| 5595 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5596 | goto done_unlock; |
| 5597 | |
| 5598 | case SET_DISK_FAULTY: |
| 5599 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5600 | goto done_unlock; |
| 5601 | |
| 5602 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5603 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | goto done_unlock; |
| 5605 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5606 | case SET_BITMAP_FILE: |
| 5607 | err = set_bitmap_file(mddev, (int)arg); |
| 5608 | goto done_unlock; |
| 5609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | err = -EINVAL; |
| 5612 | goto abort_unlock; |
| 5613 | } |
| 5614 | |
| 5615 | done_unlock: |
| 5616 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5617 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5618 | err != -EINVAL) |
| 5619 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | mddev_unlock(mddev); |
| 5621 | |
| 5622 | return err; |
| 5623 | done: |
| 5624 | if (err) |
| 5625 | MD_BUG(); |
| 5626 | abort: |
| 5627 | return err; |
| 5628 | } |
| 5629 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5630 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | { |
| 5632 | /* |
| 5633 | * Succeed if we can lock the mddev, which confirms that |
| 5634 | * it isn't being stopped right now. |
| 5635 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5636 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | int err; |
| 5638 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5639 | if (mddev->gendisk != bdev->bd_disk) { |
| 5640 | /* we are racing with mddev_put which is discarding this |
| 5641 | * bd_disk. |
| 5642 | */ |
| 5643 | mddev_put(mddev); |
| 5644 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5645 | flush_scheduled_work(); |
| 5646 | /* Then retry the open from the top */ |
| 5647 | return -ERESTARTSYS; |
| 5648 | } |
| 5649 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5650 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5651 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | goto out; |
| 5653 | |
| 5654 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5655 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5656 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5658 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | out: |
| 5660 | return err; |
| 5661 | } |
| 5662 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5663 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5665 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5667 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5668 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | mddev_put(mddev); |
| 5670 | |
| 5671 | return 0; |
| 5672 | } |
| 5673 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5674 | static int md_media_changed(struct gendisk *disk) |
| 5675 | { |
| 5676 | mddev_t *mddev = disk->private_data; |
| 5677 | |
| 5678 | return mddev->changed; |
| 5679 | } |
| 5680 | |
| 5681 | static int md_revalidate(struct gendisk *disk) |
| 5682 | { |
| 5683 | mddev_t *mddev = disk->private_data; |
| 5684 | |
| 5685 | mddev->changed = 0; |
| 5686 | return 0; |
| 5687 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5688 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | { |
| 5690 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5691 | .open = md_open, |
| 5692 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5693 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5694 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5695 | .media_changed = md_media_changed, |
| 5696 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | }; |
| 5698 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5699 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | { |
| 5701 | mdk_thread_t *thread = arg; |
| 5702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | /* |
| 5704 | * md_thread is a 'system-thread', it's priority should be very |
| 5705 | * high. We avoid resource deadlocks individually in each |
| 5706 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5707 | * the very same RT priority as kswapd, thus we will never get |
| 5708 | * into a priority inversion deadlock. |
| 5709 | * |
| 5710 | * we definitely have to have equal or higher priority than |
| 5711 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5712 | * many dirty RAID5 blocks. |
| 5713 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5715 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5716 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5718 | /* We need to wait INTERRUPTIBLE so that |
| 5719 | * we don't add to the load-average. |
| 5720 | * That means we need to be sure no signals are |
| 5721 | * pending |
| 5722 | */ |
| 5723 | if (signal_pending(current)) |
| 5724 | flush_signals(current); |
| 5725 | |
| 5726 | wait_event_interruptible_timeout |
| 5727 | (thread->wqueue, |
| 5728 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5729 | || kthread_should_stop(), |
| 5730 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
| 5732 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5733 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5734 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | return 0; |
| 5738 | } |
| 5739 | |
| 5740 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5741 | { |
| 5742 | if (thread) { |
| 5743 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5744 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5745 | wake_up(&thread->wqueue); |
| 5746 | } |
| 5747 | } |
| 5748 | |
| 5749 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5750 | const char *name) |
| 5751 | { |
| 5752 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5754 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | if (!thread) |
| 5756 | return NULL; |
| 5757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | init_waitqueue_head(&thread->wqueue); |
| 5759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | thread->run = run; |
| 5761 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5762 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5763 | thread->tsk = kthread_run(md_thread, thread, |
| 5764 | "%s_%s", |
| 5765 | mdname(thread->mddev), |
| 5766 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5767 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | kfree(thread); |
| 5769 | return NULL; |
| 5770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | return thread; |
| 5772 | } |
| 5773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | void md_unregister_thread(mdk_thread_t *thread) |
| 5775 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5776 | if (!thread) |
| 5777 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5778 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5779 | |
| 5780 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | kfree(thread); |
| 5782 | } |
| 5783 | |
| 5784 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5785 | { |
| 5786 | if (!mddev) { |
| 5787 | MD_BUG(); |
| 5788 | return; |
| 5789 | } |
| 5790 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5791 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5793 | |
| 5794 | if (mddev->external) |
| 5795 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5796 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5798 | mdname(mddev), |
| 5799 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5800 | __builtin_return_address(0),__builtin_return_address(1), |
| 5801 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5802 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5803 | if (!mddev->pers) |
| 5804 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | if (!mddev->pers->error_handler) |
| 5806 | return; |
| 5807 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5808 | if (mddev->degraded) |
| 5809 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5810 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5812 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5813 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5814 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | /* seq_file implementation /proc/mdstat */ |
| 5818 | |
| 5819 | static void status_unused(struct seq_file *seq) |
| 5820 | { |
| 5821 | int i = 0; |
| 5822 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | |
| 5824 | seq_printf(seq, "unused devices: "); |
| 5825 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5826 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | char b[BDEVNAME_SIZE]; |
| 5828 | i++; |
| 5829 | seq_printf(seq, "%s ", |
| 5830 | bdevname(rdev->bdev,b)); |
| 5831 | } |
| 5832 | if (!i) |
| 5833 | seq_printf(seq, "<none>"); |
| 5834 | |
| 5835 | seq_printf(seq, "\n"); |
| 5836 | } |
| 5837 | |
| 5838 | |
| 5839 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5840 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5841 | sector_t max_sectors, resync, res; |
| 5842 | unsigned long dt, db; |
| 5843 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5844 | int scale; |
| 5845 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5847 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | |
| 5849 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5850 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5852 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | |
| 5854 | /* |
| 5855 | * Should not happen. |
| 5856 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5857 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | MD_BUG(); |
| 5859 | return; |
| 5860 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5861 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5862 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5863 | * u32, as those are the requirements for sector_div. |
| 5864 | * Thus 'scale' must be at least 10 |
| 5865 | */ |
| 5866 | scale = 10; |
| 5867 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5868 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5869 | scale++; |
| 5870 | } |
| 5871 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5872 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5873 | |
| 5874 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5876 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | seq_printf(seq, "["); |
| 5878 | for (i = 0; i < x; i++) |
| 5879 | seq_printf(seq, "="); |
| 5880 | seq_printf(seq, ">"); |
| 5881 | for (i = 0; i < y; i++) |
| 5882 | seq_printf(seq, "."); |
| 5883 | seq_printf(seq, "] "); |
| 5884 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5885 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5886 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5887 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5888 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5889 | "check" : |
| 5890 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5891 | "resync" : "recovery"))), |
| 5892 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5893 | (unsigned long long) resync/2, |
| 5894 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | |
| 5896 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | * dt: time from mark until now |
| 5898 | * db: blocks written from mark until now |
| 5899 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5900 | * |
| 5901 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5902 | * So we divide before multiply in case it is 32bit and close |
| 5903 | * to the limit. |
| 5904 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5905 | * near the end of resync when the number of remaining sectors |
| 5906 | * is close to 'db'. |
| 5907 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5908 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | */ |
| 5910 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5911 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5912 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5913 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5915 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5916 | sector_div(rt, db/32+1); |
| 5917 | rt *= dt; |
| 5918 | rt >>= 5; |
| 5919 | |
| 5920 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5921 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5923 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5927 | { |
| 5928 | struct list_head *tmp; |
| 5929 | loff_t l = *pos; |
| 5930 | mddev_t *mddev; |
| 5931 | |
| 5932 | if (l >= 0x10000) |
| 5933 | return NULL; |
| 5934 | if (!l--) |
| 5935 | /* header */ |
| 5936 | return (void*)1; |
| 5937 | |
| 5938 | spin_lock(&all_mddevs_lock); |
| 5939 | list_for_each(tmp,&all_mddevs) |
| 5940 | if (!l--) { |
| 5941 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5942 | mddev_get(mddev); |
| 5943 | spin_unlock(&all_mddevs_lock); |
| 5944 | return mddev; |
| 5945 | } |
| 5946 | spin_unlock(&all_mddevs_lock); |
| 5947 | if (!l--) |
| 5948 | return (void*)2;/* tail */ |
| 5949 | return NULL; |
| 5950 | } |
| 5951 | |
| 5952 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5953 | { |
| 5954 | struct list_head *tmp; |
| 5955 | mddev_t *next_mddev, *mddev = v; |
| 5956 | |
| 5957 | ++*pos; |
| 5958 | if (v == (void*)2) |
| 5959 | return NULL; |
| 5960 | |
| 5961 | spin_lock(&all_mddevs_lock); |
| 5962 | if (v == (void*)1) |
| 5963 | tmp = all_mddevs.next; |
| 5964 | else |
| 5965 | tmp = mddev->all_mddevs.next; |
| 5966 | if (tmp != &all_mddevs) |
| 5967 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5968 | else { |
| 5969 | next_mddev = (void*)2; |
| 5970 | *pos = 0x10000; |
| 5971 | } |
| 5972 | spin_unlock(&all_mddevs_lock); |
| 5973 | |
| 5974 | if (v != (void*)1) |
| 5975 | mddev_put(mddev); |
| 5976 | return next_mddev; |
| 5977 | |
| 5978 | } |
| 5979 | |
| 5980 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5981 | { |
| 5982 | mddev_t *mddev = v; |
| 5983 | |
| 5984 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5985 | mddev_put(mddev); |
| 5986 | } |
| 5987 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5988 | struct mdstat_info { |
| 5989 | int event; |
| 5990 | }; |
| 5991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5993 | { |
| 5994 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5995 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5997 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5998 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | |
| 6000 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6001 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | seq_printf(seq, "Personalities : "); |
| 6003 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6004 | list_for_each_entry(pers, &pers_list, list) |
| 6005 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | |
| 6007 | spin_unlock(&pers_lock); |
| 6008 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6009 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | return 0; |
| 6011 | } |
| 6012 | if (v == (void*)2) { |
| 6013 | status_unused(seq); |
| 6014 | return 0; |
| 6015 | } |
| 6016 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6017 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6021 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6022 | mddev->pers ? "" : "in"); |
| 6023 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6024 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6026 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6027 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | seq_printf(seq, " %s", mddev->pers->name); |
| 6029 | } |
| 6030 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6031 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6032 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | char b[BDEVNAME_SIZE]; |
| 6034 | seq_printf(seq, " %s[%d]", |
| 6035 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6036 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6037 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6038 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | seq_printf(seq, "(F)"); |
| 6040 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6041 | } else if (rdev->raid_disk < 0) |
| 6042 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6043 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | if (!list_empty(&mddev->disks)) { |
| 6047 | if (mddev->pers) |
| 6048 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6049 | (unsigned long long) |
| 6050 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | else |
| 6052 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6053 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6055 | if (mddev->persistent) { |
| 6056 | if (mddev->major_version != 0 || |
| 6057 | mddev->minor_version != 90) { |
| 6058 | seq_printf(seq," super %d.%d", |
| 6059 | mddev->major_version, |
| 6060 | mddev->minor_version); |
| 6061 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6062 | } else if (mddev->external) |
| 6063 | seq_printf(seq, " super external:%s", |
| 6064 | mddev->metadata_type); |
| 6065 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6066 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | |
| 6068 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6069 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6071 | if (mddev->pers->sync_request) { |
| 6072 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6073 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6074 | seq_printf(seq, "\n "); |
| 6075 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6076 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6077 | else if (mddev->recovery_cp < MaxSector) |
| 6078 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6079 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6080 | } else |
| 6081 | seq_printf(seq, "\n "); |
| 6082 | |
| 6083 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6084 | unsigned long chunk_kb; |
| 6085 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6086 | spin_lock_irqsave(&bitmap->lock, flags); |
| 6087 | chunk_kb = bitmap->chunksize >> 10; |
| 6088 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6089 | "%lu%s chunk", |
| 6090 | bitmap->pages - bitmap->missing_pages, |
| 6091 | bitmap->pages, |
| 6092 | (bitmap->pages - bitmap->missing_pages) |
| 6093 | << (PAGE_SHIFT - 10), |
| 6094 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 6095 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6096 | if (bitmap->file) { |
| 6097 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6098 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6099 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6100 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6101 | seq_printf(seq, "\n"); |
| 6102 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | seq_printf(seq, "\n"); |
| 6106 | } |
| 6107 | mddev_unlock(mddev); |
| 6108 | |
| 6109 | return 0; |
| 6110 | } |
| 6111 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6112 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | .start = md_seq_start, |
| 6114 | .next = md_seq_next, |
| 6115 | .stop = md_seq_stop, |
| 6116 | .show = md_seq_show, |
| 6117 | }; |
| 6118 | |
| 6119 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6120 | { |
| 6121 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6122 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6123 | if (mi == NULL) |
| 6124 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | |
| 6126 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6127 | if (error) |
| 6128 | kfree(mi); |
| 6129 | else { |
| 6130 | struct seq_file *p = file->private_data; |
| 6131 | p->private = mi; |
| 6132 | mi->event = atomic_read(&md_event_count); |
| 6133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | return error; |
| 6135 | } |
| 6136 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6137 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6138 | { |
| 6139 | struct seq_file *m = filp->private_data; |
| 6140 | struct mdstat_info *mi = m->private; |
| 6141 | int mask; |
| 6142 | |
| 6143 | poll_wait(filp, &md_event_waiters, wait); |
| 6144 | |
| 6145 | /* always allow read */ |
| 6146 | mask = POLLIN | POLLRDNORM; |
| 6147 | |
| 6148 | if (mi->event != atomic_read(&md_event_count)) |
| 6149 | mask |= POLLERR | POLLPRI; |
| 6150 | return mask; |
| 6151 | } |
| 6152 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6153 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6154 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | .open = md_seq_open, |
| 6156 | .read = seq_read, |
| 6157 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6158 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6159 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | }; |
| 6161 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6162 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6165 | list_add_tail(&p->list, &pers_list); |
| 6166 | 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] | 6167 | spin_unlock(&pers_lock); |
| 6168 | return 0; |
| 6169 | } |
| 6170 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6171 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6173 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6175 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | spin_unlock(&pers_lock); |
| 6177 | return 0; |
| 6178 | } |
| 6179 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6180 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | { |
| 6182 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6184 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | |
| 6186 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6187 | rcu_read_lock(); |
| 6188 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6190 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6191 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6192 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6193 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6194 | * as sync_io is counted when a request starts, and |
| 6195 | * disk_stats is counted when it completes. |
| 6196 | * So resync activity will cause curr_events to be smaller than |
| 6197 | * when there was no such activity. |
| 6198 | * non-sync IO will cause disk_stat to increase without |
| 6199 | * increasing sync_io so curr_events will (eventually) |
| 6200 | * be larger than it was before. Once it becomes |
| 6201 | * substantially larger, the test below will cause |
| 6202 | * the array to appear non-idle, and resync will slow |
| 6203 | * down. |
| 6204 | * If there is a lot of outstanding resync activity when |
| 6205 | * we set last_event to curr_events, then all that activity |
| 6206 | * completing might cause the array to appear non-idle |
| 6207 | * and resync will be slowed down even though there might |
| 6208 | * not have been non-resync activity. This will only |
| 6209 | * happen once though. 'last_events' will soon reflect |
| 6210 | * the state where there is little or no outstanding |
| 6211 | * resync requests, and further resync activity will |
| 6212 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6213 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6215 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | rdev->last_events = curr_events; |
| 6217 | idle = 0; |
| 6218 | } |
| 6219 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6220 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | return idle; |
| 6222 | } |
| 6223 | |
| 6224 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6225 | { |
| 6226 | /* another "blocks" (512byte) blocks have been synced */ |
| 6227 | atomic_sub(blocks, &mddev->recovery_active); |
| 6228 | wake_up(&mddev->recovery_wait); |
| 6229 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6230 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | md_wakeup_thread(mddev->thread); |
| 6232 | // stop recovery, signal do_sync .... |
| 6233 | } |
| 6234 | } |
| 6235 | |
| 6236 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6237 | /* md_write_start(mddev, bi) |
| 6238 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6239 | * in superblock) before writing, schedule a superblock update |
| 6240 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6241 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6242 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6244 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6245 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6246 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6247 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6248 | BUG_ON(mddev->ro == 1); |
| 6249 | if (mddev->ro == 2) { |
| 6250 | /* need to switch to read/write */ |
| 6251 | mddev->ro = 0; |
| 6252 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6253 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6254 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6255 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6256 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6257 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6258 | if (mddev->safemode == 1) |
| 6259 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6260 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6261 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6262 | if (mddev->in_sync) { |
| 6263 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6264 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6265 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6266 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6267 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6268 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6269 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6270 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6271 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6272 | wait_event(mddev->sb_wait, |
| 6273 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6274 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | void md_write_end(mddev_t *mddev) |
| 6278 | { |
| 6279 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6280 | if (mddev->safemode == 2) |
| 6281 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6282 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6284 | } |
| 6285 | } |
| 6286 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6287 | /* md_allow_write(mddev) |
| 6288 | * Calling this ensures that the array is marked 'active' so that writes |
| 6289 | * may proceed without blocking. It is important to call this before |
| 6290 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6291 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6292 | * |
| 6293 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6294 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6295 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6296 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6297 | { |
| 6298 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6299 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6300 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6301 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6302 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6303 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6304 | |
| 6305 | spin_lock_irq(&mddev->write_lock); |
| 6306 | if (mddev->in_sync) { |
| 6307 | mddev->in_sync = 0; |
| 6308 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6309 | if (mddev->safemode_delay && |
| 6310 | mddev->safemode == 0) |
| 6311 | mddev->safemode = 1; |
| 6312 | spin_unlock_irq(&mddev->write_lock); |
| 6313 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6314 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6315 | } else |
| 6316 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6317 | |
| 6318 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6319 | return -EAGAIN; |
| 6320 | else |
| 6321 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6322 | } |
| 6323 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | #define SYNC_MARKS 10 |
| 6326 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6327 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | { |
| 6329 | mddev_t *mddev2; |
| 6330 | unsigned int currspeed = 0, |
| 6331 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6332 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | unsigned long mark[SYNC_MARKS]; |
| 6334 | sector_t mark_cnt[SYNC_MARKS]; |
| 6335 | int last_mark,m; |
| 6336 | struct list_head *tmp; |
| 6337 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6338 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6339 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6340 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | |
| 6342 | /* just incase thread restarts... */ |
| 6343 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6344 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6345 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6346 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6348 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6349 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6350 | desc = "data-check"; |
| 6351 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6352 | desc = "requested-resync"; |
| 6353 | else |
| 6354 | desc = "resync"; |
| 6355 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6356 | desc = "reshape"; |
| 6357 | else |
| 6358 | desc = "recovery"; |
| 6359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | /* we overload curr_resync somewhat here. |
| 6361 | * 0 == not engaged in resync at all |
| 6362 | * 2 == checking that there is no conflict with another sync |
| 6363 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6364 | * commense |
| 6365 | * other == active in resync - this many blocks |
| 6366 | * |
| 6367 | * Before starting a resync we must have set curr_resync to |
| 6368 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6369 | * less than ours. When we find one that is the same or higher |
| 6370 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6371 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6372 | * This will mean we have to start checking from the beginning again. |
| 6373 | * |
| 6374 | */ |
| 6375 | |
| 6376 | do { |
| 6377 | mddev->curr_resync = 2; |
| 6378 | |
| 6379 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6380 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6381 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | goto skip; |
| 6383 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6384 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | if (mddev2 == mddev) |
| 6386 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6387 | if (!mddev->parallel_resync |
| 6388 | && mddev2->curr_resync |
| 6389 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | DEFINE_WAIT(wq); |
| 6391 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6392 | /* arbitrarily yield */ |
| 6393 | mddev->curr_resync = 1; |
| 6394 | wake_up(&resync_wait); |
| 6395 | } |
| 6396 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6397 | /* no need to wait here, we can wait the next |
| 6398 | * time 'round when curr_resync == 2 |
| 6399 | */ |
| 6400 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6401 | /* We need to wait 'interruptible' so as not to |
| 6402 | * contribute to the load average, and not to |
| 6403 | * be caught by 'softlockup' |
| 6404 | */ |
| 6405 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6406 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6407 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6408 | printk(KERN_INFO "md: delaying %s of %s" |
| 6409 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6411 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6413 | if (signal_pending(current)) |
| 6414 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | schedule(); |
| 6416 | finish_wait(&resync_wait, &wq); |
| 6417 | goto try_again; |
| 6418 | } |
| 6419 | finish_wait(&resync_wait, &wq); |
| 6420 | } |
| 6421 | } |
| 6422 | } while (mddev->curr_resync < 2); |
| 6423 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6424 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6425 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6427 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | */ |
| 6429 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6430 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6431 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6432 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6433 | j = mddev->resync_min; |
| 6434 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6435 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6436 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6437 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6438 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6439 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6441 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6442 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6443 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6444 | if (rdev->raid_disk >= 0 && |
| 6445 | !test_bit(Faulty, &rdev->flags) && |
| 6446 | !test_bit(In_sync, &rdev->flags) && |
| 6447 | rdev->recovery_offset < j) |
| 6448 | j = rdev->recovery_offset; |
| 6449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6451 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6452 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6453 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6454 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6455 | "(but not more than %d KB/sec) for %s.\n", |
| 6456 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6458 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6459 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6460 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6462 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6463 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | } |
| 6465 | last_mark = 0; |
| 6466 | mddev->resync_mark = mark[last_mark]; |
| 6467 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6468 | |
| 6469 | /* |
| 6470 | * Tune reconstruction: |
| 6471 | */ |
| 6472 | window = 32*(PAGE_SIZE/512); |
| 6473 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6474 | window/2,(unsigned long long) max_sectors/2); |
| 6475 | |
| 6476 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | last_check = 0; |
| 6478 | |
| 6479 | if (j>2) { |
| 6480 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6481 | "md: resuming %s of %s from checkpoint.\n", |
| 6482 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | mddev->curr_resync = j; |
| 6484 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6485 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | |
| 6487 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6488 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6490 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6491 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6492 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6493 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6494 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6495 | > (max_sectors >> 4)) || |
| 6496 | (j - mddev->curr_resync_completed)*2 |
| 6497 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6498 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6499 | /* time to update curr_resync_completed */ |
| 6500 | blk_unplug(mddev->queue); |
| 6501 | wait_event(mddev->recovery_wait, |
| 6502 | atomic_read(&mddev->recovery_active) == 0); |
| 6503 | mddev->curr_resync_completed = |
| 6504 | mddev->curr_resync; |
| 6505 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6506 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6507 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6508 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6509 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6510 | /* As this condition is controlled by user-space, |
| 6511 | * we can block indefinitely, so use '_interruptible' |
| 6512 | * to avoid triggering warnings. |
| 6513 | */ |
| 6514 | flush_signals(current); /* just in case */ |
| 6515 | wait_event_interruptible(mddev->recovery_wait, |
| 6516 | mddev->resync_max > j |
| 6517 | || kthread_should_stop()); |
| 6518 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6519 | |
| 6520 | if (kthread_should_stop()) |
| 6521 | goto interrupted; |
| 6522 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6523 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6524 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6525 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6526 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | goto out; |
| 6528 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6529 | |
| 6530 | if (!skipped) { /* actual IO requested */ |
| 6531 | io_sectors += sectors; |
| 6532 | atomic_add(sectors, &mddev->recovery_active); |
| 6533 | } |
| 6534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | j += sectors; |
| 6536 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6537 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6538 | if (last_check == 0) |
| 6539 | /* this is the earliers that rebuilt will be |
| 6540 | * visible in /proc/mdstat |
| 6541 | */ |
| 6542 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6543 | |
| 6544 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | continue; |
| 6546 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6547 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6549 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | break; |
| 6551 | |
| 6552 | repeat: |
| 6553 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6554 | /* step marks */ |
| 6555 | int next = (last_mark+1) % SYNC_MARKS; |
| 6556 | |
| 6557 | mddev->resync_mark = mark[next]; |
| 6558 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6559 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6560 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | last_mark = next; |
| 6562 | } |
| 6563 | |
| 6564 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6565 | if (kthread_should_stop()) |
| 6566 | goto interrupted; |
| 6567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | |
| 6569 | /* |
| 6570 | * this loop exits only if either when we are slower than |
| 6571 | * the 'hard' speed limit, or the system was IO-idle for |
| 6572 | * a jiffy. |
| 6573 | * the system might be non-idle CPU-wise, but we only care |
| 6574 | * about not overloading the IO subsystem. (things like an |
| 6575 | * e2fsck being done on the RAID array should execute fast) |
| 6576 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6577 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | cond_resched(); |
| 6579 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6580 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6581 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6583 | if (currspeed > speed_min(mddev)) { |
| 6584 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6585 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6586 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | goto repeat; |
| 6588 | } |
| 6589 | } |
| 6590 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6591 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | /* |
| 6593 | * this also signals 'finished resyncing' to md_stop |
| 6594 | */ |
| 6595 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6596 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | |
| 6598 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6599 | |
| 6600 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6601 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6603 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6604 | mddev->curr_resync > 2) { |
| 6605 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6606 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6607 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6608 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6609 | "md: checkpointing %s of %s.\n", |
| 6610 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6611 | mddev->recovery_cp = mddev->curr_resync; |
| 6612 | } |
| 6613 | } else |
| 6614 | mddev->recovery_cp = MaxSector; |
| 6615 | } else { |
| 6616 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6617 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6618 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6619 | if (rdev->raid_disk >= 0 && |
| 6620 | !test_bit(Faulty, &rdev->flags) && |
| 6621 | !test_bit(In_sync, &rdev->flags) && |
| 6622 | rdev->recovery_offset < mddev->curr_resync) |
| 6623 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6626 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6628 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6629 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6630 | /* We completed so min/max setting can be forgotten if used. */ |
| 6631 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6632 | mddev->resync_min = 0; |
| 6633 | mddev->resync_max = MaxSector; |
| 6634 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6635 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6637 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6638 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6639 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | wake_up(&resync_wait); |
| 6641 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6642 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6643 | return; |
| 6644 | |
| 6645 | interrupted: |
| 6646 | /* |
| 6647 | * got a signal, exit. |
| 6648 | */ |
| 6649 | printk(KERN_INFO |
| 6650 | "md: md_do_sync() got signal ... exiting\n"); |
| 6651 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6652 | goto out; |
| 6653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6655 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | |
| 6657 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6658 | static int remove_and_add_spares(mddev_t *mddev) |
| 6659 | { |
| 6660 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6661 | int spares = 0; |
| 6662 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6663 | mddev->curr_resync_completed = 0; |
| 6664 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6665 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6666 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6667 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6668 | (test_bit(Faulty, &rdev->flags) || |
| 6669 | ! test_bit(In_sync, &rdev->flags)) && |
| 6670 | atomic_read(&rdev->nr_pending)==0) { |
| 6671 | if (mddev->pers->hot_remove_disk( |
| 6672 | mddev, rdev->raid_disk)==0) { |
| 6673 | char nm[20]; |
| 6674 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6675 | sysfs_remove_link(&mddev->kobj, nm); |
| 6676 | rdev->raid_disk = -1; |
| 6677 | } |
| 6678 | } |
| 6679 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6680 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6681 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6682 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6683 | !test_bit(In_sync, &rdev->flags) && |
| 6684 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6685 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6686 | if (rdev->raid_disk < 0 |
| 6687 | && !test_bit(Faulty, &rdev->flags)) { |
| 6688 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6689 | if (mddev->pers-> |
| 6690 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6691 | char nm[20]; |
| 6692 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6693 | if (sysfs_create_link(&mddev->kobj, |
| 6694 | &rdev->kobj, nm)) |
| 6695 | printk(KERN_WARNING |
| 6696 | "md: cannot register " |
| 6697 | "%s for %s\n", |
| 6698 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6699 | spares++; |
| 6700 | md_new_event(mddev); |
| 6701 | } else |
| 6702 | break; |
| 6703 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6704 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6705 | } |
| 6706 | return spares; |
| 6707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | /* |
| 6709 | * This routine is regularly called by all per-raid-array threads to |
| 6710 | * deal with generic issues like resync and super-block update. |
| 6711 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6712 | * need this as they never do any recovery or update the superblock. |
| 6713 | * |
| 6714 | * It does not do any resync itself, but rather "forks" off other threads |
| 6715 | * to do that as needed. |
| 6716 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6717 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6718 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | * and wakeups up this thread which will reap the thread and finish up. |
| 6720 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6721 | * |
| 6722 | * The overall approach is: |
| 6723 | * 1/ if the superblock needs updating, update it. |
| 6724 | * 2/ If a recovery thread is running, don't do anything else. |
| 6725 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6726 | * 4/ If there are any faulty devices, remove them. |
| 6727 | * 5/ If array is degraded, try to add spares devices |
| 6728 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6729 | */ |
| 6730 | void md_check_recovery(mddev_t *mddev) |
| 6731 | { |
| 6732 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | |
| 6734 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6735 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6736 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | |
| 6738 | if (mddev->ro) |
| 6739 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6740 | |
| 6741 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6742 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6743 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6744 | mdname(mddev)); |
| 6745 | mddev->safemode = 2; |
| 6746 | } |
| 6747 | flush_signals(current); |
| 6748 | } |
| 6749 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6750 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6751 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6753 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6755 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6756 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6757 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6758 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6759 | )) |
| 6760 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6761 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6762 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6763 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6764 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6765 | if (mddev->ro) { |
| 6766 | /* Only thing we do on a ro array is remove |
| 6767 | * failed devices. |
| 6768 | */ |
| 6769 | remove_and_add_spares(mddev); |
| 6770 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6771 | goto unlock; |
| 6772 | } |
| 6773 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6774 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6775 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6776 | spin_lock_irq(&mddev->write_lock); |
| 6777 | if (mddev->safemode && |
| 6778 | !atomic_read(&mddev->writes_pending) && |
| 6779 | !mddev->in_sync && |
| 6780 | mddev->recovery_cp == MaxSector) { |
| 6781 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6782 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6783 | if (mddev->persistent) |
| 6784 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6785 | } |
| 6786 | if (mddev->safemode == 1) |
| 6787 | mddev->safemode = 0; |
| 6788 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6789 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6790 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6791 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6792 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6793 | if (mddev->flags) |
| 6794 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6795 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6796 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6797 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6798 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6799 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6802 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6803 | /* resync/recovery still happening */ |
| 6804 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6805 | goto unlock; |
| 6806 | } |
| 6807 | if (mddev->sync_thread) { |
| 6808 | /* resync has finished, collect result */ |
| 6809 | md_unregister_thread(mddev->sync_thread); |
| 6810 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6811 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6812 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | /* success...*/ |
| 6814 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6815 | if (mddev->pers->spare_active(mddev)) |
| 6816 | sysfs_notify(&mddev->kobj, NULL, |
| 6817 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6819 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6820 | mddev->pers->finish_reshape) |
| 6821 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6822 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6823 | |
| 6824 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6825 | * information must be scrapped |
| 6826 | */ |
| 6827 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6828 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6829 | rdev->saved_raid_disk = -1; |
| 6830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | mddev->recovery = 0; |
| 6832 | /* flag recovery needed just to double check */ |
| 6833 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6834 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6835 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | goto unlock; |
| 6837 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6838 | /* Set RUNNING before clearing NEEDED to avoid |
| 6839 | * any transients in the value of "sync_action". |
| 6840 | */ |
| 6841 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6842 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6843 | /* Clear some bits that don't mean anything, but |
| 6844 | * might be left set |
| 6845 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6846 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6847 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6849 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6850 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | /* no recovery is running. |
| 6852 | * remove any failed drives, then |
| 6853 | * add spares if possible. |
| 6854 | * Spare are also removed and re-added, to allow |
| 6855 | * the personality to fail the re-add. |
| 6856 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6858 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6859 | if (mddev->pers->check_reshape == NULL || |
| 6860 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6861 | /* Cannot proceed */ |
| 6862 | goto unlock; |
| 6863 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6864 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6865 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6866 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6867 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6868 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6869 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6870 | } else if (mddev->recovery_cp < MaxSector) { |
| 6871 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6872 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6873 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6874 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6878 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6879 | /* We are adding a device or devices to an array |
| 6880 | * which has the bitmap stored on all devices. |
| 6881 | * So make sure all bitmap pages get written |
| 6882 | */ |
| 6883 | bitmap_write_all(mddev->bitmap); |
| 6884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6886 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6887 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | if (!mddev->sync_thread) { |
| 6889 | printk(KERN_ERR "%s: could not start resync" |
| 6890 | " thread...\n", |
| 6891 | mdname(mddev)); |
| 6892 | /* leave the spares where they are, it shouldn't hurt */ |
| 6893 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6894 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6896 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6897 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | } |
| 6899 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6900 | if (!mddev->sync_thread) { |
| 6901 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6902 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6903 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6904 | if (mddev->sysfs_action) |
| 6905 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | mddev_unlock(mddev); |
| 6908 | } |
| 6909 | } |
| 6910 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6911 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6912 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6913 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6914 | wait_event_timeout(rdev->blocked_wait, |
| 6915 | !test_bit(Blocked, &rdev->flags), |
| 6916 | msecs_to_jiffies(5000)); |
| 6917 | rdev_dec_pending(rdev, mddev); |
| 6918 | } |
| 6919 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6920 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6921 | static int md_notify_reboot(struct notifier_block *this, |
| 6922 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | { |
| 6924 | struct list_head *tmp; |
| 6925 | mddev_t *mddev; |
| 6926 | |
| 6927 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6928 | |
| 6929 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6930 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6931 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6932 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6933 | /* Force a switch to readonly even array |
| 6934 | * appears to still be in use. Hence |
| 6935 | * the '100'. |
| 6936 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6937 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6938 | mddev_unlock(mddev); |
| 6939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | /* |
| 6941 | * certain more exotic SCSI devices are known to be |
| 6942 | * volatile wrt too early system reboots. While the |
| 6943 | * right place to handle this issue is the given |
| 6944 | * driver, we do want to have a safe RAID driver ... |
| 6945 | */ |
| 6946 | mdelay(1000*1); |
| 6947 | } |
| 6948 | return NOTIFY_DONE; |
| 6949 | } |
| 6950 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6951 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | .notifier_call = md_notify_reboot, |
| 6953 | .next = NULL, |
| 6954 | .priority = INT_MAX, /* before any real devices */ |
| 6955 | }; |
| 6956 | |
| 6957 | static void md_geninit(void) |
| 6958 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6960 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6961 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6962 | } |
| 6963 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6964 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6966 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | return -1; |
| 6968 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6969 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | return -1; |
| 6971 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6972 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6973 | md_probe, NULL, NULL); |
| 6974 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | md_probe, NULL, NULL); |
| 6976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6978 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | |
| 6980 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6981 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | |
| 6985 | #ifndef MODULE |
| 6986 | |
| 6987 | /* |
| 6988 | * Searches all registered partitions for autorun RAID arrays |
| 6989 | * at boot time. |
| 6990 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6991 | |
| 6992 | static LIST_HEAD(all_detected_devices); |
| 6993 | struct detected_devices_node { |
| 6994 | struct list_head list; |
| 6995 | dev_t dev; |
| 6996 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | |
| 6998 | void md_autodetect_dev(dev_t dev) |
| 6999 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7000 | struct detected_devices_node *node_detected_dev; |
| 7001 | |
| 7002 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7003 | if (node_detected_dev) { |
| 7004 | node_detected_dev->dev = dev; |
| 7005 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7006 | } else { |
| 7007 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7008 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | |
| 7013 | static void autostart_arrays(int part) |
| 7014 | { |
| 7015 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7016 | struct detected_devices_node *node_detected_dev; |
| 7017 | dev_t dev; |
| 7018 | int i_scanned, i_passed; |
| 7019 | |
| 7020 | i_scanned = 0; |
| 7021 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | |
| 7023 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7024 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7025 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7026 | i_scanned++; |
| 7027 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7028 | struct detected_devices_node, list); |
| 7029 | list_del(&node_detected_dev->list); |
| 7030 | dev = node_detected_dev->dev; |
| 7031 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7032 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | if (IS_ERR(rdev)) |
| 7034 | continue; |
| 7035 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7036 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | MD_BUG(); |
| 7038 | continue; |
| 7039 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7040 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7042 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7044 | |
| 7045 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7046 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7047 | |
| 7048 | autorun_devices(part); |
| 7049 | } |
| 7050 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7051 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | |
| 7053 | static __exit void md_exit(void) |
| 7054 | { |
| 7055 | mddev_t *mddev; |
| 7056 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7057 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7058 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7059 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7061 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | unregister_blkdev(mdp_major, "mdp"); |
| 7063 | unregister_reboot_notifier(&md_notifier); |
| 7064 | unregister_sysctl_table(raid_table_header); |
| 7065 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7066 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7068 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | } |
| 7070 | } |
| 7071 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7072 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | module_exit(md_exit) |
| 7074 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7075 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7076 | { |
| 7077 | return sprintf(buffer, "%d", start_readonly); |
| 7078 | } |
| 7079 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7080 | { |
| 7081 | char *e; |
| 7082 | int num = simple_strtoul(val, &e, 10); |
| 7083 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7084 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7085 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7086 | } |
| 7087 | return -EINVAL; |
| 7088 | } |
| 7089 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7090 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7091 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7092 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7093 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | EXPORT_SYMBOL(register_md_personality); |
| 7096 | EXPORT_SYMBOL(unregister_md_personality); |
| 7097 | EXPORT_SYMBOL(md_error); |
| 7098 | EXPORT_SYMBOL(md_done_sync); |
| 7099 | EXPORT_SYMBOL(md_write_start); |
| 7100 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | EXPORT_SYMBOL(md_register_thread); |
| 7102 | EXPORT_SYMBOL(md_unregister_thread); |
| 7103 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | EXPORT_SYMBOL(md_check_recovery); |
| 7105 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7106 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7107 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |