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 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 71 | * Default number of read corrections we'll attempt on an rdev |
| 72 | * before ejecting it from the array. We divide the read error |
| 73 | * count by 2 for every hour elapsed between read errors. |
| 74 | */ |
| 75 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 76 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 78 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 79 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 80 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 82 | * speed limit - in case reconstruction slows down your system despite |
| 83 | * idle IO detection. |
| 84 | * |
| 85 | * 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] | 86 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | static int sysctl_speed_limit_min = 1000; |
| 90 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | static inline int speed_min(mddev_t *mddev) |
| 92 | { |
| 93 | return mddev->sync_speed_min ? |
| 94 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 95 | } |
| 96 | |
| 97 | static inline int speed_max(mddev_t *mddev) |
| 98 | { |
| 99 | return mddev->sync_speed_max ? |
| 100 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | static struct ctl_table_header *raid_table_header; |
| 104 | |
| 105 | static ctl_table raid_table[] = { |
| 106 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .procname = "speed_limit_min", |
| 108 | .data = &sysctl_speed_limit_min, |
| 109 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 110 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 111 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | }, |
| 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .procname = "speed_limit_max", |
| 115 | .data = &sysctl_speed_limit_max, |
| 116 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 117 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 118 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 120 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | static ctl_table raid_dir_table[] = { |
| 124 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .procname = "raid", |
| 126 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 127 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .child = raid_table, |
| 129 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 130 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static ctl_table raid_root_table[] = { |
| 134 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .procname = "dev", |
| 136 | .maxlen = 0, |
| 137 | .mode = 0555, |
| 138 | .child = raid_dir_table, |
| 139 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 140 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 143 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 145 | static int start_readonly; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 148 | * We have a system wide 'event count' that is incremented |
| 149 | * on any 'interesting' event, and readers of /proc/mdstat |
| 150 | * can use 'poll' or 'select' to find out when the event |
| 151 | * count increases. |
| 152 | * |
| 153 | * Events are: |
| 154 | * start array, stop array, error, add device, remove device, |
| 155 | * start build, activate spare |
| 156 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 157 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 158 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 159 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 160 | { |
| 161 | atomic_inc(&md_event_count); |
| 162 | wake_up(&md_event_waiters); |
| 163 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 165 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 166 | /* Alternate version that can be called from interrupts |
| 167 | * when calling sysfs_notify isn't needed. |
| 168 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 169 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 170 | { |
| 171 | atomic_inc(&md_event_count); |
| 172 | wake_up(&md_event_waiters); |
| 173 | } |
| 174 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * Enables to iterate over all existing md arrays |
| 177 | * all_mddevs_lock protects this list. |
| 178 | */ |
| 179 | static LIST_HEAD(all_mddevs); |
| 180 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | * iterates through all used mddevs in the system. |
| 185 | * We take care to grab the all_mddevs_lock whenever navigating |
| 186 | * the list, and to always hold a refcount when unlocked. |
| 187 | * Any code which breaks out of this loop while own |
| 188 | * a reference to the current mddev and must mddev_put it. |
| 189 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 190 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | \ |
| 192 | for (({ spin_lock(&all_mddevs_lock); \ |
| 193 | tmp = all_mddevs.next; \ |
| 194 | mddev = NULL;}); \ |
| 195 | ({ if (tmp != &all_mddevs) \ |
| 196 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 197 | spin_unlock(&all_mddevs_lock); \ |
| 198 | if (mddev) mddev_put(mddev); \ |
| 199 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 200 | tmp != &all_mddevs;}); \ |
| 201 | ({ spin_lock(&all_mddevs_lock); \ |
| 202 | tmp = tmp->next;}) \ |
| 203 | ) |
| 204 | |
| 205 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 206 | /* Rather than calling directly into the personality make_request function, |
| 207 | * IO requests come here first so that we can check if the device is |
| 208 | * being suspended pending a reconfiguration. |
| 209 | * We hold a refcount over the call to ->make_request. By the time that |
| 210 | * call has finished, the bio has been linked into some internal structure |
| 211 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 212 | */ |
| 213 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 215 | mddev_t *mddev = q->queuedata; |
| 216 | int rv; |
| 217 | if (mddev == NULL || mddev->pers == NULL) { |
| 218 | bio_io_error(bio); |
| 219 | return 0; |
| 220 | } |
| 221 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 222 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 223 | DEFINE_WAIT(__wait); |
| 224 | for (;;) { |
| 225 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 226 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 227 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 228 | break; |
| 229 | rcu_read_unlock(); |
| 230 | schedule(); |
| 231 | rcu_read_lock(); |
| 232 | } |
| 233 | finish_wait(&mddev->sb_wait, &__wait); |
| 234 | } |
| 235 | atomic_inc(&mddev->active_io); |
| 236 | rcu_read_unlock(); |
| 237 | rv = mddev->pers->make_request(q, bio); |
| 238 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 239 | wake_up(&mddev->sb_wait); |
| 240 | |
| 241 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 244 | static void mddev_suspend(mddev_t *mddev) |
| 245 | { |
| 246 | BUG_ON(mddev->suspended); |
| 247 | mddev->suspended = 1; |
| 248 | synchronize_rcu(); |
| 249 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 250 | mddev->pers->quiesce(mddev, 1); |
| 251 | md_unregister_thread(mddev->thread); |
| 252 | mddev->thread = NULL; |
| 253 | /* we now know that no code is executing in the personality module, |
| 254 | * except possibly the tail end of a ->bi_end_io function, but that |
| 255 | * is certain to complete before the module has a chance to get |
| 256 | * unloaded |
| 257 | */ |
| 258 | } |
| 259 | |
| 260 | static void mddev_resume(mddev_t *mddev) |
| 261 | { |
| 262 | mddev->suspended = 0; |
| 263 | wake_up(&mddev->sb_wait); |
| 264 | mddev->pers->quiesce(mddev, 0); |
| 265 | } |
| 266 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 267 | int mddev_congested(mddev_t *mddev, int bits) |
| 268 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 269 | if (mddev->barrier) |
| 270 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 271 | return mddev->suspended; |
| 272 | } |
| 273 | EXPORT_SYMBOL(mddev_congested); |
| 274 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 275 | /* |
| 276 | * Generic barrier handling for md |
| 277 | */ |
| 278 | |
| 279 | #define POST_REQUEST_BARRIER ((void*)1) |
| 280 | |
| 281 | static void md_end_barrier(struct bio *bio, int err) |
| 282 | { |
| 283 | mdk_rdev_t *rdev = bio->bi_private; |
| 284 | mddev_t *mddev = rdev->mddev; |
| 285 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 286 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 287 | |
| 288 | rdev_dec_pending(rdev, mddev); |
| 289 | |
| 290 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 291 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 292 | /* This was a post-request barrier */ |
| 293 | mddev->barrier = NULL; |
| 294 | wake_up(&mddev->sb_wait); |
| 295 | } else |
| 296 | /* The pre-request barrier has finished */ |
| 297 | schedule_work(&mddev->barrier_work); |
| 298 | } |
| 299 | bio_put(bio); |
| 300 | } |
| 301 | |
| 302 | static void submit_barriers(mddev_t *mddev) |
| 303 | { |
| 304 | mdk_rdev_t *rdev; |
| 305 | |
| 306 | rcu_read_lock(); |
| 307 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 308 | if (rdev->raid_disk >= 0 && |
| 309 | !test_bit(Faulty, &rdev->flags)) { |
| 310 | /* Take two references, one is dropped |
| 311 | * when request finishes, one after |
| 312 | * we reclaim rcu_read_lock |
| 313 | */ |
| 314 | struct bio *bi; |
| 315 | atomic_inc(&rdev->nr_pending); |
| 316 | atomic_inc(&rdev->nr_pending); |
| 317 | rcu_read_unlock(); |
| 318 | bi = bio_alloc(GFP_KERNEL, 0); |
| 319 | bi->bi_end_io = md_end_barrier; |
| 320 | bi->bi_private = rdev; |
| 321 | bi->bi_bdev = rdev->bdev; |
| 322 | atomic_inc(&mddev->flush_pending); |
| 323 | submit_bio(WRITE_BARRIER, bi); |
| 324 | rcu_read_lock(); |
| 325 | rdev_dec_pending(rdev, mddev); |
| 326 | } |
| 327 | rcu_read_unlock(); |
| 328 | } |
| 329 | |
| 330 | static void md_submit_barrier(struct work_struct *ws) |
| 331 | { |
| 332 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 333 | struct bio *bio = mddev->barrier; |
| 334 | |
| 335 | atomic_set(&mddev->flush_pending, 1); |
| 336 | |
| 337 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 338 | bio_endio(bio, -EOPNOTSUPP); |
| 339 | else if (bio->bi_size == 0) |
| 340 | /* an empty barrier - all done */ |
| 341 | bio_endio(bio, 0); |
| 342 | else { |
| 343 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
| 344 | if (mddev->pers->make_request(mddev->queue, bio)) |
| 345 | generic_make_request(bio); |
| 346 | mddev->barrier = POST_REQUEST_BARRIER; |
| 347 | submit_barriers(mddev); |
| 348 | } |
| 349 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 350 | mddev->barrier = NULL; |
| 351 | wake_up(&mddev->sb_wait); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 356 | { |
| 357 | spin_lock_irq(&mddev->write_lock); |
| 358 | wait_event_lock_irq(mddev->sb_wait, |
| 359 | !mddev->barrier, |
| 360 | mddev->write_lock, /*nothing*/); |
| 361 | mddev->barrier = bio; |
| 362 | spin_unlock_irq(&mddev->write_lock); |
| 363 | |
| 364 | atomic_set(&mddev->flush_pending, 1); |
| 365 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 366 | |
| 367 | submit_barriers(mddev); |
| 368 | |
| 369 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 370 | schedule_work(&mddev->barrier_work); |
| 371 | } |
| 372 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 375 | { |
| 376 | atomic_inc(&mddev->active); |
| 377 | return mddev; |
| 378 | } |
| 379 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 380 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | static void mddev_put(mddev_t *mddev) |
| 383 | { |
| 384 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 385 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 386 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 387 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 389 | if (mddev->gendisk) { |
| 390 | /* we did a probe so need to clean up. |
| 391 | * Call schedule_work inside the spinlock |
| 392 | * so that flush_scheduled_work() after |
| 393 | * mddev_find will succeed in waiting for the |
| 394 | * work to be done. |
| 395 | */ |
| 396 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 397 | schedule_work(&mddev->del_work); |
| 398 | } else |
| 399 | kfree(mddev); |
| 400 | } |
| 401 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static mddev_t * mddev_find(dev_t unit) |
| 405 | { |
| 406 | mddev_t *mddev, *new = NULL; |
| 407 | |
| 408 | retry: |
| 409 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 411 | if (unit) { |
| 412 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 413 | if (mddev->unit == unit) { |
| 414 | mddev_get(mddev); |
| 415 | spin_unlock(&all_mddevs_lock); |
| 416 | kfree(new); |
| 417 | return mddev; |
| 418 | } |
| 419 | |
| 420 | if (new) { |
| 421 | list_add(&new->all_mddevs, &all_mddevs); |
| 422 | spin_unlock(&all_mddevs_lock); |
| 423 | new->hold_active = UNTIL_IOCTL; |
| 424 | return new; |
| 425 | } |
| 426 | } else if (new) { |
| 427 | /* find an unused unit number */ |
| 428 | static int next_minor = 512; |
| 429 | int start = next_minor; |
| 430 | int is_free = 0; |
| 431 | int dev = 0; |
| 432 | while (!is_free) { |
| 433 | dev = MKDEV(MD_MAJOR, next_minor); |
| 434 | next_minor++; |
| 435 | if (next_minor > MINORMASK) |
| 436 | next_minor = 0; |
| 437 | if (next_minor == start) { |
| 438 | /* Oh dear, all in use. */ |
| 439 | spin_unlock(&all_mddevs_lock); |
| 440 | kfree(new); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | is_free = 1; |
| 445 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 446 | if (mddev->unit == dev) { |
| 447 | is_free = 0; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | new->unit = dev; |
| 452 | new->md_minor = MINOR(dev); |
| 453 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | list_add(&new->all_mddevs, &all_mddevs); |
| 455 | spin_unlock(&all_mddevs_lock); |
| 456 | return new; |
| 457 | } |
| 458 | spin_unlock(&all_mddevs_lock); |
| 459 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 460 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (!new) |
| 462 | return NULL; |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | new->unit = unit; |
| 465 | if (MAJOR(unit) == MD_MAJOR) |
| 466 | new->md_minor = MINOR(unit); |
| 467 | else |
| 468 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 469 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 470 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 471 | mutex_init(&new->reconfig_mutex); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 472 | mutex_init(&new->bitmap_info.mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | INIT_LIST_HEAD(&new->disks); |
| 474 | INIT_LIST_HEAD(&new->all_mddevs); |
| 475 | init_timer(&new->safemode_timer); |
| 476 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 477 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 478 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 479 | spin_lock_init(&new->write_lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 480 | atomic_set(&new->flush_pending, 0); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 481 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 482 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 483 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 484 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 485 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 486 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | goto retry; |
| 489 | } |
| 490 | |
| 491 | static inline int mddev_lock(mddev_t * mddev) |
| 492 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 493 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 496 | static inline int mddev_is_locked(mddev_t *mddev) |
| 497 | { |
| 498 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 499 | } |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | static inline int mddev_trylock(mddev_t * mddev) |
| 502 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 503 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static inline void mddev_unlock(mddev_t * mddev) |
| 507 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 508 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 510 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 513 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 515 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 517 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (rdev->desc_nr == nr) |
| 519 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 525 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | mdk_rdev_t *rdev; |
| 527 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 528 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (rdev->bdev->bd_dev == dev) |
| 530 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return NULL; |
| 533 | } |
| 534 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 535 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 536 | { |
| 537 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 538 | list_for_each_entry(pers, &pers_list, list) { |
| 539 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 540 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 541 | if (strcmp(pers->name, clevel)==0) |
| 542 | return pers; |
| 543 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 544 | return NULL; |
| 545 | } |
| 546 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 547 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 548 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 550 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 551 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 555 | { |
| 556 | if (rdev->sb_page) |
| 557 | MD_BUG(); |
| 558 | |
| 559 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 560 | if (!rdev->sb_page) { |
| 561 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 562 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 569 | { |
| 570 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 571 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | rdev->sb_loaded = 0; |
| 573 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 574 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 575 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 580 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 581 | { |
| 582 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 583 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 584 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 585 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 586 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 587 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 588 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 589 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 590 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 591 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 592 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 593 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 594 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 595 | } |
| 596 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 597 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 598 | { |
| 599 | struct bio *bio2 = bio->bi_private; |
| 600 | mdk_rdev_t *rdev = bio2->bi_private; |
| 601 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 602 | |
| 603 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 604 | error == -EOPNOTSUPP) { |
| 605 | unsigned long flags; |
| 606 | /* barriers don't appear to be supported :-( */ |
| 607 | set_bit(BarriersNotsupp, &rdev->flags); |
| 608 | mddev->barriers_work = 0; |
| 609 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 610 | bio2->bi_next = mddev->biolist; |
| 611 | mddev->biolist = bio2; |
| 612 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 613 | wake_up(&mddev->sb_wait); |
| 614 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 615 | } else { |
| 616 | bio_put(bio2); |
| 617 | bio->bi_private = rdev; |
| 618 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 619 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 620 | } |
| 621 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 622 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 623 | sector_t sector, int size, struct page *page) |
| 624 | { |
| 625 | /* write first size bytes of page to sector of rdev |
| 626 | * Increment mddev->pending_writes before returning |
| 627 | * and decrement it on completion, waking up sb_wait |
| 628 | * if zero is reached. |
| 629 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 630 | * |
| 631 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 632 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 633 | */ |
| 634 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 635 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 636 | |
| 637 | bio->bi_bdev = rdev->bdev; |
| 638 | bio->bi_sector = sector; |
| 639 | bio_add_page(bio, page, size, 0); |
| 640 | bio->bi_private = rdev; |
| 641 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 642 | bio->bi_rw = rw; |
| 643 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 644 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 645 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 646 | struct bio *rbio; |
| 647 | rw |= (1<<BIO_RW_BARRIER); |
| 648 | rbio = bio_clone(bio, GFP_NOIO); |
| 649 | rbio->bi_private = bio; |
| 650 | rbio->bi_end_io = super_written_barrier; |
| 651 | submit_bio(rw, rbio); |
| 652 | } else |
| 653 | submit_bio(rw, bio); |
| 654 | } |
| 655 | |
| 656 | void md_super_wait(mddev_t *mddev) |
| 657 | { |
| 658 | /* wait for all superblock writes that were scheduled to complete. |
| 659 | * if any had to be retried (due to BARRIER problems), retry them |
| 660 | */ |
| 661 | DEFINE_WAIT(wq); |
| 662 | for(;;) { |
| 663 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 664 | if (atomic_read(&mddev->pending_writes)==0) |
| 665 | break; |
| 666 | while (mddev->biolist) { |
| 667 | struct bio *bio; |
| 668 | spin_lock_irq(&mddev->write_lock); |
| 669 | bio = mddev->biolist; |
| 670 | mddev->biolist = bio->bi_next ; |
| 671 | bio->bi_next = NULL; |
| 672 | spin_unlock_irq(&mddev->write_lock); |
| 673 | submit_bio(bio->bi_rw, bio); |
| 674 | } |
| 675 | schedule(); |
| 676 | } |
| 677 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 678 | } |
| 679 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 680 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 685 | 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] | 686 | struct page *page, int rw) |
| 687 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 688 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | struct completion event; |
| 690 | int ret; |
| 691 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 692 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | bio->bi_bdev = bdev; |
| 695 | bio->bi_sector = sector; |
| 696 | bio_add_page(bio, page, size, 0); |
| 697 | init_completion(&event); |
| 698 | bio->bi_private = &event; |
| 699 | bio->bi_end_io = bi_complete; |
| 700 | submit_bio(rw, bio); |
| 701 | wait_for_completion(&event); |
| 702 | |
| 703 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 704 | bio_put(bio); |
| 705 | return ret; |
| 706 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 707 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 709 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | char b[BDEVNAME_SIZE]; |
| 712 | if (!rdev->sb_page) { |
| 713 | MD_BUG(); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | if (rdev->sb_loaded) |
| 717 | return 0; |
| 718 | |
| 719 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 720 | 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] | 721 | goto fail; |
| 722 | rdev->sb_loaded = 1; |
| 723 | return 0; |
| 724 | |
| 725 | fail: |
| 726 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 727 | bdevname(rdev->bdev,b)); |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | |
| 731 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 732 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 733 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 734 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 735 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 736 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 740 | { |
| 741 | int ret; |
| 742 | mdp_super_t *tmp1, *tmp2; |
| 743 | |
| 744 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 745 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 746 | |
| 747 | if (!tmp1 || !tmp2) { |
| 748 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 749 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | goto abort; |
| 751 | } |
| 752 | |
| 753 | *tmp1 = *sb1; |
| 754 | *tmp2 = *sb2; |
| 755 | |
| 756 | /* |
| 757 | * nr_disks is not constant |
| 758 | */ |
| 759 | tmp1->nr_disks = 0; |
| 760 | tmp2->nr_disks = 0; |
| 761 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 762 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 764 | kfree(tmp1); |
| 765 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return ret; |
| 767 | } |
| 768 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 769 | |
| 770 | static u32 md_csum_fold(u32 csum) |
| 771 | { |
| 772 | csum = (csum & 0xffff) + (csum >> 16); |
| 773 | return (csum & 0xffff) + (csum >> 16); |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 777 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 778 | u64 newcsum = 0; |
| 779 | u32 *sb32 = (u32*)sb; |
| 780 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | unsigned int disk_csum, csum; |
| 782 | |
| 783 | disk_csum = sb->sb_csum; |
| 784 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 785 | |
| 786 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 787 | newcsum += sb32[i]; |
| 788 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 789 | |
| 790 | |
| 791 | #ifdef CONFIG_ALPHA |
| 792 | /* This used to use csum_partial, which was wrong for several |
| 793 | * reasons including that different results are returned on |
| 794 | * different architectures. It isn't critical that we get exactly |
| 795 | * the same return value as before (we always csum_fold before |
| 796 | * testing, and that removes any differences). However as we |
| 797 | * know that csum_partial always returned a 16bit value on |
| 798 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 799 | */ |
| 800 | sb->sb_csum = md_csum_fold(disk_csum); |
| 801 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 803 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return csum; |
| 805 | } |
| 806 | |
| 807 | |
| 808 | /* |
| 809 | * Handle superblock details. |
| 810 | * We want to be able to handle multiple superblock formats |
| 811 | * so we have a common interface to them all, and an array of |
| 812 | * different handlers. |
| 813 | * We rely on user-space to write the initial superblock, and support |
| 814 | * reading and updating of superblocks. |
| 815 | * Interface methods are: |
| 816 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 817 | * loads and validates a superblock on dev. |
| 818 | * if refdev != NULL, compare superblocks on both devices |
| 819 | * Return: |
| 820 | * 0 - dev has a superblock that is compatible with refdev |
| 821 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 822 | * so dev should be used as the refdev in future |
| 823 | * -EINVAL superblock incompatible or invalid |
| 824 | * -othererror e.g. -EIO |
| 825 | * |
| 826 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 827 | * Verify that dev is acceptable into mddev. |
| 828 | * The first time, mddev->raid_disks will be 0, and data from |
| 829 | * dev should be merged in. Subsequent calls check that dev |
| 830 | * is new enough. Return 0 or -EINVAL |
| 831 | * |
| 832 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 833 | * Update the superblock for rdev with data in mddev |
| 834 | * This does not write to disc. |
| 835 | * |
| 836 | */ |
| 837 | |
| 838 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 839 | char *name; |
| 840 | struct module *owner; |
| 841 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 842 | int minor_version); |
| 843 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 844 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 845 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 846 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 850 | * Check that the given mddev has no bitmap. |
| 851 | * |
| 852 | * This function is called from the run method of all personalities that do not |
| 853 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 854 | * has a bitmap. Otherwise, it returns 0. |
| 855 | * |
| 856 | */ |
| 857 | int md_check_no_bitmap(mddev_t *mddev) |
| 858 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 859 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 860 | return 0; |
| 861 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 862 | mdname(mddev), mddev->pers->name); |
| 863 | return 1; |
| 864 | } |
| 865 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 866 | |
| 867 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | * load_super for 0.90.0 |
| 869 | */ |
| 870 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 871 | { |
| 872 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 873 | mdp_super_t *sb; |
| 874 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 877 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | * it's at the end of the disk. |
| 879 | * |
| 880 | * It also happens to be a multiple of 4Kb. |
| 881 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 882 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 884 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (ret) return ret; |
| 886 | |
| 887 | ret = -EINVAL; |
| 888 | |
| 889 | bdevname(rdev->bdev, b); |
| 890 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 891 | |
| 892 | if (sb->md_magic != MD_SB_MAGIC) { |
| 893 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 894 | b); |
| 895 | goto abort; |
| 896 | } |
| 897 | |
| 898 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 899 | sb->minor_version < 90 || |
| 900 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 902 | sb->major_version, sb->minor_version, |
| 903 | b); |
| 904 | goto abort; |
| 905 | } |
| 906 | |
| 907 | if (sb->raid_disks <= 0) |
| 908 | goto abort; |
| 909 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 910 | 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] | 911 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 912 | b); |
| 913 | goto abort; |
| 914 | } |
| 915 | |
| 916 | rdev->preferred_minor = sb->md_minor; |
| 917 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 918 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
| 920 | if (sb->level == LEVEL_MULTIPATH) |
| 921 | rdev->desc_nr = -1; |
| 922 | else |
| 923 | rdev->desc_nr = sb->this_disk.number; |
| 924 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 925 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 927 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | __u64 ev1, ev2; |
| 929 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 930 | if (!uuid_equal(refsb, sb)) { |
| 931 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 932 | b, bdevname(refdev->bdev,b2)); |
| 933 | goto abort; |
| 934 | } |
| 935 | if (!sb_equal(refsb, sb)) { |
| 936 | printk(KERN_WARNING "md: %s has same UUID" |
| 937 | " but different superblock to %s\n", |
| 938 | b, bdevname(refdev->bdev, b2)); |
| 939 | goto abort; |
| 940 | } |
| 941 | ev1 = md_event(sb); |
| 942 | ev2 = md_event(refsb); |
| 943 | if (ev1 > ev2) |
| 944 | ret = 1; |
| 945 | else |
| 946 | ret = 0; |
| 947 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 948 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 950 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 951 | /* "this cannot possibly happen" ... */ |
| 952 | ret = -EINVAL; |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | abort: |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * validate_super for 0.90.0 |
| 960 | */ |
| 961 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 962 | { |
| 963 | mdp_disk_t *desc; |
| 964 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 965 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 967 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 968 | clear_bit(Faulty, &rdev->flags); |
| 969 | clear_bit(In_sync, &rdev->flags); |
| 970 | clear_bit(WriteMostly, &rdev->flags); |
| 971 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | if (mddev->raid_disks == 0) { |
| 974 | mddev->major_version = 0; |
| 975 | mddev->minor_version = sb->minor_version; |
| 976 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 977 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 978 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | mddev->ctime = sb->ctime; |
| 980 | mddev->utime = sb->utime; |
| 981 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 982 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | mddev->layout = sb->layout; |
| 984 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 985 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 986 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 987 | mddev->bitmap_info.offset = 0; |
| 988 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 990 | if (mddev->minor_version >= 91) { |
| 991 | mddev->reshape_position = sb->reshape_position; |
| 992 | mddev->delta_disks = sb->delta_disks; |
| 993 | mddev->new_level = sb->new_level; |
| 994 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 995 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 996 | } else { |
| 997 | mddev->reshape_position = MaxSector; |
| 998 | mddev->delta_disks = 0; |
| 999 | mddev->new_level = mddev->level; |
| 1000 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1001 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1005 | mddev->recovery_cp = MaxSector; |
| 1006 | else { |
| 1007 | if (sb->events_hi == sb->cp_events_hi && |
| 1008 | sb->events_lo == sb->cp_events_lo) { |
| 1009 | mddev->recovery_cp = sb->recovery_cp; |
| 1010 | } else |
| 1011 | mddev->recovery_cp = 0; |
| 1012 | } |
| 1013 | |
| 1014 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1015 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1016 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1017 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1018 | |
| 1019 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1022 | mddev->bitmap_info.file == NULL) |
| 1023 | mddev->bitmap_info.offset = |
| 1024 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1025 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1026 | } else if (mddev->pers == NULL) { |
| 1027 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | ++ev1; |
| 1029 | if (ev1 < mddev->events) |
| 1030 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1031 | } else if (mddev->bitmap) { |
| 1032 | /* if adding to array with a bitmap, then we can accept an |
| 1033 | * older device ... but not too old. |
| 1034 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1035 | if (ev1 < mddev->bitmap->events_cleared) |
| 1036 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1037 | } else { |
| 1038 | if (ev1 < mddev->events) |
| 1039 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1040 | return 0; |
| 1041 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | desc = sb->disks + rdev->desc_nr; |
| 1045 | |
| 1046 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1047 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1048 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1049 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1050 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1052 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1053 | /* active but not in sync implies recovery up to |
| 1054 | * reshape position. We don't know exactly where |
| 1055 | * that is, so set to zero for now */ |
| 1056 | if (mddev->minor_version >= 91) { |
| 1057 | rdev->recovery_offset = 0; |
| 1058 | rdev->raid_disk = desc->raid_disk; |
| 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1061 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1062 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1063 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1064 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * sync_super for 0.90.0 |
| 1070 | */ |
| 1071 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1072 | { |
| 1073 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | mdk_rdev_t *rdev2; |
| 1075 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
| 1078 | /* make rdev->sb match mddev data.. |
| 1079 | * |
| 1080 | * 1/ zero out disks |
| 1081 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1082 | * 3/ any empty disks < next_spare become removed |
| 1083 | * |
| 1084 | * disks[0] gets initialised to REMOVED because |
| 1085 | * we cannot be sure from other fields if it has |
| 1086 | * been initialised or not. |
| 1087 | */ |
| 1088 | int i; |
| 1089 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1090 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1091 | rdev->sb_size = MD_SB_BYTES; |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1094 | |
| 1095 | memset(sb, 0, sizeof(*sb)); |
| 1096 | |
| 1097 | sb->md_magic = MD_SB_MAGIC; |
| 1098 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | sb->patch_version = mddev->patch_version; |
| 1100 | sb->gvalid_words = 0; /* ignored */ |
| 1101 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1102 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1103 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1104 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1105 | |
| 1106 | sb->ctime = mddev->ctime; |
| 1107 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1108 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | sb->raid_disks = mddev->raid_disks; |
| 1110 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1111 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | sb->utime = mddev->utime; |
| 1113 | sb->state = 0; |
| 1114 | sb->events_hi = (mddev->events>>32); |
| 1115 | sb->events_lo = (u32)mddev->events; |
| 1116 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1117 | if (mddev->reshape_position == MaxSector) |
| 1118 | sb->minor_version = 90; |
| 1119 | else { |
| 1120 | sb->minor_version = 91; |
| 1121 | sb->reshape_position = mddev->reshape_position; |
| 1122 | sb->new_level = mddev->new_level; |
| 1123 | sb->delta_disks = mddev->delta_disks; |
| 1124 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1125 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1126 | } |
| 1127 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | if (mddev->in_sync) |
| 1129 | { |
| 1130 | sb->recovery_cp = mddev->recovery_cp; |
| 1131 | sb->cp_events_hi = (mddev->events>>32); |
| 1132 | sb->cp_events_lo = (u32)mddev->events; |
| 1133 | if (mddev->recovery_cp == MaxSector) |
| 1134 | sb->state = (1<< MD_SB_CLEAN); |
| 1135 | } else |
| 1136 | sb->recovery_cp = 0; |
| 1137 | |
| 1138 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1139 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1141 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1142 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1145 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1147 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1148 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1149 | |
| 1150 | if (rdev2->raid_disk >= 0 && |
| 1151 | sb->minor_version >= 91) |
| 1152 | /* we have nowhere to store the recovery_offset, |
| 1153 | * but if it is not below the reshape_position, |
| 1154 | * we can piggy-back on that. |
| 1155 | */ |
| 1156 | is_active = 1; |
| 1157 | if (rdev2->raid_disk < 0 || |
| 1158 | test_bit(Faulty, &rdev2->flags)) |
| 1159 | is_active = 0; |
| 1160 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1161 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1163 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1164 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | d = &sb->disks[rdev2->desc_nr]; |
| 1166 | nr_disks++; |
| 1167 | d->number = rdev2->desc_nr; |
| 1168 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1169 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1170 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | d->raid_disk = rdev2->raid_disk; |
| 1172 | else |
| 1173 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1174 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1176 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1178 | if (test_bit(In_sync, &rdev2->flags)) |
| 1179 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | active++; |
| 1181 | working++; |
| 1182 | } else { |
| 1183 | d->state = 0; |
| 1184 | spare++; |
| 1185 | working++; |
| 1186 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1187 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1188 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1191 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1192 | mdp_disk_t *d = &sb->disks[i]; |
| 1193 | if (d->state == 0 && d->number == 0) { |
| 1194 | d->number = i; |
| 1195 | d->raid_disk = i; |
| 1196 | d->state = (1<<MD_DISK_REMOVED); |
| 1197 | d->state |= (1<<MD_DISK_FAULTY); |
| 1198 | failed++; |
| 1199 | } |
| 1200 | } |
| 1201 | sb->nr_disks = nr_disks; |
| 1202 | sb->active_disks = active; |
| 1203 | sb->working_disks = working; |
| 1204 | sb->failed_disks = failed; |
| 1205 | sb->spare_disks = spare; |
| 1206 | |
| 1207 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1208 | sb->sb_csum = calc_sb_csum(sb); |
| 1209 | } |
| 1210 | |
| 1211 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1212 | * rdev_size_change for 0.90.0 |
| 1213 | */ |
| 1214 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1215 | 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] | 1216 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1217 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1218 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1219 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1220 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1221 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1222 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1223 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1224 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1225 | rdev->sb_page); |
| 1226 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1227 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | * version 1 superblock |
| 1233 | */ |
| 1234 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1235 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1237 | __le32 disk_csum; |
| 1238 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | unsigned long long newcsum; |
| 1240 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1241 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | int i; |
| 1243 | |
| 1244 | disk_csum = sb->sb_csum; |
| 1245 | sb->sb_csum = 0; |
| 1246 | newcsum = 0; |
| 1247 | for (i=0; size>=4; size -= 4 ) |
| 1248 | newcsum += le32_to_cpu(*isuper++); |
| 1249 | |
| 1250 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1251 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
| 1253 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1254 | sb->sb_csum = disk_csum; |
| 1255 | return cpu_to_le32(csum); |
| 1256 | } |
| 1257 | |
| 1258 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1259 | { |
| 1260 | struct mdp_superblock_1 *sb; |
| 1261 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1262 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1264 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
| 1266 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1267 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | * It is always aligned to a 4K boundary and |
| 1269 | * depeding on minor_version, it can be: |
| 1270 | * 0: At least 8K, but less than 12K, from end of device |
| 1271 | * 1: At start of device |
| 1272 | * 2: 4K from start of device. |
| 1273 | */ |
| 1274 | switch(minor_version) { |
| 1275 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1276 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1277 | sb_start -= 8*2; |
| 1278 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | break; |
| 1280 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1281 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | break; |
| 1283 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1284 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | break; |
| 1286 | default: |
| 1287 | return -EINVAL; |
| 1288 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1289 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1291 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1292 | * and it is safe to read 4k, so we do that |
| 1293 | */ |
| 1294 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | if (ret) return ret; |
| 1296 | |
| 1297 | |
| 1298 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1299 | |
| 1300 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1301 | sb->major_version != cpu_to_le32(1) || |
| 1302 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1303 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1304 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | return -EINVAL; |
| 1306 | |
| 1307 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1308 | printk("md: invalid superblock checksum on %s\n", |
| 1309 | bdevname(rdev->bdev,b)); |
| 1310 | return -EINVAL; |
| 1311 | } |
| 1312 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1313 | printk("md: data_size too small on %s\n", |
| 1314 | bdevname(rdev->bdev,b)); |
| 1315 | return -EINVAL; |
| 1316 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | rdev->preferred_minor = 0xffff; |
| 1319 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1320 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1322 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1323 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1324 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1325 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1326 | |
| 1327 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1328 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1329 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1330 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1331 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1332 | rdev->desc_nr = -1; |
| 1333 | else |
| 1334 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1335 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1336 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1337 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1338 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | __u64 ev1, ev2; |
| 1340 | struct mdp_superblock_1 *refsb = |
| 1341 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1342 | |
| 1343 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1344 | sb->level != refsb->level || |
| 1345 | sb->layout != refsb->layout || |
| 1346 | sb->chunksize != refsb->chunksize) { |
| 1347 | printk(KERN_WARNING "md: %s has strangely different" |
| 1348 | " superblock to %s\n", |
| 1349 | bdevname(rdev->bdev,b), |
| 1350 | bdevname(refdev->bdev,b2)); |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | ev1 = le64_to_cpu(sb->events); |
| 1354 | ev2 = le64_to_cpu(refsb->events); |
| 1355 | |
| 1356 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1357 | ret = 1; |
| 1358 | else |
| 1359 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1361 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1362 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1363 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1365 | rdev->sectors = rdev->sb_start; |
| 1366 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1368 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1369 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1370 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1371 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1375 | { |
| 1376 | 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] | 1377 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1379 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1380 | clear_bit(Faulty, &rdev->flags); |
| 1381 | clear_bit(In_sync, &rdev->flags); |
| 1382 | clear_bit(WriteMostly, &rdev->flags); |
| 1383 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | if (mddev->raid_disks == 0) { |
| 1386 | mddev->major_version = 1; |
| 1387 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1388 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1389 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1391 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1392 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1393 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | mddev->layout = le32_to_cpu(sb->layout); |
| 1395 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1396 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1397 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1398 | mddev->bitmap_info.offset = 0; |
| 1399 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
| 1401 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1402 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1403 | |
| 1404 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1405 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1406 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1407 | mddev->bitmap_info.file == NULL ) |
| 1408 | mddev->bitmap_info.offset = |
| 1409 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1410 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1411 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1412 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1413 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1414 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1415 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1416 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1417 | } else { |
| 1418 | mddev->reshape_position = MaxSector; |
| 1419 | mddev->delta_disks = 0; |
| 1420 | mddev->new_level = mddev->level; |
| 1421 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1422 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1423 | } |
| 1424 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1425 | } else if (mddev->pers == NULL) { |
| 1426 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | ++ev1; |
| 1428 | if (ev1 < mddev->events) |
| 1429 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1430 | } else if (mddev->bitmap) { |
| 1431 | /* If adding to array with a bitmap, then we can accept an |
| 1432 | * older device, but not too old. |
| 1433 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1434 | if (ev1 < mddev->bitmap->events_cleared) |
| 1435 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1436 | } else { |
| 1437 | if (ev1 < mddev->events) |
| 1438 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1439 | return 0; |
| 1440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1442 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1443 | if (rdev->desc_nr < 0 || |
| 1444 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1445 | role = 0xffff; |
| 1446 | rdev->desc_nr = -1; |
| 1447 | } else |
| 1448 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | switch(role) { |
| 1450 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | break; |
| 1452 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1453 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | break; |
| 1455 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1456 | if ((le32_to_cpu(sb->feature_map) & |
| 1457 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1458 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1459 | else |
| 1460 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | rdev->raid_disk = role; |
| 1462 | break; |
| 1463 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1464 | if (sb->devflags & WriteMostly1) |
| 1465 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1466 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1467 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1473 | { |
| 1474 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | mdk_rdev_t *rdev2; |
| 1476 | int max_dev, i; |
| 1477 | /* make rdev->sb match mddev and rdev data. */ |
| 1478 | |
| 1479 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1480 | |
| 1481 | sb->feature_map = 0; |
| 1482 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1483 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1485 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1486 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1487 | |
| 1488 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1489 | sb->events = cpu_to_le64(mddev->events); |
| 1490 | if (mddev->in_sync) |
| 1491 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1492 | else |
| 1493 | sb->resync_offset = cpu_to_le64(0); |
| 1494 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1495 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1496 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1497 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1498 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1499 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1500 | sb->level = cpu_to_le32(mddev->level); |
| 1501 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1502 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1503 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1504 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1505 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1506 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1509 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1510 | if (rdev->recovery_offset > 0) { |
| 1511 | sb->feature_map |= |
| 1512 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1513 | sb->recovery_offset = |
| 1514 | cpu_to_le64(rdev->recovery_offset); |
| 1515 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1518 | if (mddev->reshape_position != MaxSector) { |
| 1519 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1520 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1521 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1522 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1523 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1524 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1525 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1528 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | if (rdev2->desc_nr+1 > max_dev) |
| 1530 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1531 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1532 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1533 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1534 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1535 | rdev->sb_size = max_dev * 2 + 256; |
| 1536 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1537 | if (rdev->sb_size & bmask) |
| 1538 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | for (i=0; i<max_dev;i++) |
| 1541 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1542 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1543 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1545 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1547 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1549 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1550 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | else |
| 1552 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1553 | } |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1556 | } |
| 1557 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1558 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1559 | 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] | 1560 | { |
| 1561 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1562 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1563 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1564 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1565 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1566 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1567 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1568 | max_sectors -= rdev->data_offset; |
| 1569 | if (!num_sectors || num_sectors > max_sectors) |
| 1570 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1571 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1572 | /* minor version 0 with bitmap we can't move */ |
| 1573 | return 0; |
| 1574 | } else { |
| 1575 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1576 | sector_t sb_start; |
| 1577 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1578 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1579 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1580 | if (!num_sectors || num_sectors > max_sectors) |
| 1581 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1582 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1583 | } |
| 1584 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1585 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1586 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1587 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1588 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1589 | rdev->sb_page); |
| 1590 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1591 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1594 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | [0] = { |
| 1596 | .name = "0.90.0", |
| 1597 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1598 | .load_super = super_90_load, |
| 1599 | .validate_super = super_90_validate, |
| 1600 | .sync_super = super_90_sync, |
| 1601 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | }, |
| 1603 | [1] = { |
| 1604 | .name = "md-1", |
| 1605 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1606 | .load_super = super_1_load, |
| 1607 | .validate_super = super_1_validate, |
| 1608 | .sync_super = super_1_sync, |
| 1609 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | }, |
| 1611 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
| 1613 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1614 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1615 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1617 | rcu_read_lock(); |
| 1618 | rdev_for_each_rcu(rdev, mddev1) |
| 1619 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1620 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1621 | rdev2->bdev->bd_contains) { |
| 1622 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1623 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1624 | } |
| 1625 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return 0; |
| 1627 | } |
| 1628 | |
| 1629 | static LIST_HEAD(pending_raid_disks); |
| 1630 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1631 | /* |
| 1632 | * Try to register data integrity profile for an mddev |
| 1633 | * |
| 1634 | * This is called when an array is started and after a disk has been kicked |
| 1635 | * from the array. It only succeeds if all working and active component devices |
| 1636 | * are integrity capable with matching profiles. |
| 1637 | */ |
| 1638 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1639 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1640 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1641 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1642 | if (list_empty(&mddev->disks)) |
| 1643 | return 0; /* nothing to do */ |
| 1644 | if (blk_get_integrity(mddev->gendisk)) |
| 1645 | return 0; /* already registered */ |
| 1646 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1647 | /* skip spares and non-functional disks */ |
| 1648 | if (test_bit(Faulty, &rdev->flags)) |
| 1649 | continue; |
| 1650 | if (rdev->raid_disk < 0) |
| 1651 | continue; |
| 1652 | /* |
| 1653 | * If at least one rdev is not integrity capable, we can not |
| 1654 | * enable data integrity for the md device. |
| 1655 | */ |
| 1656 | if (!bdev_get_integrity(rdev->bdev)) |
| 1657 | return -EINVAL; |
| 1658 | if (!reference) { |
| 1659 | /* Use the first rdev as the reference */ |
| 1660 | reference = rdev; |
| 1661 | continue; |
| 1662 | } |
| 1663 | /* does this rdev's profile match the reference profile? */ |
| 1664 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1665 | rdev->bdev->bd_disk) < 0) |
| 1666 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1667 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1668 | /* |
| 1669 | * All component devices are integrity capable and have matching |
| 1670 | * profiles, register the common profile for the md device. |
| 1671 | */ |
| 1672 | if (blk_integrity_register(mddev->gendisk, |
| 1673 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1674 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1675 | mdname(mddev)); |
| 1676 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1677 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1678 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1679 | mdname(mddev)); |
| 1680 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1681 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1682 | EXPORT_SYMBOL(md_integrity_register); |
| 1683 | |
| 1684 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1685 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1686 | { |
| 1687 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1688 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1689 | |
| 1690 | if (!bi_mddev) /* nothing to do */ |
| 1691 | return; |
| 1692 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1693 | return; |
| 1694 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1695 | rdev->bdev->bd_disk) >= 0) |
| 1696 | return; |
| 1697 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1698 | blk_integrity_unregister(mddev->gendisk); |
| 1699 | } |
| 1700 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1703 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1704 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1705 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1706 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1707 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
| 1709 | if (rdev->mddev) { |
| 1710 | MD_BUG(); |
| 1711 | return -EINVAL; |
| 1712 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1713 | |
| 1714 | /* prevent duplicates */ |
| 1715 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1716 | return -EEXIST; |
| 1717 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1718 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1719 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1720 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1721 | if (mddev->pers) { |
| 1722 | /* Cannot change size, so fail |
| 1723 | * If mddev->level <= 0, then we don't care |
| 1724 | * about aligning sizes (e.g. linear) |
| 1725 | */ |
| 1726 | if (mddev->level > 0) |
| 1727 | return -ENOSPC; |
| 1728 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1729 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
| 1732 | /* Verify rdev->desc_nr is unique. |
| 1733 | * If it is -1, assign a free number, else |
| 1734 | * check number is not in use |
| 1735 | */ |
| 1736 | if (rdev->desc_nr < 0) { |
| 1737 | int choice = 0; |
| 1738 | if (mddev->pers) choice = mddev->raid_disks; |
| 1739 | while (find_rdev_nr(mddev, choice)) |
| 1740 | choice++; |
| 1741 | rdev->desc_nr = choice; |
| 1742 | } else { |
| 1743 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1744 | return -EBUSY; |
| 1745 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1746 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1747 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1748 | mdname(mddev), mddev->max_disks); |
| 1749 | return -EBUSY; |
| 1750 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1751 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1752 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1753 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1756 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1757 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1758 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1759 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1760 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1761 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1762 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1763 | kobject_del(&rdev->kobj); |
| 1764 | goto fail; |
| 1765 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1766 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1767 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1768 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1769 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1770 | |
| 1771 | /* May as well allow recovery to be retried once */ |
| 1772 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1775 | |
| 1776 | fail: |
| 1777 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1778 | b, mdname(mddev)); |
| 1779 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1782 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1783 | { |
| 1784 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1785 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1786 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1790 | { |
| 1791 | char b[BDEVNAME_SIZE]; |
| 1792 | if (!rdev->mddev) { |
| 1793 | MD_BUG(); |
| 1794 | return; |
| 1795 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1796 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1797 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1799 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1800 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1801 | sysfs_put(rdev->sysfs_state); |
| 1802 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1803 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1804 | * writing to 'remove' to "dev/state". We also need |
| 1805 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1806 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1807 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1808 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1809 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1810 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * prevent the device from being mounted, repartitioned or |
| 1815 | * otherwise reused by a RAID array (or any other kernel |
| 1816 | * subsystem), by bd_claiming the device. |
| 1817 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1818 | 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] | 1819 | { |
| 1820 | int err = 0; |
| 1821 | struct block_device *bdev; |
| 1822 | char b[BDEVNAME_SIZE]; |
| 1823 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1824 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | if (IS_ERR(bdev)) { |
| 1826 | printk(KERN_ERR "md: could not open %s.\n", |
| 1827 | __bdevname(dev, b)); |
| 1828 | return PTR_ERR(bdev); |
| 1829 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1830 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | if (err) { |
| 1832 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1833 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1834 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | return err; |
| 1836 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1837 | if (!shared) |
| 1838 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | rdev->bdev = bdev; |
| 1840 | return err; |
| 1841 | } |
| 1842 | |
| 1843 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1844 | { |
| 1845 | struct block_device *bdev = rdev->bdev; |
| 1846 | rdev->bdev = NULL; |
| 1847 | if (!bdev) |
| 1848 | MD_BUG(); |
| 1849 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1850 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | void md_autodetect_dev(dev_t dev); |
| 1854 | |
| 1855 | static void export_rdev(mdk_rdev_t * rdev) |
| 1856 | { |
| 1857 | char b[BDEVNAME_SIZE]; |
| 1858 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1859 | bdevname(rdev->bdev,b)); |
| 1860 | if (rdev->mddev) |
| 1861 | MD_BUG(); |
| 1862 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1864 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1865 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | #endif |
| 1867 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1868 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1872 | { |
| 1873 | unbind_rdev_from_array(rdev); |
| 1874 | export_rdev(rdev); |
| 1875 | } |
| 1876 | |
| 1877 | static void export_array(mddev_t *mddev) |
| 1878 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1879 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1881 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | if (!rdev->mddev) { |
| 1883 | MD_BUG(); |
| 1884 | continue; |
| 1885 | } |
| 1886 | kick_rdev_from_array(rdev); |
| 1887 | } |
| 1888 | if (!list_empty(&mddev->disks)) |
| 1889 | MD_BUG(); |
| 1890 | mddev->raid_disks = 0; |
| 1891 | mddev->major_version = 0; |
| 1892 | } |
| 1893 | |
| 1894 | static void print_desc(mdp_disk_t *desc) |
| 1895 | { |
| 1896 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1897 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1898 | } |
| 1899 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1900 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | { |
| 1902 | int i; |
| 1903 | |
| 1904 | printk(KERN_INFO |
| 1905 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1906 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1907 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1908 | sb->ctime); |
| 1909 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1910 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1911 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1912 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1913 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1914 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1915 | sb->failed_disks, sb->spare_disks, |
| 1916 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1917 | |
| 1918 | printk(KERN_INFO); |
| 1919 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1920 | mdp_disk_t *desc; |
| 1921 | |
| 1922 | desc = sb->disks + i; |
| 1923 | if (desc->number || desc->major || desc->minor || |
| 1924 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1925 | printk(" D %2d: ", i); |
| 1926 | print_desc(desc); |
| 1927 | } |
| 1928 | } |
| 1929 | printk(KERN_INFO "md: THIS: "); |
| 1930 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1933 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1934 | { |
| 1935 | __u8 *uuid; |
| 1936 | |
| 1937 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1938 | printk(KERN_INFO |
| 1939 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1940 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1941 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1942 | le32_to_cpu(sb->major_version), |
| 1943 | le32_to_cpu(sb->feature_map), |
| 1944 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1945 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1946 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1947 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1948 | sb->set_name, |
| 1949 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1950 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1951 | |
| 1952 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1953 | printk(KERN_INFO |
| 1954 | "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] | 1955 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1956 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1957 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1958 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1959 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1960 | le32_to_cpu(sb->level), |
| 1961 | (unsigned long long)le64_to_cpu(sb->size), |
| 1962 | le32_to_cpu(sb->raid_disks), |
| 1963 | le32_to_cpu(sb->layout), |
| 1964 | le32_to_cpu(sb->chunksize), |
| 1965 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1966 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1967 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1968 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1969 | le32_to_cpu(sb->dev_number), |
| 1970 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1971 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1972 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1973 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1974 | sb->devflags, |
| 1975 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1976 | (unsigned long long)le64_to_cpu(sb->events), |
| 1977 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1978 | le32_to_cpu(sb->sb_csum), |
| 1979 | le32_to_cpu(sb->max_dev) |
| 1980 | ); |
| 1981 | } |
| 1982 | |
| 1983 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | { |
| 1985 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1986 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1987 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1988 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1989 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1991 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1992 | switch (major_version) { |
| 1993 | case 0: |
| 1994 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1995 | break; |
| 1996 | case 1: |
| 1997 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1998 | break; |
| 1999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } else |
| 2001 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2002 | } |
| 2003 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2004 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2006 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | mdk_rdev_t *rdev; |
| 2008 | mddev_t *mddev; |
| 2009 | char b[BDEVNAME_SIZE]; |
| 2010 | |
| 2011 | printk("\n"); |
| 2012 | printk("md: **********************************\n"); |
| 2013 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2014 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2015 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2017 | if (mddev->bitmap) |
| 2018 | bitmap_print_sb(mddev->bitmap); |
| 2019 | else |
| 2020 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2021 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2023 | printk("\n"); |
| 2024 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2025 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2026 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
| 2028 | printk("md: **********************************\n"); |
| 2029 | printk("\n"); |
| 2030 | } |
| 2031 | |
| 2032 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2033 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2035 | /* Update each superblock (in-memory image), but |
| 2036 | * if we are allowed to, skip spares which already |
| 2037 | * have the right event counter, or have one earlier |
| 2038 | * (which would mean they aren't being marked as dirty |
| 2039 | * with the rest of the array) |
| 2040 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2043 | /* First make sure individual recovery_offsets are correct */ |
| 2044 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2045 | if (rdev->raid_disk >= 0 && |
| 2046 | !test_bit(In_sync, &rdev->flags) && |
| 2047 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2048 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2049 | |
| 2050 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2051 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2052 | if (rdev->sb_events == mddev->events || |
| 2053 | (nospares && |
| 2054 | rdev->raid_disk < 0 && |
| 2055 | (rdev->sb_events&1)==0 && |
| 2056 | rdev->sb_events+1 == mddev->events)) { |
| 2057 | /* Don't update this superblock */ |
| 2058 | rdev->sb_loaded = 2; |
| 2059 | } else { |
| 2060 | super_types[mddev->major_version]. |
| 2061 | sync_super(mddev, rdev); |
| 2062 | rdev->sb_loaded = 1; |
| 2063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2067 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2070 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2071 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2073 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2074 | if (mddev->external) |
| 2075 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2077 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2078 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2079 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2080 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2081 | force_change = 1; |
| 2082 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2083 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2084 | * though if events isn't the right even/odd, we will have to do |
| 2085 | * spares after all |
| 2086 | */ |
| 2087 | nospares = 1; |
| 2088 | if (force_change) |
| 2089 | nospares = 0; |
| 2090 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2091 | /* If the array is degraded, then skipping spares is both |
| 2092 | * dangerous and fairly pointless. |
| 2093 | * Dangerous because a device that was removed from the array |
| 2094 | * might have a event_count that still looks up-to-date, |
| 2095 | * so it can be re-added without a resync. |
| 2096 | * Pointless because if there are any spares to skip, |
| 2097 | * then a recovery will happen and soon that array won't |
| 2098 | * be degraded any more and the spare can go back to sleep then. |
| 2099 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2100 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2101 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2102 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2103 | |
| 2104 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2105 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2106 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2107 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2108 | && (mddev->events & 1) |
| 2109 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2110 | mddev->events--; |
| 2111 | else { |
| 2112 | /* otherwise we have to go forward and ... */ |
| 2113 | mddev->events ++; |
| 2114 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2115 | /* .. if the array isn't clean, an 'even' event must also go |
| 2116 | * to spares. */ |
| 2117 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2118 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2119 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2120 | /* otherwise an 'odd' event must go to spares */ |
| 2121 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2122 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
| 2126 | if (!mddev->events) { |
| 2127 | /* |
| 2128 | * oops, this 64-bit counter should never wrap. |
| 2129 | * Either we are in around ~1 trillion A.C., assuming |
| 2130 | * 1 reboot per second, or we have a bug: |
| 2131 | */ |
| 2132 | MD_BUG(); |
| 2133 | mddev->events --; |
| 2134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | |
| 2136 | /* |
| 2137 | * do not write anything to disk if using |
| 2138 | * nonpersistent superblocks |
| 2139 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2140 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2141 | if (!mddev->external) |
| 2142 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2143 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2144 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2145 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2147 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2148 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2149 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
| 2151 | dprintk(KERN_INFO |
| 2152 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2153 | mdname(mddev),mddev->in_sync); |
| 2154 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2155 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2156 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | char b[BDEVNAME_SIZE]; |
| 2158 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2159 | if (rdev->sb_loaded != 1) |
| 2160 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2161 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | dprintk("(skipping faulty "); |
| 2163 | |
| 2164 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2165 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2166 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2167 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2168 | rdev->sb_page); |
| 2169 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2170 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2171 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2172 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | } else |
| 2175 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2176 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | /* only need to write one superblock... */ |
| 2178 | break; |
| 2179 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2180 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2181 | /* 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] | 2182 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2183 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2184 | if (mddev->in_sync != sync_req || |
| 2185 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2186 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2187 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2188 | goto repeat; |
| 2189 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2190 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2191 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2192 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2193 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2194 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2198 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2199 | * We want to accept with case. For this we use cmd_match. |
| 2200 | */ |
| 2201 | static int cmd_match(const char *cmd, const char *str) |
| 2202 | { |
| 2203 | /* See if cmd, written into a sysfs file, matches |
| 2204 | * str. They must either be the same, or cmd can |
| 2205 | * have a trailing newline |
| 2206 | */ |
| 2207 | while (*cmd && *str && *cmd == *str) { |
| 2208 | cmd++; |
| 2209 | str++; |
| 2210 | } |
| 2211 | if (*cmd == '\n') |
| 2212 | cmd++; |
| 2213 | if (*str || *cmd) |
| 2214 | return 0; |
| 2215 | return 1; |
| 2216 | } |
| 2217 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2218 | struct rdev_sysfs_entry { |
| 2219 | struct attribute attr; |
| 2220 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2221 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2222 | }; |
| 2223 | |
| 2224 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2225 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2226 | { |
| 2227 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2228 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2229 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2230 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2231 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2232 | sep = ","; |
| 2233 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2234 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2235 | len += sprintf(page+len, "%sin_sync",sep); |
| 2236 | sep = ","; |
| 2237 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2238 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2239 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2240 | sep = ","; |
| 2241 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2242 | if (test_bit(Blocked, &rdev->flags)) { |
| 2243 | len += sprintf(page+len, "%sblocked", sep); |
| 2244 | sep = ","; |
| 2245 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2246 | if (!test_bit(Faulty, &rdev->flags) && |
| 2247 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2248 | len += sprintf(page+len, "%sspare", sep); |
| 2249 | sep = ","; |
| 2250 | } |
| 2251 | return len+sprintf(page+len, "\n"); |
| 2252 | } |
| 2253 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2254 | static ssize_t |
| 2255 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2256 | { |
| 2257 | /* can write |
| 2258 | * faulty - simulates and error |
| 2259 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2260 | * writemostly - sets write_mostly |
| 2261 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2262 | * blocked - sets the Blocked flag |
| 2263 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2264 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2265 | */ |
| 2266 | int err = -EINVAL; |
| 2267 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2268 | md_error(rdev->mddev, rdev); |
| 2269 | err = 0; |
| 2270 | } else if (cmd_match(buf, "remove")) { |
| 2271 | if (rdev->raid_disk >= 0) |
| 2272 | err = -EBUSY; |
| 2273 | else { |
| 2274 | mddev_t *mddev = rdev->mddev; |
| 2275 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2276 | if (mddev->pers) |
| 2277 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2278 | md_new_event(mddev); |
| 2279 | err = 0; |
| 2280 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2281 | } else if (cmd_match(buf, "writemostly")) { |
| 2282 | set_bit(WriteMostly, &rdev->flags); |
| 2283 | err = 0; |
| 2284 | } else if (cmd_match(buf, "-writemostly")) { |
| 2285 | clear_bit(WriteMostly, &rdev->flags); |
| 2286 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2287 | } else if (cmd_match(buf, "blocked")) { |
| 2288 | set_bit(Blocked, &rdev->flags); |
| 2289 | err = 0; |
| 2290 | } else if (cmd_match(buf, "-blocked")) { |
| 2291 | clear_bit(Blocked, &rdev->flags); |
| 2292 | wake_up(&rdev->blocked_wait); |
| 2293 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2294 | md_wakeup_thread(rdev->mddev->thread); |
| 2295 | |
| 2296 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2297 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2298 | set_bit(In_sync, &rdev->flags); |
| 2299 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2300 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2301 | if (!err && rdev->sysfs_state) |
| 2302 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2303 | return err ? err : len; |
| 2304 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2305 | static struct rdev_sysfs_entry rdev_state = |
| 2306 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2307 | |
| 2308 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2309 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2310 | { |
| 2311 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2312 | } |
| 2313 | |
| 2314 | static ssize_t |
| 2315 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2316 | { |
| 2317 | char *e; |
| 2318 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2319 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2320 | atomic_set(&rdev->corrected_errors, n); |
| 2321 | return len; |
| 2322 | } |
| 2323 | return -EINVAL; |
| 2324 | } |
| 2325 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2326 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2327 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2328 | static ssize_t |
| 2329 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2330 | { |
| 2331 | if (rdev->raid_disk < 0) |
| 2332 | return sprintf(page, "none\n"); |
| 2333 | else |
| 2334 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2335 | } |
| 2336 | |
| 2337 | static ssize_t |
| 2338 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2339 | { |
| 2340 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2341 | int err; |
| 2342 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2343 | int slot = simple_strtoul(buf, &e, 10); |
| 2344 | if (strncmp(buf, "none", 4)==0) |
| 2345 | slot = -1; |
| 2346 | else if (e==buf || (*e && *e!= '\n')) |
| 2347 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2348 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2349 | /* Setting 'slot' on an active array requires also |
| 2350 | * updating the 'rd%d' link, and communicating |
| 2351 | * with the personality with ->hot_*_disk. |
| 2352 | * For now we only support removing |
| 2353 | * failed/spare devices. This normally happens automatically, |
| 2354 | * but not when the metadata is externally managed. |
| 2355 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2356 | if (rdev->raid_disk == -1) |
| 2357 | return -EEXIST; |
| 2358 | /* personality does all needed checks */ |
| 2359 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2360 | return -EINVAL; |
| 2361 | err = rdev->mddev->pers-> |
| 2362 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2363 | if (err) |
| 2364 | return err; |
| 2365 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2366 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2367 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2368 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2369 | } else if (rdev->mddev->pers) { |
| 2370 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2371 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2372 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2373 | */ |
| 2374 | |
| 2375 | if (rdev->raid_disk != -1) |
| 2376 | return -EBUSY; |
| 2377 | |
| 2378 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2379 | return -EINVAL; |
| 2380 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2381 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2382 | if (rdev2->raid_disk == slot) |
| 2383 | return -EEXIST; |
| 2384 | |
| 2385 | rdev->raid_disk = slot; |
| 2386 | if (test_bit(In_sync, &rdev->flags)) |
| 2387 | rdev->saved_raid_disk = slot; |
| 2388 | else |
| 2389 | rdev->saved_raid_disk = -1; |
| 2390 | err = rdev->mddev->pers-> |
| 2391 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2392 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2393 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2394 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2395 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2396 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2397 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2398 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2399 | printk(KERN_WARNING |
| 2400 | "md: cannot register " |
| 2401 | "%s for %s\n", |
| 2402 | nm, mdname(rdev->mddev)); |
| 2403 | |
| 2404 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2405 | } else { |
| 2406 | if (slot >= rdev->mddev->raid_disks) |
| 2407 | return -ENOSPC; |
| 2408 | rdev->raid_disk = slot; |
| 2409 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2410 | clear_bit(Faulty, &rdev->flags); |
| 2411 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2412 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2413 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2414 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2415 | return len; |
| 2416 | } |
| 2417 | |
| 2418 | |
| 2419 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2420 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2421 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2422 | static ssize_t |
| 2423 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2424 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2425 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | static ssize_t |
| 2429 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2430 | { |
| 2431 | char *e; |
| 2432 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2433 | if (e==buf || (*e && *e != '\n')) |
| 2434 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2435 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2436 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2437 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2438 | /* Must set offset before size, so overlap checks |
| 2439 | * can be sane */ |
| 2440 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2441 | rdev->data_offset = offset; |
| 2442 | return len; |
| 2443 | } |
| 2444 | |
| 2445 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2446 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2447 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2448 | static ssize_t |
| 2449 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2450 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2451 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2454 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2455 | { |
| 2456 | /* check if two start/length pairs overlap */ |
| 2457 | if (s1+l1 <= s2) |
| 2458 | return 0; |
| 2459 | if (s2+l2 <= s1) |
| 2460 | return 0; |
| 2461 | return 1; |
| 2462 | } |
| 2463 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2464 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2465 | { |
| 2466 | unsigned long long blocks; |
| 2467 | sector_t new; |
| 2468 | |
| 2469 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2470 | return -EINVAL; |
| 2471 | |
| 2472 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2473 | return -EINVAL; /* sector conversion overflow */ |
| 2474 | |
| 2475 | new = blocks * 2; |
| 2476 | if (new != blocks * 2) |
| 2477 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2478 | |
| 2479 | *sectors = new; |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2483 | static ssize_t |
| 2484 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2485 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2486 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2487 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2488 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2489 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2490 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2491 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2492 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2493 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2494 | sectors = super_types[my_mddev->major_version]. |
| 2495 | rdev_size_change(rdev, sectors); |
| 2496 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2497 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2498 | } else if (!sectors) |
| 2499 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2500 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2501 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2502 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2503 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2504 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2505 | rdev->sectors = sectors; |
| 2506 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2507 | /* need to check that all other rdevs with the same ->bdev |
| 2508 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2509 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2510 | * we have to change it back, we will have the lock again. |
| 2511 | */ |
| 2512 | mddev_t *mddev; |
| 2513 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2514 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2515 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2516 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2517 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2518 | mdk_rdev_t *rdev2; |
| 2519 | |
| 2520 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2521 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2522 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2523 | (rdev->bdev == rdev2->bdev && |
| 2524 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2525 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2526 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2527 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2528 | overlap = 1; |
| 2529 | break; |
| 2530 | } |
| 2531 | mddev_unlock(mddev); |
| 2532 | if (overlap) { |
| 2533 | mddev_put(mddev); |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2537 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2538 | if (overlap) { |
| 2539 | /* Someone else could have slipped in a size |
| 2540 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2541 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2542 | * safe, and trust userspace not to race with |
| 2543 | * itself |
| 2544 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2545 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2546 | return -EBUSY; |
| 2547 | } |
| 2548 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2549 | return len; |
| 2550 | } |
| 2551 | |
| 2552 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2553 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2554 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2555 | static struct attribute *rdev_default_attrs[] = { |
| 2556 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2557 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2558 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2559 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2560 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2561 | NULL, |
| 2562 | }; |
| 2563 | static ssize_t |
| 2564 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2565 | { |
| 2566 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2567 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2568 | mddev_t *mddev = rdev->mddev; |
| 2569 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2570 | |
| 2571 | if (!entry->show) |
| 2572 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2573 | |
| 2574 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2575 | if (!rv) { |
| 2576 | if (rdev->mddev == NULL) |
| 2577 | rv = -EBUSY; |
| 2578 | else |
| 2579 | rv = entry->show(rdev, page); |
| 2580 | mddev_unlock(mddev); |
| 2581 | } |
| 2582 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | static ssize_t |
| 2586 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2587 | const char *page, size_t length) |
| 2588 | { |
| 2589 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2590 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2591 | ssize_t rv; |
| 2592 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2593 | |
| 2594 | if (!entry->store) |
| 2595 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2596 | if (!capable(CAP_SYS_ADMIN)) |
| 2597 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2598 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2599 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2600 | if (rdev->mddev == NULL) |
| 2601 | rv = -EBUSY; |
| 2602 | else |
| 2603 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2604 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2605 | } |
| 2606 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static void rdev_free(struct kobject *ko) |
| 2610 | { |
| 2611 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2612 | kfree(rdev); |
| 2613 | } |
| 2614 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2615 | .show = rdev_attr_show, |
| 2616 | .store = rdev_attr_store, |
| 2617 | }; |
| 2618 | static struct kobj_type rdev_ktype = { |
| 2619 | .release = rdev_free, |
| 2620 | .sysfs_ops = &rdev_sysfs_ops, |
| 2621 | .default_attrs = rdev_default_attrs, |
| 2622 | }; |
| 2623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | /* |
| 2625 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2626 | * |
| 2627 | * mark the device faulty if: |
| 2628 | * |
| 2629 | * - the device is nonexistent (zero size) |
| 2630 | * - the device has no valid superblock |
| 2631 | * |
| 2632 | * a faulty rdev _never_ has rdev->sb set. |
| 2633 | */ |
| 2634 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2635 | { |
| 2636 | char b[BDEVNAME_SIZE]; |
| 2637 | int err; |
| 2638 | mdk_rdev_t *rdev; |
| 2639 | sector_t size; |
| 2640 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2641 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | if (!rdev) { |
| 2643 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2644 | return ERR_PTR(-ENOMEM); |
| 2645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | |
| 2647 | if ((err = alloc_disk_sb(rdev))) |
| 2648 | goto abort_free; |
| 2649 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2650 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | if (err) |
| 2652 | goto abort_free; |
| 2653 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2654 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2657 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2658 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2659 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2661 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2662 | rdev->last_read_error.tv_sec = 0; |
| 2663 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2665 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2666 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | |
| 2668 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2669 | if (!size) { |
| 2670 | printk(KERN_WARNING |
| 2671 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2672 | bdevname(rdev->bdev,b)); |
| 2673 | err = -EINVAL; |
| 2674 | goto abort_free; |
| 2675 | } |
| 2676 | |
| 2677 | if (super_format >= 0) { |
| 2678 | err = super_types[super_format]. |
| 2679 | load_super(rdev, NULL, super_minor); |
| 2680 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2681 | printk(KERN_WARNING |
| 2682 | "md: %s does not have a valid v%d.%d " |
| 2683 | "superblock, not importing!\n", |
| 2684 | bdevname(rdev->bdev,b), |
| 2685 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | goto abort_free; |
| 2687 | } |
| 2688 | if (err < 0) { |
| 2689 | printk(KERN_WARNING |
| 2690 | "md: could not read %s's sb, not importing!\n", |
| 2691 | bdevname(rdev->bdev,b)); |
| 2692 | goto abort_free; |
| 2693 | } |
| 2694 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2697 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | |
| 2699 | return rdev; |
| 2700 | |
| 2701 | abort_free: |
| 2702 | if (rdev->sb_page) { |
| 2703 | if (rdev->bdev) |
| 2704 | unlock_rdev(rdev); |
| 2705 | free_disk_sb(rdev); |
| 2706 | } |
| 2707 | kfree(rdev); |
| 2708 | return ERR_PTR(err); |
| 2709 | } |
| 2710 | |
| 2711 | /* |
| 2712 | * Check a full RAID array for plausibility |
| 2713 | */ |
| 2714 | |
| 2715 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2716 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | { |
| 2718 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2719 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | char b[BDEVNAME_SIZE]; |
| 2721 | |
| 2722 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2723 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | switch (super_types[mddev->major_version]. |
| 2725 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2726 | case 1: |
| 2727 | freshest = rdev; |
| 2728 | break; |
| 2729 | case 0: |
| 2730 | break; |
| 2731 | default: |
| 2732 | printk( KERN_ERR \ |
| 2733 | "md: fatal superblock inconsistency in %s" |
| 2734 | " -- removing from array\n", |
| 2735 | bdevname(rdev->bdev,b)); |
| 2736 | kick_rdev_from_array(rdev); |
| 2737 | } |
| 2738 | |
| 2739 | |
| 2740 | super_types[mddev->major_version]. |
| 2741 | validate_super(mddev, freshest); |
| 2742 | |
| 2743 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2744 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2745 | if (rdev->desc_nr >= mddev->max_disks || |
| 2746 | i > mddev->max_disks) { |
| 2747 | printk(KERN_WARNING |
| 2748 | "md: %s: %s: only %d devices permitted\n", |
| 2749 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2750 | mddev->max_disks); |
| 2751 | kick_rdev_from_array(rdev); |
| 2752 | continue; |
| 2753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | if (rdev != freshest) |
| 2755 | if (super_types[mddev->major_version]. |
| 2756 | validate_super(mddev, rdev)) { |
| 2757 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2758 | " from array!\n", |
| 2759 | bdevname(rdev->bdev,b)); |
| 2760 | kick_rdev_from_array(rdev); |
| 2761 | continue; |
| 2762 | } |
| 2763 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2764 | rdev->desc_nr = i++; |
| 2765 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2766 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2767 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2768 | rdev->raid_disk = -1; |
| 2769 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | } |
| 2771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2774 | /* Read a fixed-point number. |
| 2775 | * Numbers in sysfs attributes should be in "standard" units where |
| 2776 | * possible, so time should be in seconds. |
| 2777 | * However we internally use a a much smaller unit such as |
| 2778 | * milliseconds or jiffies. |
| 2779 | * This function takes a decimal number with a possible fractional |
| 2780 | * component, and produces an integer which is the result of |
| 2781 | * multiplying that number by 10^'scale'. |
| 2782 | * all without any floating-point arithmetic. |
| 2783 | */ |
| 2784 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2785 | { |
| 2786 | unsigned long result = 0; |
| 2787 | long decimals = -1; |
| 2788 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2789 | if (*cp == '.') |
| 2790 | decimals = 0; |
| 2791 | else if (decimals < scale) { |
| 2792 | unsigned int value; |
| 2793 | value = *cp - '0'; |
| 2794 | result = result * 10 + value; |
| 2795 | if (decimals >= 0) |
| 2796 | decimals++; |
| 2797 | } |
| 2798 | cp++; |
| 2799 | } |
| 2800 | if (*cp == '\n') |
| 2801 | cp++; |
| 2802 | if (*cp) |
| 2803 | return -EINVAL; |
| 2804 | if (decimals < 0) |
| 2805 | decimals = 0; |
| 2806 | while (decimals < scale) { |
| 2807 | result *= 10; |
| 2808 | decimals ++; |
| 2809 | } |
| 2810 | *res = result; |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2815 | static void md_safemode_timeout(unsigned long data); |
| 2816 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2817 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2818 | safe_delay_show(mddev_t *mddev, char *page) |
| 2819 | { |
| 2820 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2821 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2822 | } |
| 2823 | static ssize_t |
| 2824 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2825 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2826 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2827 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2828 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2829 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2830 | if (msec == 0) |
| 2831 | mddev->safemode_delay = 0; |
| 2832 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2833 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2834 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2835 | if (mddev->safemode_delay == 0) |
| 2836 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2837 | if (mddev->safemode_delay < old_delay) |
| 2838 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2839 | } |
| 2840 | return len; |
| 2841 | } |
| 2842 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2843 | __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] | 2844 | |
| 2845 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2846 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2847 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2848 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2849 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2850 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2851 | else if (mddev->clevel[0]) |
| 2852 | return sprintf(page, "%s\n", mddev->clevel); |
| 2853 | else if (mddev->level != LEVEL_NONE) |
| 2854 | return sprintf(page, "%d\n", mddev->level); |
| 2855 | else |
| 2856 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2857 | } |
| 2858 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2859 | static ssize_t |
| 2860 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2861 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2862 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2863 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2864 | struct mdk_personality *pers; |
| 2865 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2866 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2867 | |
| 2868 | if (mddev->pers == NULL) { |
| 2869 | if (len == 0) |
| 2870 | return 0; |
| 2871 | if (len >= sizeof(mddev->clevel)) |
| 2872 | return -ENOSPC; |
| 2873 | strncpy(mddev->clevel, buf, len); |
| 2874 | if (mddev->clevel[len-1] == '\n') |
| 2875 | len--; |
| 2876 | mddev->clevel[len] = 0; |
| 2877 | mddev->level = LEVEL_NONE; |
| 2878 | return rv; |
| 2879 | } |
| 2880 | |
| 2881 | /* request to change the personality. Need to ensure: |
| 2882 | * - array is not engaged in resync/recovery/reshape |
| 2883 | * - old personality can be suspended |
| 2884 | * - new personality will access other array. |
| 2885 | */ |
| 2886 | |
| 2887 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2888 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2889 | |
| 2890 | if (!mddev->pers->quiesce) { |
| 2891 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2892 | mdname(mddev), mddev->pers->name); |
| 2893 | return -EINVAL; |
| 2894 | } |
| 2895 | |
| 2896 | /* Now find the new personality */ |
| 2897 | if (len == 0 || len >= sizeof(level)) |
| 2898 | return -EINVAL; |
| 2899 | strncpy(level, buf, len); |
| 2900 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2901 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2902 | level[len] = 0; |
| 2903 | |
| 2904 | request_module("md-%s", level); |
| 2905 | spin_lock(&pers_lock); |
| 2906 | pers = find_pers(LEVEL_NONE, level); |
| 2907 | if (!pers || !try_module_get(pers->owner)) { |
| 2908 | spin_unlock(&pers_lock); |
| 2909 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2910 | return -EINVAL; |
| 2911 | } |
| 2912 | spin_unlock(&pers_lock); |
| 2913 | |
| 2914 | if (pers == mddev->pers) { |
| 2915 | /* Nothing to do! */ |
| 2916 | module_put(pers->owner); |
| 2917 | return rv; |
| 2918 | } |
| 2919 | if (!pers->takeover) { |
| 2920 | module_put(pers->owner); |
| 2921 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2922 | mdname(mddev), level); |
| 2923 | return -EINVAL; |
| 2924 | } |
| 2925 | |
| 2926 | /* ->takeover must set new_* and/or delta_disks |
| 2927 | * if it succeeds, and may set them when it fails. |
| 2928 | */ |
| 2929 | priv = pers->takeover(mddev); |
| 2930 | if (IS_ERR(priv)) { |
| 2931 | mddev->new_level = mddev->level; |
| 2932 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2933 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2934 | mddev->raid_disks -= mddev->delta_disks; |
| 2935 | mddev->delta_disks = 0; |
| 2936 | module_put(pers->owner); |
| 2937 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2938 | mdname(mddev), level); |
| 2939 | return PTR_ERR(priv); |
| 2940 | } |
| 2941 | |
| 2942 | /* Looks like we have a winner */ |
| 2943 | mddev_suspend(mddev); |
| 2944 | mddev->pers->stop(mddev); |
| 2945 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2946 | /* Invalidate devices that are now superfluous */ |
| 2947 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2948 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2949 | rdev->raid_disk = -1; |
| 2950 | clear_bit(In_sync, &rdev->flags); |
| 2951 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2952 | mddev->pers = pers; |
| 2953 | mddev->private = priv; |
| 2954 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2955 | mddev->level = mddev->new_level; |
| 2956 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2957 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2958 | mddev->delta_disks = 0; |
| 2959 | pers->run(mddev); |
| 2960 | mddev_resume(mddev); |
| 2961 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2962 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2963 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2964 | return rv; |
| 2965 | } |
| 2966 | |
| 2967 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2968 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2969 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2970 | |
| 2971 | static ssize_t |
| 2972 | layout_show(mddev_t *mddev, char *page) |
| 2973 | { |
| 2974 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2975 | if (mddev->reshape_position != MaxSector && |
| 2976 | mddev->layout != mddev->new_layout) |
| 2977 | return sprintf(page, "%d (%d)\n", |
| 2978 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2979 | return sprintf(page, "%d\n", mddev->layout); |
| 2980 | } |
| 2981 | |
| 2982 | static ssize_t |
| 2983 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2984 | { |
| 2985 | char *e; |
| 2986 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2987 | |
| 2988 | if (!*buf || (*e && *e != '\n')) |
| 2989 | return -EINVAL; |
| 2990 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2991 | if (mddev->pers) { |
| 2992 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2993 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2994 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2995 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2996 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2997 | if (err) { |
| 2998 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2999 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3000 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3001 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3002 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3003 | if (mddev->reshape_position == MaxSector) |
| 3004 | mddev->layout = n; |
| 3005 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3006 | return len; |
| 3007 | } |
| 3008 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3009 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3010 | |
| 3011 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3012 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3013 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3014 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3015 | if (mddev->raid_disks == 0) |
| 3016 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3017 | if (mddev->reshape_position != MaxSector && |
| 3018 | mddev->delta_disks != 0) |
| 3019 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3020 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3021 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3022 | } |
| 3023 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3024 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3025 | |
| 3026 | static ssize_t |
| 3027 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3028 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3029 | char *e; |
| 3030 | int rv = 0; |
| 3031 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3032 | |
| 3033 | if (!*buf || (*e && *e != '\n')) |
| 3034 | return -EINVAL; |
| 3035 | |
| 3036 | if (mddev->pers) |
| 3037 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3038 | else if (mddev->reshape_position != MaxSector) { |
| 3039 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3040 | mddev->delta_disks = n - olddisks; |
| 3041 | mddev->raid_disks = n; |
| 3042 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3043 | mddev->raid_disks = n; |
| 3044 | return rv ? rv : len; |
| 3045 | } |
| 3046 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3047 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3048 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3049 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3050 | chunk_size_show(mddev_t *mddev, char *page) |
| 3051 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3052 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3053 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3054 | return sprintf(page, "%d (%d)\n", |
| 3055 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3056 | mddev->chunk_sectors << 9); |
| 3057 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | static ssize_t |
| 3061 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3062 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3063 | char *e; |
| 3064 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3065 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3066 | if (!*buf || (*e && *e != '\n')) |
| 3067 | return -EINVAL; |
| 3068 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3069 | if (mddev->pers) { |
| 3070 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3071 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3072 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3073 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3074 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3075 | if (err) { |
| 3076 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3077 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3078 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3079 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3080 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3081 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3082 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3083 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3084 | return len; |
| 3085 | } |
| 3086 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3087 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3088 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3089 | static ssize_t |
| 3090 | resync_start_show(mddev_t *mddev, char *page) |
| 3091 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3092 | if (mddev->recovery_cp == MaxSector) |
| 3093 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3094 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3095 | } |
| 3096 | |
| 3097 | static ssize_t |
| 3098 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3099 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3100 | char *e; |
| 3101 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3102 | |
| 3103 | if (mddev->pers) |
| 3104 | return -EBUSY; |
| 3105 | if (!*buf || (*e && *e != '\n')) |
| 3106 | return -EINVAL; |
| 3107 | |
| 3108 | mddev->recovery_cp = n; |
| 3109 | return len; |
| 3110 | } |
| 3111 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3112 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3113 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3114 | /* |
| 3115 | * The array state can be: |
| 3116 | * |
| 3117 | * clear |
| 3118 | * No devices, no size, no level |
| 3119 | * Equivalent to STOP_ARRAY ioctl |
| 3120 | * inactive |
| 3121 | * May have some settings, but array is not active |
| 3122 | * all IO results in error |
| 3123 | * When written, doesn't tear down array, but just stops it |
| 3124 | * suspended (not supported yet) |
| 3125 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3126 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3127 | * readonly |
| 3128 | * no resync can happen. no superblocks get written. |
| 3129 | * write requests fail |
| 3130 | * read-auto |
| 3131 | * like readonly, but behaves like 'clean' on a write request. |
| 3132 | * |
| 3133 | * clean - no pending writes, but otherwise active. |
| 3134 | * When written to inactive array, starts without resync |
| 3135 | * If a write request arrives then |
| 3136 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3137 | * if not known, block and switch to write-pending |
| 3138 | * If written to an active array that has pending writes, then fails. |
| 3139 | * active |
| 3140 | * fully active: IO and resync can be happening. |
| 3141 | * When written to inactive array, starts with resync |
| 3142 | * |
| 3143 | * write-pending |
| 3144 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3145 | * |
| 3146 | * active-idle |
| 3147 | * like active, but no writes have been seen for a while (100msec). |
| 3148 | * |
| 3149 | */ |
| 3150 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3151 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3152 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3153 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3154 | "write-pending", "active-idle", NULL }; |
| 3155 | |
| 3156 | static int match_word(const char *word, char **list) |
| 3157 | { |
| 3158 | int n; |
| 3159 | for (n=0; list[n]; n++) |
| 3160 | if (cmd_match(word, list[n])) |
| 3161 | break; |
| 3162 | return n; |
| 3163 | } |
| 3164 | |
| 3165 | static ssize_t |
| 3166 | array_state_show(mddev_t *mddev, char *page) |
| 3167 | { |
| 3168 | enum array_state st = inactive; |
| 3169 | |
| 3170 | if (mddev->pers) |
| 3171 | switch(mddev->ro) { |
| 3172 | case 1: |
| 3173 | st = readonly; |
| 3174 | break; |
| 3175 | case 2: |
| 3176 | st = read_auto; |
| 3177 | break; |
| 3178 | case 0: |
| 3179 | if (mddev->in_sync) |
| 3180 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3181 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3182 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3183 | else if (mddev->safemode) |
| 3184 | st = active_idle; |
| 3185 | else |
| 3186 | st = active; |
| 3187 | } |
| 3188 | else { |
| 3189 | if (list_empty(&mddev->disks) && |
| 3190 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3191 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3192 | st = clear; |
| 3193 | else |
| 3194 | st = inactive; |
| 3195 | } |
| 3196 | return sprintf(page, "%s\n", array_states[st]); |
| 3197 | } |
| 3198 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3199 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3200 | static int do_md_run(mddev_t * mddev); |
| 3201 | static int restart_array(mddev_t *mddev); |
| 3202 | |
| 3203 | static ssize_t |
| 3204 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3205 | { |
| 3206 | int err = -EINVAL; |
| 3207 | enum array_state st = match_word(buf, array_states); |
| 3208 | switch(st) { |
| 3209 | case bad_word: |
| 3210 | break; |
| 3211 | case clear: |
| 3212 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3213 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3214 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3215 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3216 | break; |
| 3217 | case inactive: |
| 3218 | /* stopping an active array */ |
| 3219 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3220 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3221 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3222 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3223 | } else |
| 3224 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3225 | break; |
| 3226 | case suspended: |
| 3227 | break; /* not supported yet */ |
| 3228 | case readonly: |
| 3229 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3230 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3231 | else { |
| 3232 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3233 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3234 | err = do_md_run(mddev); |
| 3235 | } |
| 3236 | break; |
| 3237 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3238 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3239 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3240 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3241 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3242 | err = restart_array(mddev); |
| 3243 | if (err == 0) { |
| 3244 | mddev->ro = 2; |
| 3245 | set_disk_ro(mddev->gendisk, 0); |
| 3246 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3247 | } else { |
| 3248 | mddev->ro = 2; |
| 3249 | err = do_md_run(mddev); |
| 3250 | } |
| 3251 | break; |
| 3252 | case clean: |
| 3253 | if (mddev->pers) { |
| 3254 | restart_array(mddev); |
| 3255 | spin_lock_irq(&mddev->write_lock); |
| 3256 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3257 | if (mddev->in_sync == 0) { |
| 3258 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3259 | if (mddev->safemode == 1) |
| 3260 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3261 | if (mddev->persistent) |
| 3262 | set_bit(MD_CHANGE_CLEAN, |
| 3263 | &mddev->flags); |
| 3264 | } |
| 3265 | err = 0; |
| 3266 | } else |
| 3267 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3268 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3269 | } else |
| 3270 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3271 | break; |
| 3272 | case active: |
| 3273 | if (mddev->pers) { |
| 3274 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3275 | if (mddev->external) |
| 3276 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3277 | wake_up(&mddev->sb_wait); |
| 3278 | err = 0; |
| 3279 | } else { |
| 3280 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3281 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3282 | err = do_md_run(mddev); |
| 3283 | } |
| 3284 | break; |
| 3285 | case write_pending: |
| 3286 | case active_idle: |
| 3287 | /* these cannot be set */ |
| 3288 | break; |
| 3289 | } |
| 3290 | if (err) |
| 3291 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3292 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3293 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3294 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3295 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3296 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3297 | static struct md_sysfs_entry md_array_state = |
| 3298 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3299 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3300 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3301 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3302 | return sprintf(page, "%d\n", |
| 3303 | atomic_read(&mddev->max_corr_read_errors)); |
| 3304 | } |
| 3305 | |
| 3306 | static ssize_t |
| 3307 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3308 | { |
| 3309 | char *e; |
| 3310 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3311 | |
| 3312 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3313 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3314 | return len; |
| 3315 | } |
| 3316 | return -EINVAL; |
| 3317 | } |
| 3318 | |
| 3319 | static struct md_sysfs_entry max_corr_read_errors = |
| 3320 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3321 | max_corrected_read_errors_store); |
| 3322 | |
| 3323 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3324 | null_show(mddev_t *mddev, char *page) |
| 3325 | { |
| 3326 | return -EINVAL; |
| 3327 | } |
| 3328 | |
| 3329 | static ssize_t |
| 3330 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3331 | { |
| 3332 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3333 | /* The new device is added to the array. |
| 3334 | * If the array has a persistent superblock, we read the |
| 3335 | * superblock to initialise info and check validity. |
| 3336 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3337 | * which mainly checks size. |
| 3338 | */ |
| 3339 | char *e; |
| 3340 | int major = simple_strtoul(buf, &e, 10); |
| 3341 | int minor; |
| 3342 | dev_t dev; |
| 3343 | mdk_rdev_t *rdev; |
| 3344 | int err; |
| 3345 | |
| 3346 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3347 | return -EINVAL; |
| 3348 | minor = simple_strtoul(e+1, &e, 10); |
| 3349 | if (*e && *e != '\n') |
| 3350 | return -EINVAL; |
| 3351 | dev = MKDEV(major, minor); |
| 3352 | if (major != MAJOR(dev) || |
| 3353 | minor != MINOR(dev)) |
| 3354 | return -EOVERFLOW; |
| 3355 | |
| 3356 | |
| 3357 | if (mddev->persistent) { |
| 3358 | rdev = md_import_device(dev, mddev->major_version, |
| 3359 | mddev->minor_version); |
| 3360 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3361 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3362 | mdk_rdev_t, same_set); |
| 3363 | err = super_types[mddev->major_version] |
| 3364 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3365 | if (err < 0) |
| 3366 | goto out; |
| 3367 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3368 | } else if (mddev->external) |
| 3369 | rdev = md_import_device(dev, -2, -1); |
| 3370 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3371 | rdev = md_import_device(dev, -1, -1); |
| 3372 | |
| 3373 | if (IS_ERR(rdev)) |
| 3374 | return PTR_ERR(rdev); |
| 3375 | err = bind_rdev_to_array(rdev, mddev); |
| 3376 | out: |
| 3377 | if (err) |
| 3378 | export_rdev(rdev); |
| 3379 | return err ? err : len; |
| 3380 | } |
| 3381 | |
| 3382 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3383 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3384 | |
| 3385 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3386 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3387 | { |
| 3388 | char *end; |
| 3389 | unsigned long chunk, end_chunk; |
| 3390 | |
| 3391 | if (!mddev->bitmap) |
| 3392 | goto out; |
| 3393 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3394 | while (*buf) { |
| 3395 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3396 | if (buf == end) break; |
| 3397 | if (*end == '-') { /* range */ |
| 3398 | buf = end + 1; |
| 3399 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3400 | if (buf == end) break; |
| 3401 | } |
| 3402 | if (*end && !isspace(*end)) break; |
| 3403 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3404 | buf = end; |
| 3405 | while (isspace(*buf)) buf++; |
| 3406 | } |
| 3407 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3408 | out: |
| 3409 | return len; |
| 3410 | } |
| 3411 | |
| 3412 | static struct md_sysfs_entry md_bitmap = |
| 3413 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3414 | |
| 3415 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3416 | size_show(mddev_t *mddev, char *page) |
| 3417 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3418 | return sprintf(page, "%llu\n", |
| 3419 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3420 | } |
| 3421 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3422 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3423 | |
| 3424 | static ssize_t |
| 3425 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3426 | { |
| 3427 | /* If array is inactive, we can reduce the component size, but |
| 3428 | * not increase it (except from 0). |
| 3429 | * If array is active, we can try an on-line resize |
| 3430 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3431 | sector_t sectors; |
| 3432 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3433 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3434 | if (err < 0) |
| 3435 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3436 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3437 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3438 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3439 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3440 | if (mddev->dev_sectors == 0 || |
| 3441 | mddev->dev_sectors > sectors) |
| 3442 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3443 | else |
| 3444 | err = -ENOSPC; |
| 3445 | } |
| 3446 | return err ? err : len; |
| 3447 | } |
| 3448 | |
| 3449 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3450 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3451 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3452 | |
| 3453 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3454 | * This is one of |
| 3455 | * 'none' for arrays with no metadata (good luck...) |
| 3456 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3457 | * or N.M for internally known formats |
| 3458 | */ |
| 3459 | static ssize_t |
| 3460 | metadata_show(mddev_t *mddev, char *page) |
| 3461 | { |
| 3462 | if (mddev->persistent) |
| 3463 | return sprintf(page, "%d.%d\n", |
| 3464 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3465 | else if (mddev->external) |
| 3466 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3467 | else |
| 3468 | return sprintf(page, "none\n"); |
| 3469 | } |
| 3470 | |
| 3471 | static ssize_t |
| 3472 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3473 | { |
| 3474 | int major, minor; |
| 3475 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3476 | /* Changing the details of 'external' metadata is |
| 3477 | * always permitted. Otherwise there must be |
| 3478 | * no devices attached to the array. |
| 3479 | */ |
| 3480 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3481 | ; |
| 3482 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3483 | return -EBUSY; |
| 3484 | |
| 3485 | if (cmd_match(buf, "none")) { |
| 3486 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3487 | mddev->external = 0; |
| 3488 | mddev->major_version = 0; |
| 3489 | mddev->minor_version = 90; |
| 3490 | return len; |
| 3491 | } |
| 3492 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3493 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3494 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3495 | namelen = sizeof(mddev->metadata_type)-1; |
| 3496 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3497 | mddev->metadata_type[namelen] = 0; |
| 3498 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3499 | mddev->metadata_type[--namelen] = 0; |
| 3500 | mddev->persistent = 0; |
| 3501 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3502 | mddev->major_version = 0; |
| 3503 | mddev->minor_version = 90; |
| 3504 | return len; |
| 3505 | } |
| 3506 | major = simple_strtoul(buf, &e, 10); |
| 3507 | if (e==buf || *e != '.') |
| 3508 | return -EINVAL; |
| 3509 | buf = e+1; |
| 3510 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3511 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3512 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3513 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3514 | return -ENOENT; |
| 3515 | mddev->major_version = major; |
| 3516 | mddev->minor_version = minor; |
| 3517 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3518 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3519 | return len; |
| 3520 | } |
| 3521 | |
| 3522 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3523 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3524 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3525 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3526 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3527 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3528 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3529 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3530 | type = "frozen"; |
| 3531 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3532 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3533 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3534 | type = "reshape"; |
| 3535 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3536 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3537 | type = "resync"; |
| 3538 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3539 | type = "check"; |
| 3540 | else |
| 3541 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3542 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3543 | type = "recover"; |
| 3544 | } |
| 3545 | return sprintf(page, "%s\n", type); |
| 3546 | } |
| 3547 | |
| 3548 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3549 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3550 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3551 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3552 | return -EINVAL; |
| 3553 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3554 | if (cmd_match(page, "frozen")) |
| 3555 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3556 | else |
| 3557 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3558 | |
| 3559 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3560 | if (mddev->sync_thread) { |
| 3561 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3562 | md_unregister_thread(mddev->sync_thread); |
| 3563 | mddev->sync_thread = NULL; |
| 3564 | mddev->recovery = 0; |
| 3565 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3566 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3567 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3568 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3569 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3570 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3571 | else if (cmd_match(page, "recover")) { |
| 3572 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3573 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3574 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3575 | int err; |
| 3576 | if (mddev->pers->start_reshape == NULL) |
| 3577 | return -EINVAL; |
| 3578 | err = mddev->pers->start_reshape(mddev); |
| 3579 | if (err) |
| 3580 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3581 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3582 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3583 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3584 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3585 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3586 | return -EINVAL; |
| 3587 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3588 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3589 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3590 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3591 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3592 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3593 | return len; |
| 3594 | } |
| 3595 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3596 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3597 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3598 | { |
| 3599 | return sprintf(page, "%llu\n", |
| 3600 | (unsigned long long) mddev->resync_mismatches); |
| 3601 | } |
| 3602 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3603 | static struct md_sysfs_entry md_scan_mode = |
| 3604 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3605 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3606 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3607 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3608 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3609 | static ssize_t |
| 3610 | sync_min_show(mddev_t *mddev, char *page) |
| 3611 | { |
| 3612 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3613 | mddev->sync_speed_min ? "local": "system"); |
| 3614 | } |
| 3615 | |
| 3616 | static ssize_t |
| 3617 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3618 | { |
| 3619 | int min; |
| 3620 | char *e; |
| 3621 | if (strncmp(buf, "system", 6)==0) { |
| 3622 | mddev->sync_speed_min = 0; |
| 3623 | return len; |
| 3624 | } |
| 3625 | min = simple_strtoul(buf, &e, 10); |
| 3626 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3627 | return -EINVAL; |
| 3628 | mddev->sync_speed_min = min; |
| 3629 | return len; |
| 3630 | } |
| 3631 | |
| 3632 | static struct md_sysfs_entry md_sync_min = |
| 3633 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3634 | |
| 3635 | static ssize_t |
| 3636 | sync_max_show(mddev_t *mddev, char *page) |
| 3637 | { |
| 3638 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3639 | mddev->sync_speed_max ? "local": "system"); |
| 3640 | } |
| 3641 | |
| 3642 | static ssize_t |
| 3643 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3644 | { |
| 3645 | int max; |
| 3646 | char *e; |
| 3647 | if (strncmp(buf, "system", 6)==0) { |
| 3648 | mddev->sync_speed_max = 0; |
| 3649 | return len; |
| 3650 | } |
| 3651 | max = simple_strtoul(buf, &e, 10); |
| 3652 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3653 | return -EINVAL; |
| 3654 | mddev->sync_speed_max = max; |
| 3655 | return len; |
| 3656 | } |
| 3657 | |
| 3658 | static struct md_sysfs_entry md_sync_max = |
| 3659 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3660 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3661 | static ssize_t |
| 3662 | degraded_show(mddev_t *mddev, char *page) |
| 3663 | { |
| 3664 | return sprintf(page, "%d\n", mddev->degraded); |
| 3665 | } |
| 3666 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3667 | |
| 3668 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3669 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3670 | { |
| 3671 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3672 | } |
| 3673 | |
| 3674 | static ssize_t |
| 3675 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3676 | { |
| 3677 | long n; |
| 3678 | |
| 3679 | if (strict_strtol(buf, 10, &n)) |
| 3680 | return -EINVAL; |
| 3681 | |
| 3682 | if (n != 0 && n != 1) |
| 3683 | return -EINVAL; |
| 3684 | |
| 3685 | mddev->parallel_resync = n; |
| 3686 | |
| 3687 | if (mddev->sync_thread) |
| 3688 | wake_up(&resync_wait); |
| 3689 | |
| 3690 | return len; |
| 3691 | } |
| 3692 | |
| 3693 | /* force parallel resync, even with shared block devices */ |
| 3694 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3695 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3696 | sync_force_parallel_show, sync_force_parallel_store); |
| 3697 | |
| 3698 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3699 | sync_speed_show(mddev_t *mddev, char *page) |
| 3700 | { |
| 3701 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3702 | if (mddev->curr_resync == 0) |
| 3703 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3704 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3705 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3706 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3707 | db = resync - mddev->resync_mark_cnt; |
| 3708 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3709 | } |
| 3710 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3711 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3712 | |
| 3713 | static ssize_t |
| 3714 | sync_completed_show(mddev_t *mddev, char *page) |
| 3715 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3716 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3717 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3718 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3719 | return sprintf(page, "none\n"); |
| 3720 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3721 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3722 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3723 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3724 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3725 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3726 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3727 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3730 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3731 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3732 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3733 | min_sync_show(mddev_t *mddev, char *page) |
| 3734 | { |
| 3735 | return sprintf(page, "%llu\n", |
| 3736 | (unsigned long long)mddev->resync_min); |
| 3737 | } |
| 3738 | static ssize_t |
| 3739 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3740 | { |
| 3741 | unsigned long long min; |
| 3742 | if (strict_strtoull(buf, 10, &min)) |
| 3743 | return -EINVAL; |
| 3744 | if (min > mddev->resync_max) |
| 3745 | return -EINVAL; |
| 3746 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3747 | return -EBUSY; |
| 3748 | |
| 3749 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3750 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3751 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3752 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3753 | return -EINVAL; |
| 3754 | } |
| 3755 | mddev->resync_min = min; |
| 3756 | |
| 3757 | return len; |
| 3758 | } |
| 3759 | |
| 3760 | static struct md_sysfs_entry md_min_sync = |
| 3761 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3762 | |
| 3763 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3764 | max_sync_show(mddev_t *mddev, char *page) |
| 3765 | { |
| 3766 | if (mddev->resync_max == MaxSector) |
| 3767 | return sprintf(page, "max\n"); |
| 3768 | else |
| 3769 | return sprintf(page, "%llu\n", |
| 3770 | (unsigned long long)mddev->resync_max); |
| 3771 | } |
| 3772 | static ssize_t |
| 3773 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3774 | { |
| 3775 | if (strncmp(buf, "max", 3) == 0) |
| 3776 | mddev->resync_max = MaxSector; |
| 3777 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3778 | unsigned long long max; |
| 3779 | if (strict_strtoull(buf, 10, &max)) |
| 3780 | return -EINVAL; |
| 3781 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3782 | return -EINVAL; |
| 3783 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3784 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3785 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3786 | return -EBUSY; |
| 3787 | |
| 3788 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3789 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3790 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3791 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3792 | return -EINVAL; |
| 3793 | } |
| 3794 | mddev->resync_max = max; |
| 3795 | } |
| 3796 | wake_up(&mddev->recovery_wait); |
| 3797 | return len; |
| 3798 | } |
| 3799 | |
| 3800 | static struct md_sysfs_entry md_max_sync = |
| 3801 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3802 | |
| 3803 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3804 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3805 | { |
| 3806 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3807 | } |
| 3808 | |
| 3809 | static ssize_t |
| 3810 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3811 | { |
| 3812 | char *e; |
| 3813 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3814 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3815 | if (mddev->pers == NULL || |
| 3816 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3817 | return -EINVAL; |
| 3818 | if (buf == e || (*e && *e != '\n')) |
| 3819 | return -EINVAL; |
| 3820 | if (new >= mddev->suspend_hi || |
| 3821 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3822 | mddev->suspend_lo = new; |
| 3823 | mddev->pers->quiesce(mddev, 2); |
| 3824 | return len; |
| 3825 | } else |
| 3826 | return -EINVAL; |
| 3827 | } |
| 3828 | static struct md_sysfs_entry md_suspend_lo = |
| 3829 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3830 | |
| 3831 | |
| 3832 | static ssize_t |
| 3833 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3834 | { |
| 3835 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3836 | } |
| 3837 | |
| 3838 | static ssize_t |
| 3839 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3840 | { |
| 3841 | char *e; |
| 3842 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3843 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3844 | if (mddev->pers == NULL || |
| 3845 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3846 | return -EINVAL; |
| 3847 | if (buf == e || (*e && *e != '\n')) |
| 3848 | return -EINVAL; |
| 3849 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3850 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3851 | mddev->suspend_hi = new; |
| 3852 | mddev->pers->quiesce(mddev, 1); |
| 3853 | mddev->pers->quiesce(mddev, 0); |
| 3854 | return len; |
| 3855 | } else |
| 3856 | return -EINVAL; |
| 3857 | } |
| 3858 | static struct md_sysfs_entry md_suspend_hi = |
| 3859 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3860 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3861 | static ssize_t |
| 3862 | reshape_position_show(mddev_t *mddev, char *page) |
| 3863 | { |
| 3864 | if (mddev->reshape_position != MaxSector) |
| 3865 | return sprintf(page, "%llu\n", |
| 3866 | (unsigned long long)mddev->reshape_position); |
| 3867 | strcpy(page, "none\n"); |
| 3868 | return 5; |
| 3869 | } |
| 3870 | |
| 3871 | static ssize_t |
| 3872 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3873 | { |
| 3874 | char *e; |
| 3875 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3876 | if (mddev->pers) |
| 3877 | return -EBUSY; |
| 3878 | if (buf == e || (*e && *e != '\n')) |
| 3879 | return -EINVAL; |
| 3880 | mddev->reshape_position = new; |
| 3881 | mddev->delta_disks = 0; |
| 3882 | mddev->new_level = mddev->level; |
| 3883 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3884 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3885 | return len; |
| 3886 | } |
| 3887 | |
| 3888 | static struct md_sysfs_entry md_reshape_position = |
| 3889 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3890 | reshape_position_store); |
| 3891 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3892 | static ssize_t |
| 3893 | array_size_show(mddev_t *mddev, char *page) |
| 3894 | { |
| 3895 | if (mddev->external_size) |
| 3896 | return sprintf(page, "%llu\n", |
| 3897 | (unsigned long long)mddev->array_sectors/2); |
| 3898 | else |
| 3899 | return sprintf(page, "default\n"); |
| 3900 | } |
| 3901 | |
| 3902 | static ssize_t |
| 3903 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3904 | { |
| 3905 | sector_t sectors; |
| 3906 | |
| 3907 | if (strncmp(buf, "default", 7) == 0) { |
| 3908 | if (mddev->pers) |
| 3909 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3910 | else |
| 3911 | sectors = mddev->array_sectors; |
| 3912 | |
| 3913 | mddev->external_size = 0; |
| 3914 | } else { |
| 3915 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3916 | return -EINVAL; |
| 3917 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3918 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3919 | |
| 3920 | mddev->external_size = 1; |
| 3921 | } |
| 3922 | |
| 3923 | mddev->array_sectors = sectors; |
| 3924 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3925 | if (mddev->pers) |
| 3926 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3927 | |
| 3928 | return len; |
| 3929 | } |
| 3930 | |
| 3931 | static struct md_sysfs_entry md_array_size = |
| 3932 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3933 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3934 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3935 | static struct attribute *md_default_attrs[] = { |
| 3936 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3937 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3938 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3939 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3940 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3941 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3942 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3943 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3944 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3945 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3946 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3947 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3948 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3949 | NULL, |
| 3950 | }; |
| 3951 | |
| 3952 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3953 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3954 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3955 | &md_sync_min.attr, |
| 3956 | &md_sync_max.attr, |
| 3957 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3958 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3959 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3960 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3961 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3962 | &md_suspend_lo.attr, |
| 3963 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3964 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3965 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3966 | NULL, |
| 3967 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3968 | static struct attribute_group md_redundancy_group = { |
| 3969 | .name = NULL, |
| 3970 | .attrs = md_redundancy_attrs, |
| 3971 | }; |
| 3972 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3973 | |
| 3974 | static ssize_t |
| 3975 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3976 | { |
| 3977 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3978 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3979 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3980 | |
| 3981 | if (!entry->show) |
| 3982 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3983 | rv = mddev_lock(mddev); |
| 3984 | if (!rv) { |
| 3985 | rv = entry->show(mddev, page); |
| 3986 | mddev_unlock(mddev); |
| 3987 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3988 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | static ssize_t |
| 3992 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3993 | const char *page, size_t length) |
| 3994 | { |
| 3995 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3996 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3997 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3998 | |
| 3999 | if (!entry->store) |
| 4000 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4001 | if (!capable(CAP_SYS_ADMIN)) |
| 4002 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4003 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4004 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4005 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4006 | if (!rv) { |
| 4007 | rv = entry->store(mddev, page, length); |
| 4008 | mddev_unlock(mddev); |
| 4009 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4010 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | static void md_free(struct kobject *ko) |
| 4014 | { |
| 4015 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4016 | |
| 4017 | if (mddev->sysfs_state) |
| 4018 | sysfs_put(mddev->sysfs_state); |
| 4019 | |
| 4020 | if (mddev->gendisk) { |
| 4021 | del_gendisk(mddev->gendisk); |
| 4022 | put_disk(mddev->gendisk); |
| 4023 | } |
| 4024 | if (mddev->queue) |
| 4025 | blk_cleanup_queue(mddev->queue); |
| 4026 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4027 | kfree(mddev); |
| 4028 | } |
| 4029 | |
| 4030 | static struct sysfs_ops md_sysfs_ops = { |
| 4031 | .show = md_attr_show, |
| 4032 | .store = md_attr_store, |
| 4033 | }; |
| 4034 | static struct kobj_type md_ktype = { |
| 4035 | .release = md_free, |
| 4036 | .sysfs_ops = &md_sysfs_ops, |
| 4037 | .default_attrs = md_default_attrs, |
| 4038 | }; |
| 4039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | int mdp_major = 0; |
| 4041 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4042 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4043 | { |
| 4044 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4045 | |
| 4046 | if (mddev->private == &md_redundancy_group) { |
| 4047 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 4048 | if (mddev->sysfs_action) |
| 4049 | sysfs_put(mddev->sysfs_action); |
| 4050 | mddev->sysfs_action = NULL; |
| 4051 | mddev->private = NULL; |
| 4052 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4053 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4054 | kobject_del(&mddev->kobj); |
| 4055 | kobject_put(&mddev->kobj); |
| 4056 | } |
| 4057 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4058 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4060 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | mddev_t *mddev = mddev_find(dev); |
| 4062 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4063 | int partitioned; |
| 4064 | int shift; |
| 4065 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4066 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | |
| 4068 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4069 | return -ENODEV; |
| 4070 | |
| 4071 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4072 | shift = partitioned ? MdpMinorShift : 0; |
| 4073 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4075 | /* wait for any previous instance if this device |
| 4076 | * to be completed removed (mddev_delayed_delete). |
| 4077 | */ |
| 4078 | flush_scheduled_work(); |
| 4079 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4080 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4081 | error = -EEXIST; |
| 4082 | if (mddev->gendisk) |
| 4083 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4084 | |
| 4085 | if (name) { |
| 4086 | /* Need to ensure that 'name' is not a duplicate. |
| 4087 | */ |
| 4088 | mddev_t *mddev2; |
| 4089 | spin_lock(&all_mddevs_lock); |
| 4090 | |
| 4091 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4092 | if (mddev2->gendisk && |
| 4093 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4094 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4095 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4096 | } |
| 4097 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4099 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4100 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4101 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4102 | if (!mddev->queue) |
| 4103 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4104 | mddev->queue->queuedata = mddev; |
| 4105 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4106 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4107 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4108 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4109 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | disk = alloc_disk(1 << shift); |
| 4112 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4113 | blk_cleanup_queue(mddev->queue); |
| 4114 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4115 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4117 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4119 | if (name) |
| 4120 | strcpy(disk->disk_name, name); |
| 4121 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4123 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | disk->fops = &md_fops; |
| 4126 | disk->private_data = mddev; |
| 4127 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4128 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4129 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4130 | * remove it now. |
| 4131 | */ |
| 4132 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | add_disk(disk); |
| 4134 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4135 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4136 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4137 | if (error) { |
| 4138 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4139 | * __must_check, we must do something with the result |
| 4140 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4141 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4142 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4143 | error = 0; |
| 4144 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4145 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4146 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4147 | abort: |
| 4148 | mutex_unlock(&disks_mutex); |
| 4149 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4150 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4151 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4152 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4153 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4154 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4158 | { |
| 4159 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | return NULL; |
| 4161 | } |
| 4162 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4163 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4164 | { |
| 4165 | /* val must be "md_*" where * is not all digits. |
| 4166 | * We allocate an array with a large free minor number, and |
| 4167 | * set the name to val. val must not already be an active name. |
| 4168 | */ |
| 4169 | int len = strlen(val); |
| 4170 | char buf[DISK_NAME_LEN]; |
| 4171 | |
| 4172 | while (len && val[len-1] == '\n') |
| 4173 | len--; |
| 4174 | if (len >= DISK_NAME_LEN) |
| 4175 | return -E2BIG; |
| 4176 | strlcpy(buf, val, len+1); |
| 4177 | if (strncmp(buf, "md_", 3) != 0) |
| 4178 | return -EINVAL; |
| 4179 | return md_alloc(0, buf); |
| 4180 | } |
| 4181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | static void md_safemode_timeout(unsigned long data) |
| 4183 | { |
| 4184 | mddev_t *mddev = (mddev_t *) data; |
| 4185 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4186 | if (!atomic_read(&mddev->writes_pending)) { |
| 4187 | mddev->safemode = 1; |
| 4188 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4189 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | md_wakeup_thread(mddev->thread); |
| 4192 | } |
| 4193 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4194 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | |
| 4196 | static int do_md_run(mddev_t * mddev) |
| 4197 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4198 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | mdk_rdev_t *rdev; |
| 4200 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4201 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4203 | if (list_empty(&mddev->disks)) |
| 4204 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | |
| 4207 | if (mddev->pers) |
| 4208 | return -EBUSY; |
| 4209 | |
| 4210 | /* |
| 4211 | * Analyze all RAID superblock(s) |
| 4212 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4213 | if (!mddev->raid_disks) { |
| 4214 | if (!mddev->persistent) |
| 4215 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4216 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4219 | if (mddev->level != LEVEL_NONE) |
| 4220 | request_module("md-level-%d", mddev->level); |
| 4221 | else if (mddev->clevel[0]) |
| 4222 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | |
| 4224 | /* |
| 4225 | * Drop all container device buffers, from now on |
| 4226 | * the only valid external interface is through the md |
| 4227 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4229 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4230 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | continue; |
| 4232 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4233 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4234 | |
| 4235 | /* perform some consistency tests on the device. |
| 4236 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4237 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4238 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4239 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4240 | if (mddev->dev_sectors && |
| 4241 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4242 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4243 | printk("md: %s: data overlaps metadata\n", |
| 4244 | mdname(mddev)); |
| 4245 | return -EINVAL; |
| 4246 | } |
| 4247 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4248 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4249 | > rdev->data_offset) { |
| 4250 | printk("md: %s: metadata overlaps data\n", |
| 4251 | mdname(mddev)); |
| 4252 | return -EINVAL; |
| 4253 | } |
| 4254 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4255 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | md_probe(mddev->unit, NULL, NULL); |
| 4259 | disk = mddev->gendisk; |
| 4260 | if (!disk) |
| 4261 | return -ENOMEM; |
| 4262 | |
| 4263 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4264 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4265 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4267 | if (mddev->level != LEVEL_NONE) |
| 4268 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4269 | mddev->level); |
| 4270 | else |
| 4271 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4272 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | return -EINVAL; |
| 4274 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4275 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4277 | if (mddev->level != pers->level) { |
| 4278 | mddev->level = pers->level; |
| 4279 | mddev->new_level = pers->level; |
| 4280 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4281 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4283 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4284 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4285 | /* This personality cannot handle reshaping... */ |
| 4286 | mddev->pers = NULL; |
| 4287 | module_put(pers->owner); |
| 4288 | return -EINVAL; |
| 4289 | } |
| 4290 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4291 | if (pers->sync_request) { |
| 4292 | /* Warn if this is a potentially silly |
| 4293 | * configuration. |
| 4294 | */ |
| 4295 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4296 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4297 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4298 | |
| 4299 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4300 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4301 | if (rdev < rdev2 && |
| 4302 | rdev->bdev->bd_contains == |
| 4303 | rdev2->bdev->bd_contains) { |
| 4304 | printk(KERN_WARNING |
| 4305 | "%s: WARNING: %s appears to be" |
| 4306 | " on the same physical disk as" |
| 4307 | " %s.\n", |
| 4308 | mdname(mddev), |
| 4309 | bdevname(rdev->bdev,b), |
| 4310 | bdevname(rdev2->bdev,b2)); |
| 4311 | warned = 1; |
| 4312 | } |
| 4313 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4314 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4315 | if (warned) |
| 4316 | printk(KERN_WARNING |
| 4317 | "True protection against single-disk" |
| 4318 | " failure might be compromised.\n"); |
| 4319 | } |
| 4320 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4321 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4322 | /* may be over-ridden by personality */ |
| 4323 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4324 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4325 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4326 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4328 | if (start_readonly) |
| 4329 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4330 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4331 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4332 | if (err) |
| 4333 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4334 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4335 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4336 | " but 'external_size' not in effect?\n", __func__); |
| 4337 | printk(KERN_ERR |
| 4338 | "md: invalid array_size %llu > default size %llu\n", |
| 4339 | (unsigned long long)mddev->array_sectors / 2, |
| 4340 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4341 | err = -EINVAL; |
| 4342 | mddev->pers->stop(mddev); |
| 4343 | } |
| 4344 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4345 | err = bitmap_create(mddev); |
| 4346 | if (err) { |
| 4347 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4348 | mdname(mddev), err); |
| 4349 | mddev->pers->stop(mddev); |
| 4350 | } |
| 4351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | module_put(mddev->pers->owner); |
| 4354 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4355 | bitmap_destroy(mddev); |
| 4356 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4358 | if (mddev->pers->sync_request) { |
| 4359 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4360 | printk(KERN_WARNING |
| 4361 | "md: cannot register extra attributes for %s\n", |
| 4362 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4363 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4364 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4365 | mddev->ro = 0; |
| 4366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4368 | atomic_set(&mddev->max_corr_read_errors, |
| 4369 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | mddev->safemode = 0; |
| 4371 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4372 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4373 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4375 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4376 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4377 | if (rdev->raid_disk >= 0) { |
| 4378 | char nm[20]; |
| 4379 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4380 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4381 | printk("md: cannot register %s for %s\n", |
| 4382 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | |
| 4385 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4386 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4387 | if (mddev->flags) |
| 4388 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4390 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4392 | /* If there is a partially-recovered drive we need to |
| 4393 | * start recovery here. If we leave it to md_check_recovery, |
| 4394 | * it will remove the drives and not do the right thing |
| 4395 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4396 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4397 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4398 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4399 | if (rdev->raid_disk >= 0 && |
| 4400 | !test_bit(In_sync, &rdev->flags) && |
| 4401 | !test_bit(Faulty, &rdev->flags)) |
| 4402 | /* complete an interrupted recovery */ |
| 4403 | spares++; |
| 4404 | if (spares && mddev->pers->sync_request) { |
| 4405 | mddev->recovery = 0; |
| 4406 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4407 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4408 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4409 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4410 | if (!mddev->sync_thread) { |
| 4411 | printk(KERN_ERR "%s: could not start resync" |
| 4412 | " thread...\n", |
| 4413 | mdname(mddev)); |
| 4414 | /* leave the spares where they are, it shouldn't hurt */ |
| 4415 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4416 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4417 | } |
| 4418 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4419 | md_wakeup_thread(mddev->thread); |
| 4420 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4421 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4422 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4423 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4424 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4425 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4426 | if (mddev->sysfs_action) |
| 4427 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4428 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4429 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | return 0; |
| 4431 | } |
| 4432 | |
| 4433 | static int restart_array(mddev_t *mddev) |
| 4434 | { |
| 4435 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4437 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4439 | return -ENXIO; |
| 4440 | if (!mddev->pers) |
| 4441 | return -EINVAL; |
| 4442 | if (!mddev->ro) |
| 4443 | return -EBUSY; |
| 4444 | mddev->safemode = 0; |
| 4445 | mddev->ro = 0; |
| 4446 | set_disk_ro(disk, 0); |
| 4447 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4448 | mdname(mddev)); |
| 4449 | /* Kick recovery or resync if necessary */ |
| 4450 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4451 | md_wakeup_thread(mddev->thread); |
| 4452 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4453 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4454 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | } |
| 4456 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4457 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4458 | * to the file ourselves */ |
| 4459 | static int deny_bitmap_write_access(struct file * file) |
| 4460 | { |
| 4461 | struct inode *inode = file->f_mapping->host; |
| 4462 | |
| 4463 | spin_lock(&inode->i_lock); |
| 4464 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4465 | spin_unlock(&inode->i_lock); |
| 4466 | return -ETXTBSY; |
| 4467 | } |
| 4468 | atomic_set(&inode->i_writecount, -1); |
| 4469 | spin_unlock(&inode->i_lock); |
| 4470 | |
| 4471 | return 0; |
| 4472 | } |
| 4473 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4474 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4475 | { |
| 4476 | struct inode *inode = file->f_mapping->host; |
| 4477 | |
| 4478 | spin_lock(&inode->i_lock); |
| 4479 | atomic_set(&inode->i_writecount, 1); |
| 4480 | spin_unlock(&inode->i_lock); |
| 4481 | } |
| 4482 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4483 | /* mode: |
| 4484 | * 0 - completely stop and dis-assemble array |
| 4485 | * 1 - switch to readonly |
| 4486 | * 2 - stop but do not disassemble array |
| 4487 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4488 | 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] | 4489 | { |
| 4490 | int err = 0; |
| 4491 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4492 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4494 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4495 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4496 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4497 | err = -EBUSY; |
| 4498 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | |
| 4500 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4501 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4503 | md_unregister_thread(mddev->sync_thread); |
| 4504 | mddev->sync_thread = NULL; |
| 4505 | } |
| 4506 | |
| 4507 | del_timer_sync(&mddev->safemode_timer); |
| 4508 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4509 | switch(mode) { |
| 4510 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4512 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | goto out; |
| 4514 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4515 | break; |
| 4516 | case 0: /* disassemble */ |
| 4517 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4518 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4519 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | if (mddev->ro) |
| 4521 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4524 | mddev->queue->merge_bvec_fn = NULL; |
| 4525 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4526 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4528 | if (mddev->pers->sync_request) |
| 4529 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4531 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4532 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4533 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4534 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4535 | if (rdev->raid_disk >= 0) { |
| 4536 | char nm[20]; |
| 4537 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4538 | sysfs_remove_link(&mddev->kobj, nm); |
| 4539 | } |
| 4540 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4541 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4542 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | if (mddev->ro) |
| 4545 | mddev->ro = 0; |
| 4546 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4547 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | /* mark array as shutdown cleanly */ |
| 4549 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4550 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4552 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4554 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4555 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4557 | out: |
| 4558 | mutex_unlock(&mddev->open_mutex); |
| 4559 | if (err) |
| 4560 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | /* |
| 4562 | * Free resources if final stop |
| 4563 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4564 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4567 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4568 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4569 | if (mddev->bitmap_info.file) { |
| 4570 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4571 | fput(mddev->bitmap_info.file); |
| 4572 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4573 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4574 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4575 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4576 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4577 | flush_scheduled_work(); |
| 4578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | export_array(mddev); |
| 4580 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4581 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4582 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4583 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4584 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4585 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4586 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4587 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4588 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4589 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4590 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4591 | mddev->level = LEVEL_NONE; |
| 4592 | mddev->clevel[0] = 0; |
| 4593 | mddev->flags = 0; |
| 4594 | mddev->ro = 0; |
| 4595 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4596 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4597 | mddev->ctime = mddev->utime = 0; |
| 4598 | mddev->layout = 0; |
| 4599 | mddev->max_disks = 0; |
| 4600 | mddev->events = 0; |
| 4601 | mddev->delta_disks = 0; |
| 4602 | mddev->new_level = LEVEL_NONE; |
| 4603 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4604 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4605 | mddev->curr_resync = 0; |
| 4606 | mddev->resync_mismatches = 0; |
| 4607 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4608 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4609 | mddev->recovery = 0; |
| 4610 | mddev->in_sync = 0; |
| 4611 | mddev->changed = 0; |
| 4612 | mddev->degraded = 0; |
| 4613 | mddev->barriers_work = 0; |
| 4614 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4615 | mddev->bitmap_info.offset = 0; |
| 4616 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 4617 | mddev->bitmap_info.chunksize = 0; |
| 4618 | mddev->bitmap_info.daemon_sleep = 0; |
| 4619 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4620 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4621 | if (mddev->hold_active == UNTIL_STOP) |
| 4622 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4623 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4624 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4626 | mdname(mddev)); |
| 4627 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4628 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4629 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4630 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | return err; |
| 4632 | } |
| 4633 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4634 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | static void autorun_array(mddev_t *mddev) |
| 4636 | { |
| 4637 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | int err; |
| 4639 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4640 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | |
| 4643 | printk(KERN_INFO "md: running: "); |
| 4644 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4645 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | char b[BDEVNAME_SIZE]; |
| 4647 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4648 | } |
| 4649 | printk("\n"); |
| 4650 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4651 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | if (err) { |
| 4653 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4654 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | /* |
| 4659 | * lets try to run arrays based on all disks that have arrived |
| 4660 | * until now. (those are in pending_raid_disks) |
| 4661 | * |
| 4662 | * the method: pick the first pending disk, collect all disks with |
| 4663 | * the same UUID, remove all from the pending list and put them into |
| 4664 | * the 'same_array' list. Then order this list based on superblock |
| 4665 | * update time (freshest comes first), kick out 'old' disks and |
| 4666 | * compare superblocks. If everything's fine then run it. |
| 4667 | * |
| 4668 | * If "unit" is allocated, then bump its reference count |
| 4669 | */ |
| 4670 | static void autorun_devices(int part) |
| 4671 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4672 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | mddev_t *mddev; |
| 4674 | char b[BDEVNAME_SIZE]; |
| 4675 | |
| 4676 | printk(KERN_INFO "md: autorun ...\n"); |
| 4677 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4678 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4680 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | rdev0 = list_entry(pending_raid_disks.next, |
| 4682 | mdk_rdev_t, same_set); |
| 4683 | |
| 4684 | printk(KERN_INFO "md: considering %s ...\n", |
| 4685 | bdevname(rdev0->bdev,b)); |
| 4686 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4687 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4689 | printk(KERN_INFO "md: adding %s ...\n", |
| 4690 | bdevname(rdev->bdev,b)); |
| 4691 | list_move(&rdev->same_set, &candidates); |
| 4692 | } |
| 4693 | /* |
| 4694 | * now we have a set of devices, with all of them having |
| 4695 | * mostly sane superblocks. It's time to allocate the |
| 4696 | * mddev. |
| 4697 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4698 | if (part) { |
| 4699 | dev = MKDEV(mdp_major, |
| 4700 | rdev0->preferred_minor << MdpMinorShift); |
| 4701 | unit = MINOR(dev) >> MdpMinorShift; |
| 4702 | } else { |
| 4703 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4704 | unit = MINOR(dev); |
| 4705 | } |
| 4706 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4708 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4709 | break; |
| 4710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | |
| 4712 | md_probe(dev, NULL, NULL); |
| 4713 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4714 | if (!mddev || !mddev->gendisk) { |
| 4715 | if (mddev) |
| 4716 | mddev_put(mddev); |
| 4717 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | "md: cannot allocate memory for md drive.\n"); |
| 4719 | break; |
| 4720 | } |
| 4721 | if (mddev_lock(mddev)) |
| 4722 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4723 | mdname(mddev)); |
| 4724 | else if (mddev->raid_disks || mddev->major_version |
| 4725 | || !list_empty(&mddev->disks)) { |
| 4726 | printk(KERN_WARNING |
| 4727 | "md: %s already running, cannot run %s\n", |
| 4728 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4729 | mddev_unlock(mddev); |
| 4730 | } else { |
| 4731 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4732 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4733 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | list_del_init(&rdev->same_set); |
| 4735 | if (bind_rdev_to_array(rdev, mddev)) |
| 4736 | export_rdev(rdev); |
| 4737 | } |
| 4738 | autorun_array(mddev); |
| 4739 | mddev_unlock(mddev); |
| 4740 | } |
| 4741 | /* on success, candidates will be empty, on error |
| 4742 | * it won't... |
| 4743 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4744 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4745 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | mddev_put(mddev); |
| 4749 | } |
| 4750 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4751 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4752 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | static int get_version(void __user * arg) |
| 4755 | { |
| 4756 | mdu_version_t ver; |
| 4757 | |
| 4758 | ver.major = MD_MAJOR_VERSION; |
| 4759 | ver.minor = MD_MINOR_VERSION; |
| 4760 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4761 | |
| 4762 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4763 | return -EFAULT; |
| 4764 | |
| 4765 | return 0; |
| 4766 | } |
| 4767 | |
| 4768 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4769 | { |
| 4770 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4771 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4774 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4775 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4777 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | failed++; |
| 4779 | else { |
| 4780 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4781 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4782 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | else |
| 4784 | spare++; |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | info.major_version = mddev->major_version; |
| 4789 | info.minor_version = mddev->minor_version; |
| 4790 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4791 | info.ctime = mddev->ctime; |
| 4792 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4793 | info.size = mddev->dev_sectors / 2; |
| 4794 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4795 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | info.nr_disks = nr; |
| 4797 | info.raid_disks = mddev->raid_disks; |
| 4798 | info.md_minor = mddev->md_minor; |
| 4799 | info.not_persistent= !mddev->persistent; |
| 4800 | |
| 4801 | info.utime = mddev->utime; |
| 4802 | info.state = 0; |
| 4803 | if (mddev->in_sync) |
| 4804 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4805 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4806 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4807 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | info.working_disks = working; |
| 4809 | info.failed_disks = failed; |
| 4810 | info.spare_disks = spare; |
| 4811 | |
| 4812 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4813 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | |
| 4815 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4816 | return -EFAULT; |
| 4817 | |
| 4818 | return 0; |
| 4819 | } |
| 4820 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4821 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4822 | { |
| 4823 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4824 | char *ptr, *buf = NULL; |
| 4825 | int err = -ENOMEM; |
| 4826 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4827 | if (md_allow_write(mddev)) |
| 4828 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4829 | else |
| 4830 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4831 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4832 | if (!file) |
| 4833 | goto out; |
| 4834 | |
| 4835 | /* bitmap disabled, zero the first byte and copy out */ |
| 4836 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4837 | file->pathname[0] = '\0'; |
| 4838 | goto copy_out; |
| 4839 | } |
| 4840 | |
| 4841 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4842 | if (!buf) |
| 4843 | goto out; |
| 4844 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4845 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4846 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4847 | goto out; |
| 4848 | |
| 4849 | strcpy(file->pathname, ptr); |
| 4850 | |
| 4851 | copy_out: |
| 4852 | err = 0; |
| 4853 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4854 | err = -EFAULT; |
| 4855 | out: |
| 4856 | kfree(buf); |
| 4857 | kfree(file); |
| 4858 | return err; |
| 4859 | } |
| 4860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4862 | { |
| 4863 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | mdk_rdev_t *rdev; |
| 4865 | |
| 4866 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4867 | return -EFAULT; |
| 4868 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4869 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | if (rdev) { |
| 4871 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4872 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4873 | info.raid_disk = rdev->raid_disk; |
| 4874 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4875 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4877 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4879 | info.state |= (1<<MD_DISK_SYNC); |
| 4880 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4881 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4882 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | } else { |
| 4884 | info.major = info.minor = 0; |
| 4885 | info.raid_disk = -1; |
| 4886 | info.state = (1<<MD_DISK_REMOVED); |
| 4887 | } |
| 4888 | |
| 4889 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4890 | return -EFAULT; |
| 4891 | |
| 4892 | return 0; |
| 4893 | } |
| 4894 | |
| 4895 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4896 | { |
| 4897 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4898 | mdk_rdev_t *rdev; |
| 4899 | dev_t dev = MKDEV(info->major,info->minor); |
| 4900 | |
| 4901 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4902 | return -EOVERFLOW; |
| 4903 | |
| 4904 | if (!mddev->raid_disks) { |
| 4905 | int err; |
| 4906 | /* expecting a device which has a superblock */ |
| 4907 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4908 | if (IS_ERR(rdev)) { |
| 4909 | printk(KERN_WARNING |
| 4910 | "md: md_import_device returned %ld\n", |
| 4911 | PTR_ERR(rdev)); |
| 4912 | return PTR_ERR(rdev); |
| 4913 | } |
| 4914 | if (!list_empty(&mddev->disks)) { |
| 4915 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4916 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4917 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4919 | if (err < 0) { |
| 4920 | printk(KERN_WARNING |
| 4921 | "md: %s has different UUID to %s\n", |
| 4922 | bdevname(rdev->bdev,b), |
| 4923 | bdevname(rdev0->bdev,b2)); |
| 4924 | export_rdev(rdev); |
| 4925 | return -EINVAL; |
| 4926 | } |
| 4927 | } |
| 4928 | err = bind_rdev_to_array(rdev, mddev); |
| 4929 | if (err) |
| 4930 | export_rdev(rdev); |
| 4931 | return err; |
| 4932 | } |
| 4933 | |
| 4934 | /* |
| 4935 | * add_new_disk can be used once the array is assembled |
| 4936 | * to add "hot spares". They must already have a superblock |
| 4937 | * written |
| 4938 | */ |
| 4939 | if (mddev->pers) { |
| 4940 | int err; |
| 4941 | if (!mddev->pers->hot_add_disk) { |
| 4942 | printk(KERN_WARNING |
| 4943 | "%s: personality does not support diskops!\n", |
| 4944 | mdname(mddev)); |
| 4945 | return -EINVAL; |
| 4946 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4947 | if (mddev->persistent) |
| 4948 | rdev = md_import_device(dev, mddev->major_version, |
| 4949 | mddev->minor_version); |
| 4950 | else |
| 4951 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | if (IS_ERR(rdev)) { |
| 4953 | printk(KERN_WARNING |
| 4954 | "md: md_import_device returned %ld\n", |
| 4955 | PTR_ERR(rdev)); |
| 4956 | return PTR_ERR(rdev); |
| 4957 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4958 | /* set save_raid_disk if appropriate */ |
| 4959 | if (!mddev->persistent) { |
| 4960 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4961 | info->raid_disk < mddev->raid_disks) |
| 4962 | rdev->raid_disk = info->raid_disk; |
| 4963 | else |
| 4964 | rdev->raid_disk = -1; |
| 4965 | } else |
| 4966 | super_types[mddev->major_version]. |
| 4967 | validate_super(mddev, rdev); |
| 4968 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4969 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4970 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4971 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4972 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4973 | else |
| 4974 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | rdev->raid_disk = -1; |
| 4977 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4978 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4979 | /* If there is hot_add_disk but no hot_remove_disk |
| 4980 | * then added disks for geometry changes, |
| 4981 | * and should be added immediately. |
| 4982 | */ |
| 4983 | super_types[mddev->major_version]. |
| 4984 | validate_super(mddev, rdev); |
| 4985 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4986 | if (err) |
| 4987 | unbind_rdev_from_array(rdev); |
| 4988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | if (err) |
| 4990 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4991 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4992 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4993 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4994 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4995 | if (mddev->degraded) |
| 4996 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4997 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4998 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | return err; |
| 5000 | } |
| 5001 | |
| 5002 | /* otherwise, add_new_disk is only allowed |
| 5003 | * for major_version==0 superblocks |
| 5004 | */ |
| 5005 | if (mddev->major_version != 0) { |
| 5006 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5007 | mdname(mddev)); |
| 5008 | return -EINVAL; |
| 5009 | } |
| 5010 | |
| 5011 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5012 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5013 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | if (IS_ERR(rdev)) { |
| 5015 | printk(KERN_WARNING |
| 5016 | "md: error, md_import_device() returned %ld\n", |
| 5017 | PTR_ERR(rdev)); |
| 5018 | return PTR_ERR(rdev); |
| 5019 | } |
| 5020 | rdev->desc_nr = info->number; |
| 5021 | if (info->raid_disk < mddev->raid_disks) |
| 5022 | rdev->raid_disk = info->raid_disk; |
| 5023 | else |
| 5024 | rdev->raid_disk = -1; |
| 5025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5027 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5028 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5030 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5031 | set_bit(WriteMostly, &rdev->flags); |
| 5032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | if (!mddev->persistent) { |
| 5034 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5035 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5037 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5038 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5040 | err = bind_rdev_to_array(rdev, mddev); |
| 5041 | if (err) { |
| 5042 | export_rdev(rdev); |
| 5043 | return err; |
| 5044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | return 0; |
| 5048 | } |
| 5049 | |
| 5050 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5051 | { |
| 5052 | char b[BDEVNAME_SIZE]; |
| 5053 | mdk_rdev_t *rdev; |
| 5054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | rdev = find_rdev(mddev, dev); |
| 5056 | if (!rdev) |
| 5057 | return -ENXIO; |
| 5058 | |
| 5059 | if (rdev->raid_disk >= 0) |
| 5060 | goto busy; |
| 5061 | |
| 5062 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5063 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5064 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | |
| 5066 | return 0; |
| 5067 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5068 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5070 | return -EBUSY; |
| 5071 | } |
| 5072 | |
| 5073 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5074 | { |
| 5075 | char b[BDEVNAME_SIZE]; |
| 5076 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | mdk_rdev_t *rdev; |
| 5078 | |
| 5079 | if (!mddev->pers) |
| 5080 | return -ENODEV; |
| 5081 | |
| 5082 | if (mddev->major_version != 0) { |
| 5083 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5084 | " version-0 superblocks.\n", |
| 5085 | mdname(mddev)); |
| 5086 | return -EINVAL; |
| 5087 | } |
| 5088 | if (!mddev->pers->hot_add_disk) { |
| 5089 | printk(KERN_WARNING |
| 5090 | "%s: personality does not support diskops!\n", |
| 5091 | mdname(mddev)); |
| 5092 | return -EINVAL; |
| 5093 | } |
| 5094 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5095 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | if (IS_ERR(rdev)) { |
| 5097 | printk(KERN_WARNING |
| 5098 | "md: error, md_import_device() returned %ld\n", |
| 5099 | PTR_ERR(rdev)); |
| 5100 | return -EINVAL; |
| 5101 | } |
| 5102 | |
| 5103 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5104 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5106 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5108 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5110 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | printk(KERN_WARNING |
| 5112 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5113 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5114 | err = -EINVAL; |
| 5115 | goto abort_export; |
| 5116 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5117 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5119 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5120 | err = bind_rdev_to_array(rdev, mddev); |
| 5121 | if (err) |
| 5122 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | |
| 5124 | /* |
| 5125 | * The rest should better be atomic, we can have disk failures |
| 5126 | * noticed in interrupt contexts ... |
| 5127 | */ |
| 5128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | rdev->raid_disk = -1; |
| 5130 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5131 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | |
| 5133 | /* |
| 5134 | * Kick recovery, maybe this spare has to be added to the |
| 5135 | * array immediately. |
| 5136 | */ |
| 5137 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5138 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5139 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | return 0; |
| 5141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | abort_export: |
| 5143 | export_rdev(rdev); |
| 5144 | return err; |
| 5145 | } |
| 5146 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5147 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5148 | { |
| 5149 | int err; |
| 5150 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5151 | if (mddev->pers) { |
| 5152 | if (!mddev->pers->quiesce) |
| 5153 | return -EBUSY; |
| 5154 | if (mddev->recovery || mddev->sync_thread) |
| 5155 | return -EBUSY; |
| 5156 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5159 | |
| 5160 | if (fd >= 0) { |
| 5161 | if (mddev->bitmap) |
| 5162 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5163 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5164 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5165 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5166 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5167 | mdname(mddev)); |
| 5168 | return -EBADF; |
| 5169 | } |
| 5170 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5171 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5172 | if (err) { |
| 5173 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5174 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5175 | fput(mddev->bitmap_info.file); |
| 5176 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5177 | return err; |
| 5178 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5179 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5180 | } else if (mddev->bitmap == NULL) |
| 5181 | return -ENOENT; /* cannot remove what isn't there */ |
| 5182 | err = 0; |
| 5183 | if (mddev->pers) { |
| 5184 | mddev->pers->quiesce(mddev, 1); |
| 5185 | if (fd >= 0) |
| 5186 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5187 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5188 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5189 | fd = -1; /* make sure to put the file */ |
| 5190 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5191 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5192 | } |
| 5193 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5194 | if (mddev->bitmap_info.file) { |
| 5195 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5196 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5197 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5198 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5199 | } |
| 5200 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5201 | return err; |
| 5202 | } |
| 5203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | /* |
| 5205 | * set_array_info is used two different ways |
| 5206 | * The original usage is when creating a new array. |
| 5207 | * In this usage, raid_disks is > 0 and it together with |
| 5208 | * level, size, not_persistent,layout,chunksize determine the |
| 5209 | * shape of the array. |
| 5210 | * This will always create an array with a type-0.90.0 superblock. |
| 5211 | * The newer usage is when assembling an array. |
| 5212 | * In this case raid_disks will be 0, and the major_version field is |
| 5213 | * use to determine which style super-blocks are to be found on the devices. |
| 5214 | * The minor and patch _version numbers are also kept incase the |
| 5215 | * super_block handler wishes to interpret them. |
| 5216 | */ |
| 5217 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5218 | { |
| 5219 | |
| 5220 | if (info->raid_disks == 0) { |
| 5221 | /* just setting version number for superblock loading */ |
| 5222 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5223 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | super_types[info->major_version].name == NULL) { |
| 5225 | /* maybe try to auto-load a module? */ |
| 5226 | printk(KERN_INFO |
| 5227 | "md: superblock version %d not known\n", |
| 5228 | info->major_version); |
| 5229 | return -EINVAL; |
| 5230 | } |
| 5231 | mddev->major_version = info->major_version; |
| 5232 | mddev->minor_version = info->minor_version; |
| 5233 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5234 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | return 0; |
| 5236 | } |
| 5237 | mddev->major_version = MD_MAJOR_VERSION; |
| 5238 | mddev->minor_version = MD_MINOR_VERSION; |
| 5239 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5240 | mddev->ctime = get_seconds(); |
| 5241 | |
| 5242 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5243 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5244 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | mddev->raid_disks = info->raid_disks; |
| 5246 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5247 | * openned |
| 5248 | */ |
| 5249 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5250 | mddev->recovery_cp = MaxSector; |
| 5251 | else |
| 5252 | mddev->recovery_cp = 0; |
| 5253 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5254 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | |
| 5256 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5257 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | |
| 5259 | mddev->max_disks = MD_SB_DISKS; |
| 5260 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5261 | if (mddev->persistent) |
| 5262 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5263 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5265 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5266 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5267 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5268 | mddev->reshape_position = MaxSector; |
| 5269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | /* |
| 5271 | * Generate a 128 bit UUID |
| 5272 | */ |
| 5273 | get_random_bytes(mddev->uuid, 16); |
| 5274 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5275 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5276 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5277 | mddev->new_layout = mddev->layout; |
| 5278 | mddev->delta_disks = 0; |
| 5279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | return 0; |
| 5281 | } |
| 5282 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5283 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5284 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5285 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5286 | |
| 5287 | if (mddev->external_size) |
| 5288 | return; |
| 5289 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5290 | mddev->array_sectors = array_sectors; |
| 5291 | } |
| 5292 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5293 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5294 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5295 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5296 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5297 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5298 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5299 | |
| 5300 | if (mddev->pers->resize == NULL) |
| 5301 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5302 | /* The "num_sectors" is the number of sectors of each device that |
| 5303 | * is used. This can only make sense for arrays with redundancy. |
| 5304 | * linear and raid0 always use whatever space is available. We can only |
| 5305 | * consider changing this number if no resync or reconstruction is |
| 5306 | * 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] | 5307 | * 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] | 5308 | * of each device. If num_sectors is zero, we find the largest size |
| 5309 | * that fits. |
| 5310 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5311 | */ |
| 5312 | if (mddev->sync_thread) |
| 5313 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5314 | if (mddev->bitmap) |
| 5315 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5316 | * grow, and re-add. |
| 5317 | */ |
| 5318 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5319 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5320 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5321 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5322 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5323 | num_sectors = avail; |
| 5324 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5325 | return -ENOSPC; |
| 5326 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5327 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5328 | if (!rv) |
| 5329 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5330 | return rv; |
| 5331 | } |
| 5332 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5333 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5334 | { |
| 5335 | int rv; |
| 5336 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5337 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5338 | return -EINVAL; |
| 5339 | if (raid_disks <= 0 || |
| 5340 | raid_disks >= mddev->max_disks) |
| 5341 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5342 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5343 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5344 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5345 | |
| 5346 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5347 | return rv; |
| 5348 | } |
| 5349 | |
| 5350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | /* |
| 5352 | * update_array_info is used to change the configuration of an |
| 5353 | * on-line array. |
| 5354 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5355 | * fields in the info are checked against the array. |
| 5356 | * Any differences that cannot be handled will cause an error. |
| 5357 | * Normally, only one change can be managed at a time. |
| 5358 | */ |
| 5359 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5360 | { |
| 5361 | int rv = 0; |
| 5362 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5363 | int state = 0; |
| 5364 | |
| 5365 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5366 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5367 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | |
| 5369 | if (mddev->major_version != info->major_version || |
| 5370 | mddev->minor_version != info->minor_version || |
| 5371 | /* mddev->patch_version != info->patch_version || */ |
| 5372 | mddev->ctime != info->ctime || |
| 5373 | mddev->level != info->level || |
| 5374 | /* mddev->layout != info->layout || */ |
| 5375 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5376 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5377 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5378 | ((state^info->state) & 0xfffffe00) |
| 5379 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | return -EINVAL; |
| 5381 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5382 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5383 | cnt++; |
| 5384 | if (mddev->raid_disks != info->raid_disks) |
| 5385 | cnt++; |
| 5386 | if (mddev->layout != info->layout) |
| 5387 | cnt++; |
| 5388 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5389 | cnt++; |
| 5390 | if (cnt == 0) |
| 5391 | return 0; |
| 5392 | if (cnt > 1) |
| 5393 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | |
| 5395 | if (mddev->layout != info->layout) { |
| 5396 | /* Change layout |
| 5397 | * we don't need to do anything at the md level, the |
| 5398 | * personality will take care of it all. |
| 5399 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5400 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5402 | else { |
| 5403 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5404 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5405 | if (rv) |
| 5406 | mddev->new_layout = mddev->layout; |
| 5407 | return rv; |
| 5408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5410 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5411 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5413 | if (mddev->raid_disks != info->raid_disks) |
| 5414 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5416 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5417 | if (mddev->pers->quiesce == NULL) |
| 5418 | return -EINVAL; |
| 5419 | if (mddev->recovery || mddev->sync_thread) |
| 5420 | return -EBUSY; |
| 5421 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5422 | /* add the bitmap */ |
| 5423 | if (mddev->bitmap) |
| 5424 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5425 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5426 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5427 | mddev->bitmap_info.offset = |
| 5428 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5429 | mddev->pers->quiesce(mddev, 1); |
| 5430 | rv = bitmap_create(mddev); |
| 5431 | if (rv) |
| 5432 | bitmap_destroy(mddev); |
| 5433 | mddev->pers->quiesce(mddev, 0); |
| 5434 | } else { |
| 5435 | /* remove the bitmap */ |
| 5436 | if (!mddev->bitmap) |
| 5437 | return -ENOENT; |
| 5438 | if (mddev->bitmap->file) |
| 5439 | return -EINVAL; |
| 5440 | mddev->pers->quiesce(mddev, 1); |
| 5441 | bitmap_destroy(mddev); |
| 5442 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5443 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5444 | } |
| 5445 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5446 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | return rv; |
| 5448 | } |
| 5449 | |
| 5450 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5451 | { |
| 5452 | mdk_rdev_t *rdev; |
| 5453 | |
| 5454 | if (mddev->pers == NULL) |
| 5455 | return -ENODEV; |
| 5456 | |
| 5457 | rdev = find_rdev(mddev, dev); |
| 5458 | if (!rdev) |
| 5459 | return -ENODEV; |
| 5460 | |
| 5461 | md_error(mddev, rdev); |
| 5462 | return 0; |
| 5463 | } |
| 5464 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5465 | /* |
| 5466 | * We have a problem here : there is no easy way to give a CHS |
| 5467 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5468 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5469 | * dosfs just mad... ;-) |
| 5470 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5471 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5472 | { |
| 5473 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5474 | |
| 5475 | geo->heads = 2; |
| 5476 | geo->sectors = 4; |
| 5477 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5478 | return 0; |
| 5479 | } |
| 5480 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5481 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | unsigned int cmd, unsigned long arg) |
| 5483 | { |
| 5484 | int err = 0; |
| 5485 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | mddev_t *mddev = NULL; |
| 5487 | |
| 5488 | if (!capable(CAP_SYS_ADMIN)) |
| 5489 | return -EACCES; |
| 5490 | |
| 5491 | /* |
| 5492 | * Commands dealing with the RAID driver but not any |
| 5493 | * particular array: |
| 5494 | */ |
| 5495 | switch (cmd) |
| 5496 | { |
| 5497 | case RAID_VERSION: |
| 5498 | err = get_version(argp); |
| 5499 | goto done; |
| 5500 | |
| 5501 | case PRINT_RAID_DEBUG: |
| 5502 | err = 0; |
| 5503 | md_print_devices(); |
| 5504 | goto done; |
| 5505 | |
| 5506 | #ifndef MODULE |
| 5507 | case RAID_AUTORUN: |
| 5508 | err = 0; |
| 5509 | autostart_arrays(arg); |
| 5510 | goto done; |
| 5511 | #endif |
| 5512 | default:; |
| 5513 | } |
| 5514 | |
| 5515 | /* |
| 5516 | * Commands creating/starting a new array: |
| 5517 | */ |
| 5518 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5519 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | |
| 5521 | if (!mddev) { |
| 5522 | BUG(); |
| 5523 | goto abort; |
| 5524 | } |
| 5525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | err = mddev_lock(mddev); |
| 5527 | if (err) { |
| 5528 | printk(KERN_INFO |
| 5529 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5530 | err, cmd); |
| 5531 | goto abort; |
| 5532 | } |
| 5533 | |
| 5534 | switch (cmd) |
| 5535 | { |
| 5536 | case SET_ARRAY_INFO: |
| 5537 | { |
| 5538 | mdu_array_info_t info; |
| 5539 | if (!arg) |
| 5540 | memset(&info, 0, sizeof(info)); |
| 5541 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5542 | err = -EFAULT; |
| 5543 | goto abort_unlock; |
| 5544 | } |
| 5545 | if (mddev->pers) { |
| 5546 | err = update_array_info(mddev, &info); |
| 5547 | if (err) { |
| 5548 | printk(KERN_WARNING "md: couldn't update" |
| 5549 | " array info. %d\n", err); |
| 5550 | goto abort_unlock; |
| 5551 | } |
| 5552 | goto done_unlock; |
| 5553 | } |
| 5554 | if (!list_empty(&mddev->disks)) { |
| 5555 | printk(KERN_WARNING |
| 5556 | "md: array %s already has disks!\n", |
| 5557 | mdname(mddev)); |
| 5558 | err = -EBUSY; |
| 5559 | goto abort_unlock; |
| 5560 | } |
| 5561 | if (mddev->raid_disks) { |
| 5562 | printk(KERN_WARNING |
| 5563 | "md: array %s already initialised!\n", |
| 5564 | mdname(mddev)); |
| 5565 | err = -EBUSY; |
| 5566 | goto abort_unlock; |
| 5567 | } |
| 5568 | err = set_array_info(mddev, &info); |
| 5569 | if (err) { |
| 5570 | printk(KERN_WARNING "md: couldn't set" |
| 5571 | " array info. %d\n", err); |
| 5572 | goto abort_unlock; |
| 5573 | } |
| 5574 | } |
| 5575 | goto done_unlock; |
| 5576 | |
| 5577 | default:; |
| 5578 | } |
| 5579 | |
| 5580 | /* |
| 5581 | * Commands querying/configuring an existing array: |
| 5582 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5583 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5584 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5585 | if ((!mddev->raid_disks && !mddev->external) |
| 5586 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5587 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5588 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | err = -ENODEV; |
| 5590 | goto abort_unlock; |
| 5591 | } |
| 5592 | |
| 5593 | /* |
| 5594 | * Commands even a read-only array can execute: |
| 5595 | */ |
| 5596 | switch (cmd) |
| 5597 | { |
| 5598 | case GET_ARRAY_INFO: |
| 5599 | err = get_array_info(mddev, argp); |
| 5600 | goto done_unlock; |
| 5601 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5602 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5603 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5604 | goto done_unlock; |
| 5605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | case GET_DISK_INFO: |
| 5607 | err = get_disk_info(mddev, argp); |
| 5608 | goto done_unlock; |
| 5609 | |
| 5610 | case RESTART_ARRAY_RW: |
| 5611 | err = restart_array(mddev); |
| 5612 | goto done_unlock; |
| 5613 | |
| 5614 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5615 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | goto done_unlock; |
| 5617 | |
| 5618 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5619 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | goto done_unlock; |
| 5621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | /* |
| 5625 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5626 | * superblock, so we do not allow them on read-only arrays. |
| 5627 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5628 | * here and hit the 'default' below, so only disallow |
| 5629 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5631 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5632 | if (mddev->ro == 2) { |
| 5633 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5634 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5635 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5636 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5637 | } else { |
| 5638 | err = -EROFS; |
| 5639 | goto abort_unlock; |
| 5640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | switch (cmd) |
| 5644 | { |
| 5645 | case ADD_NEW_DISK: |
| 5646 | { |
| 5647 | mdu_disk_info_t info; |
| 5648 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5649 | err = -EFAULT; |
| 5650 | else |
| 5651 | err = add_new_disk(mddev, &info); |
| 5652 | goto done_unlock; |
| 5653 | } |
| 5654 | |
| 5655 | case HOT_REMOVE_DISK: |
| 5656 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5657 | goto done_unlock; |
| 5658 | |
| 5659 | case HOT_ADD_DISK: |
| 5660 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5661 | goto done_unlock; |
| 5662 | |
| 5663 | case SET_DISK_FAULTY: |
| 5664 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5665 | goto done_unlock; |
| 5666 | |
| 5667 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5668 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | goto done_unlock; |
| 5670 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5671 | case SET_BITMAP_FILE: |
| 5672 | err = set_bitmap_file(mddev, (int)arg); |
| 5673 | goto done_unlock; |
| 5674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | err = -EINVAL; |
| 5677 | goto abort_unlock; |
| 5678 | } |
| 5679 | |
| 5680 | done_unlock: |
| 5681 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5682 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5683 | err != -EINVAL) |
| 5684 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | mddev_unlock(mddev); |
| 5686 | |
| 5687 | return err; |
| 5688 | done: |
| 5689 | if (err) |
| 5690 | MD_BUG(); |
| 5691 | abort: |
| 5692 | return err; |
| 5693 | } |
| 5694 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5695 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | { |
| 5697 | /* |
| 5698 | * Succeed if we can lock the mddev, which confirms that |
| 5699 | * it isn't being stopped right now. |
| 5700 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5701 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | int err; |
| 5703 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5704 | if (mddev->gendisk != bdev->bd_disk) { |
| 5705 | /* we are racing with mddev_put which is discarding this |
| 5706 | * bd_disk. |
| 5707 | */ |
| 5708 | mddev_put(mddev); |
| 5709 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5710 | flush_scheduled_work(); |
| 5711 | /* Then retry the open from the top */ |
| 5712 | return -ERESTARTSYS; |
| 5713 | } |
| 5714 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5715 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5716 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | goto out; |
| 5718 | |
| 5719 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5720 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5721 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5723 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | out: |
| 5725 | return err; |
| 5726 | } |
| 5727 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5728 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5730 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5732 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5733 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | mddev_put(mddev); |
| 5735 | |
| 5736 | return 0; |
| 5737 | } |
| 5738 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5739 | static int md_media_changed(struct gendisk *disk) |
| 5740 | { |
| 5741 | mddev_t *mddev = disk->private_data; |
| 5742 | |
| 5743 | return mddev->changed; |
| 5744 | } |
| 5745 | |
| 5746 | static int md_revalidate(struct gendisk *disk) |
| 5747 | { |
| 5748 | mddev_t *mddev = disk->private_data; |
| 5749 | |
| 5750 | mddev->changed = 0; |
| 5751 | return 0; |
| 5752 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5753 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | { |
| 5755 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5756 | .open = md_open, |
| 5757 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5758 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5759 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5760 | .media_changed = md_media_changed, |
| 5761 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | }; |
| 5763 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5764 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | { |
| 5766 | mdk_thread_t *thread = arg; |
| 5767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | /* |
| 5769 | * md_thread is a 'system-thread', it's priority should be very |
| 5770 | * high. We avoid resource deadlocks individually in each |
| 5771 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5772 | * the very same RT priority as kswapd, thus we will never get |
| 5773 | * into a priority inversion deadlock. |
| 5774 | * |
| 5775 | * we definitely have to have equal or higher priority than |
| 5776 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5777 | * many dirty RAID5 blocks. |
| 5778 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5780 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5781 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5783 | /* We need to wait INTERRUPTIBLE so that |
| 5784 | * we don't add to the load-average. |
| 5785 | * That means we need to be sure no signals are |
| 5786 | * pending |
| 5787 | */ |
| 5788 | if (signal_pending(current)) |
| 5789 | flush_signals(current); |
| 5790 | |
| 5791 | wait_event_interruptible_timeout |
| 5792 | (thread->wqueue, |
| 5793 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5794 | || kthread_should_stop(), |
| 5795 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | |
| 5797 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5798 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5799 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5802 | return 0; |
| 5803 | } |
| 5804 | |
| 5805 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5806 | { |
| 5807 | if (thread) { |
| 5808 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5809 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5810 | wake_up(&thread->wqueue); |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5815 | const char *name) |
| 5816 | { |
| 5817 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5819 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | if (!thread) |
| 5821 | return NULL; |
| 5822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | init_waitqueue_head(&thread->wqueue); |
| 5824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | thread->run = run; |
| 5826 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5827 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5828 | thread->tsk = kthread_run(md_thread, thread, |
| 5829 | "%s_%s", |
| 5830 | mdname(thread->mddev), |
| 5831 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5832 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | kfree(thread); |
| 5834 | return NULL; |
| 5835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | return thread; |
| 5837 | } |
| 5838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | void md_unregister_thread(mdk_thread_t *thread) |
| 5840 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5841 | if (!thread) |
| 5842 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5843 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5844 | |
| 5845 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | kfree(thread); |
| 5847 | } |
| 5848 | |
| 5849 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5850 | { |
| 5851 | if (!mddev) { |
| 5852 | MD_BUG(); |
| 5853 | return; |
| 5854 | } |
| 5855 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5856 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5858 | |
| 5859 | if (mddev->external) |
| 5860 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5861 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5863 | mdname(mddev), |
| 5864 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5865 | __builtin_return_address(0),__builtin_return_address(1), |
| 5866 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5867 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5868 | if (!mddev->pers) |
| 5869 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | if (!mddev->pers->error_handler) |
| 5871 | return; |
| 5872 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5873 | if (mddev->degraded) |
| 5874 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5875 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5877 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5878 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5879 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | /* seq_file implementation /proc/mdstat */ |
| 5883 | |
| 5884 | static void status_unused(struct seq_file *seq) |
| 5885 | { |
| 5886 | int i = 0; |
| 5887 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | |
| 5889 | seq_printf(seq, "unused devices: "); |
| 5890 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5891 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | char b[BDEVNAME_SIZE]; |
| 5893 | i++; |
| 5894 | seq_printf(seq, "%s ", |
| 5895 | bdevname(rdev->bdev,b)); |
| 5896 | } |
| 5897 | if (!i) |
| 5898 | seq_printf(seq, "<none>"); |
| 5899 | |
| 5900 | seq_printf(seq, "\n"); |
| 5901 | } |
| 5902 | |
| 5903 | |
| 5904 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5905 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5906 | sector_t max_sectors, resync, res; |
| 5907 | unsigned long dt, db; |
| 5908 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5909 | int scale; |
| 5910 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5912 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | |
| 5914 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5915 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5917 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | |
| 5919 | /* |
| 5920 | * Should not happen. |
| 5921 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5922 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | MD_BUG(); |
| 5924 | return; |
| 5925 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5926 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5927 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5928 | * u32, as those are the requirements for sector_div. |
| 5929 | * Thus 'scale' must be at least 10 |
| 5930 | */ |
| 5931 | scale = 10; |
| 5932 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5933 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5934 | scale++; |
| 5935 | } |
| 5936 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5937 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5938 | |
| 5939 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5941 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | seq_printf(seq, "["); |
| 5943 | for (i = 0; i < x; i++) |
| 5944 | seq_printf(seq, "="); |
| 5945 | seq_printf(seq, ">"); |
| 5946 | for (i = 0; i < y; i++) |
| 5947 | seq_printf(seq, "."); |
| 5948 | seq_printf(seq, "] "); |
| 5949 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5950 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5951 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5952 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5953 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5954 | "check" : |
| 5955 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5956 | "resync" : "recovery"))), |
| 5957 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5958 | (unsigned long long) resync/2, |
| 5959 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | |
| 5961 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | * dt: time from mark until now |
| 5963 | * db: blocks written from mark until now |
| 5964 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5965 | * |
| 5966 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5967 | * So we divide before multiply in case it is 32bit and close |
| 5968 | * to the limit. |
| 5969 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5970 | * near the end of resync when the number of remaining sectors |
| 5971 | * is close to 'db'. |
| 5972 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5973 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | */ |
| 5975 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5976 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5977 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5978 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5980 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5981 | sector_div(rt, db/32+1); |
| 5982 | rt *= dt; |
| 5983 | rt >>= 5; |
| 5984 | |
| 5985 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5986 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5988 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5992 | { |
| 5993 | struct list_head *tmp; |
| 5994 | loff_t l = *pos; |
| 5995 | mddev_t *mddev; |
| 5996 | |
| 5997 | if (l >= 0x10000) |
| 5998 | return NULL; |
| 5999 | if (!l--) |
| 6000 | /* header */ |
| 6001 | return (void*)1; |
| 6002 | |
| 6003 | spin_lock(&all_mddevs_lock); |
| 6004 | list_for_each(tmp,&all_mddevs) |
| 6005 | if (!l--) { |
| 6006 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6007 | mddev_get(mddev); |
| 6008 | spin_unlock(&all_mddevs_lock); |
| 6009 | return mddev; |
| 6010 | } |
| 6011 | spin_unlock(&all_mddevs_lock); |
| 6012 | if (!l--) |
| 6013 | return (void*)2;/* tail */ |
| 6014 | return NULL; |
| 6015 | } |
| 6016 | |
| 6017 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6018 | { |
| 6019 | struct list_head *tmp; |
| 6020 | mddev_t *next_mddev, *mddev = v; |
| 6021 | |
| 6022 | ++*pos; |
| 6023 | if (v == (void*)2) |
| 6024 | return NULL; |
| 6025 | |
| 6026 | spin_lock(&all_mddevs_lock); |
| 6027 | if (v == (void*)1) |
| 6028 | tmp = all_mddevs.next; |
| 6029 | else |
| 6030 | tmp = mddev->all_mddevs.next; |
| 6031 | if (tmp != &all_mddevs) |
| 6032 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6033 | else { |
| 6034 | next_mddev = (void*)2; |
| 6035 | *pos = 0x10000; |
| 6036 | } |
| 6037 | spin_unlock(&all_mddevs_lock); |
| 6038 | |
| 6039 | if (v != (void*)1) |
| 6040 | mddev_put(mddev); |
| 6041 | return next_mddev; |
| 6042 | |
| 6043 | } |
| 6044 | |
| 6045 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6046 | { |
| 6047 | mddev_t *mddev = v; |
| 6048 | |
| 6049 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6050 | mddev_put(mddev); |
| 6051 | } |
| 6052 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6053 | struct mdstat_info { |
| 6054 | int event; |
| 6055 | }; |
| 6056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6058 | { |
| 6059 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6060 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6062 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6063 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | |
| 6065 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6066 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | seq_printf(seq, "Personalities : "); |
| 6068 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6069 | list_for_each_entry(pers, &pers_list, list) |
| 6070 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
| 6072 | spin_unlock(&pers_lock); |
| 6073 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6074 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | return 0; |
| 6076 | } |
| 6077 | if (v == (void*)2) { |
| 6078 | status_unused(seq); |
| 6079 | return 0; |
| 6080 | } |
| 6081 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6082 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6086 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6087 | mddev->pers ? "" : "in"); |
| 6088 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6089 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6091 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6092 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | seq_printf(seq, " %s", mddev->pers->name); |
| 6094 | } |
| 6095 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6096 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6097 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | char b[BDEVNAME_SIZE]; |
| 6099 | seq_printf(seq, " %s[%d]", |
| 6100 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6101 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6102 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6103 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | seq_printf(seq, "(F)"); |
| 6105 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6106 | } else if (rdev->raid_disk < 0) |
| 6107 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6108 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | if (!list_empty(&mddev->disks)) { |
| 6112 | if (mddev->pers) |
| 6113 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6114 | (unsigned long long) |
| 6115 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | else |
| 6117 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6118 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6120 | if (mddev->persistent) { |
| 6121 | if (mddev->major_version != 0 || |
| 6122 | mddev->minor_version != 90) { |
| 6123 | seq_printf(seq," super %d.%d", |
| 6124 | mddev->major_version, |
| 6125 | mddev->minor_version); |
| 6126 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6127 | } else if (mddev->external) |
| 6128 | seq_printf(seq, " super external:%s", |
| 6129 | mddev->metadata_type); |
| 6130 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6131 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | |
| 6133 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6134 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6136 | if (mddev->pers->sync_request) { |
| 6137 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6138 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6139 | seq_printf(seq, "\n "); |
| 6140 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6141 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6142 | else if (mddev->recovery_cp < MaxSector) |
| 6143 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6144 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6145 | } else |
| 6146 | seq_printf(seq, "\n "); |
| 6147 | |
| 6148 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6149 | unsigned long chunk_kb; |
| 6150 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6151 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6152 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6153 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6154 | "%lu%s chunk", |
| 6155 | bitmap->pages - bitmap->missing_pages, |
| 6156 | bitmap->pages, |
| 6157 | (bitmap->pages - bitmap->missing_pages) |
| 6158 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6159 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6160 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6161 | if (bitmap->file) { |
| 6162 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6163 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6164 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6165 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6166 | seq_printf(seq, "\n"); |
| 6167 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | } |
| 6169 | |
| 6170 | seq_printf(seq, "\n"); |
| 6171 | } |
| 6172 | mddev_unlock(mddev); |
| 6173 | |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6177 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | .start = md_seq_start, |
| 6179 | .next = md_seq_next, |
| 6180 | .stop = md_seq_stop, |
| 6181 | .show = md_seq_show, |
| 6182 | }; |
| 6183 | |
| 6184 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6185 | { |
| 6186 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6187 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6188 | if (mi == NULL) |
| 6189 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | |
| 6191 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6192 | if (error) |
| 6193 | kfree(mi); |
| 6194 | else { |
| 6195 | struct seq_file *p = file->private_data; |
| 6196 | p->private = mi; |
| 6197 | mi->event = atomic_read(&md_event_count); |
| 6198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | return error; |
| 6200 | } |
| 6201 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6202 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6203 | { |
| 6204 | struct seq_file *m = filp->private_data; |
| 6205 | struct mdstat_info *mi = m->private; |
| 6206 | int mask; |
| 6207 | |
| 6208 | poll_wait(filp, &md_event_waiters, wait); |
| 6209 | |
| 6210 | /* always allow read */ |
| 6211 | mask = POLLIN | POLLRDNORM; |
| 6212 | |
| 6213 | if (mi->event != atomic_read(&md_event_count)) |
| 6214 | mask |= POLLERR | POLLPRI; |
| 6215 | return mask; |
| 6216 | } |
| 6217 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6218 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6219 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | .open = md_seq_open, |
| 6221 | .read = seq_read, |
| 6222 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6223 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6224 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | }; |
| 6226 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6227 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6230 | list_add_tail(&p->list, &pers_list); |
| 6231 | 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] | 6232 | spin_unlock(&pers_lock); |
| 6233 | return 0; |
| 6234 | } |
| 6235 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6236 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6238 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6240 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | spin_unlock(&pers_lock); |
| 6242 | return 0; |
| 6243 | } |
| 6244 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6245 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | { |
| 6247 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6249 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | |
| 6251 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6252 | rcu_read_lock(); |
| 6253 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6255 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6256 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6257 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6258 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6259 | * as sync_io is counted when a request starts, and |
| 6260 | * disk_stats is counted when it completes. |
| 6261 | * So resync activity will cause curr_events to be smaller than |
| 6262 | * when there was no such activity. |
| 6263 | * non-sync IO will cause disk_stat to increase without |
| 6264 | * increasing sync_io so curr_events will (eventually) |
| 6265 | * be larger than it was before. Once it becomes |
| 6266 | * substantially larger, the test below will cause |
| 6267 | * the array to appear non-idle, and resync will slow |
| 6268 | * down. |
| 6269 | * If there is a lot of outstanding resync activity when |
| 6270 | * we set last_event to curr_events, then all that activity |
| 6271 | * completing might cause the array to appear non-idle |
| 6272 | * and resync will be slowed down even though there might |
| 6273 | * not have been non-resync activity. This will only |
| 6274 | * happen once though. 'last_events' will soon reflect |
| 6275 | * the state where there is little or no outstanding |
| 6276 | * resync requests, and further resync activity will |
| 6277 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6278 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6280 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | rdev->last_events = curr_events; |
| 6282 | idle = 0; |
| 6283 | } |
| 6284 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6285 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | return idle; |
| 6287 | } |
| 6288 | |
| 6289 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6290 | { |
| 6291 | /* another "blocks" (512byte) blocks have been synced */ |
| 6292 | atomic_sub(blocks, &mddev->recovery_active); |
| 6293 | wake_up(&mddev->recovery_wait); |
| 6294 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6295 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6296 | md_wakeup_thread(mddev->thread); |
| 6297 | // stop recovery, signal do_sync .... |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6302 | /* md_write_start(mddev, bi) |
| 6303 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6304 | * in superblock) before writing, schedule a superblock update |
| 6305 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6306 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6307 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6309 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6310 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6311 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6312 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6313 | BUG_ON(mddev->ro == 1); |
| 6314 | if (mddev->ro == 2) { |
| 6315 | /* need to switch to read/write */ |
| 6316 | mddev->ro = 0; |
| 6317 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6318 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6319 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6320 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6321 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6322 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6323 | if (mddev->safemode == 1) |
| 6324 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6325 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6326 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6327 | if (mddev->in_sync) { |
| 6328 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6329 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6330 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6331 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6332 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6333 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6334 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6335 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6336 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6337 | wait_event(mddev->sb_wait, |
| 6338 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6339 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | void md_write_end(mddev_t *mddev) |
| 6343 | { |
| 6344 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6345 | if (mddev->safemode == 2) |
| 6346 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6347 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6349 | } |
| 6350 | } |
| 6351 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6352 | /* md_allow_write(mddev) |
| 6353 | * Calling this ensures that the array is marked 'active' so that writes |
| 6354 | * may proceed without blocking. It is important to call this before |
| 6355 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6356 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6357 | * |
| 6358 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6359 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6360 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6361 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6362 | { |
| 6363 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6364 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6365 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6366 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6367 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6368 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6369 | |
| 6370 | spin_lock_irq(&mddev->write_lock); |
| 6371 | if (mddev->in_sync) { |
| 6372 | mddev->in_sync = 0; |
| 6373 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6374 | if (mddev->safemode_delay && |
| 6375 | mddev->safemode == 0) |
| 6376 | mddev->safemode = 1; |
| 6377 | spin_unlock_irq(&mddev->write_lock); |
| 6378 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6379 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6380 | } else |
| 6381 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6382 | |
| 6383 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6384 | return -EAGAIN; |
| 6385 | else |
| 6386 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6387 | } |
| 6388 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | #define SYNC_MARKS 10 |
| 6391 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6392 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | { |
| 6394 | mddev_t *mddev2; |
| 6395 | unsigned int currspeed = 0, |
| 6396 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6397 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | unsigned long mark[SYNC_MARKS]; |
| 6399 | sector_t mark_cnt[SYNC_MARKS]; |
| 6400 | int last_mark,m; |
| 6401 | struct list_head *tmp; |
| 6402 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6403 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6404 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6405 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | |
| 6407 | /* just incase thread restarts... */ |
| 6408 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6409 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6410 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6411 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6413 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6414 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6415 | desc = "data-check"; |
| 6416 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6417 | desc = "requested-resync"; |
| 6418 | else |
| 6419 | desc = "resync"; |
| 6420 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6421 | desc = "reshape"; |
| 6422 | else |
| 6423 | desc = "recovery"; |
| 6424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | /* we overload curr_resync somewhat here. |
| 6426 | * 0 == not engaged in resync at all |
| 6427 | * 2 == checking that there is no conflict with another sync |
| 6428 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6429 | * commense |
| 6430 | * other == active in resync - this many blocks |
| 6431 | * |
| 6432 | * Before starting a resync we must have set curr_resync to |
| 6433 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6434 | * less than ours. When we find one that is the same or higher |
| 6435 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6436 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6437 | * This will mean we have to start checking from the beginning again. |
| 6438 | * |
| 6439 | */ |
| 6440 | |
| 6441 | do { |
| 6442 | mddev->curr_resync = 2; |
| 6443 | |
| 6444 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6445 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6446 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | goto skip; |
| 6448 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6449 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | if (mddev2 == mddev) |
| 6451 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6452 | if (!mddev->parallel_resync |
| 6453 | && mddev2->curr_resync |
| 6454 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | DEFINE_WAIT(wq); |
| 6456 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6457 | /* arbitrarily yield */ |
| 6458 | mddev->curr_resync = 1; |
| 6459 | wake_up(&resync_wait); |
| 6460 | } |
| 6461 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6462 | /* no need to wait here, we can wait the next |
| 6463 | * time 'round when curr_resync == 2 |
| 6464 | */ |
| 6465 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6466 | /* We need to wait 'interruptible' so as not to |
| 6467 | * contribute to the load average, and not to |
| 6468 | * be caught by 'softlockup' |
| 6469 | */ |
| 6470 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6471 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6472 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6473 | printk(KERN_INFO "md: delaying %s of %s" |
| 6474 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6476 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6478 | if (signal_pending(current)) |
| 6479 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | schedule(); |
| 6481 | finish_wait(&resync_wait, &wq); |
| 6482 | goto try_again; |
| 6483 | } |
| 6484 | finish_wait(&resync_wait, &wq); |
| 6485 | } |
| 6486 | } |
| 6487 | } while (mddev->curr_resync < 2); |
| 6488 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6489 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6490 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6492 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | */ |
| 6494 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6495 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6496 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6497 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6498 | j = mddev->resync_min; |
| 6499 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6500 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6501 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6502 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6503 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6504 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6506 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6507 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6508 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6509 | if (rdev->raid_disk >= 0 && |
| 6510 | !test_bit(Faulty, &rdev->flags) && |
| 6511 | !test_bit(In_sync, &rdev->flags) && |
| 6512 | rdev->recovery_offset < j) |
| 6513 | j = rdev->recovery_offset; |
| 6514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6516 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6517 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6518 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6519 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6520 | "(but not more than %d KB/sec) for %s.\n", |
| 6521 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6523 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6524 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6525 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6527 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6528 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | } |
| 6530 | last_mark = 0; |
| 6531 | mddev->resync_mark = mark[last_mark]; |
| 6532 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6533 | |
| 6534 | /* |
| 6535 | * Tune reconstruction: |
| 6536 | */ |
| 6537 | window = 32*(PAGE_SIZE/512); |
| 6538 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6539 | window/2,(unsigned long long) max_sectors/2); |
| 6540 | |
| 6541 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | last_check = 0; |
| 6543 | |
| 6544 | if (j>2) { |
| 6545 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6546 | "md: resuming %s of %s from checkpoint.\n", |
| 6547 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | mddev->curr_resync = j; |
| 6549 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6550 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | |
| 6552 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6553 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6555 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6556 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6557 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6558 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6559 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6560 | > (max_sectors >> 4)) || |
| 6561 | (j - mddev->curr_resync_completed)*2 |
| 6562 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6563 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6564 | /* time to update curr_resync_completed */ |
| 6565 | blk_unplug(mddev->queue); |
| 6566 | wait_event(mddev->recovery_wait, |
| 6567 | atomic_read(&mddev->recovery_active) == 0); |
| 6568 | mddev->curr_resync_completed = |
| 6569 | mddev->curr_resync; |
| 6570 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6571 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6572 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6573 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6574 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6575 | /* As this condition is controlled by user-space, |
| 6576 | * we can block indefinitely, so use '_interruptible' |
| 6577 | * to avoid triggering warnings. |
| 6578 | */ |
| 6579 | flush_signals(current); /* just in case */ |
| 6580 | wait_event_interruptible(mddev->recovery_wait, |
| 6581 | mddev->resync_max > j |
| 6582 | || kthread_should_stop()); |
| 6583 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6584 | |
| 6585 | if (kthread_should_stop()) |
| 6586 | goto interrupted; |
| 6587 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6588 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6589 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6590 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6591 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | goto out; |
| 6593 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6594 | |
| 6595 | if (!skipped) { /* actual IO requested */ |
| 6596 | io_sectors += sectors; |
| 6597 | atomic_add(sectors, &mddev->recovery_active); |
| 6598 | } |
| 6599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | j += sectors; |
| 6601 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6602 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6603 | if (last_check == 0) |
| 6604 | /* this is the earliers that rebuilt will be |
| 6605 | * visible in /proc/mdstat |
| 6606 | */ |
| 6607 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6608 | |
| 6609 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | continue; |
| 6611 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6612 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6614 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | break; |
| 6616 | |
| 6617 | repeat: |
| 6618 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6619 | /* step marks */ |
| 6620 | int next = (last_mark+1) % SYNC_MARKS; |
| 6621 | |
| 6622 | mddev->resync_mark = mark[next]; |
| 6623 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6624 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6625 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | last_mark = next; |
| 6627 | } |
| 6628 | |
| 6629 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6630 | if (kthread_should_stop()) |
| 6631 | goto interrupted; |
| 6632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | |
| 6634 | /* |
| 6635 | * this loop exits only if either when we are slower than |
| 6636 | * the 'hard' speed limit, or the system was IO-idle for |
| 6637 | * a jiffy. |
| 6638 | * the system might be non-idle CPU-wise, but we only care |
| 6639 | * about not overloading the IO subsystem. (things like an |
| 6640 | * e2fsck being done on the RAID array should execute fast) |
| 6641 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6642 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | cond_resched(); |
| 6644 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6645 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6646 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6648 | if (currspeed > speed_min(mddev)) { |
| 6649 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6650 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6651 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | goto repeat; |
| 6653 | } |
| 6654 | } |
| 6655 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6656 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | /* |
| 6658 | * this also signals 'finished resyncing' to md_stop |
| 6659 | */ |
| 6660 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6661 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | |
| 6663 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6664 | |
| 6665 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6666 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6667 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6668 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6669 | mddev->curr_resync > 2) { |
| 6670 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6671 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6672 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6673 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6674 | "md: checkpointing %s of %s.\n", |
| 6675 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6676 | mddev->recovery_cp = mddev->curr_resync; |
| 6677 | } |
| 6678 | } else |
| 6679 | mddev->recovery_cp = MaxSector; |
| 6680 | } else { |
| 6681 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6682 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6683 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6684 | if (rdev->raid_disk >= 0 && |
| 6685 | !test_bit(Faulty, &rdev->flags) && |
| 6686 | !test_bit(In_sync, &rdev->flags) && |
| 6687 | rdev->recovery_offset < mddev->curr_resync) |
| 6688 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6691 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6693 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6694 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6695 | /* We completed so min/max setting can be forgotten if used. */ |
| 6696 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6697 | mddev->resync_min = 0; |
| 6698 | mddev->resync_max = MaxSector; |
| 6699 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6700 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6702 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6703 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6704 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | wake_up(&resync_wait); |
| 6706 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6707 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6708 | return; |
| 6709 | |
| 6710 | interrupted: |
| 6711 | /* |
| 6712 | * got a signal, exit. |
| 6713 | */ |
| 6714 | printk(KERN_INFO |
| 6715 | "md: md_do_sync() got signal ... exiting\n"); |
| 6716 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6717 | goto out; |
| 6718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6720 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6721 | |
| 6722 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6723 | static int remove_and_add_spares(mddev_t *mddev) |
| 6724 | { |
| 6725 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6726 | int spares = 0; |
| 6727 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6728 | mddev->curr_resync_completed = 0; |
| 6729 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6730 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6731 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6732 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6733 | (test_bit(Faulty, &rdev->flags) || |
| 6734 | ! test_bit(In_sync, &rdev->flags)) && |
| 6735 | atomic_read(&rdev->nr_pending)==0) { |
| 6736 | if (mddev->pers->hot_remove_disk( |
| 6737 | mddev, rdev->raid_disk)==0) { |
| 6738 | char nm[20]; |
| 6739 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6740 | sysfs_remove_link(&mddev->kobj, nm); |
| 6741 | rdev->raid_disk = -1; |
| 6742 | } |
| 6743 | } |
| 6744 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6745 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6746 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6747 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6748 | !test_bit(In_sync, &rdev->flags) && |
| 6749 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6750 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6751 | if (rdev->raid_disk < 0 |
| 6752 | && !test_bit(Faulty, &rdev->flags)) { |
| 6753 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6754 | if (mddev->pers-> |
| 6755 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6756 | char nm[20]; |
| 6757 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6758 | if (sysfs_create_link(&mddev->kobj, |
| 6759 | &rdev->kobj, nm)) |
| 6760 | printk(KERN_WARNING |
| 6761 | "md: cannot register " |
| 6762 | "%s for %s\n", |
| 6763 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6764 | spares++; |
| 6765 | md_new_event(mddev); |
| 6766 | } else |
| 6767 | break; |
| 6768 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6769 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6770 | } |
| 6771 | return spares; |
| 6772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | /* |
| 6774 | * This routine is regularly called by all per-raid-array threads to |
| 6775 | * deal with generic issues like resync and super-block update. |
| 6776 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6777 | * need this as they never do any recovery or update the superblock. |
| 6778 | * |
| 6779 | * It does not do any resync itself, but rather "forks" off other threads |
| 6780 | * to do that as needed. |
| 6781 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6782 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6783 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | * and wakeups up this thread which will reap the thread and finish up. |
| 6785 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6786 | * |
| 6787 | * The overall approach is: |
| 6788 | * 1/ if the superblock needs updating, update it. |
| 6789 | * 2/ If a recovery thread is running, don't do anything else. |
| 6790 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6791 | * 4/ If there are any faulty devices, remove them. |
| 6792 | * 5/ If array is degraded, try to add spares devices |
| 6793 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6794 | */ |
| 6795 | void md_check_recovery(mddev_t *mddev) |
| 6796 | { |
| 6797 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | |
| 6799 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6800 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6801 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
| 6803 | if (mddev->ro) |
| 6804 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6805 | |
| 6806 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6807 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6808 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6809 | mdname(mddev)); |
| 6810 | mddev->safemode = 2; |
| 6811 | } |
| 6812 | flush_signals(current); |
| 6813 | } |
| 6814 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6815 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6816 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6818 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6820 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6821 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6822 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6823 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | )) |
| 6825 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6826 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6827 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6828 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6829 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6830 | if (mddev->ro) { |
| 6831 | /* Only thing we do on a ro array is remove |
| 6832 | * failed devices. |
| 6833 | */ |
| 6834 | remove_and_add_spares(mddev); |
| 6835 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6836 | goto unlock; |
| 6837 | } |
| 6838 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6839 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6840 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6841 | spin_lock_irq(&mddev->write_lock); |
| 6842 | if (mddev->safemode && |
| 6843 | !atomic_read(&mddev->writes_pending) && |
| 6844 | !mddev->in_sync && |
| 6845 | mddev->recovery_cp == MaxSector) { |
| 6846 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6847 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6848 | if (mddev->persistent) |
| 6849 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6850 | } |
| 6851 | if (mddev->safemode == 1) |
| 6852 | mddev->safemode = 0; |
| 6853 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6854 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6855 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6856 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6857 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6858 | if (mddev->flags) |
| 6859 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6860 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6861 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6862 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6863 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6864 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6867 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6868 | /* resync/recovery still happening */ |
| 6869 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6870 | goto unlock; |
| 6871 | } |
| 6872 | if (mddev->sync_thread) { |
| 6873 | /* resync has finished, collect result */ |
| 6874 | md_unregister_thread(mddev->sync_thread); |
| 6875 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6876 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6877 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | /* success...*/ |
| 6879 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6880 | if (mddev->pers->spare_active(mddev)) |
| 6881 | sysfs_notify(&mddev->kobj, NULL, |
| 6882 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6884 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6885 | mddev->pers->finish_reshape) |
| 6886 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6887 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6888 | |
| 6889 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6890 | * information must be scrapped |
| 6891 | */ |
| 6892 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6893 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6894 | rdev->saved_raid_disk = -1; |
| 6895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | mddev->recovery = 0; |
| 6897 | /* flag recovery needed just to double check */ |
| 6898 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6899 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6900 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | goto unlock; |
| 6902 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6903 | /* Set RUNNING before clearing NEEDED to avoid |
| 6904 | * any transients in the value of "sync_action". |
| 6905 | */ |
| 6906 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6907 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6908 | /* Clear some bits that don't mean anything, but |
| 6909 | * might be left set |
| 6910 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6911 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6912 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6914 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6915 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | /* no recovery is running. |
| 6917 | * remove any failed drives, then |
| 6918 | * add spares if possible. |
| 6919 | * Spare are also removed and re-added, to allow |
| 6920 | * the personality to fail the re-add. |
| 6921 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6922 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6923 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6924 | if (mddev->pers->check_reshape == NULL || |
| 6925 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6926 | /* Cannot proceed */ |
| 6927 | goto unlock; |
| 6928 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6929 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6930 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6931 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6932 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6933 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6934 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6935 | } else if (mddev->recovery_cp < MaxSector) { |
| 6936 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6937 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6938 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6939 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6943 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6944 | /* We are adding a device or devices to an array |
| 6945 | * which has the bitmap stored on all devices. |
| 6946 | * So make sure all bitmap pages get written |
| 6947 | */ |
| 6948 | bitmap_write_all(mddev->bitmap); |
| 6949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6951 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6952 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | if (!mddev->sync_thread) { |
| 6954 | printk(KERN_ERR "%s: could not start resync" |
| 6955 | " thread...\n", |
| 6956 | mdname(mddev)); |
| 6957 | /* leave the spares where they are, it shouldn't hurt */ |
| 6958 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6959 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6961 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6962 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | } |
| 6964 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6965 | if (!mddev->sync_thread) { |
| 6966 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6967 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6968 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6969 | if (mddev->sysfs_action) |
| 6970 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | mddev_unlock(mddev); |
| 6973 | } |
| 6974 | } |
| 6975 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6976 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6977 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6978 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6979 | wait_event_timeout(rdev->blocked_wait, |
| 6980 | !test_bit(Blocked, &rdev->flags), |
| 6981 | msecs_to_jiffies(5000)); |
| 6982 | rdev_dec_pending(rdev, mddev); |
| 6983 | } |
| 6984 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6985 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6986 | static int md_notify_reboot(struct notifier_block *this, |
| 6987 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | { |
| 6989 | struct list_head *tmp; |
| 6990 | mddev_t *mddev; |
| 6991 | |
| 6992 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6993 | |
| 6994 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6995 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6996 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6997 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6998 | /* Force a switch to readonly even array |
| 6999 | * appears to still be in use. Hence |
| 7000 | * the '100'. |
| 7001 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7002 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7003 | mddev_unlock(mddev); |
| 7004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | /* |
| 7006 | * certain more exotic SCSI devices are known to be |
| 7007 | * volatile wrt too early system reboots. While the |
| 7008 | * right place to handle this issue is the given |
| 7009 | * driver, we do want to have a safe RAID driver ... |
| 7010 | */ |
| 7011 | mdelay(1000*1); |
| 7012 | } |
| 7013 | return NOTIFY_DONE; |
| 7014 | } |
| 7015 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7016 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | .notifier_call = md_notify_reboot, |
| 7018 | .next = NULL, |
| 7019 | .priority = INT_MAX, /* before any real devices */ |
| 7020 | }; |
| 7021 | |
| 7022 | static void md_geninit(void) |
| 7023 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7025 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7026 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7027 | } |
| 7028 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7029 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7031 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | return -1; |
| 7033 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7034 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | return -1; |
| 7036 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7037 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7038 | md_probe, NULL, NULL); |
| 7039 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | md_probe, NULL, NULL); |
| 7041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7043 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | |
| 7045 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7046 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | |
| 7050 | #ifndef MODULE |
| 7051 | |
| 7052 | /* |
| 7053 | * Searches all registered partitions for autorun RAID arrays |
| 7054 | * at boot time. |
| 7055 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7056 | |
| 7057 | static LIST_HEAD(all_detected_devices); |
| 7058 | struct detected_devices_node { |
| 7059 | struct list_head list; |
| 7060 | dev_t dev; |
| 7061 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | |
| 7063 | void md_autodetect_dev(dev_t dev) |
| 7064 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7065 | struct detected_devices_node *node_detected_dev; |
| 7066 | |
| 7067 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7068 | if (node_detected_dev) { |
| 7069 | node_detected_dev->dev = dev; |
| 7070 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7071 | } else { |
| 7072 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7073 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | |
| 7078 | static void autostart_arrays(int part) |
| 7079 | { |
| 7080 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7081 | struct detected_devices_node *node_detected_dev; |
| 7082 | dev_t dev; |
| 7083 | int i_scanned, i_passed; |
| 7084 | |
| 7085 | i_scanned = 0; |
| 7086 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | |
| 7088 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7089 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7090 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7091 | i_scanned++; |
| 7092 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7093 | struct detected_devices_node, list); |
| 7094 | list_del(&node_detected_dev->list); |
| 7095 | dev = node_detected_dev->dev; |
| 7096 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7097 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7098 | if (IS_ERR(rdev)) |
| 7099 | continue; |
| 7100 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7101 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | MD_BUG(); |
| 7103 | continue; |
| 7104 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7105 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7106 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7107 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7109 | |
| 7110 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7111 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | |
| 7113 | autorun_devices(part); |
| 7114 | } |
| 7115 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7116 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | |
| 7118 | static __exit void md_exit(void) |
| 7119 | { |
| 7120 | mddev_t *mddev; |
| 7121 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7122 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7123 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7124 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7126 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | unregister_blkdev(mdp_major, "mdp"); |
| 7128 | unregister_reboot_notifier(&md_notifier); |
| 7129 | unregister_sysctl_table(raid_table_header); |
| 7130 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7131 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7133 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | } |
| 7135 | } |
| 7136 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7137 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | module_exit(md_exit) |
| 7139 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7140 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7141 | { |
| 7142 | return sprintf(buffer, "%d", start_readonly); |
| 7143 | } |
| 7144 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7145 | { |
| 7146 | char *e; |
| 7147 | int num = simple_strtoul(val, &e, 10); |
| 7148 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7149 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7150 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7151 | } |
| 7152 | return -EINVAL; |
| 7153 | } |
| 7154 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7155 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7156 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7157 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7158 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | EXPORT_SYMBOL(register_md_personality); |
| 7161 | EXPORT_SYMBOL(unregister_md_personality); |
| 7162 | EXPORT_SYMBOL(md_error); |
| 7163 | EXPORT_SYMBOL(md_done_sync); |
| 7164 | EXPORT_SYMBOL(md_write_start); |
| 7165 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | EXPORT_SYMBOL(md_register_thread); |
| 7167 | EXPORT_SYMBOL(md_unregister_thread); |
| 7168 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7169 | EXPORT_SYMBOL(md_check_recovery); |
| 7170 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7171 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7172 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7173 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |