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> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
| 46 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 48 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 49 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include <linux/raid/md_p.h> |
| 51 | #include <linux/raid/md_u.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 53 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #define DEBUG 0 |
| 56 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 57 | |
| 58 | |
| 59 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 60 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 63 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(pers_lock); |
| 65 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 66 | static void md_print_devices(void); |
| 67 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 68 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 69 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 70 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 73 | * Default number of read corrections we'll attempt on an rdev |
| 74 | * before ejecting it from the array. We divide the read error |
| 75 | * count by 2 for every hour elapsed between read errors. |
| 76 | */ |
| 77 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 78 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 80 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 81 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 82 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 84 | * speed limit - in case reconstruction slows down your system despite |
| 85 | * idle IO detection. |
| 86 | * |
| 87 | * 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] | 88 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | static int sysctl_speed_limit_min = 1000; |
| 92 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 93 | static inline int speed_min(mddev_t *mddev) |
| 94 | { |
| 95 | return mddev->sync_speed_min ? |
| 96 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 97 | } |
| 98 | |
| 99 | static inline int speed_max(mddev_t *mddev) |
| 100 | { |
| 101 | return mddev->sync_speed_max ? |
| 102 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | static struct ctl_table_header *raid_table_header; |
| 106 | |
| 107 | static ctl_table raid_table[] = { |
| 108 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .procname = "speed_limit_min", |
| 110 | .data = &sysctl_speed_limit_min, |
| 111 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 112 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 113 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }, |
| 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .procname = "speed_limit_max", |
| 117 | .data = &sysctl_speed_limit_max, |
| 118 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 119 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 120 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 122 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static ctl_table raid_dir_table[] = { |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "raid", |
| 128 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 129 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .child = raid_table, |
| 131 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 132 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static ctl_table raid_root_table[] = { |
| 136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .procname = "dev", |
| 138 | .maxlen = 0, |
| 139 | .mode = 0555, |
| 140 | .child = raid_dir_table, |
| 141 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 142 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 145 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 147 | static int start_readonly; |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 150 | * We have a system wide 'event count' that is incremented |
| 151 | * on any 'interesting' event, and readers of /proc/mdstat |
| 152 | * can use 'poll' or 'select' to find out when the event |
| 153 | * count increases. |
| 154 | * |
| 155 | * Events are: |
| 156 | * start array, stop array, error, add device, remove device, |
| 157 | * start build, activate spare |
| 158 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 159 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 160 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 161 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 162 | { |
| 163 | atomic_inc(&md_event_count); |
| 164 | wake_up(&md_event_waiters); |
| 165 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 166 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 168 | /* Alternate version that can be called from interrupts |
| 169 | * when calling sysfs_notify isn't needed. |
| 170 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 171 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 172 | { |
| 173 | atomic_inc(&md_event_count); |
| 174 | wake_up(&md_event_waiters); |
| 175 | } |
| 176 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * Enables to iterate over all existing md arrays |
| 179 | * all_mddevs_lock protects this list. |
| 180 | */ |
| 181 | static LIST_HEAD(all_mddevs); |
| 182 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * iterates through all used mddevs in the system. |
| 187 | * We take care to grab the all_mddevs_lock whenever navigating |
| 188 | * the list, and to always hold a refcount when unlocked. |
| 189 | * Any code which breaks out of this loop while own |
| 190 | * a reference to the current mddev and must mddev_put it. |
| 191 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 192 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | \ |
| 194 | for (({ spin_lock(&all_mddevs_lock); \ |
| 195 | tmp = all_mddevs.next; \ |
| 196 | mddev = NULL;}); \ |
| 197 | ({ if (tmp != &all_mddevs) \ |
| 198 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 199 | spin_unlock(&all_mddevs_lock); \ |
| 200 | if (mddev) mddev_put(mddev); \ |
| 201 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 202 | tmp != &all_mddevs;}); \ |
| 203 | ({ spin_lock(&all_mddevs_lock); \ |
| 204 | tmp = tmp->next;}) \ |
| 205 | ) |
| 206 | |
| 207 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 208 | /* Rather than calling directly into the personality make_request function, |
| 209 | * IO requests come here first so that we can check if the device is |
| 210 | * being suspended pending a reconfiguration. |
| 211 | * We hold a refcount over the call to ->make_request. By the time that |
| 212 | * call has finished, the bio has been linked into some internal structure |
| 213 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 214 | */ |
| 215 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 217 | mddev_t *mddev = q->queuedata; |
| 218 | int rv; |
| 219 | if (mddev == NULL || mddev->pers == NULL) { |
| 220 | bio_io_error(bio); |
| 221 | return 0; |
| 222 | } |
| 223 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 224 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 225 | DEFINE_WAIT(__wait); |
| 226 | for (;;) { |
| 227 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 228 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 229 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 230 | break; |
| 231 | rcu_read_unlock(); |
| 232 | schedule(); |
| 233 | rcu_read_lock(); |
| 234 | } |
| 235 | finish_wait(&mddev->sb_wait, &__wait); |
| 236 | } |
| 237 | atomic_inc(&mddev->active_io); |
| 238 | rcu_read_unlock(); |
| 239 | rv = mddev->pers->make_request(q, bio); |
| 240 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 241 | wake_up(&mddev->sb_wait); |
| 242 | |
| 243 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 246 | static void mddev_suspend(mddev_t *mddev) |
| 247 | { |
| 248 | BUG_ON(mddev->suspended); |
| 249 | mddev->suspended = 1; |
| 250 | synchronize_rcu(); |
| 251 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 252 | mddev->pers->quiesce(mddev, 1); |
| 253 | md_unregister_thread(mddev->thread); |
| 254 | mddev->thread = NULL; |
| 255 | /* we now know that no code is executing in the personality module, |
| 256 | * except possibly the tail end of a ->bi_end_io function, but that |
| 257 | * is certain to complete before the module has a chance to get |
| 258 | * unloaded |
| 259 | */ |
| 260 | } |
| 261 | |
| 262 | static void mddev_resume(mddev_t *mddev) |
| 263 | { |
| 264 | mddev->suspended = 0; |
| 265 | wake_up(&mddev->sb_wait); |
| 266 | mddev->pers->quiesce(mddev, 0); |
| 267 | } |
| 268 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 269 | int mddev_congested(mddev_t *mddev, int bits) |
| 270 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 271 | if (mddev->barrier) |
| 272 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 273 | return mddev->suspended; |
| 274 | } |
| 275 | EXPORT_SYMBOL(mddev_congested); |
| 276 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 277 | /* |
| 278 | * Generic barrier handling for md |
| 279 | */ |
| 280 | |
| 281 | #define POST_REQUEST_BARRIER ((void*)1) |
| 282 | |
| 283 | static void md_end_barrier(struct bio *bio, int err) |
| 284 | { |
| 285 | mdk_rdev_t *rdev = bio->bi_private; |
| 286 | mddev_t *mddev = rdev->mddev; |
| 287 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 288 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 289 | |
| 290 | rdev_dec_pending(rdev, mddev); |
| 291 | |
| 292 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 293 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 294 | /* This was a post-request barrier */ |
| 295 | mddev->barrier = NULL; |
| 296 | wake_up(&mddev->sb_wait); |
| 297 | } else |
| 298 | /* The pre-request barrier has finished */ |
| 299 | schedule_work(&mddev->barrier_work); |
| 300 | } |
| 301 | bio_put(bio); |
| 302 | } |
| 303 | |
| 304 | static void submit_barriers(mddev_t *mddev) |
| 305 | { |
| 306 | mdk_rdev_t *rdev; |
| 307 | |
| 308 | rcu_read_lock(); |
| 309 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 310 | if (rdev->raid_disk >= 0 && |
| 311 | !test_bit(Faulty, &rdev->flags)) { |
| 312 | /* Take two references, one is dropped |
| 313 | * when request finishes, one after |
| 314 | * we reclaim rcu_read_lock |
| 315 | */ |
| 316 | struct bio *bi; |
| 317 | atomic_inc(&rdev->nr_pending); |
| 318 | atomic_inc(&rdev->nr_pending); |
| 319 | rcu_read_unlock(); |
| 320 | bi = bio_alloc(GFP_KERNEL, 0); |
| 321 | bi->bi_end_io = md_end_barrier; |
| 322 | bi->bi_private = rdev; |
| 323 | bi->bi_bdev = rdev->bdev; |
| 324 | atomic_inc(&mddev->flush_pending); |
| 325 | submit_bio(WRITE_BARRIER, bi); |
| 326 | rcu_read_lock(); |
| 327 | rdev_dec_pending(rdev, mddev); |
| 328 | } |
| 329 | rcu_read_unlock(); |
| 330 | } |
| 331 | |
| 332 | static void md_submit_barrier(struct work_struct *ws) |
| 333 | { |
| 334 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 335 | struct bio *bio = mddev->barrier; |
| 336 | |
| 337 | atomic_set(&mddev->flush_pending, 1); |
| 338 | |
| 339 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 340 | bio_endio(bio, -EOPNOTSUPP); |
| 341 | else if (bio->bi_size == 0) |
| 342 | /* an empty barrier - all done */ |
| 343 | bio_endio(bio, 0); |
| 344 | else { |
| 345 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
| 346 | if (mddev->pers->make_request(mddev->queue, bio)) |
| 347 | generic_make_request(bio); |
| 348 | mddev->barrier = POST_REQUEST_BARRIER; |
| 349 | submit_barriers(mddev); |
| 350 | } |
| 351 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 352 | mddev->barrier = NULL; |
| 353 | wake_up(&mddev->sb_wait); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 358 | { |
| 359 | spin_lock_irq(&mddev->write_lock); |
| 360 | wait_event_lock_irq(mddev->sb_wait, |
| 361 | !mddev->barrier, |
| 362 | mddev->write_lock, /*nothing*/); |
| 363 | mddev->barrier = bio; |
| 364 | spin_unlock_irq(&mddev->write_lock); |
| 365 | |
| 366 | atomic_set(&mddev->flush_pending, 1); |
| 367 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 368 | |
| 369 | submit_barriers(mddev); |
| 370 | |
| 371 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 372 | schedule_work(&mddev->barrier_work); |
| 373 | } |
| 374 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 377 | { |
| 378 | atomic_inc(&mddev->active); |
| 379 | return mddev; |
| 380 | } |
| 381 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 382 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | static void mddev_put(mddev_t *mddev) |
| 385 | { |
| 386 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 387 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 388 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 389 | mddev->ctime == 0 && !mddev->hold_active) { |
| 390 | /* Array is not configured at all, and not held active, |
| 391 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 393 | if (mddev->gendisk) { |
| 394 | /* we did a probe so need to clean up. |
| 395 | * Call schedule_work inside the spinlock |
| 396 | * so that flush_scheduled_work() after |
| 397 | * mddev_find will succeed in waiting for the |
| 398 | * work to be done. |
| 399 | */ |
| 400 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 401 | schedule_work(&mddev->del_work); |
| 402 | } else |
| 403 | kfree(mddev); |
| 404 | } |
| 405 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static mddev_t * mddev_find(dev_t unit) |
| 409 | { |
| 410 | mddev_t *mddev, *new = NULL; |
| 411 | |
| 412 | retry: |
| 413 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 415 | if (unit) { |
| 416 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 417 | if (mddev->unit == unit) { |
| 418 | mddev_get(mddev); |
| 419 | spin_unlock(&all_mddevs_lock); |
| 420 | kfree(new); |
| 421 | return mddev; |
| 422 | } |
| 423 | |
| 424 | if (new) { |
| 425 | list_add(&new->all_mddevs, &all_mddevs); |
| 426 | spin_unlock(&all_mddevs_lock); |
| 427 | new->hold_active = UNTIL_IOCTL; |
| 428 | return new; |
| 429 | } |
| 430 | } else if (new) { |
| 431 | /* find an unused unit number */ |
| 432 | static int next_minor = 512; |
| 433 | int start = next_minor; |
| 434 | int is_free = 0; |
| 435 | int dev = 0; |
| 436 | while (!is_free) { |
| 437 | dev = MKDEV(MD_MAJOR, next_minor); |
| 438 | next_minor++; |
| 439 | if (next_minor > MINORMASK) |
| 440 | next_minor = 0; |
| 441 | if (next_minor == start) { |
| 442 | /* Oh dear, all in use. */ |
| 443 | spin_unlock(&all_mddevs_lock); |
| 444 | kfree(new); |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | is_free = 1; |
| 449 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 450 | if (mddev->unit == dev) { |
| 451 | is_free = 0; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | new->unit = dev; |
| 456 | new->md_minor = MINOR(dev); |
| 457 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | list_add(&new->all_mddevs, &all_mddevs); |
| 459 | spin_unlock(&all_mddevs_lock); |
| 460 | return new; |
| 461 | } |
| 462 | spin_unlock(&all_mddevs_lock); |
| 463 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 464 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (!new) |
| 466 | return NULL; |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | new->unit = unit; |
| 469 | if (MAJOR(unit) == MD_MAJOR) |
| 470 | new->md_minor = MINOR(unit); |
| 471 | else |
| 472 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 473 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 474 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 475 | mutex_init(&new->reconfig_mutex); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 476 | mutex_init(&new->bitmap_info.mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | INIT_LIST_HEAD(&new->disks); |
| 478 | INIT_LIST_HEAD(&new->all_mddevs); |
| 479 | init_timer(&new->safemode_timer); |
| 480 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 481 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 482 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 483 | spin_lock_init(&new->write_lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 484 | atomic_set(&new->flush_pending, 0); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 485 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 486 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 487 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 488 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 489 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 490 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | goto retry; |
| 493 | } |
| 494 | |
| 495 | static inline int mddev_lock(mddev_t * mddev) |
| 496 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 497 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 500 | static inline int mddev_is_locked(mddev_t *mddev) |
| 501 | { |
| 502 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 503 | } |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | static inline int mddev_trylock(mddev_t * mddev) |
| 506 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 507 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 510 | static struct attribute_group md_redundancy_group; |
| 511 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 512 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 514 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 515 | /* These cannot be removed under reconfig_mutex as |
| 516 | * an access to the files will try to take reconfig_mutex |
| 517 | * while holding the file unremovable, which leads to |
| 518 | * a deadlock. |
| 519 | * So hold open_mutex instead - we are allowed to take |
| 520 | * it while holding reconfig_mutex, and md_run can |
| 521 | * use it to wait for the remove to complete. |
| 522 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 523 | struct attribute_group *to_remove = mddev->to_remove; |
| 524 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 525 | mutex_lock(&mddev->open_mutex); |
| 526 | mutex_unlock(&mddev->reconfig_mutex); |
| 527 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 528 | if (to_remove != &md_redundancy_group) |
| 529 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 530 | if (mddev->pers == NULL || |
| 531 | mddev->pers->sync_request == NULL) { |
| 532 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 533 | if (mddev->sysfs_action) |
| 534 | sysfs_put(mddev->sysfs_action); |
| 535 | mddev->sysfs_action = NULL; |
| 536 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 537 | mutex_unlock(&mddev->open_mutex); |
| 538 | } else |
| 539 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 541 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 544 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 546 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 548 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (rdev->desc_nr == nr) |
| 550 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 556 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | mdk_rdev_t *rdev; |
| 558 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 559 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (rdev->bdev->bd_dev == dev) |
| 561 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return NULL; |
| 564 | } |
| 565 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 566 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 567 | { |
| 568 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 569 | list_for_each_entry(pers, &pers_list, list) { |
| 570 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 571 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 572 | if (strcmp(pers->name, clevel)==0) |
| 573 | return pers; |
| 574 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 575 | return NULL; |
| 576 | } |
| 577 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 578 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 579 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 581 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 582 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 586 | { |
| 587 | if (rdev->sb_page) |
| 588 | MD_BUG(); |
| 589 | |
| 590 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 591 | if (!rdev->sb_page) { |
| 592 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 593 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 600 | { |
| 601 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 602 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | rdev->sb_loaded = 0; |
| 604 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 605 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 606 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 611 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 612 | { |
| 613 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 614 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 615 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 616 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 617 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 618 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 619 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 620 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 621 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 622 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 623 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 624 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 625 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 626 | } |
| 627 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 628 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 629 | { |
| 630 | struct bio *bio2 = bio->bi_private; |
| 631 | mdk_rdev_t *rdev = bio2->bi_private; |
| 632 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 633 | |
| 634 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 635 | error == -EOPNOTSUPP) { |
| 636 | unsigned long flags; |
| 637 | /* barriers don't appear to be supported :-( */ |
| 638 | set_bit(BarriersNotsupp, &rdev->flags); |
| 639 | mddev->barriers_work = 0; |
| 640 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 641 | bio2->bi_next = mddev->biolist; |
| 642 | mddev->biolist = bio2; |
| 643 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 644 | wake_up(&mddev->sb_wait); |
| 645 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 646 | } else { |
| 647 | bio_put(bio2); |
| 648 | bio->bi_private = rdev; |
| 649 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 650 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 651 | } |
| 652 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 653 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 654 | sector_t sector, int size, struct page *page) |
| 655 | { |
| 656 | /* write first size bytes of page to sector of rdev |
| 657 | * Increment mddev->pending_writes before returning |
| 658 | * and decrement it on completion, waking up sb_wait |
| 659 | * if zero is reached. |
| 660 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 661 | * |
| 662 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 663 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 664 | */ |
| 665 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 666 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 667 | |
| 668 | bio->bi_bdev = rdev->bdev; |
| 669 | bio->bi_sector = sector; |
| 670 | bio_add_page(bio, page, size, 0); |
| 671 | bio->bi_private = rdev; |
| 672 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 673 | bio->bi_rw = rw; |
| 674 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 675 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 676 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 677 | struct bio *rbio; |
| 678 | rw |= (1<<BIO_RW_BARRIER); |
| 679 | rbio = bio_clone(bio, GFP_NOIO); |
| 680 | rbio->bi_private = bio; |
| 681 | rbio->bi_end_io = super_written_barrier; |
| 682 | submit_bio(rw, rbio); |
| 683 | } else |
| 684 | submit_bio(rw, bio); |
| 685 | } |
| 686 | |
| 687 | void md_super_wait(mddev_t *mddev) |
| 688 | { |
| 689 | /* wait for all superblock writes that were scheduled to complete. |
| 690 | * if any had to be retried (due to BARRIER problems), retry them |
| 691 | */ |
| 692 | DEFINE_WAIT(wq); |
| 693 | for(;;) { |
| 694 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 695 | if (atomic_read(&mddev->pending_writes)==0) |
| 696 | break; |
| 697 | while (mddev->biolist) { |
| 698 | struct bio *bio; |
| 699 | spin_lock_irq(&mddev->write_lock); |
| 700 | bio = mddev->biolist; |
| 701 | mddev->biolist = bio->bi_next ; |
| 702 | bio->bi_next = NULL; |
| 703 | spin_unlock_irq(&mddev->write_lock); |
| 704 | submit_bio(bio->bi_rw, bio); |
| 705 | } |
| 706 | schedule(); |
| 707 | } |
| 708 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 709 | } |
| 710 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 711 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 716 | 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] | 717 | struct page *page, int rw) |
| 718 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 719 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | struct completion event; |
| 721 | int ret; |
| 722 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 723 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | bio->bi_bdev = bdev; |
| 726 | bio->bi_sector = sector; |
| 727 | bio_add_page(bio, page, size, 0); |
| 728 | init_completion(&event); |
| 729 | bio->bi_private = &event; |
| 730 | bio->bi_end_io = bi_complete; |
| 731 | submit_bio(rw, bio); |
| 732 | wait_for_completion(&event); |
| 733 | |
| 734 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 735 | bio_put(bio); |
| 736 | return ret; |
| 737 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 738 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 740 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
| 742 | char b[BDEVNAME_SIZE]; |
| 743 | if (!rdev->sb_page) { |
| 744 | MD_BUG(); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | if (rdev->sb_loaded) |
| 748 | return 0; |
| 749 | |
| 750 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 751 | 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] | 752 | goto fail; |
| 753 | rdev->sb_loaded = 1; |
| 754 | return 0; |
| 755 | |
| 756 | fail: |
| 757 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 758 | bdevname(rdev->bdev,b)); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | |
| 762 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 763 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 764 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 765 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 766 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 767 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 771 | { |
| 772 | int ret; |
| 773 | mdp_super_t *tmp1, *tmp2; |
| 774 | |
| 775 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 776 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 777 | |
| 778 | if (!tmp1 || !tmp2) { |
| 779 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 780 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | goto abort; |
| 782 | } |
| 783 | |
| 784 | *tmp1 = *sb1; |
| 785 | *tmp2 = *sb2; |
| 786 | |
| 787 | /* |
| 788 | * nr_disks is not constant |
| 789 | */ |
| 790 | tmp1->nr_disks = 0; |
| 791 | tmp2->nr_disks = 0; |
| 792 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 793 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 795 | kfree(tmp1); |
| 796 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return ret; |
| 798 | } |
| 799 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 800 | |
| 801 | static u32 md_csum_fold(u32 csum) |
| 802 | { |
| 803 | csum = (csum & 0xffff) + (csum >> 16); |
| 804 | return (csum & 0xffff) + (csum >> 16); |
| 805 | } |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 808 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 809 | u64 newcsum = 0; |
| 810 | u32 *sb32 = (u32*)sb; |
| 811 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | unsigned int disk_csum, csum; |
| 813 | |
| 814 | disk_csum = sb->sb_csum; |
| 815 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 816 | |
| 817 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 818 | newcsum += sb32[i]; |
| 819 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 820 | |
| 821 | |
| 822 | #ifdef CONFIG_ALPHA |
| 823 | /* This used to use csum_partial, which was wrong for several |
| 824 | * reasons including that different results are returned on |
| 825 | * different architectures. It isn't critical that we get exactly |
| 826 | * the same return value as before (we always csum_fold before |
| 827 | * testing, and that removes any differences). However as we |
| 828 | * know that csum_partial always returned a 16bit value on |
| 829 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 830 | */ |
| 831 | sb->sb_csum = md_csum_fold(disk_csum); |
| 832 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 834 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | return csum; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /* |
| 840 | * Handle superblock details. |
| 841 | * We want to be able to handle multiple superblock formats |
| 842 | * so we have a common interface to them all, and an array of |
| 843 | * different handlers. |
| 844 | * We rely on user-space to write the initial superblock, and support |
| 845 | * reading and updating of superblocks. |
| 846 | * Interface methods are: |
| 847 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 848 | * loads and validates a superblock on dev. |
| 849 | * if refdev != NULL, compare superblocks on both devices |
| 850 | * Return: |
| 851 | * 0 - dev has a superblock that is compatible with refdev |
| 852 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 853 | * so dev should be used as the refdev in future |
| 854 | * -EINVAL superblock incompatible or invalid |
| 855 | * -othererror e.g. -EIO |
| 856 | * |
| 857 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 858 | * Verify that dev is acceptable into mddev. |
| 859 | * The first time, mddev->raid_disks will be 0, and data from |
| 860 | * dev should be merged in. Subsequent calls check that dev |
| 861 | * is new enough. Return 0 or -EINVAL |
| 862 | * |
| 863 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 864 | * Update the superblock for rdev with data in mddev |
| 865 | * This does not write to disc. |
| 866 | * |
| 867 | */ |
| 868 | |
| 869 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 870 | char *name; |
| 871 | struct module *owner; |
| 872 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 873 | int minor_version); |
| 874 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 875 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 876 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 877 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | }; |
| 879 | |
| 880 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 881 | * Check that the given mddev has no bitmap. |
| 882 | * |
| 883 | * This function is called from the run method of all personalities that do not |
| 884 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 885 | * has a bitmap. Otherwise, it returns 0. |
| 886 | * |
| 887 | */ |
| 888 | int md_check_no_bitmap(mddev_t *mddev) |
| 889 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 890 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 891 | return 0; |
| 892 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 893 | mdname(mddev), mddev->pers->name); |
| 894 | return 1; |
| 895 | } |
| 896 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 897 | |
| 898 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | * load_super for 0.90.0 |
| 900 | */ |
| 901 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 902 | { |
| 903 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 904 | mdp_super_t *sb; |
| 905 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 908 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | * it's at the end of the disk. |
| 910 | * |
| 911 | * It also happens to be a multiple of 4Kb. |
| 912 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 913 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 915 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (ret) return ret; |
| 917 | |
| 918 | ret = -EINVAL; |
| 919 | |
| 920 | bdevname(rdev->bdev, b); |
| 921 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 922 | |
| 923 | if (sb->md_magic != MD_SB_MAGIC) { |
| 924 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 925 | b); |
| 926 | goto abort; |
| 927 | } |
| 928 | |
| 929 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 930 | sb->minor_version < 90 || |
| 931 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 933 | sb->major_version, sb->minor_version, |
| 934 | b); |
| 935 | goto abort; |
| 936 | } |
| 937 | |
| 938 | if (sb->raid_disks <= 0) |
| 939 | goto abort; |
| 940 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 941 | 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] | 942 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 943 | b); |
| 944 | goto abort; |
| 945 | } |
| 946 | |
| 947 | rdev->preferred_minor = sb->md_minor; |
| 948 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 949 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
| 951 | if (sb->level == LEVEL_MULTIPATH) |
| 952 | rdev->desc_nr = -1; |
| 953 | else |
| 954 | rdev->desc_nr = sb->this_disk.number; |
| 955 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 956 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 958 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | __u64 ev1, ev2; |
| 960 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 961 | if (!uuid_equal(refsb, sb)) { |
| 962 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 963 | b, bdevname(refdev->bdev,b2)); |
| 964 | goto abort; |
| 965 | } |
| 966 | if (!sb_equal(refsb, sb)) { |
| 967 | printk(KERN_WARNING "md: %s has same UUID" |
| 968 | " but different superblock to %s\n", |
| 969 | b, bdevname(refdev->bdev, b2)); |
| 970 | goto abort; |
| 971 | } |
| 972 | ev1 = md_event(sb); |
| 973 | ev2 = md_event(refsb); |
| 974 | if (ev1 > ev2) |
| 975 | ret = 1; |
| 976 | else |
| 977 | ret = 0; |
| 978 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 979 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 981 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 982 | /* "this cannot possibly happen" ... */ |
| 983 | ret = -EINVAL; |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | abort: |
| 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * validate_super for 0.90.0 |
| 991 | */ |
| 992 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 993 | { |
| 994 | mdp_disk_t *desc; |
| 995 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 996 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 998 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 999 | clear_bit(Faulty, &rdev->flags); |
| 1000 | clear_bit(In_sync, &rdev->flags); |
| 1001 | clear_bit(WriteMostly, &rdev->flags); |
| 1002 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | if (mddev->raid_disks == 0) { |
| 1005 | mddev->major_version = 0; |
| 1006 | mddev->minor_version = sb->minor_version; |
| 1007 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1008 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1009 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | mddev->ctime = sb->ctime; |
| 1011 | mddev->utime = sb->utime; |
| 1012 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1013 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | mddev->layout = sb->layout; |
| 1015 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1016 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1017 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1018 | mddev->bitmap_info.offset = 0; |
| 1019 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1021 | if (mddev->minor_version >= 91) { |
| 1022 | mddev->reshape_position = sb->reshape_position; |
| 1023 | mddev->delta_disks = sb->delta_disks; |
| 1024 | mddev->new_level = sb->new_level; |
| 1025 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1026 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1027 | } else { |
| 1028 | mddev->reshape_position = MaxSector; |
| 1029 | mddev->delta_disks = 0; |
| 1030 | mddev->new_level = mddev->level; |
| 1031 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1032 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1036 | mddev->recovery_cp = MaxSector; |
| 1037 | else { |
| 1038 | if (sb->events_hi == sb->cp_events_hi && |
| 1039 | sb->events_lo == sb->cp_events_lo) { |
| 1040 | mddev->recovery_cp = sb->recovery_cp; |
| 1041 | } else |
| 1042 | mddev->recovery_cp = 0; |
| 1043 | } |
| 1044 | |
| 1045 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1046 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1047 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1048 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1049 | |
| 1050 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1051 | |
| 1052 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1053 | mddev->bitmap_info.file == NULL) |
| 1054 | mddev->bitmap_info.offset = |
| 1055 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1056 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1057 | } else if (mddev->pers == NULL) { |
| 1058 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | ++ev1; |
| 1060 | if (ev1 < mddev->events) |
| 1061 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1062 | } else if (mddev->bitmap) { |
| 1063 | /* if adding to array with a bitmap, then we can accept an |
| 1064 | * older device ... but not too old. |
| 1065 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1066 | if (ev1 < mddev->bitmap->events_cleared) |
| 1067 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1068 | } else { |
| 1069 | if (ev1 < mddev->events) |
| 1070 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1071 | return 0; |
| 1072 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | desc = sb->disks + rdev->desc_nr; |
| 1076 | |
| 1077 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1078 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1079 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1080 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1081 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1083 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1084 | /* active but not in sync implies recovery up to |
| 1085 | * reshape position. We don't know exactly where |
| 1086 | * that is, so set to zero for now */ |
| 1087 | if (mddev->minor_version >= 91) { |
| 1088 | rdev->recovery_offset = 0; |
| 1089 | rdev->raid_disk = desc->raid_disk; |
| 1090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1092 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1093 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1094 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1095 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * sync_super for 0.90.0 |
| 1101 | */ |
| 1102 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1103 | { |
| 1104 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | mdk_rdev_t *rdev2; |
| 1106 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
| 1109 | /* make rdev->sb match mddev data.. |
| 1110 | * |
| 1111 | * 1/ zero out disks |
| 1112 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1113 | * 3/ any empty disks < next_spare become removed |
| 1114 | * |
| 1115 | * disks[0] gets initialised to REMOVED because |
| 1116 | * we cannot be sure from other fields if it has |
| 1117 | * been initialised or not. |
| 1118 | */ |
| 1119 | int i; |
| 1120 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1121 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1122 | rdev->sb_size = MD_SB_BYTES; |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1125 | |
| 1126 | memset(sb, 0, sizeof(*sb)); |
| 1127 | |
| 1128 | sb->md_magic = MD_SB_MAGIC; |
| 1129 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | sb->patch_version = mddev->patch_version; |
| 1131 | sb->gvalid_words = 0; /* ignored */ |
| 1132 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1133 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1134 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1135 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1136 | |
| 1137 | sb->ctime = mddev->ctime; |
| 1138 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1139 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | sb->raid_disks = mddev->raid_disks; |
| 1141 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1142 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | sb->utime = mddev->utime; |
| 1144 | sb->state = 0; |
| 1145 | sb->events_hi = (mddev->events>>32); |
| 1146 | sb->events_lo = (u32)mddev->events; |
| 1147 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1148 | if (mddev->reshape_position == MaxSector) |
| 1149 | sb->minor_version = 90; |
| 1150 | else { |
| 1151 | sb->minor_version = 91; |
| 1152 | sb->reshape_position = mddev->reshape_position; |
| 1153 | sb->new_level = mddev->new_level; |
| 1154 | sb->delta_disks = mddev->delta_disks; |
| 1155 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1156 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1157 | } |
| 1158 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (mddev->in_sync) |
| 1160 | { |
| 1161 | sb->recovery_cp = mddev->recovery_cp; |
| 1162 | sb->cp_events_hi = (mddev->events>>32); |
| 1163 | sb->cp_events_lo = (u32)mddev->events; |
| 1164 | if (mddev->recovery_cp == MaxSector) |
| 1165 | sb->state = (1<< MD_SB_CLEAN); |
| 1166 | } else |
| 1167 | sb->recovery_cp = 0; |
| 1168 | |
| 1169 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1170 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1172 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1173 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1176 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1178 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1179 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1180 | |
| 1181 | if (rdev2->raid_disk >= 0 && |
| 1182 | sb->minor_version >= 91) |
| 1183 | /* we have nowhere to store the recovery_offset, |
| 1184 | * but if it is not below the reshape_position, |
| 1185 | * we can piggy-back on that. |
| 1186 | */ |
| 1187 | is_active = 1; |
| 1188 | if (rdev2->raid_disk < 0 || |
| 1189 | test_bit(Faulty, &rdev2->flags)) |
| 1190 | is_active = 0; |
| 1191 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1192 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1194 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1195 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | d = &sb->disks[rdev2->desc_nr]; |
| 1197 | nr_disks++; |
| 1198 | d->number = rdev2->desc_nr; |
| 1199 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1200 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1201 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | d->raid_disk = rdev2->raid_disk; |
| 1203 | else |
| 1204 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1205 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1207 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1209 | if (test_bit(In_sync, &rdev2->flags)) |
| 1210 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | active++; |
| 1212 | working++; |
| 1213 | } else { |
| 1214 | d->state = 0; |
| 1215 | spare++; |
| 1216 | working++; |
| 1217 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1218 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1219 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1222 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1223 | mdp_disk_t *d = &sb->disks[i]; |
| 1224 | if (d->state == 0 && d->number == 0) { |
| 1225 | d->number = i; |
| 1226 | d->raid_disk = i; |
| 1227 | d->state = (1<<MD_DISK_REMOVED); |
| 1228 | d->state |= (1<<MD_DISK_FAULTY); |
| 1229 | failed++; |
| 1230 | } |
| 1231 | } |
| 1232 | sb->nr_disks = nr_disks; |
| 1233 | sb->active_disks = active; |
| 1234 | sb->working_disks = working; |
| 1235 | sb->failed_disks = failed; |
| 1236 | sb->spare_disks = spare; |
| 1237 | |
| 1238 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1239 | sb->sb_csum = calc_sb_csum(sb); |
| 1240 | } |
| 1241 | |
| 1242 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1243 | * rdev_size_change for 0.90.0 |
| 1244 | */ |
| 1245 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1246 | 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] | 1247 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1248 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1249 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1250 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1251 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1252 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1253 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1254 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1255 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1256 | rdev->sb_page); |
| 1257 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1258 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | |
| 1262 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | * version 1 superblock |
| 1264 | */ |
| 1265 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1266 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1268 | __le32 disk_csum; |
| 1269 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | unsigned long long newcsum; |
| 1271 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1272 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | int i; |
| 1274 | |
| 1275 | disk_csum = sb->sb_csum; |
| 1276 | sb->sb_csum = 0; |
| 1277 | newcsum = 0; |
| 1278 | for (i=0; size>=4; size -= 4 ) |
| 1279 | newcsum += le32_to_cpu(*isuper++); |
| 1280 | |
| 1281 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1282 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
| 1284 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1285 | sb->sb_csum = disk_csum; |
| 1286 | return cpu_to_le32(csum); |
| 1287 | } |
| 1288 | |
| 1289 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1290 | { |
| 1291 | struct mdp_superblock_1 *sb; |
| 1292 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1293 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1295 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1298 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | * It is always aligned to a 4K boundary and |
| 1300 | * depeding on minor_version, it can be: |
| 1301 | * 0: At least 8K, but less than 12K, from end of device |
| 1302 | * 1: At start of device |
| 1303 | * 2: 4K from start of device. |
| 1304 | */ |
| 1305 | switch(minor_version) { |
| 1306 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1307 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1308 | sb_start -= 8*2; |
| 1309 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | break; |
| 1311 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1312 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | break; |
| 1314 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1315 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | break; |
| 1317 | default: |
| 1318 | return -EINVAL; |
| 1319 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1320 | rdev->sb_start = sb_start; |
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 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1323 | * and it is safe to read 4k, so we do that |
| 1324 | */ |
| 1325 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | if (ret) return ret; |
| 1327 | |
| 1328 | |
| 1329 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1330 | |
| 1331 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1332 | sb->major_version != cpu_to_le32(1) || |
| 1333 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1334 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1335 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | return -EINVAL; |
| 1337 | |
| 1338 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1339 | printk("md: invalid superblock checksum on %s\n", |
| 1340 | bdevname(rdev->bdev,b)); |
| 1341 | return -EINVAL; |
| 1342 | } |
| 1343 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1344 | printk("md: data_size too small on %s\n", |
| 1345 | bdevname(rdev->bdev,b)); |
| 1346 | return -EINVAL; |
| 1347 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | rdev->preferred_minor = 0xffff; |
| 1350 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1351 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1353 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1354 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1355 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1356 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1357 | |
| 1358 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1359 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1360 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1361 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1362 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1363 | rdev->desc_nr = -1; |
| 1364 | else |
| 1365 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1366 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1367 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1368 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1369 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | __u64 ev1, ev2; |
| 1371 | struct mdp_superblock_1 *refsb = |
| 1372 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1373 | |
| 1374 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1375 | sb->level != refsb->level || |
| 1376 | sb->layout != refsb->layout || |
| 1377 | sb->chunksize != refsb->chunksize) { |
| 1378 | printk(KERN_WARNING "md: %s has strangely different" |
| 1379 | " superblock to %s\n", |
| 1380 | bdevname(rdev->bdev,b), |
| 1381 | bdevname(refdev->bdev,b2)); |
| 1382 | return -EINVAL; |
| 1383 | } |
| 1384 | ev1 = le64_to_cpu(sb->events); |
| 1385 | ev2 = le64_to_cpu(refsb->events); |
| 1386 | |
| 1387 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1388 | ret = 1; |
| 1389 | else |
| 1390 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1392 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1393 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1394 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1396 | rdev->sectors = rdev->sb_start; |
| 1397 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1399 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1400 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1401 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1402 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1406 | { |
| 1407 | 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] | 1408 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1410 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1411 | clear_bit(Faulty, &rdev->flags); |
| 1412 | clear_bit(In_sync, &rdev->flags); |
| 1413 | clear_bit(WriteMostly, &rdev->flags); |
| 1414 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | if (mddev->raid_disks == 0) { |
| 1417 | mddev->major_version = 1; |
| 1418 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1419 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1420 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1422 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1423 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1424 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | mddev->layout = le32_to_cpu(sb->layout); |
| 1426 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1427 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1428 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1429 | mddev->bitmap_info.offset = 0; |
| 1430 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
| 1432 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1433 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1434 | |
| 1435 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1436 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1437 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1438 | mddev->bitmap_info.file == NULL ) |
| 1439 | mddev->bitmap_info.offset = |
| 1440 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1441 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1442 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1443 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1444 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1445 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1446 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1447 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1448 | } else { |
| 1449 | mddev->reshape_position = MaxSector; |
| 1450 | mddev->delta_disks = 0; |
| 1451 | mddev->new_level = mddev->level; |
| 1452 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1453 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1456 | } else if (mddev->pers == NULL) { |
| 1457 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | ++ev1; |
| 1459 | if (ev1 < mddev->events) |
| 1460 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1461 | } else if (mddev->bitmap) { |
| 1462 | /* If adding to array with a bitmap, then we can accept an |
| 1463 | * older device, but not too old. |
| 1464 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1465 | if (ev1 < mddev->bitmap->events_cleared) |
| 1466 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1467 | } else { |
| 1468 | if (ev1 < mddev->events) |
| 1469 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1470 | return 0; |
| 1471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1473 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1474 | if (rdev->desc_nr < 0 || |
| 1475 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1476 | role = 0xffff; |
| 1477 | rdev->desc_nr = -1; |
| 1478 | } else |
| 1479 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | switch(role) { |
| 1481 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1484 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | break; |
| 1486 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1487 | if ((le32_to_cpu(sb->feature_map) & |
| 1488 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1489 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1490 | else |
| 1491 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | rdev->raid_disk = role; |
| 1493 | break; |
| 1494 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1495 | if (sb->devflags & WriteMostly1) |
| 1496 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1497 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1498 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1504 | { |
| 1505 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | mdk_rdev_t *rdev2; |
| 1507 | int max_dev, i; |
| 1508 | /* make rdev->sb match mddev and rdev data. */ |
| 1509 | |
| 1510 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1511 | |
| 1512 | sb->feature_map = 0; |
| 1513 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1514 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1516 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1517 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1518 | |
| 1519 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1520 | sb->events = cpu_to_le64(mddev->events); |
| 1521 | if (mddev->in_sync) |
| 1522 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1523 | else |
| 1524 | sb->resync_offset = cpu_to_le64(0); |
| 1525 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1526 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1527 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1528 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1529 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1530 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1531 | sb->level = cpu_to_le32(mddev->level); |
| 1532 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1533 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1534 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1535 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1536 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1537 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1538 | |
| 1539 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1540 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1541 | sb->feature_map |= |
| 1542 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1543 | sb->recovery_offset = |
| 1544 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1547 | if (mddev->reshape_position != MaxSector) { |
| 1548 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1549 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1550 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1551 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1552 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1553 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1554 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1557 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | if (rdev2->desc_nr+1 > max_dev) |
| 1559 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1560 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1561 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1562 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1563 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1564 | rdev->sb_size = max_dev * 2 + 256; |
| 1565 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1566 | if (rdev->sb_size & bmask) |
| 1567 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | for (i=0; i<max_dev;i++) |
| 1570 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1571 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1572 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1574 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1576 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1578 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1579 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | else |
| 1581 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1582 | } |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1585 | } |
| 1586 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1587 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1588 | 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] | 1589 | { |
| 1590 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1591 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1592 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1593 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1594 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1595 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1596 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1597 | max_sectors -= rdev->data_offset; |
| 1598 | if (!num_sectors || num_sectors > max_sectors) |
| 1599 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1600 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1601 | /* minor version 0 with bitmap we can't move */ |
| 1602 | return 0; |
| 1603 | } else { |
| 1604 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1605 | sector_t sb_start; |
| 1606 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1607 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1608 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1609 | if (!num_sectors || num_sectors > max_sectors) |
| 1610 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1611 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1612 | } |
| 1613 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1614 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1615 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1616 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1617 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1618 | rdev->sb_page); |
| 1619 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1620 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1623 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | [0] = { |
| 1625 | .name = "0.90.0", |
| 1626 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1627 | .load_super = super_90_load, |
| 1628 | .validate_super = super_90_validate, |
| 1629 | .sync_super = super_90_sync, |
| 1630 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | }, |
| 1632 | [1] = { |
| 1633 | .name = "md-1", |
| 1634 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1635 | .load_super = super_1_load, |
| 1636 | .validate_super = super_1_validate, |
| 1637 | .sync_super = super_1_sync, |
| 1638 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | }, |
| 1640 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
| 1642 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1643 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1644 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1646 | rcu_read_lock(); |
| 1647 | rdev_for_each_rcu(rdev, mddev1) |
| 1648 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1649 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1650 | rdev2->bdev->bd_contains) { |
| 1651 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1652 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1653 | } |
| 1654 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | static LIST_HEAD(pending_raid_disks); |
| 1659 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1660 | /* |
| 1661 | * Try to register data integrity profile for an mddev |
| 1662 | * |
| 1663 | * This is called when an array is started and after a disk has been kicked |
| 1664 | * from the array. It only succeeds if all working and active component devices |
| 1665 | * are integrity capable with matching profiles. |
| 1666 | */ |
| 1667 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1668 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1669 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1670 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1671 | if (list_empty(&mddev->disks)) |
| 1672 | return 0; /* nothing to do */ |
| 1673 | if (blk_get_integrity(mddev->gendisk)) |
| 1674 | return 0; /* already registered */ |
| 1675 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1676 | /* skip spares and non-functional disks */ |
| 1677 | if (test_bit(Faulty, &rdev->flags)) |
| 1678 | continue; |
| 1679 | if (rdev->raid_disk < 0) |
| 1680 | continue; |
| 1681 | /* |
| 1682 | * If at least one rdev is not integrity capable, we can not |
| 1683 | * enable data integrity for the md device. |
| 1684 | */ |
| 1685 | if (!bdev_get_integrity(rdev->bdev)) |
| 1686 | return -EINVAL; |
| 1687 | if (!reference) { |
| 1688 | /* Use the first rdev as the reference */ |
| 1689 | reference = rdev; |
| 1690 | continue; |
| 1691 | } |
| 1692 | /* does this rdev's profile match the reference profile? */ |
| 1693 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1694 | rdev->bdev->bd_disk) < 0) |
| 1695 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1696 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1697 | /* |
| 1698 | * All component devices are integrity capable and have matching |
| 1699 | * profiles, register the common profile for the md device. |
| 1700 | */ |
| 1701 | if (blk_integrity_register(mddev->gendisk, |
| 1702 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1703 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1704 | mdname(mddev)); |
| 1705 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1706 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1707 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1708 | mdname(mddev)); |
| 1709 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1710 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1711 | EXPORT_SYMBOL(md_integrity_register); |
| 1712 | |
| 1713 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1714 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1715 | { |
| 1716 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1717 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1718 | |
| 1719 | if (!bi_mddev) /* nothing to do */ |
| 1720 | return; |
| 1721 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1722 | return; |
| 1723 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1724 | rdev->bdev->bd_disk) >= 0) |
| 1725 | return; |
| 1726 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1727 | blk_integrity_unregister(mddev->gendisk); |
| 1728 | } |
| 1729 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1732 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1733 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1734 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1735 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1736 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | |
| 1738 | if (rdev->mddev) { |
| 1739 | MD_BUG(); |
| 1740 | return -EINVAL; |
| 1741 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1742 | |
| 1743 | /* prevent duplicates */ |
| 1744 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1745 | return -EEXIST; |
| 1746 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1747 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1748 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1749 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1750 | if (mddev->pers) { |
| 1751 | /* Cannot change size, so fail |
| 1752 | * If mddev->level <= 0, then we don't care |
| 1753 | * about aligning sizes (e.g. linear) |
| 1754 | */ |
| 1755 | if (mddev->level > 0) |
| 1756 | return -ENOSPC; |
| 1757 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1758 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
| 1761 | /* Verify rdev->desc_nr is unique. |
| 1762 | * If it is -1, assign a free number, else |
| 1763 | * check number is not in use |
| 1764 | */ |
| 1765 | if (rdev->desc_nr < 0) { |
| 1766 | int choice = 0; |
| 1767 | if (mddev->pers) choice = mddev->raid_disks; |
| 1768 | while (find_rdev_nr(mddev, choice)) |
| 1769 | choice++; |
| 1770 | rdev->desc_nr = choice; |
| 1771 | } else { |
| 1772 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1773 | return -EBUSY; |
| 1774 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1775 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1776 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1777 | mdname(mddev), mddev->max_disks); |
| 1778 | return -EBUSY; |
| 1779 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1780 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1781 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1782 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1785 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1786 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1787 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1788 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1789 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1790 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1791 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1792 | kobject_del(&rdev->kobj); |
| 1793 | goto fail; |
| 1794 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1795 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1796 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1797 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1798 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1799 | |
| 1800 | /* May as well allow recovery to be retried once */ |
| 1801 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1804 | |
| 1805 | fail: |
| 1806 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1807 | b, mdname(mddev)); |
| 1808 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1811 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1812 | { |
| 1813 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1814 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1815 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1819 | { |
| 1820 | char b[BDEVNAME_SIZE]; |
| 1821 | if (!rdev->mddev) { |
| 1822 | MD_BUG(); |
| 1823 | return; |
| 1824 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1825 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1826 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1828 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1829 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1830 | sysfs_put(rdev->sysfs_state); |
| 1831 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1832 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1833 | * writing to 'remove' to "dev/state". We also need |
| 1834 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1835 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1836 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1837 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1838 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1839 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /* |
| 1843 | * prevent the device from being mounted, repartitioned or |
| 1844 | * otherwise reused by a RAID array (or any other kernel |
| 1845 | * subsystem), by bd_claiming the device. |
| 1846 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1847 | 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] | 1848 | { |
| 1849 | int err = 0; |
| 1850 | struct block_device *bdev; |
| 1851 | char b[BDEVNAME_SIZE]; |
| 1852 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1853 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | if (IS_ERR(bdev)) { |
| 1855 | printk(KERN_ERR "md: could not open %s.\n", |
| 1856 | __bdevname(dev, b)); |
| 1857 | return PTR_ERR(bdev); |
| 1858 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1859 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | if (err) { |
| 1861 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1862 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1863 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | return err; |
| 1865 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1866 | if (!shared) |
| 1867 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | rdev->bdev = bdev; |
| 1869 | return err; |
| 1870 | } |
| 1871 | |
| 1872 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1873 | { |
| 1874 | struct block_device *bdev = rdev->bdev; |
| 1875 | rdev->bdev = NULL; |
| 1876 | if (!bdev) |
| 1877 | MD_BUG(); |
| 1878 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1879 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | void md_autodetect_dev(dev_t dev); |
| 1883 | |
| 1884 | static void export_rdev(mdk_rdev_t * rdev) |
| 1885 | { |
| 1886 | char b[BDEVNAME_SIZE]; |
| 1887 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1888 | bdevname(rdev->bdev,b)); |
| 1889 | if (rdev->mddev) |
| 1890 | MD_BUG(); |
| 1891 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1893 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1894 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | #endif |
| 1896 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1897 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1901 | { |
| 1902 | unbind_rdev_from_array(rdev); |
| 1903 | export_rdev(rdev); |
| 1904 | } |
| 1905 | |
| 1906 | static void export_array(mddev_t *mddev) |
| 1907 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1908 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1910 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | if (!rdev->mddev) { |
| 1912 | MD_BUG(); |
| 1913 | continue; |
| 1914 | } |
| 1915 | kick_rdev_from_array(rdev); |
| 1916 | } |
| 1917 | if (!list_empty(&mddev->disks)) |
| 1918 | MD_BUG(); |
| 1919 | mddev->raid_disks = 0; |
| 1920 | mddev->major_version = 0; |
| 1921 | } |
| 1922 | |
| 1923 | static void print_desc(mdp_disk_t *desc) |
| 1924 | { |
| 1925 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1926 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1927 | } |
| 1928 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1929 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | { |
| 1931 | int i; |
| 1932 | |
| 1933 | printk(KERN_INFO |
| 1934 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1935 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1936 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1937 | sb->ctime); |
| 1938 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1939 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1940 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1941 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1942 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1943 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1944 | sb->failed_disks, sb->spare_disks, |
| 1945 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1946 | |
| 1947 | printk(KERN_INFO); |
| 1948 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1949 | mdp_disk_t *desc; |
| 1950 | |
| 1951 | desc = sb->disks + i; |
| 1952 | if (desc->number || desc->major || desc->minor || |
| 1953 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1954 | printk(" D %2d: ", i); |
| 1955 | print_desc(desc); |
| 1956 | } |
| 1957 | } |
| 1958 | printk(KERN_INFO "md: THIS: "); |
| 1959 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1962 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1963 | { |
| 1964 | __u8 *uuid; |
| 1965 | |
| 1966 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1967 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1968 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1969 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1970 | le32_to_cpu(sb->major_version), |
| 1971 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1972 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1973 | sb->set_name, |
| 1974 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1975 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1976 | |
| 1977 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1978 | printk(KERN_INFO |
| 1979 | "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] | 1980 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1981 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1982 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1983 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1984 | le32_to_cpu(sb->level), |
| 1985 | (unsigned long long)le64_to_cpu(sb->size), |
| 1986 | le32_to_cpu(sb->raid_disks), |
| 1987 | le32_to_cpu(sb->layout), |
| 1988 | le32_to_cpu(sb->chunksize), |
| 1989 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1990 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1991 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1992 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1993 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1994 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1995 | sb->devflags, |
| 1996 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1997 | (unsigned long long)le64_to_cpu(sb->events), |
| 1998 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1999 | le32_to_cpu(sb->sb_csum), |
| 2000 | le32_to_cpu(sb->max_dev) |
| 2001 | ); |
| 2002 | } |
| 2003 | |
| 2004 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | { |
| 2006 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2007 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2008 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2009 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2010 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2012 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2013 | switch (major_version) { |
| 2014 | case 0: |
| 2015 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2016 | break; |
| 2017 | case 1: |
| 2018 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2019 | break; |
| 2020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } else |
| 2022 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2023 | } |
| 2024 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2025 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2027 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | mdk_rdev_t *rdev; |
| 2029 | mddev_t *mddev; |
| 2030 | char b[BDEVNAME_SIZE]; |
| 2031 | |
| 2032 | printk("\n"); |
| 2033 | printk("md: **********************************\n"); |
| 2034 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2035 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2036 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2038 | if (mddev->bitmap) |
| 2039 | bitmap_print_sb(mddev->bitmap); |
| 2040 | else |
| 2041 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2042 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2044 | printk("\n"); |
| 2045 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2046 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2047 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | } |
| 2049 | printk("md: **********************************\n"); |
| 2050 | printk("\n"); |
| 2051 | } |
| 2052 | |
| 2053 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2054 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2056 | /* Update each superblock (in-memory image), but |
| 2057 | * if we are allowed to, skip spares which already |
| 2058 | * have the right event counter, or have one earlier |
| 2059 | * (which would mean they aren't being marked as dirty |
| 2060 | * with the rest of the array) |
| 2061 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2064 | /* First make sure individual recovery_offsets are correct */ |
| 2065 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2066 | if (rdev->raid_disk >= 0 && |
| 2067 | !test_bit(In_sync, &rdev->flags) && |
| 2068 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2069 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2070 | |
| 2071 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2072 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2073 | if (rdev->sb_events == mddev->events || |
| 2074 | (nospares && |
| 2075 | rdev->raid_disk < 0 && |
| 2076 | (rdev->sb_events&1)==0 && |
| 2077 | rdev->sb_events+1 == mddev->events)) { |
| 2078 | /* Don't update this superblock */ |
| 2079 | rdev->sb_loaded = 2; |
| 2080 | } else { |
| 2081 | super_types[mddev->major_version]. |
| 2082 | sync_super(mddev, rdev); |
| 2083 | rdev->sb_loaded = 1; |
| 2084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2088 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2091 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2092 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2094 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2095 | if (mddev->external) |
| 2096 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2098 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2099 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2100 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2101 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2102 | force_change = 1; |
| 2103 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2104 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2105 | * though if events isn't the right even/odd, we will have to do |
| 2106 | * spares after all |
| 2107 | */ |
| 2108 | nospares = 1; |
| 2109 | if (force_change) |
| 2110 | nospares = 0; |
| 2111 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2112 | /* If the array is degraded, then skipping spares is both |
| 2113 | * dangerous and fairly pointless. |
| 2114 | * Dangerous because a device that was removed from the array |
| 2115 | * might have a event_count that still looks up-to-date, |
| 2116 | * so it can be re-added without a resync. |
| 2117 | * Pointless because if there are any spares to skip, |
| 2118 | * then a recovery will happen and soon that array won't |
| 2119 | * be degraded any more and the spare can go back to sleep then. |
| 2120 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2121 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2122 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2123 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2124 | |
| 2125 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2126 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2127 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2128 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2129 | && (mddev->events & 1) |
| 2130 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2131 | mddev->events--; |
| 2132 | else { |
| 2133 | /* otherwise we have to go forward and ... */ |
| 2134 | mddev->events ++; |
| 2135 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2136 | /* .. if the array isn't clean, an 'even' event must also go |
| 2137 | * to spares. */ |
| 2138 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2139 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2140 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2141 | /* otherwise an 'odd' event must go to spares */ |
| 2142 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2143 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2144 | } |
| 2145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | |
| 2147 | if (!mddev->events) { |
| 2148 | /* |
| 2149 | * oops, this 64-bit counter should never wrap. |
| 2150 | * Either we are in around ~1 trillion A.C., assuming |
| 2151 | * 1 reboot per second, or we have a bug: |
| 2152 | */ |
| 2153 | MD_BUG(); |
| 2154 | mddev->events --; |
| 2155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | |
| 2157 | /* |
| 2158 | * do not write anything to disk if using |
| 2159 | * nonpersistent superblocks |
| 2160 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2161 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2162 | if (!mddev->external) |
| 2163 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2164 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2165 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2166 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2168 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2169 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2170 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
| 2172 | dprintk(KERN_INFO |
| 2173 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2174 | mdname(mddev),mddev->in_sync); |
| 2175 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2176 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2177 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | char b[BDEVNAME_SIZE]; |
| 2179 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2180 | if (rdev->sb_loaded != 1) |
| 2181 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2182 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | dprintk("(skipping faulty "); |
| 2184 | |
| 2185 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2186 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2187 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2188 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2189 | rdev->sb_page); |
| 2190 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2191 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2192 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2193 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } else |
| 2196 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2197 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | /* only need to write one superblock... */ |
| 2199 | break; |
| 2200 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2201 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2202 | /* 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] | 2203 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2204 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2205 | if (mddev->in_sync != sync_req || |
| 2206 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2207 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2208 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2209 | goto repeat; |
| 2210 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2211 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2212 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2213 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2214 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2215 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2219 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2220 | * We want to accept with case. For this we use cmd_match. |
| 2221 | */ |
| 2222 | static int cmd_match(const char *cmd, const char *str) |
| 2223 | { |
| 2224 | /* See if cmd, written into a sysfs file, matches |
| 2225 | * str. They must either be the same, or cmd can |
| 2226 | * have a trailing newline |
| 2227 | */ |
| 2228 | while (*cmd && *str && *cmd == *str) { |
| 2229 | cmd++; |
| 2230 | str++; |
| 2231 | } |
| 2232 | if (*cmd == '\n') |
| 2233 | cmd++; |
| 2234 | if (*str || *cmd) |
| 2235 | return 0; |
| 2236 | return 1; |
| 2237 | } |
| 2238 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2239 | struct rdev_sysfs_entry { |
| 2240 | struct attribute attr; |
| 2241 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2242 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2243 | }; |
| 2244 | |
| 2245 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2246 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2247 | { |
| 2248 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2249 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2250 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2251 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2252 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2253 | sep = ","; |
| 2254 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2255 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2256 | len += sprintf(page+len, "%sin_sync",sep); |
| 2257 | sep = ","; |
| 2258 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2259 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2260 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2261 | sep = ","; |
| 2262 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2263 | if (test_bit(Blocked, &rdev->flags)) { |
| 2264 | len += sprintf(page+len, "%sblocked", sep); |
| 2265 | sep = ","; |
| 2266 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2267 | if (!test_bit(Faulty, &rdev->flags) && |
| 2268 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2269 | len += sprintf(page+len, "%sspare", sep); |
| 2270 | sep = ","; |
| 2271 | } |
| 2272 | return len+sprintf(page+len, "\n"); |
| 2273 | } |
| 2274 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2275 | static ssize_t |
| 2276 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2277 | { |
| 2278 | /* can write |
| 2279 | * faulty - simulates and error |
| 2280 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2281 | * writemostly - sets write_mostly |
| 2282 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2283 | * blocked - sets the Blocked flag |
| 2284 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2285 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2286 | */ |
| 2287 | int err = -EINVAL; |
| 2288 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2289 | md_error(rdev->mddev, rdev); |
| 2290 | err = 0; |
| 2291 | } else if (cmd_match(buf, "remove")) { |
| 2292 | if (rdev->raid_disk >= 0) |
| 2293 | err = -EBUSY; |
| 2294 | else { |
| 2295 | mddev_t *mddev = rdev->mddev; |
| 2296 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2297 | if (mddev->pers) |
| 2298 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2299 | md_new_event(mddev); |
| 2300 | err = 0; |
| 2301 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2302 | } else if (cmd_match(buf, "writemostly")) { |
| 2303 | set_bit(WriteMostly, &rdev->flags); |
| 2304 | err = 0; |
| 2305 | } else if (cmd_match(buf, "-writemostly")) { |
| 2306 | clear_bit(WriteMostly, &rdev->flags); |
| 2307 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2308 | } else if (cmd_match(buf, "blocked")) { |
| 2309 | set_bit(Blocked, &rdev->flags); |
| 2310 | err = 0; |
| 2311 | } else if (cmd_match(buf, "-blocked")) { |
| 2312 | clear_bit(Blocked, &rdev->flags); |
| 2313 | wake_up(&rdev->blocked_wait); |
| 2314 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2315 | md_wakeup_thread(rdev->mddev->thread); |
| 2316 | |
| 2317 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2318 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2319 | set_bit(In_sync, &rdev->flags); |
| 2320 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2321 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2322 | if (!err && rdev->sysfs_state) |
| 2323 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2324 | return err ? err : len; |
| 2325 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2326 | static struct rdev_sysfs_entry rdev_state = |
| 2327 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2328 | |
| 2329 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2330 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2331 | { |
| 2332 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2333 | } |
| 2334 | |
| 2335 | static ssize_t |
| 2336 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2337 | { |
| 2338 | char *e; |
| 2339 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2340 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2341 | atomic_set(&rdev->corrected_errors, n); |
| 2342 | return len; |
| 2343 | } |
| 2344 | return -EINVAL; |
| 2345 | } |
| 2346 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2347 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2348 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2349 | static ssize_t |
| 2350 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2351 | { |
| 2352 | if (rdev->raid_disk < 0) |
| 2353 | return sprintf(page, "none\n"); |
| 2354 | else |
| 2355 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2356 | } |
| 2357 | |
| 2358 | static ssize_t |
| 2359 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2360 | { |
| 2361 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2362 | int err; |
| 2363 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2364 | int slot = simple_strtoul(buf, &e, 10); |
| 2365 | if (strncmp(buf, "none", 4)==0) |
| 2366 | slot = -1; |
| 2367 | else if (e==buf || (*e && *e!= '\n')) |
| 2368 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2369 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2370 | /* Setting 'slot' on an active array requires also |
| 2371 | * updating the 'rd%d' link, and communicating |
| 2372 | * with the personality with ->hot_*_disk. |
| 2373 | * For now we only support removing |
| 2374 | * failed/spare devices. This normally happens automatically, |
| 2375 | * but not when the metadata is externally managed. |
| 2376 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2377 | if (rdev->raid_disk == -1) |
| 2378 | return -EEXIST; |
| 2379 | /* personality does all needed checks */ |
| 2380 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2381 | return -EINVAL; |
| 2382 | err = rdev->mddev->pers-> |
| 2383 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2384 | if (err) |
| 2385 | return err; |
| 2386 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2387 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2388 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2389 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2390 | } else if (rdev->mddev->pers) { |
| 2391 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2392 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2393 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2394 | */ |
| 2395 | |
| 2396 | if (rdev->raid_disk != -1) |
| 2397 | return -EBUSY; |
| 2398 | |
| 2399 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2400 | return -EINVAL; |
| 2401 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2402 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2403 | if (rdev2->raid_disk == slot) |
| 2404 | return -EEXIST; |
| 2405 | |
| 2406 | rdev->raid_disk = slot; |
| 2407 | if (test_bit(In_sync, &rdev->flags)) |
| 2408 | rdev->saved_raid_disk = slot; |
| 2409 | else |
| 2410 | rdev->saved_raid_disk = -1; |
| 2411 | err = rdev->mddev->pers-> |
| 2412 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2413 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2414 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2415 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2416 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2417 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2418 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2419 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2420 | printk(KERN_WARNING |
| 2421 | "md: cannot register " |
| 2422 | "%s for %s\n", |
| 2423 | nm, mdname(rdev->mddev)); |
| 2424 | |
| 2425 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2426 | } else { |
| 2427 | if (slot >= rdev->mddev->raid_disks) |
| 2428 | return -ENOSPC; |
| 2429 | rdev->raid_disk = slot; |
| 2430 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2431 | clear_bit(Faulty, &rdev->flags); |
| 2432 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2433 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2434 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2435 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2436 | return len; |
| 2437 | } |
| 2438 | |
| 2439 | |
| 2440 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2441 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2442 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2443 | static ssize_t |
| 2444 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2445 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2446 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | static ssize_t |
| 2450 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2451 | { |
| 2452 | char *e; |
| 2453 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2454 | if (e==buf || (*e && *e != '\n')) |
| 2455 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2456 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2457 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2458 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2459 | /* Must set offset before size, so overlap checks |
| 2460 | * can be sane */ |
| 2461 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2462 | rdev->data_offset = offset; |
| 2463 | return len; |
| 2464 | } |
| 2465 | |
| 2466 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2467 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2468 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2469 | static ssize_t |
| 2470 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2471 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2472 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2473 | } |
| 2474 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2475 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2476 | { |
| 2477 | /* check if two start/length pairs overlap */ |
| 2478 | if (s1+l1 <= s2) |
| 2479 | return 0; |
| 2480 | if (s2+l2 <= s1) |
| 2481 | return 0; |
| 2482 | return 1; |
| 2483 | } |
| 2484 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2485 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2486 | { |
| 2487 | unsigned long long blocks; |
| 2488 | sector_t new; |
| 2489 | |
| 2490 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2491 | return -EINVAL; |
| 2492 | |
| 2493 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2494 | return -EINVAL; /* sector conversion overflow */ |
| 2495 | |
| 2496 | new = blocks * 2; |
| 2497 | if (new != blocks * 2) |
| 2498 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2499 | |
| 2500 | *sectors = new; |
| 2501 | return 0; |
| 2502 | } |
| 2503 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2504 | static ssize_t |
| 2505 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2506 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2507 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2508 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2509 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2510 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2511 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2512 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2513 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2514 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2515 | sectors = super_types[my_mddev->major_version]. |
| 2516 | rdev_size_change(rdev, sectors); |
| 2517 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2518 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2519 | } else if (!sectors) |
| 2520 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2521 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2522 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2523 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2524 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2525 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2526 | rdev->sectors = sectors; |
| 2527 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2528 | /* need to check that all other rdevs with the same ->bdev |
| 2529 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2530 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2531 | * we have to change it back, we will have the lock again. |
| 2532 | */ |
| 2533 | mddev_t *mddev; |
| 2534 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2535 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2536 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2537 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2538 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2539 | mdk_rdev_t *rdev2; |
| 2540 | |
| 2541 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2542 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2543 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2544 | (rdev->bdev == rdev2->bdev && |
| 2545 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2546 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2547 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2548 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2549 | overlap = 1; |
| 2550 | break; |
| 2551 | } |
| 2552 | mddev_unlock(mddev); |
| 2553 | if (overlap) { |
| 2554 | mddev_put(mddev); |
| 2555 | break; |
| 2556 | } |
| 2557 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2558 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2559 | if (overlap) { |
| 2560 | /* Someone else could have slipped in a size |
| 2561 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2562 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2563 | * safe, and trust userspace not to race with |
| 2564 | * itself |
| 2565 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2566 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2567 | return -EBUSY; |
| 2568 | } |
| 2569 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2570 | return len; |
| 2571 | } |
| 2572 | |
| 2573 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2574 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2575 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2576 | |
| 2577 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2578 | { |
| 2579 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2580 | |
| 2581 | if (test_bit(In_sync, &rdev->flags) || |
| 2582 | recovery_start == MaxSector) |
| 2583 | return sprintf(page, "none\n"); |
| 2584 | |
| 2585 | return sprintf(page, "%llu\n", recovery_start); |
| 2586 | } |
| 2587 | |
| 2588 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2589 | { |
| 2590 | unsigned long long recovery_start; |
| 2591 | |
| 2592 | if (cmd_match(buf, "none")) |
| 2593 | recovery_start = MaxSector; |
| 2594 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2595 | return -EINVAL; |
| 2596 | |
| 2597 | if (rdev->mddev->pers && |
| 2598 | rdev->raid_disk >= 0) |
| 2599 | return -EBUSY; |
| 2600 | |
| 2601 | rdev->recovery_offset = recovery_start; |
| 2602 | if (recovery_start == MaxSector) |
| 2603 | set_bit(In_sync, &rdev->flags); |
| 2604 | else |
| 2605 | clear_bit(In_sync, &rdev->flags); |
| 2606 | return len; |
| 2607 | } |
| 2608 | |
| 2609 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2610 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2611 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2612 | static struct attribute *rdev_default_attrs[] = { |
| 2613 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2614 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2615 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2616 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2617 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2618 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2619 | NULL, |
| 2620 | }; |
| 2621 | static ssize_t |
| 2622 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2623 | { |
| 2624 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2625 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2626 | mddev_t *mddev = rdev->mddev; |
| 2627 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2628 | |
| 2629 | if (!entry->show) |
| 2630 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2631 | |
| 2632 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2633 | if (!rv) { |
| 2634 | if (rdev->mddev == NULL) |
| 2635 | rv = -EBUSY; |
| 2636 | else |
| 2637 | rv = entry->show(rdev, page); |
| 2638 | mddev_unlock(mddev); |
| 2639 | } |
| 2640 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | static ssize_t |
| 2644 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2645 | const char *page, size_t length) |
| 2646 | { |
| 2647 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2648 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2649 | ssize_t rv; |
| 2650 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2651 | |
| 2652 | if (!entry->store) |
| 2653 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2654 | if (!capable(CAP_SYS_ADMIN)) |
| 2655 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2656 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2657 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2658 | if (rdev->mddev == NULL) |
| 2659 | rv = -EBUSY; |
| 2660 | else |
| 2661 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2662 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2663 | } |
| 2664 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | static void rdev_free(struct kobject *ko) |
| 2668 | { |
| 2669 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2670 | kfree(rdev); |
| 2671 | } |
| 2672 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2673 | .show = rdev_attr_show, |
| 2674 | .store = rdev_attr_store, |
| 2675 | }; |
| 2676 | static struct kobj_type rdev_ktype = { |
| 2677 | .release = rdev_free, |
| 2678 | .sysfs_ops = &rdev_sysfs_ops, |
| 2679 | .default_attrs = rdev_default_attrs, |
| 2680 | }; |
| 2681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | /* |
| 2683 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2684 | * |
| 2685 | * mark the device faulty if: |
| 2686 | * |
| 2687 | * - the device is nonexistent (zero size) |
| 2688 | * - the device has no valid superblock |
| 2689 | * |
| 2690 | * a faulty rdev _never_ has rdev->sb set. |
| 2691 | */ |
| 2692 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2693 | { |
| 2694 | char b[BDEVNAME_SIZE]; |
| 2695 | int err; |
| 2696 | mdk_rdev_t *rdev; |
| 2697 | sector_t size; |
| 2698 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2699 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | if (!rdev) { |
| 2701 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2702 | return ERR_PTR(-ENOMEM); |
| 2703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
| 2705 | if ((err = alloc_disk_sb(rdev))) |
| 2706 | goto abort_free; |
| 2707 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2708 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | if (err) |
| 2710 | goto abort_free; |
| 2711 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2712 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2715 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2716 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2717 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2719 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2720 | rdev->last_read_error.tv_sec = 0; |
| 2721 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2723 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2724 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
| 2726 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2727 | if (!size) { |
| 2728 | printk(KERN_WARNING |
| 2729 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2730 | bdevname(rdev->bdev,b)); |
| 2731 | err = -EINVAL; |
| 2732 | goto abort_free; |
| 2733 | } |
| 2734 | |
| 2735 | if (super_format >= 0) { |
| 2736 | err = super_types[super_format]. |
| 2737 | load_super(rdev, NULL, super_minor); |
| 2738 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2739 | printk(KERN_WARNING |
| 2740 | "md: %s does not have a valid v%d.%d " |
| 2741 | "superblock, not importing!\n", |
| 2742 | bdevname(rdev->bdev,b), |
| 2743 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | goto abort_free; |
| 2745 | } |
| 2746 | if (err < 0) { |
| 2747 | printk(KERN_WARNING |
| 2748 | "md: could not read %s's sb, not importing!\n", |
| 2749 | bdevname(rdev->bdev,b)); |
| 2750 | goto abort_free; |
| 2751 | } |
| 2752 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2755 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | |
| 2757 | return rdev; |
| 2758 | |
| 2759 | abort_free: |
| 2760 | if (rdev->sb_page) { |
| 2761 | if (rdev->bdev) |
| 2762 | unlock_rdev(rdev); |
| 2763 | free_disk_sb(rdev); |
| 2764 | } |
| 2765 | kfree(rdev); |
| 2766 | return ERR_PTR(err); |
| 2767 | } |
| 2768 | |
| 2769 | /* |
| 2770 | * Check a full RAID array for plausibility |
| 2771 | */ |
| 2772 | |
| 2773 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2774 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | { |
| 2776 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2777 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | char b[BDEVNAME_SIZE]; |
| 2779 | |
| 2780 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2781 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | switch (super_types[mddev->major_version]. |
| 2783 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2784 | case 1: |
| 2785 | freshest = rdev; |
| 2786 | break; |
| 2787 | case 0: |
| 2788 | break; |
| 2789 | default: |
| 2790 | printk( KERN_ERR \ |
| 2791 | "md: fatal superblock inconsistency in %s" |
| 2792 | " -- removing from array\n", |
| 2793 | bdevname(rdev->bdev,b)); |
| 2794 | kick_rdev_from_array(rdev); |
| 2795 | } |
| 2796 | |
| 2797 | |
| 2798 | super_types[mddev->major_version]. |
| 2799 | validate_super(mddev, freshest); |
| 2800 | |
| 2801 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2802 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2803 | if (rdev->desc_nr >= mddev->max_disks || |
| 2804 | i > mddev->max_disks) { |
| 2805 | printk(KERN_WARNING |
| 2806 | "md: %s: %s: only %d devices permitted\n", |
| 2807 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2808 | mddev->max_disks); |
| 2809 | kick_rdev_from_array(rdev); |
| 2810 | continue; |
| 2811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | if (rdev != freshest) |
| 2813 | if (super_types[mddev->major_version]. |
| 2814 | validate_super(mddev, rdev)) { |
| 2815 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2816 | " from array!\n", |
| 2817 | bdevname(rdev->bdev,b)); |
| 2818 | kick_rdev_from_array(rdev); |
| 2819 | continue; |
| 2820 | } |
| 2821 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2822 | rdev->desc_nr = i++; |
| 2823 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2824 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2825 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2826 | rdev->raid_disk = -1; |
| 2827 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | } |
| 2829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2832 | /* Read a fixed-point number. |
| 2833 | * Numbers in sysfs attributes should be in "standard" units where |
| 2834 | * possible, so time should be in seconds. |
| 2835 | * However we internally use a a much smaller unit such as |
| 2836 | * milliseconds or jiffies. |
| 2837 | * This function takes a decimal number with a possible fractional |
| 2838 | * component, and produces an integer which is the result of |
| 2839 | * multiplying that number by 10^'scale'. |
| 2840 | * all without any floating-point arithmetic. |
| 2841 | */ |
| 2842 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2843 | { |
| 2844 | unsigned long result = 0; |
| 2845 | long decimals = -1; |
| 2846 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2847 | if (*cp == '.') |
| 2848 | decimals = 0; |
| 2849 | else if (decimals < scale) { |
| 2850 | unsigned int value; |
| 2851 | value = *cp - '0'; |
| 2852 | result = result * 10 + value; |
| 2853 | if (decimals >= 0) |
| 2854 | decimals++; |
| 2855 | } |
| 2856 | cp++; |
| 2857 | } |
| 2858 | if (*cp == '\n') |
| 2859 | cp++; |
| 2860 | if (*cp) |
| 2861 | return -EINVAL; |
| 2862 | if (decimals < 0) |
| 2863 | decimals = 0; |
| 2864 | while (decimals < scale) { |
| 2865 | result *= 10; |
| 2866 | decimals ++; |
| 2867 | } |
| 2868 | *res = result; |
| 2869 | return 0; |
| 2870 | } |
| 2871 | |
| 2872 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2873 | static void md_safemode_timeout(unsigned long data); |
| 2874 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2875 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2876 | safe_delay_show(mddev_t *mddev, char *page) |
| 2877 | { |
| 2878 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2879 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2880 | } |
| 2881 | static ssize_t |
| 2882 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2883 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2884 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2885 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2886 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2887 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2888 | if (msec == 0) |
| 2889 | mddev->safemode_delay = 0; |
| 2890 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2891 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2892 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2893 | if (mddev->safemode_delay == 0) |
| 2894 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2895 | if (mddev->safemode_delay < old_delay) |
| 2896 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2897 | } |
| 2898 | return len; |
| 2899 | } |
| 2900 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2901 | __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] | 2902 | |
| 2903 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2904 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2905 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2906 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2907 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2908 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2909 | else if (mddev->clevel[0]) |
| 2910 | return sprintf(page, "%s\n", mddev->clevel); |
| 2911 | else if (mddev->level != LEVEL_NONE) |
| 2912 | return sprintf(page, "%d\n", mddev->level); |
| 2913 | else |
| 2914 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2917 | static ssize_t |
| 2918 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2919 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2920 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2921 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2922 | struct mdk_personality *pers; |
| 2923 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2924 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2925 | |
| 2926 | if (mddev->pers == NULL) { |
| 2927 | if (len == 0) |
| 2928 | return 0; |
| 2929 | if (len >= sizeof(mddev->clevel)) |
| 2930 | return -ENOSPC; |
| 2931 | strncpy(mddev->clevel, buf, len); |
| 2932 | if (mddev->clevel[len-1] == '\n') |
| 2933 | len--; |
| 2934 | mddev->clevel[len] = 0; |
| 2935 | mddev->level = LEVEL_NONE; |
| 2936 | return rv; |
| 2937 | } |
| 2938 | |
| 2939 | /* request to change the personality. Need to ensure: |
| 2940 | * - array is not engaged in resync/recovery/reshape |
| 2941 | * - old personality can be suspended |
| 2942 | * - new personality will access other array. |
| 2943 | */ |
| 2944 | |
| 2945 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2946 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2947 | |
| 2948 | if (!mddev->pers->quiesce) { |
| 2949 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2950 | mdname(mddev), mddev->pers->name); |
| 2951 | return -EINVAL; |
| 2952 | } |
| 2953 | |
| 2954 | /* Now find the new personality */ |
| 2955 | if (len == 0 || len >= sizeof(level)) |
| 2956 | return -EINVAL; |
| 2957 | strncpy(level, buf, len); |
| 2958 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2959 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2960 | level[len] = 0; |
| 2961 | |
| 2962 | request_module("md-%s", level); |
| 2963 | spin_lock(&pers_lock); |
| 2964 | pers = find_pers(LEVEL_NONE, level); |
| 2965 | if (!pers || !try_module_get(pers->owner)) { |
| 2966 | spin_unlock(&pers_lock); |
| 2967 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2968 | return -EINVAL; |
| 2969 | } |
| 2970 | spin_unlock(&pers_lock); |
| 2971 | |
| 2972 | if (pers == mddev->pers) { |
| 2973 | /* Nothing to do! */ |
| 2974 | module_put(pers->owner); |
| 2975 | return rv; |
| 2976 | } |
| 2977 | if (!pers->takeover) { |
| 2978 | module_put(pers->owner); |
| 2979 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2980 | mdname(mddev), level); |
| 2981 | return -EINVAL; |
| 2982 | } |
| 2983 | |
| 2984 | /* ->takeover must set new_* and/or delta_disks |
| 2985 | * if it succeeds, and may set them when it fails. |
| 2986 | */ |
| 2987 | priv = pers->takeover(mddev); |
| 2988 | if (IS_ERR(priv)) { |
| 2989 | mddev->new_level = mddev->level; |
| 2990 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2991 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2992 | mddev->raid_disks -= mddev->delta_disks; |
| 2993 | mddev->delta_disks = 0; |
| 2994 | module_put(pers->owner); |
| 2995 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2996 | mdname(mddev), level); |
| 2997 | return PTR_ERR(priv); |
| 2998 | } |
| 2999 | |
| 3000 | /* Looks like we have a winner */ |
| 3001 | mddev_suspend(mddev); |
| 3002 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3003 | |
| 3004 | if (mddev->pers->sync_request == NULL && |
| 3005 | pers->sync_request != NULL) { |
| 3006 | /* need to add the md_redundancy_group */ |
| 3007 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3008 | printk(KERN_WARNING |
| 3009 | "md: cannot register extra attributes for %s\n", |
| 3010 | mdname(mddev)); |
| 3011 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3012 | } |
| 3013 | if (mddev->pers->sync_request != NULL && |
| 3014 | pers->sync_request == NULL) { |
| 3015 | /* need to remove the md_redundancy_group */ |
| 3016 | if (mddev->to_remove == NULL) |
| 3017 | mddev->to_remove = &md_redundancy_group; |
| 3018 | } |
| 3019 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3020 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3021 | /* Invalidate devices that are now superfluous */ |
| 3022 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3023 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 3024 | rdev->raid_disk = -1; |
| 3025 | clear_bit(In_sync, &rdev->flags); |
| 3026 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3027 | mddev->pers = pers; |
| 3028 | mddev->private = priv; |
| 3029 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3030 | mddev->level = mddev->new_level; |
| 3031 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3032 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3033 | mddev->delta_disks = 0; |
| 3034 | pers->run(mddev); |
| 3035 | mddev_resume(mddev); |
| 3036 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3037 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3038 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3039 | return rv; |
| 3040 | } |
| 3041 | |
| 3042 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3043 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3044 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3045 | |
| 3046 | static ssize_t |
| 3047 | layout_show(mddev_t *mddev, char *page) |
| 3048 | { |
| 3049 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3050 | if (mddev->reshape_position != MaxSector && |
| 3051 | mddev->layout != mddev->new_layout) |
| 3052 | return sprintf(page, "%d (%d)\n", |
| 3053 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3054 | return sprintf(page, "%d\n", mddev->layout); |
| 3055 | } |
| 3056 | |
| 3057 | static ssize_t |
| 3058 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3059 | { |
| 3060 | char *e; |
| 3061 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3062 | |
| 3063 | if (!*buf || (*e && *e != '\n')) |
| 3064 | return -EINVAL; |
| 3065 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3066 | if (mddev->pers) { |
| 3067 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3068 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3069 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3070 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3071 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3072 | if (err) { |
| 3073 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3074 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3075 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3076 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3077 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3078 | if (mddev->reshape_position == MaxSector) |
| 3079 | mddev->layout = n; |
| 3080 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3081 | return len; |
| 3082 | } |
| 3083 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3084 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3085 | |
| 3086 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3087 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3088 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3089 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3090 | if (mddev->raid_disks == 0) |
| 3091 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3092 | if (mddev->reshape_position != MaxSector && |
| 3093 | mddev->delta_disks != 0) |
| 3094 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3095 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3096 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3097 | } |
| 3098 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3099 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3100 | |
| 3101 | static ssize_t |
| 3102 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3103 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3104 | char *e; |
| 3105 | int rv = 0; |
| 3106 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3107 | |
| 3108 | if (!*buf || (*e && *e != '\n')) |
| 3109 | return -EINVAL; |
| 3110 | |
| 3111 | if (mddev->pers) |
| 3112 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3113 | else if (mddev->reshape_position != MaxSector) { |
| 3114 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3115 | mddev->delta_disks = n - olddisks; |
| 3116 | mddev->raid_disks = n; |
| 3117 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3118 | mddev->raid_disks = n; |
| 3119 | return rv ? rv : len; |
| 3120 | } |
| 3121 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3122 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3123 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3124 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3125 | chunk_size_show(mddev_t *mddev, char *page) |
| 3126 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3127 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3128 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3129 | return sprintf(page, "%d (%d)\n", |
| 3130 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3131 | mddev->chunk_sectors << 9); |
| 3132 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | static ssize_t |
| 3136 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3137 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3138 | char *e; |
| 3139 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3140 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3141 | if (!*buf || (*e && *e != '\n')) |
| 3142 | return -EINVAL; |
| 3143 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3144 | if (mddev->pers) { |
| 3145 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3146 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3147 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3148 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3149 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3150 | if (err) { |
| 3151 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3152 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3153 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3154 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3155 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3156 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3157 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3158 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3159 | return len; |
| 3160 | } |
| 3161 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3162 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3163 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3164 | static ssize_t |
| 3165 | resync_start_show(mddev_t *mddev, char *page) |
| 3166 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3167 | if (mddev->recovery_cp == MaxSector) |
| 3168 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3169 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3170 | } |
| 3171 | |
| 3172 | static ssize_t |
| 3173 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3174 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3175 | char *e; |
| 3176 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3177 | |
| 3178 | if (mddev->pers) |
| 3179 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3180 | if (cmd_match(buf, "none")) |
| 3181 | n = MaxSector; |
| 3182 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3183 | return -EINVAL; |
| 3184 | |
| 3185 | mddev->recovery_cp = n; |
| 3186 | return len; |
| 3187 | } |
| 3188 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3189 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3190 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3191 | /* |
| 3192 | * The array state can be: |
| 3193 | * |
| 3194 | * clear |
| 3195 | * No devices, no size, no level |
| 3196 | * Equivalent to STOP_ARRAY ioctl |
| 3197 | * inactive |
| 3198 | * May have some settings, but array is not active |
| 3199 | * all IO results in error |
| 3200 | * When written, doesn't tear down array, but just stops it |
| 3201 | * suspended (not supported yet) |
| 3202 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3203 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3204 | * readonly |
| 3205 | * no resync can happen. no superblocks get written. |
| 3206 | * write requests fail |
| 3207 | * read-auto |
| 3208 | * like readonly, but behaves like 'clean' on a write request. |
| 3209 | * |
| 3210 | * clean - no pending writes, but otherwise active. |
| 3211 | * When written to inactive array, starts without resync |
| 3212 | * If a write request arrives then |
| 3213 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3214 | * if not known, block and switch to write-pending |
| 3215 | * If written to an active array that has pending writes, then fails. |
| 3216 | * active |
| 3217 | * fully active: IO and resync can be happening. |
| 3218 | * When written to inactive array, starts with resync |
| 3219 | * |
| 3220 | * write-pending |
| 3221 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3222 | * |
| 3223 | * active-idle |
| 3224 | * like active, but no writes have been seen for a while (100msec). |
| 3225 | * |
| 3226 | */ |
| 3227 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3228 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3229 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3230 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3231 | "write-pending", "active-idle", NULL }; |
| 3232 | |
| 3233 | static int match_word(const char *word, char **list) |
| 3234 | { |
| 3235 | int n; |
| 3236 | for (n=0; list[n]; n++) |
| 3237 | if (cmd_match(word, list[n])) |
| 3238 | break; |
| 3239 | return n; |
| 3240 | } |
| 3241 | |
| 3242 | static ssize_t |
| 3243 | array_state_show(mddev_t *mddev, char *page) |
| 3244 | { |
| 3245 | enum array_state st = inactive; |
| 3246 | |
| 3247 | if (mddev->pers) |
| 3248 | switch(mddev->ro) { |
| 3249 | case 1: |
| 3250 | st = readonly; |
| 3251 | break; |
| 3252 | case 2: |
| 3253 | st = read_auto; |
| 3254 | break; |
| 3255 | case 0: |
| 3256 | if (mddev->in_sync) |
| 3257 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3258 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3259 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3260 | else if (mddev->safemode) |
| 3261 | st = active_idle; |
| 3262 | else |
| 3263 | st = active; |
| 3264 | } |
| 3265 | else { |
| 3266 | if (list_empty(&mddev->disks) && |
| 3267 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3268 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3269 | st = clear; |
| 3270 | else |
| 3271 | st = inactive; |
| 3272 | } |
| 3273 | return sprintf(page, "%s\n", array_states[st]); |
| 3274 | } |
| 3275 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3276 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3277 | static int do_md_run(mddev_t * mddev); |
| 3278 | static int restart_array(mddev_t *mddev); |
| 3279 | |
| 3280 | static ssize_t |
| 3281 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3282 | { |
| 3283 | int err = -EINVAL; |
| 3284 | enum array_state st = match_word(buf, array_states); |
| 3285 | switch(st) { |
| 3286 | case bad_word: |
| 3287 | break; |
| 3288 | case clear: |
| 3289 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3290 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3291 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3292 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3293 | break; |
| 3294 | case inactive: |
| 3295 | /* stopping an active array */ |
| 3296 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3297 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3298 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3299 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3300 | } else |
| 3301 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3302 | break; |
| 3303 | case suspended: |
| 3304 | break; /* not supported yet */ |
| 3305 | case readonly: |
| 3306 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3307 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3308 | else { |
| 3309 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3310 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3311 | err = do_md_run(mddev); |
| 3312 | } |
| 3313 | break; |
| 3314 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3315 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3316 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3317 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3318 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3319 | err = restart_array(mddev); |
| 3320 | if (err == 0) { |
| 3321 | mddev->ro = 2; |
| 3322 | set_disk_ro(mddev->gendisk, 0); |
| 3323 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3324 | } else { |
| 3325 | mddev->ro = 2; |
| 3326 | err = do_md_run(mddev); |
| 3327 | } |
| 3328 | break; |
| 3329 | case clean: |
| 3330 | if (mddev->pers) { |
| 3331 | restart_array(mddev); |
| 3332 | spin_lock_irq(&mddev->write_lock); |
| 3333 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3334 | if (mddev->in_sync == 0) { |
| 3335 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3336 | if (mddev->safemode == 1) |
| 3337 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3338 | if (mddev->persistent) |
| 3339 | set_bit(MD_CHANGE_CLEAN, |
| 3340 | &mddev->flags); |
| 3341 | } |
| 3342 | err = 0; |
| 3343 | } else |
| 3344 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3345 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3346 | } else |
| 3347 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3348 | break; |
| 3349 | case active: |
| 3350 | if (mddev->pers) { |
| 3351 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3352 | if (mddev->external) |
| 3353 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3354 | wake_up(&mddev->sb_wait); |
| 3355 | err = 0; |
| 3356 | } else { |
| 3357 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3358 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3359 | err = do_md_run(mddev); |
| 3360 | } |
| 3361 | break; |
| 3362 | case write_pending: |
| 3363 | case active_idle: |
| 3364 | /* these cannot be set */ |
| 3365 | break; |
| 3366 | } |
| 3367 | if (err) |
| 3368 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3369 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3370 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3371 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3372 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3373 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3374 | static struct md_sysfs_entry md_array_state = |
| 3375 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3376 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3377 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3378 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3379 | return sprintf(page, "%d\n", |
| 3380 | atomic_read(&mddev->max_corr_read_errors)); |
| 3381 | } |
| 3382 | |
| 3383 | static ssize_t |
| 3384 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3385 | { |
| 3386 | char *e; |
| 3387 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3388 | |
| 3389 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3390 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3391 | return len; |
| 3392 | } |
| 3393 | return -EINVAL; |
| 3394 | } |
| 3395 | |
| 3396 | static struct md_sysfs_entry max_corr_read_errors = |
| 3397 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3398 | max_corrected_read_errors_store); |
| 3399 | |
| 3400 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3401 | null_show(mddev_t *mddev, char *page) |
| 3402 | { |
| 3403 | return -EINVAL; |
| 3404 | } |
| 3405 | |
| 3406 | static ssize_t |
| 3407 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3408 | { |
| 3409 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3410 | /* The new device is added to the array. |
| 3411 | * If the array has a persistent superblock, we read the |
| 3412 | * superblock to initialise info and check validity. |
| 3413 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3414 | * which mainly checks size. |
| 3415 | */ |
| 3416 | char *e; |
| 3417 | int major = simple_strtoul(buf, &e, 10); |
| 3418 | int minor; |
| 3419 | dev_t dev; |
| 3420 | mdk_rdev_t *rdev; |
| 3421 | int err; |
| 3422 | |
| 3423 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3424 | return -EINVAL; |
| 3425 | minor = simple_strtoul(e+1, &e, 10); |
| 3426 | if (*e && *e != '\n') |
| 3427 | return -EINVAL; |
| 3428 | dev = MKDEV(major, minor); |
| 3429 | if (major != MAJOR(dev) || |
| 3430 | minor != MINOR(dev)) |
| 3431 | return -EOVERFLOW; |
| 3432 | |
| 3433 | |
| 3434 | if (mddev->persistent) { |
| 3435 | rdev = md_import_device(dev, mddev->major_version, |
| 3436 | mddev->minor_version); |
| 3437 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3438 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3439 | mdk_rdev_t, same_set); |
| 3440 | err = super_types[mddev->major_version] |
| 3441 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3442 | if (err < 0) |
| 3443 | goto out; |
| 3444 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3445 | } else if (mddev->external) |
| 3446 | rdev = md_import_device(dev, -2, -1); |
| 3447 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3448 | rdev = md_import_device(dev, -1, -1); |
| 3449 | |
| 3450 | if (IS_ERR(rdev)) |
| 3451 | return PTR_ERR(rdev); |
| 3452 | err = bind_rdev_to_array(rdev, mddev); |
| 3453 | out: |
| 3454 | if (err) |
| 3455 | export_rdev(rdev); |
| 3456 | return err ? err : len; |
| 3457 | } |
| 3458 | |
| 3459 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3460 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3461 | |
| 3462 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3463 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3464 | { |
| 3465 | char *end; |
| 3466 | unsigned long chunk, end_chunk; |
| 3467 | |
| 3468 | if (!mddev->bitmap) |
| 3469 | goto out; |
| 3470 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3471 | while (*buf) { |
| 3472 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3473 | if (buf == end) break; |
| 3474 | if (*end == '-') { /* range */ |
| 3475 | buf = end + 1; |
| 3476 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3477 | if (buf == end) break; |
| 3478 | } |
| 3479 | if (*end && !isspace(*end)) break; |
| 3480 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3481 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3482 | } |
| 3483 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3484 | out: |
| 3485 | return len; |
| 3486 | } |
| 3487 | |
| 3488 | static struct md_sysfs_entry md_bitmap = |
| 3489 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3490 | |
| 3491 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3492 | size_show(mddev_t *mddev, char *page) |
| 3493 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3494 | return sprintf(page, "%llu\n", |
| 3495 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3496 | } |
| 3497 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3498 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3499 | |
| 3500 | static ssize_t |
| 3501 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3502 | { |
| 3503 | /* If array is inactive, we can reduce the component size, but |
| 3504 | * not increase it (except from 0). |
| 3505 | * If array is active, we can try an on-line resize |
| 3506 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3507 | sector_t sectors; |
| 3508 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3509 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3510 | if (err < 0) |
| 3511 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3512 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3513 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3514 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3515 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3516 | if (mddev->dev_sectors == 0 || |
| 3517 | mddev->dev_sectors > sectors) |
| 3518 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3519 | else |
| 3520 | err = -ENOSPC; |
| 3521 | } |
| 3522 | return err ? err : len; |
| 3523 | } |
| 3524 | |
| 3525 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3526 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3527 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3528 | |
| 3529 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3530 | * This is one of |
| 3531 | * 'none' for arrays with no metadata (good luck...) |
| 3532 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3533 | * or N.M for internally known formats |
| 3534 | */ |
| 3535 | static ssize_t |
| 3536 | metadata_show(mddev_t *mddev, char *page) |
| 3537 | { |
| 3538 | if (mddev->persistent) |
| 3539 | return sprintf(page, "%d.%d\n", |
| 3540 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3541 | else if (mddev->external) |
| 3542 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3543 | else |
| 3544 | return sprintf(page, "none\n"); |
| 3545 | } |
| 3546 | |
| 3547 | static ssize_t |
| 3548 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3549 | { |
| 3550 | int major, minor; |
| 3551 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3552 | /* Changing the details of 'external' metadata is |
| 3553 | * always permitted. Otherwise there must be |
| 3554 | * no devices attached to the array. |
| 3555 | */ |
| 3556 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3557 | ; |
| 3558 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3559 | return -EBUSY; |
| 3560 | |
| 3561 | if (cmd_match(buf, "none")) { |
| 3562 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3563 | mddev->external = 0; |
| 3564 | mddev->major_version = 0; |
| 3565 | mddev->minor_version = 90; |
| 3566 | return len; |
| 3567 | } |
| 3568 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3569 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3570 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3571 | namelen = sizeof(mddev->metadata_type)-1; |
| 3572 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3573 | mddev->metadata_type[namelen] = 0; |
| 3574 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3575 | mddev->metadata_type[--namelen] = 0; |
| 3576 | mddev->persistent = 0; |
| 3577 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3578 | mddev->major_version = 0; |
| 3579 | mddev->minor_version = 90; |
| 3580 | return len; |
| 3581 | } |
| 3582 | major = simple_strtoul(buf, &e, 10); |
| 3583 | if (e==buf || *e != '.') |
| 3584 | return -EINVAL; |
| 3585 | buf = e+1; |
| 3586 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3587 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3588 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3589 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3590 | return -ENOENT; |
| 3591 | mddev->major_version = major; |
| 3592 | mddev->minor_version = minor; |
| 3593 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3594 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3595 | return len; |
| 3596 | } |
| 3597 | |
| 3598 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3599 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3600 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3601 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3602 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3603 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3604 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3605 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3606 | type = "frozen"; |
| 3607 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3608 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3609 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3610 | type = "reshape"; |
| 3611 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3612 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3613 | type = "resync"; |
| 3614 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3615 | type = "check"; |
| 3616 | else |
| 3617 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3618 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3619 | type = "recover"; |
| 3620 | } |
| 3621 | return sprintf(page, "%s\n", type); |
| 3622 | } |
| 3623 | |
| 3624 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3625 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3626 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3627 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3628 | return -EINVAL; |
| 3629 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3630 | if (cmd_match(page, "frozen")) |
| 3631 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3632 | else |
| 3633 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3634 | |
| 3635 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3636 | if (mddev->sync_thread) { |
| 3637 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3638 | md_unregister_thread(mddev->sync_thread); |
| 3639 | mddev->sync_thread = NULL; |
| 3640 | mddev->recovery = 0; |
| 3641 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3642 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3643 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3644 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3645 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3646 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3647 | else if (cmd_match(page, "recover")) { |
| 3648 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3649 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3650 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3651 | int err; |
| 3652 | if (mddev->pers->start_reshape == NULL) |
| 3653 | return -EINVAL; |
| 3654 | err = mddev->pers->start_reshape(mddev); |
| 3655 | if (err) |
| 3656 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3657 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3658 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3659 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3660 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3661 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3662 | return -EINVAL; |
| 3663 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3664 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3665 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3666 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3667 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3668 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3669 | return len; |
| 3670 | } |
| 3671 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3672 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3673 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3674 | { |
| 3675 | return sprintf(page, "%llu\n", |
| 3676 | (unsigned long long) mddev->resync_mismatches); |
| 3677 | } |
| 3678 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3679 | static struct md_sysfs_entry md_scan_mode = |
| 3680 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3681 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3682 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3683 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3684 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3685 | static ssize_t |
| 3686 | sync_min_show(mddev_t *mddev, char *page) |
| 3687 | { |
| 3688 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3689 | mddev->sync_speed_min ? "local": "system"); |
| 3690 | } |
| 3691 | |
| 3692 | static ssize_t |
| 3693 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3694 | { |
| 3695 | int min; |
| 3696 | char *e; |
| 3697 | if (strncmp(buf, "system", 6)==0) { |
| 3698 | mddev->sync_speed_min = 0; |
| 3699 | return len; |
| 3700 | } |
| 3701 | min = simple_strtoul(buf, &e, 10); |
| 3702 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3703 | return -EINVAL; |
| 3704 | mddev->sync_speed_min = min; |
| 3705 | return len; |
| 3706 | } |
| 3707 | |
| 3708 | static struct md_sysfs_entry md_sync_min = |
| 3709 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3710 | |
| 3711 | static ssize_t |
| 3712 | sync_max_show(mddev_t *mddev, char *page) |
| 3713 | { |
| 3714 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3715 | mddev->sync_speed_max ? "local": "system"); |
| 3716 | } |
| 3717 | |
| 3718 | static ssize_t |
| 3719 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3720 | { |
| 3721 | int max; |
| 3722 | char *e; |
| 3723 | if (strncmp(buf, "system", 6)==0) { |
| 3724 | mddev->sync_speed_max = 0; |
| 3725 | return len; |
| 3726 | } |
| 3727 | max = simple_strtoul(buf, &e, 10); |
| 3728 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3729 | return -EINVAL; |
| 3730 | mddev->sync_speed_max = max; |
| 3731 | return len; |
| 3732 | } |
| 3733 | |
| 3734 | static struct md_sysfs_entry md_sync_max = |
| 3735 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3736 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3737 | static ssize_t |
| 3738 | degraded_show(mddev_t *mddev, char *page) |
| 3739 | { |
| 3740 | return sprintf(page, "%d\n", mddev->degraded); |
| 3741 | } |
| 3742 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3743 | |
| 3744 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3745 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3746 | { |
| 3747 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3748 | } |
| 3749 | |
| 3750 | static ssize_t |
| 3751 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3752 | { |
| 3753 | long n; |
| 3754 | |
| 3755 | if (strict_strtol(buf, 10, &n)) |
| 3756 | return -EINVAL; |
| 3757 | |
| 3758 | if (n != 0 && n != 1) |
| 3759 | return -EINVAL; |
| 3760 | |
| 3761 | mddev->parallel_resync = n; |
| 3762 | |
| 3763 | if (mddev->sync_thread) |
| 3764 | wake_up(&resync_wait); |
| 3765 | |
| 3766 | return len; |
| 3767 | } |
| 3768 | |
| 3769 | /* force parallel resync, even with shared block devices */ |
| 3770 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3771 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3772 | sync_force_parallel_show, sync_force_parallel_store); |
| 3773 | |
| 3774 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3775 | sync_speed_show(mddev_t *mddev, char *page) |
| 3776 | { |
| 3777 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3778 | if (mddev->curr_resync == 0) |
| 3779 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3780 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3781 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3782 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3783 | db = resync - mddev->resync_mark_cnt; |
| 3784 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3785 | } |
| 3786 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3787 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3788 | |
| 3789 | static ssize_t |
| 3790 | sync_completed_show(mddev_t *mddev, char *page) |
| 3791 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3792 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3793 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3794 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3795 | return sprintf(page, "none\n"); |
| 3796 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3797 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3798 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3799 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3800 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3801 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3802 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3803 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3804 | } |
| 3805 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3806 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3807 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3808 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3809 | min_sync_show(mddev_t *mddev, char *page) |
| 3810 | { |
| 3811 | return sprintf(page, "%llu\n", |
| 3812 | (unsigned long long)mddev->resync_min); |
| 3813 | } |
| 3814 | static ssize_t |
| 3815 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3816 | { |
| 3817 | unsigned long long min; |
| 3818 | if (strict_strtoull(buf, 10, &min)) |
| 3819 | return -EINVAL; |
| 3820 | if (min > mddev->resync_max) |
| 3821 | return -EINVAL; |
| 3822 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3823 | return -EBUSY; |
| 3824 | |
| 3825 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3826 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3827 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3828 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3829 | return -EINVAL; |
| 3830 | } |
| 3831 | mddev->resync_min = min; |
| 3832 | |
| 3833 | return len; |
| 3834 | } |
| 3835 | |
| 3836 | static struct md_sysfs_entry md_min_sync = |
| 3837 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3838 | |
| 3839 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3840 | max_sync_show(mddev_t *mddev, char *page) |
| 3841 | { |
| 3842 | if (mddev->resync_max == MaxSector) |
| 3843 | return sprintf(page, "max\n"); |
| 3844 | else |
| 3845 | return sprintf(page, "%llu\n", |
| 3846 | (unsigned long long)mddev->resync_max); |
| 3847 | } |
| 3848 | static ssize_t |
| 3849 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3850 | { |
| 3851 | if (strncmp(buf, "max", 3) == 0) |
| 3852 | mddev->resync_max = MaxSector; |
| 3853 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3854 | unsigned long long max; |
| 3855 | if (strict_strtoull(buf, 10, &max)) |
| 3856 | return -EINVAL; |
| 3857 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3858 | return -EINVAL; |
| 3859 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3860 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3861 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3862 | return -EBUSY; |
| 3863 | |
| 3864 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3865 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3866 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3867 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3868 | return -EINVAL; |
| 3869 | } |
| 3870 | mddev->resync_max = max; |
| 3871 | } |
| 3872 | wake_up(&mddev->recovery_wait); |
| 3873 | return len; |
| 3874 | } |
| 3875 | |
| 3876 | static struct md_sysfs_entry md_max_sync = |
| 3877 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3878 | |
| 3879 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3880 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3881 | { |
| 3882 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3883 | } |
| 3884 | |
| 3885 | static ssize_t |
| 3886 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3887 | { |
| 3888 | char *e; |
| 3889 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3890 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3891 | if (mddev->pers == NULL || |
| 3892 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3893 | return -EINVAL; |
| 3894 | if (buf == e || (*e && *e != '\n')) |
| 3895 | return -EINVAL; |
| 3896 | if (new >= mddev->suspend_hi || |
| 3897 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3898 | mddev->suspend_lo = new; |
| 3899 | mddev->pers->quiesce(mddev, 2); |
| 3900 | return len; |
| 3901 | } else |
| 3902 | return -EINVAL; |
| 3903 | } |
| 3904 | static struct md_sysfs_entry md_suspend_lo = |
| 3905 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3906 | |
| 3907 | |
| 3908 | static ssize_t |
| 3909 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3910 | { |
| 3911 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3912 | } |
| 3913 | |
| 3914 | static ssize_t |
| 3915 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3916 | { |
| 3917 | char *e; |
| 3918 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3919 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3920 | if (mddev->pers == NULL || |
| 3921 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3922 | return -EINVAL; |
| 3923 | if (buf == e || (*e && *e != '\n')) |
| 3924 | return -EINVAL; |
| 3925 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3926 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3927 | mddev->suspend_hi = new; |
| 3928 | mddev->pers->quiesce(mddev, 1); |
| 3929 | mddev->pers->quiesce(mddev, 0); |
| 3930 | return len; |
| 3931 | } else |
| 3932 | return -EINVAL; |
| 3933 | } |
| 3934 | static struct md_sysfs_entry md_suspend_hi = |
| 3935 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3936 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3937 | static ssize_t |
| 3938 | reshape_position_show(mddev_t *mddev, char *page) |
| 3939 | { |
| 3940 | if (mddev->reshape_position != MaxSector) |
| 3941 | return sprintf(page, "%llu\n", |
| 3942 | (unsigned long long)mddev->reshape_position); |
| 3943 | strcpy(page, "none\n"); |
| 3944 | return 5; |
| 3945 | } |
| 3946 | |
| 3947 | static ssize_t |
| 3948 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3949 | { |
| 3950 | char *e; |
| 3951 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3952 | if (mddev->pers) |
| 3953 | return -EBUSY; |
| 3954 | if (buf == e || (*e && *e != '\n')) |
| 3955 | return -EINVAL; |
| 3956 | mddev->reshape_position = new; |
| 3957 | mddev->delta_disks = 0; |
| 3958 | mddev->new_level = mddev->level; |
| 3959 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3960 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3961 | return len; |
| 3962 | } |
| 3963 | |
| 3964 | static struct md_sysfs_entry md_reshape_position = |
| 3965 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3966 | reshape_position_store); |
| 3967 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3968 | static ssize_t |
| 3969 | array_size_show(mddev_t *mddev, char *page) |
| 3970 | { |
| 3971 | if (mddev->external_size) |
| 3972 | return sprintf(page, "%llu\n", |
| 3973 | (unsigned long long)mddev->array_sectors/2); |
| 3974 | else |
| 3975 | return sprintf(page, "default\n"); |
| 3976 | } |
| 3977 | |
| 3978 | static ssize_t |
| 3979 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3980 | { |
| 3981 | sector_t sectors; |
| 3982 | |
| 3983 | if (strncmp(buf, "default", 7) == 0) { |
| 3984 | if (mddev->pers) |
| 3985 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3986 | else |
| 3987 | sectors = mddev->array_sectors; |
| 3988 | |
| 3989 | mddev->external_size = 0; |
| 3990 | } else { |
| 3991 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3992 | return -EINVAL; |
| 3993 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3994 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3995 | |
| 3996 | mddev->external_size = 1; |
| 3997 | } |
| 3998 | |
| 3999 | mddev->array_sectors = sectors; |
| 4000 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4001 | if (mddev->pers) |
| 4002 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4003 | |
| 4004 | return len; |
| 4005 | } |
| 4006 | |
| 4007 | static struct md_sysfs_entry md_array_size = |
| 4008 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4009 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4010 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4011 | static struct attribute *md_default_attrs[] = { |
| 4012 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4013 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4014 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4015 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4016 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4017 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4018 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4019 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4020 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4021 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4022 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4023 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4024 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4025 | NULL, |
| 4026 | }; |
| 4027 | |
| 4028 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4029 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4030 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4031 | &md_sync_min.attr, |
| 4032 | &md_sync_max.attr, |
| 4033 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4034 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4035 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4036 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4037 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4038 | &md_suspend_lo.attr, |
| 4039 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4040 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4041 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4042 | NULL, |
| 4043 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4044 | static struct attribute_group md_redundancy_group = { |
| 4045 | .name = NULL, |
| 4046 | .attrs = md_redundancy_attrs, |
| 4047 | }; |
| 4048 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4049 | |
| 4050 | static ssize_t |
| 4051 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4052 | { |
| 4053 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4054 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4055 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4056 | |
| 4057 | if (!entry->show) |
| 4058 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4059 | rv = mddev_lock(mddev); |
| 4060 | if (!rv) { |
| 4061 | rv = entry->show(mddev, page); |
| 4062 | mddev_unlock(mddev); |
| 4063 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4064 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | static ssize_t |
| 4068 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4069 | const char *page, size_t length) |
| 4070 | { |
| 4071 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4072 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4073 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4074 | |
| 4075 | if (!entry->store) |
| 4076 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4077 | if (!capable(CAP_SYS_ADMIN)) |
| 4078 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4079 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4080 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4081 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4082 | if (!rv) { |
| 4083 | rv = entry->store(mddev, page, length); |
| 4084 | mddev_unlock(mddev); |
| 4085 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4086 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | static void md_free(struct kobject *ko) |
| 4090 | { |
| 4091 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4092 | |
| 4093 | if (mddev->sysfs_state) |
| 4094 | sysfs_put(mddev->sysfs_state); |
| 4095 | |
| 4096 | if (mddev->gendisk) { |
| 4097 | del_gendisk(mddev->gendisk); |
| 4098 | put_disk(mddev->gendisk); |
| 4099 | } |
| 4100 | if (mddev->queue) |
| 4101 | blk_cleanup_queue(mddev->queue); |
| 4102 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4103 | kfree(mddev); |
| 4104 | } |
| 4105 | |
| 4106 | static struct sysfs_ops md_sysfs_ops = { |
| 4107 | .show = md_attr_show, |
| 4108 | .store = md_attr_store, |
| 4109 | }; |
| 4110 | static struct kobj_type md_ktype = { |
| 4111 | .release = md_free, |
| 4112 | .sysfs_ops = &md_sysfs_ops, |
| 4113 | .default_attrs = md_default_attrs, |
| 4114 | }; |
| 4115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | int mdp_major = 0; |
| 4117 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4118 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4119 | { |
| 4120 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4121 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4122 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4123 | kobject_del(&mddev->kobj); |
| 4124 | kobject_put(&mddev->kobj); |
| 4125 | } |
| 4126 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4127 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4129 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | mddev_t *mddev = mddev_find(dev); |
| 4131 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4132 | int partitioned; |
| 4133 | int shift; |
| 4134 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4135 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | |
| 4137 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4138 | return -ENODEV; |
| 4139 | |
| 4140 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4141 | shift = partitioned ? MdpMinorShift : 0; |
| 4142 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4144 | /* wait for any previous instance if this device |
| 4145 | * to be completed removed (mddev_delayed_delete). |
| 4146 | */ |
| 4147 | flush_scheduled_work(); |
| 4148 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4149 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4150 | error = -EEXIST; |
| 4151 | if (mddev->gendisk) |
| 4152 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4153 | |
| 4154 | if (name) { |
| 4155 | /* Need to ensure that 'name' is not a duplicate. |
| 4156 | */ |
| 4157 | mddev_t *mddev2; |
| 4158 | spin_lock(&all_mddevs_lock); |
| 4159 | |
| 4160 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4161 | if (mddev2->gendisk && |
| 4162 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4163 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4164 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4165 | } |
| 4166 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4168 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4169 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4170 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4171 | if (!mddev->queue) |
| 4172 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4173 | mddev->queue->queuedata = mddev; |
| 4174 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4175 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4176 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4177 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4178 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | disk = alloc_disk(1 << shift); |
| 4181 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4182 | blk_cleanup_queue(mddev->queue); |
| 4183 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4184 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4186 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4188 | if (name) |
| 4189 | strcpy(disk->disk_name, name); |
| 4190 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4192 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | disk->fops = &md_fops; |
| 4195 | disk->private_data = mddev; |
| 4196 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4197 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4198 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4199 | * remove it now. |
| 4200 | */ |
| 4201 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | add_disk(disk); |
| 4203 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4204 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4205 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4206 | if (error) { |
| 4207 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4208 | * __must_check, we must do something with the result |
| 4209 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4210 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4211 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4212 | error = 0; |
| 4213 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4214 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4215 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4216 | abort: |
| 4217 | mutex_unlock(&disks_mutex); |
| 4218 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4219 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4220 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4221 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4222 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4223 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4227 | { |
| 4228 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | return NULL; |
| 4230 | } |
| 4231 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4232 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4233 | { |
| 4234 | /* val must be "md_*" where * is not all digits. |
| 4235 | * We allocate an array with a large free minor number, and |
| 4236 | * set the name to val. val must not already be an active name. |
| 4237 | */ |
| 4238 | int len = strlen(val); |
| 4239 | char buf[DISK_NAME_LEN]; |
| 4240 | |
| 4241 | while (len && val[len-1] == '\n') |
| 4242 | len--; |
| 4243 | if (len >= DISK_NAME_LEN) |
| 4244 | return -E2BIG; |
| 4245 | strlcpy(buf, val, len+1); |
| 4246 | if (strncmp(buf, "md_", 3) != 0) |
| 4247 | return -EINVAL; |
| 4248 | return md_alloc(0, buf); |
| 4249 | } |
| 4250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | static void md_safemode_timeout(unsigned long data) |
| 4252 | { |
| 4253 | mddev_t *mddev = (mddev_t *) data; |
| 4254 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4255 | if (!atomic_read(&mddev->writes_pending)) { |
| 4256 | mddev->safemode = 1; |
| 4257 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4258 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | md_wakeup_thread(mddev->thread); |
| 4261 | } |
| 4262 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4263 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | |
| 4265 | static int do_md_run(mddev_t * mddev) |
| 4266 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4267 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | mdk_rdev_t *rdev; |
| 4269 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4270 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4272 | if (list_empty(&mddev->disks)) |
| 4273 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | |
| 4276 | if (mddev->pers) |
| 4277 | return -EBUSY; |
| 4278 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4279 | /* These two calls synchronise us with the |
| 4280 | * sysfs_remove_group calls in mddev_unlock, |
| 4281 | * so they must have completed. |
| 4282 | */ |
| 4283 | mutex_lock(&mddev->open_mutex); |
| 4284 | mutex_unlock(&mddev->open_mutex); |
| 4285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | /* |
| 4287 | * Analyze all RAID superblock(s) |
| 4288 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4289 | if (!mddev->raid_disks) { |
| 4290 | if (!mddev->persistent) |
| 4291 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4292 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4295 | if (mddev->level != LEVEL_NONE) |
| 4296 | request_module("md-level-%d", mddev->level); |
| 4297 | else if (mddev->clevel[0]) |
| 4298 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | |
| 4300 | /* |
| 4301 | * Drop all container device buffers, from now on |
| 4302 | * the only valid external interface is through the md |
| 4303 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4305 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4306 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | continue; |
| 4308 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4309 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4310 | |
| 4311 | /* perform some consistency tests on the device. |
| 4312 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4313 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4314 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4315 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4316 | if (mddev->dev_sectors && |
| 4317 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4318 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4319 | printk("md: %s: data overlaps metadata\n", |
| 4320 | mdname(mddev)); |
| 4321 | return -EINVAL; |
| 4322 | } |
| 4323 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4324 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4325 | > rdev->data_offset) { |
| 4326 | printk("md: %s: metadata overlaps data\n", |
| 4327 | mdname(mddev)); |
| 4328 | return -EINVAL; |
| 4329 | } |
| 4330 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4331 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | } |
| 4333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | |
| 4336 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4337 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4338 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4340 | if (mddev->level != LEVEL_NONE) |
| 4341 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4342 | mddev->level); |
| 4343 | else |
| 4344 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4345 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | return -EINVAL; |
| 4347 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4348 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4350 | if (mddev->level != pers->level) { |
| 4351 | mddev->level = pers->level; |
| 4352 | mddev->new_level = pers->level; |
| 4353 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4354 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4356 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4357 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4358 | /* This personality cannot handle reshaping... */ |
| 4359 | mddev->pers = NULL; |
| 4360 | module_put(pers->owner); |
| 4361 | return -EINVAL; |
| 4362 | } |
| 4363 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4364 | if (pers->sync_request) { |
| 4365 | /* Warn if this is a potentially silly |
| 4366 | * configuration. |
| 4367 | */ |
| 4368 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4369 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4370 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4371 | |
| 4372 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4373 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4374 | if (rdev < rdev2 && |
| 4375 | rdev->bdev->bd_contains == |
| 4376 | rdev2->bdev->bd_contains) { |
| 4377 | printk(KERN_WARNING |
| 4378 | "%s: WARNING: %s appears to be" |
| 4379 | " on the same physical disk as" |
| 4380 | " %s.\n", |
| 4381 | mdname(mddev), |
| 4382 | bdevname(rdev->bdev,b), |
| 4383 | bdevname(rdev2->bdev,b2)); |
| 4384 | warned = 1; |
| 4385 | } |
| 4386 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4387 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4388 | if (warned) |
| 4389 | printk(KERN_WARNING |
| 4390 | "True protection against single-disk" |
| 4391 | " failure might be compromised.\n"); |
| 4392 | } |
| 4393 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4394 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4395 | /* may be over-ridden by personality */ |
| 4396 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4397 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4398 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4399 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4401 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4402 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4403 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4404 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4405 | if (err) |
| 4406 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4407 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4408 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4409 | " but 'external_size' not in effect?\n", __func__); |
| 4410 | printk(KERN_ERR |
| 4411 | "md: invalid array_size %llu > default size %llu\n", |
| 4412 | (unsigned long long)mddev->array_sectors / 2, |
| 4413 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4414 | err = -EINVAL; |
| 4415 | mddev->pers->stop(mddev); |
| 4416 | } |
| 4417 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4418 | err = bitmap_create(mddev); |
| 4419 | if (err) { |
| 4420 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4421 | mdname(mddev), err); |
| 4422 | mddev->pers->stop(mddev); |
| 4423 | } |
| 4424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | module_put(mddev->pers->owner); |
| 4427 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4428 | bitmap_destroy(mddev); |
| 4429 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4431 | if (mddev->pers->sync_request) { |
| 4432 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4433 | printk(KERN_WARNING |
| 4434 | "md: cannot register extra attributes for %s\n", |
| 4435 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4436 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4437 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4438 | mddev->ro = 0; |
| 4439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4441 | atomic_set(&mddev->max_corr_read_errors, |
| 4442 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | mddev->safemode = 0; |
| 4444 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4445 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4446 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4448 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4449 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4450 | if (rdev->raid_disk >= 0) { |
| 4451 | char nm[20]; |
| 4452 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4453 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4454 | printk("md: cannot register %s for %s\n", |
| 4455 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | |
| 4458 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4459 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4460 | if (mddev->flags) |
| 4461 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4463 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4465 | md_wakeup_thread(mddev->thread); |
| 4466 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4467 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4468 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4469 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4470 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4471 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4472 | if (mddev->sysfs_action) |
| 4473 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4474 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4475 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | return 0; |
| 4477 | } |
| 4478 | |
| 4479 | static int restart_array(mddev_t *mddev) |
| 4480 | { |
| 4481 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4483 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4485 | return -ENXIO; |
| 4486 | if (!mddev->pers) |
| 4487 | return -EINVAL; |
| 4488 | if (!mddev->ro) |
| 4489 | return -EBUSY; |
| 4490 | mddev->safemode = 0; |
| 4491 | mddev->ro = 0; |
| 4492 | set_disk_ro(disk, 0); |
| 4493 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4494 | mdname(mddev)); |
| 4495 | /* Kick recovery or resync if necessary */ |
| 4496 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4497 | md_wakeup_thread(mddev->thread); |
| 4498 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4499 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4500 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4503 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4504 | * to the file ourselves */ |
| 4505 | static int deny_bitmap_write_access(struct file * file) |
| 4506 | { |
| 4507 | struct inode *inode = file->f_mapping->host; |
| 4508 | |
| 4509 | spin_lock(&inode->i_lock); |
| 4510 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4511 | spin_unlock(&inode->i_lock); |
| 4512 | return -ETXTBSY; |
| 4513 | } |
| 4514 | atomic_set(&inode->i_writecount, -1); |
| 4515 | spin_unlock(&inode->i_lock); |
| 4516 | |
| 4517 | return 0; |
| 4518 | } |
| 4519 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4520 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4521 | { |
| 4522 | struct inode *inode = file->f_mapping->host; |
| 4523 | |
| 4524 | spin_lock(&inode->i_lock); |
| 4525 | atomic_set(&inode->i_writecount, 1); |
| 4526 | spin_unlock(&inode->i_lock); |
| 4527 | } |
| 4528 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4529 | /* mode: |
| 4530 | * 0 - completely stop and dis-assemble array |
| 4531 | * 1 - switch to readonly |
| 4532 | * 2 - stop but do not disassemble array |
| 4533 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4534 | 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] | 4535 | { |
| 4536 | int err = 0; |
| 4537 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4538 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4540 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4541 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4542 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4543 | err = -EBUSY; |
| 4544 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | |
| 4546 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4547 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4549 | md_unregister_thread(mddev->sync_thread); |
| 4550 | mddev->sync_thread = NULL; |
| 4551 | } |
| 4552 | |
| 4553 | del_timer_sync(&mddev->safemode_timer); |
| 4554 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4555 | switch(mode) { |
| 4556 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4558 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | goto out; |
| 4560 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4561 | break; |
| 4562 | case 0: /* disassemble */ |
| 4563 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4564 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4565 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | if (mddev->ro) |
| 4567 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4570 | mddev->queue->merge_bvec_fn = NULL; |
| 4571 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4572 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | module_put(mddev->pers->owner); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 4574 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4575 | mddev->to_remove = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4577 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4578 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4579 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4580 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4581 | if (rdev->raid_disk >= 0) { |
| 4582 | char nm[20]; |
| 4583 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4584 | sysfs_remove_link(&mddev->kobj, nm); |
| 4585 | } |
| 4586 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4587 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4588 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | if (mddev->ro) |
| 4591 | mddev->ro = 0; |
| 4592 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4593 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | /* mark array as shutdown cleanly */ |
| 4595 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4596 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4598 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4600 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4601 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4603 | out: |
| 4604 | mutex_unlock(&mddev->open_mutex); |
| 4605 | if (err) |
| 4606 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | /* |
| 4608 | * Free resources if final stop |
| 4609 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4610 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4613 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4614 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4615 | if (mddev->bitmap_info.file) { |
| 4616 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4617 | fput(mddev->bitmap_info.file); |
| 4618 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4619 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4620 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | export_array(mddev); |
| 4623 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4624 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4625 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4626 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4627 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4628 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4629 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4630 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4631 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4632 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4633 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4634 | mddev->level = LEVEL_NONE; |
| 4635 | mddev->clevel[0] = 0; |
| 4636 | mddev->flags = 0; |
| 4637 | mddev->ro = 0; |
| 4638 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4639 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4640 | mddev->ctime = mddev->utime = 0; |
| 4641 | mddev->layout = 0; |
| 4642 | mddev->max_disks = 0; |
| 4643 | mddev->events = 0; |
| 4644 | mddev->delta_disks = 0; |
| 4645 | mddev->new_level = LEVEL_NONE; |
| 4646 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4647 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4648 | mddev->curr_resync = 0; |
| 4649 | mddev->resync_mismatches = 0; |
| 4650 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4651 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4652 | mddev->recovery = 0; |
| 4653 | mddev->in_sync = 0; |
| 4654 | mddev->changed = 0; |
| 4655 | mddev->degraded = 0; |
| 4656 | mddev->barriers_work = 0; |
| 4657 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4658 | mddev->bitmap_info.offset = 0; |
| 4659 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 4660 | mddev->bitmap_info.chunksize = 0; |
| 4661 | mddev->bitmap_info.daemon_sleep = 0; |
| 4662 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4663 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4664 | if (mddev->hold_active == UNTIL_STOP) |
| 4665 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4666 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4667 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4669 | mdname(mddev)); |
| 4670 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4671 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4672 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4673 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | return err; |
| 4675 | } |
| 4676 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4677 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | static void autorun_array(mddev_t *mddev) |
| 4679 | { |
| 4680 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | int err; |
| 4682 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4683 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | |
| 4686 | printk(KERN_INFO "md: running: "); |
| 4687 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4688 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | char b[BDEVNAME_SIZE]; |
| 4690 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4691 | } |
| 4692 | printk("\n"); |
| 4693 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4694 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | if (err) { |
| 4696 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4697 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | /* |
| 4702 | * lets try to run arrays based on all disks that have arrived |
| 4703 | * until now. (those are in pending_raid_disks) |
| 4704 | * |
| 4705 | * the method: pick the first pending disk, collect all disks with |
| 4706 | * the same UUID, remove all from the pending list and put them into |
| 4707 | * the 'same_array' list. Then order this list based on superblock |
| 4708 | * update time (freshest comes first), kick out 'old' disks and |
| 4709 | * compare superblocks. If everything's fine then run it. |
| 4710 | * |
| 4711 | * If "unit" is allocated, then bump its reference count |
| 4712 | */ |
| 4713 | static void autorun_devices(int part) |
| 4714 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4715 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | mddev_t *mddev; |
| 4717 | char b[BDEVNAME_SIZE]; |
| 4718 | |
| 4719 | printk(KERN_INFO "md: autorun ...\n"); |
| 4720 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4721 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4723 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | rdev0 = list_entry(pending_raid_disks.next, |
| 4725 | mdk_rdev_t, same_set); |
| 4726 | |
| 4727 | printk(KERN_INFO "md: considering %s ...\n", |
| 4728 | bdevname(rdev0->bdev,b)); |
| 4729 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4730 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4732 | printk(KERN_INFO "md: adding %s ...\n", |
| 4733 | bdevname(rdev->bdev,b)); |
| 4734 | list_move(&rdev->same_set, &candidates); |
| 4735 | } |
| 4736 | /* |
| 4737 | * now we have a set of devices, with all of them having |
| 4738 | * mostly sane superblocks. It's time to allocate the |
| 4739 | * mddev. |
| 4740 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4741 | if (part) { |
| 4742 | dev = MKDEV(mdp_major, |
| 4743 | rdev0->preferred_minor << MdpMinorShift); |
| 4744 | unit = MINOR(dev) >> MdpMinorShift; |
| 4745 | } else { |
| 4746 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4747 | unit = MINOR(dev); |
| 4748 | } |
| 4749 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4751 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4752 | break; |
| 4753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
| 4755 | md_probe(dev, NULL, NULL); |
| 4756 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4757 | if (!mddev || !mddev->gendisk) { |
| 4758 | if (mddev) |
| 4759 | mddev_put(mddev); |
| 4760 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | "md: cannot allocate memory for md drive.\n"); |
| 4762 | break; |
| 4763 | } |
| 4764 | if (mddev_lock(mddev)) |
| 4765 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4766 | mdname(mddev)); |
| 4767 | else if (mddev->raid_disks || mddev->major_version |
| 4768 | || !list_empty(&mddev->disks)) { |
| 4769 | printk(KERN_WARNING |
| 4770 | "md: %s already running, cannot run %s\n", |
| 4771 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4772 | mddev_unlock(mddev); |
| 4773 | } else { |
| 4774 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4775 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4776 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | list_del_init(&rdev->same_set); |
| 4778 | if (bind_rdev_to_array(rdev, mddev)) |
| 4779 | export_rdev(rdev); |
| 4780 | } |
| 4781 | autorun_array(mddev); |
| 4782 | mddev_unlock(mddev); |
| 4783 | } |
| 4784 | /* on success, candidates will be empty, on error |
| 4785 | * it won't... |
| 4786 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4787 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4788 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | mddev_put(mddev); |
| 4792 | } |
| 4793 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4794 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4795 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | static int get_version(void __user * arg) |
| 4798 | { |
| 4799 | mdu_version_t ver; |
| 4800 | |
| 4801 | ver.major = MD_MAJOR_VERSION; |
| 4802 | ver.minor = MD_MINOR_VERSION; |
| 4803 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4804 | |
| 4805 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4806 | return -EFAULT; |
| 4807 | |
| 4808 | return 0; |
| 4809 | } |
| 4810 | |
| 4811 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4812 | { |
| 4813 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4814 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4817 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4818 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4820 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | failed++; |
| 4822 | else { |
| 4823 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4824 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4825 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | else |
| 4827 | spare++; |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | info.major_version = mddev->major_version; |
| 4832 | info.minor_version = mddev->minor_version; |
| 4833 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4834 | info.ctime = mddev->ctime; |
| 4835 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4836 | info.size = mddev->dev_sectors / 2; |
| 4837 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4838 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | info.nr_disks = nr; |
| 4840 | info.raid_disks = mddev->raid_disks; |
| 4841 | info.md_minor = mddev->md_minor; |
| 4842 | info.not_persistent= !mddev->persistent; |
| 4843 | |
| 4844 | info.utime = mddev->utime; |
| 4845 | info.state = 0; |
| 4846 | if (mddev->in_sync) |
| 4847 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4848 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4849 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4850 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | info.working_disks = working; |
| 4852 | info.failed_disks = failed; |
| 4853 | info.spare_disks = spare; |
| 4854 | |
| 4855 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4856 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | |
| 4858 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4859 | return -EFAULT; |
| 4860 | |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4864 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4865 | { |
| 4866 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4867 | char *ptr, *buf = NULL; |
| 4868 | int err = -ENOMEM; |
| 4869 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4870 | if (md_allow_write(mddev)) |
| 4871 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4872 | else |
| 4873 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4874 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4875 | if (!file) |
| 4876 | goto out; |
| 4877 | |
| 4878 | /* bitmap disabled, zero the first byte and copy out */ |
| 4879 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4880 | file->pathname[0] = '\0'; |
| 4881 | goto copy_out; |
| 4882 | } |
| 4883 | |
| 4884 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4885 | if (!buf) |
| 4886 | goto out; |
| 4887 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4888 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4889 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4890 | goto out; |
| 4891 | |
| 4892 | strcpy(file->pathname, ptr); |
| 4893 | |
| 4894 | copy_out: |
| 4895 | err = 0; |
| 4896 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4897 | err = -EFAULT; |
| 4898 | out: |
| 4899 | kfree(buf); |
| 4900 | kfree(file); |
| 4901 | return err; |
| 4902 | } |
| 4903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4905 | { |
| 4906 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | mdk_rdev_t *rdev; |
| 4908 | |
| 4909 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4910 | return -EFAULT; |
| 4911 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4912 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | if (rdev) { |
| 4914 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4915 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4916 | info.raid_disk = rdev->raid_disk; |
| 4917 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4918 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4920 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4922 | info.state |= (1<<MD_DISK_SYNC); |
| 4923 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4924 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4925 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | } else { |
| 4927 | info.major = info.minor = 0; |
| 4928 | info.raid_disk = -1; |
| 4929 | info.state = (1<<MD_DISK_REMOVED); |
| 4930 | } |
| 4931 | |
| 4932 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4933 | return -EFAULT; |
| 4934 | |
| 4935 | return 0; |
| 4936 | } |
| 4937 | |
| 4938 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4939 | { |
| 4940 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4941 | mdk_rdev_t *rdev; |
| 4942 | dev_t dev = MKDEV(info->major,info->minor); |
| 4943 | |
| 4944 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4945 | return -EOVERFLOW; |
| 4946 | |
| 4947 | if (!mddev->raid_disks) { |
| 4948 | int err; |
| 4949 | /* expecting a device which has a superblock */ |
| 4950 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4951 | if (IS_ERR(rdev)) { |
| 4952 | printk(KERN_WARNING |
| 4953 | "md: md_import_device returned %ld\n", |
| 4954 | PTR_ERR(rdev)); |
| 4955 | return PTR_ERR(rdev); |
| 4956 | } |
| 4957 | if (!list_empty(&mddev->disks)) { |
| 4958 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4959 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4960 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4962 | if (err < 0) { |
| 4963 | printk(KERN_WARNING |
| 4964 | "md: %s has different UUID to %s\n", |
| 4965 | bdevname(rdev->bdev,b), |
| 4966 | bdevname(rdev0->bdev,b2)); |
| 4967 | export_rdev(rdev); |
| 4968 | return -EINVAL; |
| 4969 | } |
| 4970 | } |
| 4971 | err = bind_rdev_to_array(rdev, mddev); |
| 4972 | if (err) |
| 4973 | export_rdev(rdev); |
| 4974 | return err; |
| 4975 | } |
| 4976 | |
| 4977 | /* |
| 4978 | * add_new_disk can be used once the array is assembled |
| 4979 | * to add "hot spares". They must already have a superblock |
| 4980 | * written |
| 4981 | */ |
| 4982 | if (mddev->pers) { |
| 4983 | int err; |
| 4984 | if (!mddev->pers->hot_add_disk) { |
| 4985 | printk(KERN_WARNING |
| 4986 | "%s: personality does not support diskops!\n", |
| 4987 | mdname(mddev)); |
| 4988 | return -EINVAL; |
| 4989 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4990 | if (mddev->persistent) |
| 4991 | rdev = md_import_device(dev, mddev->major_version, |
| 4992 | mddev->minor_version); |
| 4993 | else |
| 4994 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | if (IS_ERR(rdev)) { |
| 4996 | printk(KERN_WARNING |
| 4997 | "md: md_import_device returned %ld\n", |
| 4998 | PTR_ERR(rdev)); |
| 4999 | return PTR_ERR(rdev); |
| 5000 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5001 | /* set save_raid_disk if appropriate */ |
| 5002 | if (!mddev->persistent) { |
| 5003 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5004 | info->raid_disk < mddev->raid_disks) |
| 5005 | rdev->raid_disk = info->raid_disk; |
| 5006 | else |
| 5007 | rdev->raid_disk = -1; |
| 5008 | } else |
| 5009 | super_types[mddev->major_version]. |
| 5010 | validate_super(mddev, rdev); |
| 5011 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5012 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5013 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5014 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5015 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5016 | else |
| 5017 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | rdev->raid_disk = -1; |
| 5020 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5021 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5022 | /* If there is hot_add_disk but no hot_remove_disk |
| 5023 | * then added disks for geometry changes, |
| 5024 | * and should be added immediately. |
| 5025 | */ |
| 5026 | super_types[mddev->major_version]. |
| 5027 | validate_super(mddev, rdev); |
| 5028 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5029 | if (err) |
| 5030 | unbind_rdev_from_array(rdev); |
| 5031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | if (err) |
| 5033 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5034 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5035 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5036 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5037 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5038 | if (mddev->degraded) |
| 5039 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5040 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5041 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | return err; |
| 5043 | } |
| 5044 | |
| 5045 | /* otherwise, add_new_disk is only allowed |
| 5046 | * for major_version==0 superblocks |
| 5047 | */ |
| 5048 | if (mddev->major_version != 0) { |
| 5049 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5050 | mdname(mddev)); |
| 5051 | return -EINVAL; |
| 5052 | } |
| 5053 | |
| 5054 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5055 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5056 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | if (IS_ERR(rdev)) { |
| 5058 | printk(KERN_WARNING |
| 5059 | "md: error, md_import_device() returned %ld\n", |
| 5060 | PTR_ERR(rdev)); |
| 5061 | return PTR_ERR(rdev); |
| 5062 | } |
| 5063 | rdev->desc_nr = info->number; |
| 5064 | if (info->raid_disk < mddev->raid_disks) |
| 5065 | rdev->raid_disk = info->raid_disk; |
| 5066 | else |
| 5067 | rdev->raid_disk = -1; |
| 5068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5070 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5071 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5073 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5074 | set_bit(WriteMostly, &rdev->flags); |
| 5075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | if (!mddev->persistent) { |
| 5077 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5078 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5080 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5081 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5083 | err = bind_rdev_to_array(rdev, mddev); |
| 5084 | if (err) { |
| 5085 | export_rdev(rdev); |
| 5086 | return err; |
| 5087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
| 5093 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5094 | { |
| 5095 | char b[BDEVNAME_SIZE]; |
| 5096 | mdk_rdev_t *rdev; |
| 5097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | rdev = find_rdev(mddev, dev); |
| 5099 | if (!rdev) |
| 5100 | return -ENXIO; |
| 5101 | |
| 5102 | if (rdev->raid_disk >= 0) |
| 5103 | goto busy; |
| 5104 | |
| 5105 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5106 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5107 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | |
| 5109 | return 0; |
| 5110 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5111 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5113 | return -EBUSY; |
| 5114 | } |
| 5115 | |
| 5116 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5117 | { |
| 5118 | char b[BDEVNAME_SIZE]; |
| 5119 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | mdk_rdev_t *rdev; |
| 5121 | |
| 5122 | if (!mddev->pers) |
| 5123 | return -ENODEV; |
| 5124 | |
| 5125 | if (mddev->major_version != 0) { |
| 5126 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5127 | " version-0 superblocks.\n", |
| 5128 | mdname(mddev)); |
| 5129 | return -EINVAL; |
| 5130 | } |
| 5131 | if (!mddev->pers->hot_add_disk) { |
| 5132 | printk(KERN_WARNING |
| 5133 | "%s: personality does not support diskops!\n", |
| 5134 | mdname(mddev)); |
| 5135 | return -EINVAL; |
| 5136 | } |
| 5137 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5138 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | if (IS_ERR(rdev)) { |
| 5140 | printk(KERN_WARNING |
| 5141 | "md: error, md_import_device() returned %ld\n", |
| 5142 | PTR_ERR(rdev)); |
| 5143 | return -EINVAL; |
| 5144 | } |
| 5145 | |
| 5146 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5147 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5149 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5151 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5153 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | printk(KERN_WARNING |
| 5155 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5156 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5157 | err = -EINVAL; |
| 5158 | goto abort_export; |
| 5159 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5160 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5162 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5163 | err = bind_rdev_to_array(rdev, mddev); |
| 5164 | if (err) |
| 5165 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | |
| 5167 | /* |
| 5168 | * The rest should better be atomic, we can have disk failures |
| 5169 | * noticed in interrupt contexts ... |
| 5170 | */ |
| 5171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | rdev->raid_disk = -1; |
| 5173 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5174 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | |
| 5176 | /* |
| 5177 | * Kick recovery, maybe this spare has to be added to the |
| 5178 | * array immediately. |
| 5179 | */ |
| 5180 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5181 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5182 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | return 0; |
| 5184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | abort_export: |
| 5186 | export_rdev(rdev); |
| 5187 | return err; |
| 5188 | } |
| 5189 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5190 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5191 | { |
| 5192 | int err; |
| 5193 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5194 | if (mddev->pers) { |
| 5195 | if (!mddev->pers->quiesce) |
| 5196 | return -EBUSY; |
| 5197 | if (mddev->recovery || mddev->sync_thread) |
| 5198 | return -EBUSY; |
| 5199 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5200 | } |
| 5201 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5202 | |
| 5203 | if (fd >= 0) { |
| 5204 | if (mddev->bitmap) |
| 5205 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5206 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5207 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5208 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5209 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5210 | mdname(mddev)); |
| 5211 | return -EBADF; |
| 5212 | } |
| 5213 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5214 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5215 | if (err) { |
| 5216 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5217 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5218 | fput(mddev->bitmap_info.file); |
| 5219 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5220 | return err; |
| 5221 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5222 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5223 | } else if (mddev->bitmap == NULL) |
| 5224 | return -ENOENT; /* cannot remove what isn't there */ |
| 5225 | err = 0; |
| 5226 | if (mddev->pers) { |
| 5227 | mddev->pers->quiesce(mddev, 1); |
| 5228 | if (fd >= 0) |
| 5229 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5230 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5231 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5232 | fd = -1; /* make sure to put the file */ |
| 5233 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5234 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5235 | } |
| 5236 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5237 | if (mddev->bitmap_info.file) { |
| 5238 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5239 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5240 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5241 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5244 | return err; |
| 5245 | } |
| 5246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | /* |
| 5248 | * set_array_info is used two different ways |
| 5249 | * The original usage is when creating a new array. |
| 5250 | * In this usage, raid_disks is > 0 and it together with |
| 5251 | * level, size, not_persistent,layout,chunksize determine the |
| 5252 | * shape of the array. |
| 5253 | * This will always create an array with a type-0.90.0 superblock. |
| 5254 | * The newer usage is when assembling an array. |
| 5255 | * In this case raid_disks will be 0, and the major_version field is |
| 5256 | * use to determine which style super-blocks are to be found on the devices. |
| 5257 | * The minor and patch _version numbers are also kept incase the |
| 5258 | * super_block handler wishes to interpret them. |
| 5259 | */ |
| 5260 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5261 | { |
| 5262 | |
| 5263 | if (info->raid_disks == 0) { |
| 5264 | /* just setting version number for superblock loading */ |
| 5265 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5266 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | super_types[info->major_version].name == NULL) { |
| 5268 | /* maybe try to auto-load a module? */ |
| 5269 | printk(KERN_INFO |
| 5270 | "md: superblock version %d not known\n", |
| 5271 | info->major_version); |
| 5272 | return -EINVAL; |
| 5273 | } |
| 5274 | mddev->major_version = info->major_version; |
| 5275 | mddev->minor_version = info->minor_version; |
| 5276 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5277 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5278 | /* ensure mddev_put doesn't delete this now that there |
| 5279 | * is some minimal configuration. |
| 5280 | */ |
| 5281 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | return 0; |
| 5283 | } |
| 5284 | mddev->major_version = MD_MAJOR_VERSION; |
| 5285 | mddev->minor_version = MD_MINOR_VERSION; |
| 5286 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5287 | mddev->ctime = get_seconds(); |
| 5288 | |
| 5289 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5290 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5291 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | mddev->raid_disks = info->raid_disks; |
| 5293 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5294 | * openned |
| 5295 | */ |
| 5296 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5297 | mddev->recovery_cp = MaxSector; |
| 5298 | else |
| 5299 | mddev->recovery_cp = 0; |
| 5300 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5301 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | |
| 5303 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5304 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | |
| 5306 | mddev->max_disks = MD_SB_DISKS; |
| 5307 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5308 | if (mddev->persistent) |
| 5309 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5310 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5312 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5313 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5314 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5315 | mddev->reshape_position = MaxSector; |
| 5316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | /* |
| 5318 | * Generate a 128 bit UUID |
| 5319 | */ |
| 5320 | get_random_bytes(mddev->uuid, 16); |
| 5321 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5322 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5323 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5324 | mddev->new_layout = mddev->layout; |
| 5325 | mddev->delta_disks = 0; |
| 5326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | return 0; |
| 5328 | } |
| 5329 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5330 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5331 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5332 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5333 | |
| 5334 | if (mddev->external_size) |
| 5335 | return; |
| 5336 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5337 | mddev->array_sectors = array_sectors; |
| 5338 | } |
| 5339 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5340 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5341 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5342 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5343 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5344 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5345 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5346 | |
| 5347 | if (mddev->pers->resize == NULL) |
| 5348 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5349 | /* The "num_sectors" is the number of sectors of each device that |
| 5350 | * is used. This can only make sense for arrays with redundancy. |
| 5351 | * linear and raid0 always use whatever space is available. We can only |
| 5352 | * consider changing this number if no resync or reconstruction is |
| 5353 | * 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] | 5354 | * 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] | 5355 | * of each device. If num_sectors is zero, we find the largest size |
| 5356 | * that fits. |
| 5357 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5358 | */ |
| 5359 | if (mddev->sync_thread) |
| 5360 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5361 | if (mddev->bitmap) |
| 5362 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5363 | * grow, and re-add. |
| 5364 | */ |
| 5365 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5366 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5367 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5368 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5369 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5370 | num_sectors = avail; |
| 5371 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5372 | return -ENOSPC; |
| 5373 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5374 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5375 | if (!rv) |
| 5376 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5377 | return rv; |
| 5378 | } |
| 5379 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5380 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5381 | { |
| 5382 | int rv; |
| 5383 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5384 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5385 | return -EINVAL; |
| 5386 | if (raid_disks <= 0 || |
| 5387 | raid_disks >= mddev->max_disks) |
| 5388 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5389 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5390 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5391 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5392 | |
| 5393 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5394 | return rv; |
| 5395 | } |
| 5396 | |
| 5397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | /* |
| 5399 | * update_array_info is used to change the configuration of an |
| 5400 | * on-line array. |
| 5401 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5402 | * fields in the info are checked against the array. |
| 5403 | * Any differences that cannot be handled will cause an error. |
| 5404 | * Normally, only one change can be managed at a time. |
| 5405 | */ |
| 5406 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5407 | { |
| 5408 | int rv = 0; |
| 5409 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5410 | int state = 0; |
| 5411 | |
| 5412 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5413 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5414 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | |
| 5416 | if (mddev->major_version != info->major_version || |
| 5417 | mddev->minor_version != info->minor_version || |
| 5418 | /* mddev->patch_version != info->patch_version || */ |
| 5419 | mddev->ctime != info->ctime || |
| 5420 | mddev->level != info->level || |
| 5421 | /* mddev->layout != info->layout || */ |
| 5422 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5423 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5424 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5425 | ((state^info->state) & 0xfffffe00) |
| 5426 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | return -EINVAL; |
| 5428 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5429 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5430 | cnt++; |
| 5431 | if (mddev->raid_disks != info->raid_disks) |
| 5432 | cnt++; |
| 5433 | if (mddev->layout != info->layout) |
| 5434 | cnt++; |
| 5435 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5436 | cnt++; |
| 5437 | if (cnt == 0) |
| 5438 | return 0; |
| 5439 | if (cnt > 1) |
| 5440 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | |
| 5442 | if (mddev->layout != info->layout) { |
| 5443 | /* Change layout |
| 5444 | * we don't need to do anything at the md level, the |
| 5445 | * personality will take care of it all. |
| 5446 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5447 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5449 | else { |
| 5450 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5451 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5452 | if (rv) |
| 5453 | mddev->new_layout = mddev->layout; |
| 5454 | return rv; |
| 5455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5457 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5458 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5460 | if (mddev->raid_disks != info->raid_disks) |
| 5461 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5463 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5464 | if (mddev->pers->quiesce == NULL) |
| 5465 | return -EINVAL; |
| 5466 | if (mddev->recovery || mddev->sync_thread) |
| 5467 | return -EBUSY; |
| 5468 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5469 | /* add the bitmap */ |
| 5470 | if (mddev->bitmap) |
| 5471 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5472 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5473 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5474 | mddev->bitmap_info.offset = |
| 5475 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5476 | mddev->pers->quiesce(mddev, 1); |
| 5477 | rv = bitmap_create(mddev); |
| 5478 | if (rv) |
| 5479 | bitmap_destroy(mddev); |
| 5480 | mddev->pers->quiesce(mddev, 0); |
| 5481 | } else { |
| 5482 | /* remove the bitmap */ |
| 5483 | if (!mddev->bitmap) |
| 5484 | return -ENOENT; |
| 5485 | if (mddev->bitmap->file) |
| 5486 | return -EINVAL; |
| 5487 | mddev->pers->quiesce(mddev, 1); |
| 5488 | bitmap_destroy(mddev); |
| 5489 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5490 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5491 | } |
| 5492 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5493 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | return rv; |
| 5495 | } |
| 5496 | |
| 5497 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5498 | { |
| 5499 | mdk_rdev_t *rdev; |
| 5500 | |
| 5501 | if (mddev->pers == NULL) |
| 5502 | return -ENODEV; |
| 5503 | |
| 5504 | rdev = find_rdev(mddev, dev); |
| 5505 | if (!rdev) |
| 5506 | return -ENODEV; |
| 5507 | |
| 5508 | md_error(mddev, rdev); |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5512 | /* |
| 5513 | * We have a problem here : there is no easy way to give a CHS |
| 5514 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5515 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5516 | * dosfs just mad... ;-) |
| 5517 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5518 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5519 | { |
| 5520 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5521 | |
| 5522 | geo->heads = 2; |
| 5523 | geo->sectors = 4; |
| 5524 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5525 | return 0; |
| 5526 | } |
| 5527 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5528 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | unsigned int cmd, unsigned long arg) |
| 5530 | { |
| 5531 | int err = 0; |
| 5532 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5534 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | |
| 5536 | if (!capable(CAP_SYS_ADMIN)) |
| 5537 | return -EACCES; |
| 5538 | |
| 5539 | /* |
| 5540 | * Commands dealing with the RAID driver but not any |
| 5541 | * particular array: |
| 5542 | */ |
| 5543 | switch (cmd) |
| 5544 | { |
| 5545 | case RAID_VERSION: |
| 5546 | err = get_version(argp); |
| 5547 | goto done; |
| 5548 | |
| 5549 | case PRINT_RAID_DEBUG: |
| 5550 | err = 0; |
| 5551 | md_print_devices(); |
| 5552 | goto done; |
| 5553 | |
| 5554 | #ifndef MODULE |
| 5555 | case RAID_AUTORUN: |
| 5556 | err = 0; |
| 5557 | autostart_arrays(arg); |
| 5558 | goto done; |
| 5559 | #endif |
| 5560 | default:; |
| 5561 | } |
| 5562 | |
| 5563 | /* |
| 5564 | * Commands creating/starting a new array: |
| 5565 | */ |
| 5566 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5567 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | |
| 5569 | if (!mddev) { |
| 5570 | BUG(); |
| 5571 | goto abort; |
| 5572 | } |
| 5573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | err = mddev_lock(mddev); |
| 5575 | if (err) { |
| 5576 | printk(KERN_INFO |
| 5577 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5578 | err, cmd); |
| 5579 | goto abort; |
| 5580 | } |
| 5581 | |
| 5582 | switch (cmd) |
| 5583 | { |
| 5584 | case SET_ARRAY_INFO: |
| 5585 | { |
| 5586 | mdu_array_info_t info; |
| 5587 | if (!arg) |
| 5588 | memset(&info, 0, sizeof(info)); |
| 5589 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5590 | err = -EFAULT; |
| 5591 | goto abort_unlock; |
| 5592 | } |
| 5593 | if (mddev->pers) { |
| 5594 | err = update_array_info(mddev, &info); |
| 5595 | if (err) { |
| 5596 | printk(KERN_WARNING "md: couldn't update" |
| 5597 | " array info. %d\n", err); |
| 5598 | goto abort_unlock; |
| 5599 | } |
| 5600 | goto done_unlock; |
| 5601 | } |
| 5602 | if (!list_empty(&mddev->disks)) { |
| 5603 | printk(KERN_WARNING |
| 5604 | "md: array %s already has disks!\n", |
| 5605 | mdname(mddev)); |
| 5606 | err = -EBUSY; |
| 5607 | goto abort_unlock; |
| 5608 | } |
| 5609 | if (mddev->raid_disks) { |
| 5610 | printk(KERN_WARNING |
| 5611 | "md: array %s already initialised!\n", |
| 5612 | mdname(mddev)); |
| 5613 | err = -EBUSY; |
| 5614 | goto abort_unlock; |
| 5615 | } |
| 5616 | err = set_array_info(mddev, &info); |
| 5617 | if (err) { |
| 5618 | printk(KERN_WARNING "md: couldn't set" |
| 5619 | " array info. %d\n", err); |
| 5620 | goto abort_unlock; |
| 5621 | } |
| 5622 | } |
| 5623 | goto done_unlock; |
| 5624 | |
| 5625 | default:; |
| 5626 | } |
| 5627 | |
| 5628 | /* |
| 5629 | * Commands querying/configuring an existing array: |
| 5630 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5631 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5632 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5633 | if ((!mddev->raid_disks && !mddev->external) |
| 5634 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5635 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5636 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | err = -ENODEV; |
| 5638 | goto abort_unlock; |
| 5639 | } |
| 5640 | |
| 5641 | /* |
| 5642 | * Commands even a read-only array can execute: |
| 5643 | */ |
| 5644 | switch (cmd) |
| 5645 | { |
| 5646 | case GET_ARRAY_INFO: |
| 5647 | err = get_array_info(mddev, argp); |
| 5648 | goto done_unlock; |
| 5649 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5650 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5651 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5652 | goto done_unlock; |
| 5653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | case GET_DISK_INFO: |
| 5655 | err = get_disk_info(mddev, argp); |
| 5656 | goto done_unlock; |
| 5657 | |
| 5658 | case RESTART_ARRAY_RW: |
| 5659 | err = restart_array(mddev); |
| 5660 | goto done_unlock; |
| 5661 | |
| 5662 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5663 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | goto done_unlock; |
| 5665 | |
| 5666 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5667 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | goto done_unlock; |
| 5669 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5670 | case BLKROSET: |
| 5671 | if (get_user(ro, (int __user *)(arg))) { |
| 5672 | err = -EFAULT; |
| 5673 | goto done_unlock; |
| 5674 | } |
| 5675 | err = -EINVAL; |
| 5676 | |
| 5677 | /* if the bdev is going readonly the value of mddev->ro |
| 5678 | * does not matter, no writes are coming |
| 5679 | */ |
| 5680 | if (ro) |
| 5681 | goto done_unlock; |
| 5682 | |
| 5683 | /* are we are already prepared for writes? */ |
| 5684 | if (mddev->ro != 1) |
| 5685 | goto done_unlock; |
| 5686 | |
| 5687 | /* transitioning to readauto need only happen for |
| 5688 | * arrays that call md_write_start |
| 5689 | */ |
| 5690 | if (mddev->pers) { |
| 5691 | err = restart_array(mddev); |
| 5692 | if (err == 0) { |
| 5693 | mddev->ro = 2; |
| 5694 | set_disk_ro(mddev->gendisk, 0); |
| 5695 | } |
| 5696 | } |
| 5697 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | /* |
| 5701 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5702 | * superblock, so we do not allow them on read-only arrays. |
| 5703 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5704 | * here and hit the 'default' below, so only disallow |
| 5705 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5707 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5708 | if (mddev->ro == 2) { |
| 5709 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5710 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5711 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5712 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5713 | } else { |
| 5714 | err = -EROFS; |
| 5715 | goto abort_unlock; |
| 5716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | switch (cmd) |
| 5720 | { |
| 5721 | case ADD_NEW_DISK: |
| 5722 | { |
| 5723 | mdu_disk_info_t info; |
| 5724 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5725 | err = -EFAULT; |
| 5726 | else |
| 5727 | err = add_new_disk(mddev, &info); |
| 5728 | goto done_unlock; |
| 5729 | } |
| 5730 | |
| 5731 | case HOT_REMOVE_DISK: |
| 5732 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5733 | goto done_unlock; |
| 5734 | |
| 5735 | case HOT_ADD_DISK: |
| 5736 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5737 | goto done_unlock; |
| 5738 | |
| 5739 | case SET_DISK_FAULTY: |
| 5740 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5741 | goto done_unlock; |
| 5742 | |
| 5743 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5744 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | goto done_unlock; |
| 5746 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5747 | case SET_BITMAP_FILE: |
| 5748 | err = set_bitmap_file(mddev, (int)arg); |
| 5749 | goto done_unlock; |
| 5750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | err = -EINVAL; |
| 5753 | goto abort_unlock; |
| 5754 | } |
| 5755 | |
| 5756 | done_unlock: |
| 5757 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5758 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5759 | err != -EINVAL) |
| 5760 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | mddev_unlock(mddev); |
| 5762 | |
| 5763 | return err; |
| 5764 | done: |
| 5765 | if (err) |
| 5766 | MD_BUG(); |
| 5767 | abort: |
| 5768 | return err; |
| 5769 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5770 | #ifdef CONFIG_COMPAT |
| 5771 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5772 | unsigned int cmd, unsigned long arg) |
| 5773 | { |
| 5774 | switch (cmd) { |
| 5775 | case HOT_REMOVE_DISK: |
| 5776 | case HOT_ADD_DISK: |
| 5777 | case SET_DISK_FAULTY: |
| 5778 | case SET_BITMAP_FILE: |
| 5779 | /* These take in integer arg, do not convert */ |
| 5780 | break; |
| 5781 | default: |
| 5782 | arg = (unsigned long)compat_ptr(arg); |
| 5783 | break; |
| 5784 | } |
| 5785 | |
| 5786 | return md_ioctl(bdev, mode, cmd, arg); |
| 5787 | } |
| 5788 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5790 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | { |
| 5792 | /* |
| 5793 | * Succeed if we can lock the mddev, which confirms that |
| 5794 | * it isn't being stopped right now. |
| 5795 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5796 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | int err; |
| 5798 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5799 | if (mddev->gendisk != bdev->bd_disk) { |
| 5800 | /* we are racing with mddev_put which is discarding this |
| 5801 | * bd_disk. |
| 5802 | */ |
| 5803 | mddev_put(mddev); |
| 5804 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5805 | flush_scheduled_work(); |
| 5806 | /* Then retry the open from the top */ |
| 5807 | return -ERESTARTSYS; |
| 5808 | } |
| 5809 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5810 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5811 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | goto out; |
| 5813 | |
| 5814 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5815 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5816 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5818 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | out: |
| 5820 | return err; |
| 5821 | } |
| 5822 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5823 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5825 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5827 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5828 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | mddev_put(mddev); |
| 5830 | |
| 5831 | return 0; |
| 5832 | } |
| 5833 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5834 | static int md_media_changed(struct gendisk *disk) |
| 5835 | { |
| 5836 | mddev_t *mddev = disk->private_data; |
| 5837 | |
| 5838 | return mddev->changed; |
| 5839 | } |
| 5840 | |
| 5841 | static int md_revalidate(struct gendisk *disk) |
| 5842 | { |
| 5843 | mddev_t *mddev = disk->private_data; |
| 5844 | |
| 5845 | mddev->changed = 0; |
| 5846 | return 0; |
| 5847 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5848 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | { |
| 5850 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5851 | .open = md_open, |
| 5852 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5853 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5854 | #ifdef CONFIG_COMPAT |
| 5855 | .compat_ioctl = md_compat_ioctl, |
| 5856 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5857 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5858 | .media_changed = md_media_changed, |
| 5859 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | }; |
| 5861 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5862 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | { |
| 5864 | mdk_thread_t *thread = arg; |
| 5865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | /* |
| 5867 | * md_thread is a 'system-thread', it's priority should be very |
| 5868 | * high. We avoid resource deadlocks individually in each |
| 5869 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5870 | * the very same RT priority as kswapd, thus we will never get |
| 5871 | * into a priority inversion deadlock. |
| 5872 | * |
| 5873 | * we definitely have to have equal or higher priority than |
| 5874 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5875 | * many dirty RAID5 blocks. |
| 5876 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5878 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5879 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5881 | /* We need to wait INTERRUPTIBLE so that |
| 5882 | * we don't add to the load-average. |
| 5883 | * That means we need to be sure no signals are |
| 5884 | * pending |
| 5885 | */ |
| 5886 | if (signal_pending(current)) |
| 5887 | flush_signals(current); |
| 5888 | |
| 5889 | wait_event_interruptible_timeout |
| 5890 | (thread->wqueue, |
| 5891 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5892 | || kthread_should_stop(), |
| 5893 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | |
| 5895 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5896 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5897 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | return 0; |
| 5901 | } |
| 5902 | |
| 5903 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5904 | { |
| 5905 | if (thread) { |
| 5906 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5907 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5908 | wake_up(&thread->wqueue); |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5913 | const char *name) |
| 5914 | { |
| 5915 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5917 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | if (!thread) |
| 5919 | return NULL; |
| 5920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | init_waitqueue_head(&thread->wqueue); |
| 5922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | thread->run = run; |
| 5924 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5925 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5926 | thread->tsk = kthread_run(md_thread, thread, |
| 5927 | "%s_%s", |
| 5928 | mdname(thread->mddev), |
| 5929 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5930 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | kfree(thread); |
| 5932 | return NULL; |
| 5933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | return thread; |
| 5935 | } |
| 5936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | void md_unregister_thread(mdk_thread_t *thread) |
| 5938 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5939 | if (!thread) |
| 5940 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5941 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5942 | |
| 5943 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | kfree(thread); |
| 5945 | } |
| 5946 | |
| 5947 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5948 | { |
| 5949 | if (!mddev) { |
| 5950 | MD_BUG(); |
| 5951 | return; |
| 5952 | } |
| 5953 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5954 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5956 | |
| 5957 | if (mddev->external) |
| 5958 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5959 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5961 | mdname(mddev), |
| 5962 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5963 | __builtin_return_address(0),__builtin_return_address(1), |
| 5964 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5965 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5966 | if (!mddev->pers) |
| 5967 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | if (!mddev->pers->error_handler) |
| 5969 | return; |
| 5970 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5971 | if (mddev->degraded) |
| 5972 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame^] | 5973 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5975 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5976 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5977 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | } |
| 5979 | |
| 5980 | /* seq_file implementation /proc/mdstat */ |
| 5981 | |
| 5982 | static void status_unused(struct seq_file *seq) |
| 5983 | { |
| 5984 | int i = 0; |
| 5985 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | |
| 5987 | seq_printf(seq, "unused devices: "); |
| 5988 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5989 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | char b[BDEVNAME_SIZE]; |
| 5991 | i++; |
| 5992 | seq_printf(seq, "%s ", |
| 5993 | bdevname(rdev->bdev,b)); |
| 5994 | } |
| 5995 | if (!i) |
| 5996 | seq_printf(seq, "<none>"); |
| 5997 | |
| 5998 | seq_printf(seq, "\n"); |
| 5999 | } |
| 6000 | |
| 6001 | |
| 6002 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6003 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6004 | sector_t max_sectors, resync, res; |
| 6005 | unsigned long dt, db; |
| 6006 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6007 | int scale; |
| 6008 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6010 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | |
| 6012 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6013 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6015 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | |
| 6017 | /* |
| 6018 | * Should not happen. |
| 6019 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6020 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | MD_BUG(); |
| 6022 | return; |
| 6023 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6024 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6025 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6026 | * u32, as those are the requirements for sector_div. |
| 6027 | * Thus 'scale' must be at least 10 |
| 6028 | */ |
| 6029 | scale = 10; |
| 6030 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6031 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6032 | scale++; |
| 6033 | } |
| 6034 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6035 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6036 | |
| 6037 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6039 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | seq_printf(seq, "["); |
| 6041 | for (i = 0; i < x; i++) |
| 6042 | seq_printf(seq, "="); |
| 6043 | seq_printf(seq, ">"); |
| 6044 | for (i = 0; i < y; i++) |
| 6045 | seq_printf(seq, "."); |
| 6046 | seq_printf(seq, "] "); |
| 6047 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6048 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6049 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6050 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6051 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6052 | "check" : |
| 6053 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6054 | "resync" : "recovery"))), |
| 6055 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6056 | (unsigned long long) resync/2, |
| 6057 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | |
| 6059 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | * dt: time from mark until now |
| 6061 | * db: blocks written from mark until now |
| 6062 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6063 | * |
| 6064 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6065 | * So we divide before multiply in case it is 32bit and close |
| 6066 | * to the limit. |
| 6067 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6068 | * near the end of resync when the number of remaining sectors |
| 6069 | * is close to 'db'. |
| 6070 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6071 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | */ |
| 6073 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6074 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6075 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6076 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6078 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6079 | sector_div(rt, db/32+1); |
| 6080 | rt *= dt; |
| 6081 | rt >>= 5; |
| 6082 | |
| 6083 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6084 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6086 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6090 | { |
| 6091 | struct list_head *tmp; |
| 6092 | loff_t l = *pos; |
| 6093 | mddev_t *mddev; |
| 6094 | |
| 6095 | if (l >= 0x10000) |
| 6096 | return NULL; |
| 6097 | if (!l--) |
| 6098 | /* header */ |
| 6099 | return (void*)1; |
| 6100 | |
| 6101 | spin_lock(&all_mddevs_lock); |
| 6102 | list_for_each(tmp,&all_mddevs) |
| 6103 | if (!l--) { |
| 6104 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6105 | mddev_get(mddev); |
| 6106 | spin_unlock(&all_mddevs_lock); |
| 6107 | return mddev; |
| 6108 | } |
| 6109 | spin_unlock(&all_mddevs_lock); |
| 6110 | if (!l--) |
| 6111 | return (void*)2;/* tail */ |
| 6112 | return NULL; |
| 6113 | } |
| 6114 | |
| 6115 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6116 | { |
| 6117 | struct list_head *tmp; |
| 6118 | mddev_t *next_mddev, *mddev = v; |
| 6119 | |
| 6120 | ++*pos; |
| 6121 | if (v == (void*)2) |
| 6122 | return NULL; |
| 6123 | |
| 6124 | spin_lock(&all_mddevs_lock); |
| 6125 | if (v == (void*)1) |
| 6126 | tmp = all_mddevs.next; |
| 6127 | else |
| 6128 | tmp = mddev->all_mddevs.next; |
| 6129 | if (tmp != &all_mddevs) |
| 6130 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6131 | else { |
| 6132 | next_mddev = (void*)2; |
| 6133 | *pos = 0x10000; |
| 6134 | } |
| 6135 | spin_unlock(&all_mddevs_lock); |
| 6136 | |
| 6137 | if (v != (void*)1) |
| 6138 | mddev_put(mddev); |
| 6139 | return next_mddev; |
| 6140 | |
| 6141 | } |
| 6142 | |
| 6143 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6144 | { |
| 6145 | mddev_t *mddev = v; |
| 6146 | |
| 6147 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6148 | mddev_put(mddev); |
| 6149 | } |
| 6150 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6151 | struct mdstat_info { |
| 6152 | int event; |
| 6153 | }; |
| 6154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6156 | { |
| 6157 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6158 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6160 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6161 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | |
| 6163 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6164 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | seq_printf(seq, "Personalities : "); |
| 6166 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6167 | list_for_each_entry(pers, &pers_list, list) |
| 6168 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | |
| 6170 | spin_unlock(&pers_lock); |
| 6171 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6172 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | return 0; |
| 6174 | } |
| 6175 | if (v == (void*)2) { |
| 6176 | status_unused(seq); |
| 6177 | return 0; |
| 6178 | } |
| 6179 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6180 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6184 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6185 | mddev->pers ? "" : "in"); |
| 6186 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6187 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6189 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6190 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | seq_printf(seq, " %s", mddev->pers->name); |
| 6192 | } |
| 6193 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6194 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6195 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | char b[BDEVNAME_SIZE]; |
| 6197 | seq_printf(seq, " %s[%d]", |
| 6198 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6199 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6200 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6201 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | seq_printf(seq, "(F)"); |
| 6203 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6204 | } else if (rdev->raid_disk < 0) |
| 6205 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6206 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | if (!list_empty(&mddev->disks)) { |
| 6210 | if (mddev->pers) |
| 6211 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6212 | (unsigned long long) |
| 6213 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | else |
| 6215 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6216 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6218 | if (mddev->persistent) { |
| 6219 | if (mddev->major_version != 0 || |
| 6220 | mddev->minor_version != 90) { |
| 6221 | seq_printf(seq," super %d.%d", |
| 6222 | mddev->major_version, |
| 6223 | mddev->minor_version); |
| 6224 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6225 | } else if (mddev->external) |
| 6226 | seq_printf(seq, " super external:%s", |
| 6227 | mddev->metadata_type); |
| 6228 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6229 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | |
| 6231 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6232 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6234 | if (mddev->pers->sync_request) { |
| 6235 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6236 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6237 | seq_printf(seq, "\n "); |
| 6238 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6239 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6240 | else if (mddev->recovery_cp < MaxSector) |
| 6241 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6242 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6243 | } else |
| 6244 | seq_printf(seq, "\n "); |
| 6245 | |
| 6246 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6247 | unsigned long chunk_kb; |
| 6248 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6249 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6250 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6251 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6252 | "%lu%s chunk", |
| 6253 | bitmap->pages - bitmap->missing_pages, |
| 6254 | bitmap->pages, |
| 6255 | (bitmap->pages - bitmap->missing_pages) |
| 6256 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6257 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6258 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6259 | if (bitmap->file) { |
| 6260 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6261 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6262 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6263 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6264 | seq_printf(seq, "\n"); |
| 6265 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | seq_printf(seq, "\n"); |
| 6269 | } |
| 6270 | mddev_unlock(mddev); |
| 6271 | |
| 6272 | return 0; |
| 6273 | } |
| 6274 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6275 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | .start = md_seq_start, |
| 6277 | .next = md_seq_next, |
| 6278 | .stop = md_seq_stop, |
| 6279 | .show = md_seq_show, |
| 6280 | }; |
| 6281 | |
| 6282 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6283 | { |
| 6284 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6285 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6286 | if (mi == NULL) |
| 6287 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | |
| 6289 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6290 | if (error) |
| 6291 | kfree(mi); |
| 6292 | else { |
| 6293 | struct seq_file *p = file->private_data; |
| 6294 | p->private = mi; |
| 6295 | mi->event = atomic_read(&md_event_count); |
| 6296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | return error; |
| 6298 | } |
| 6299 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6300 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6301 | { |
| 6302 | struct seq_file *m = filp->private_data; |
| 6303 | struct mdstat_info *mi = m->private; |
| 6304 | int mask; |
| 6305 | |
| 6306 | poll_wait(filp, &md_event_waiters, wait); |
| 6307 | |
| 6308 | /* always allow read */ |
| 6309 | mask = POLLIN | POLLRDNORM; |
| 6310 | |
| 6311 | if (mi->event != atomic_read(&md_event_count)) |
| 6312 | mask |= POLLERR | POLLPRI; |
| 6313 | return mask; |
| 6314 | } |
| 6315 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6316 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6317 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | .open = md_seq_open, |
| 6319 | .read = seq_read, |
| 6320 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6321 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6322 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | }; |
| 6324 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6325 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6328 | list_add_tail(&p->list, &pers_list); |
| 6329 | 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] | 6330 | spin_unlock(&pers_lock); |
| 6331 | return 0; |
| 6332 | } |
| 6333 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6334 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6336 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6338 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | spin_unlock(&pers_lock); |
| 6340 | return 0; |
| 6341 | } |
| 6342 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6343 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | { |
| 6345 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6347 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | |
| 6349 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6350 | rcu_read_lock(); |
| 6351 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6353 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6354 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6355 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6356 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6357 | * as sync_io is counted when a request starts, and |
| 6358 | * disk_stats is counted when it completes. |
| 6359 | * So resync activity will cause curr_events to be smaller than |
| 6360 | * when there was no such activity. |
| 6361 | * non-sync IO will cause disk_stat to increase without |
| 6362 | * increasing sync_io so curr_events will (eventually) |
| 6363 | * be larger than it was before. Once it becomes |
| 6364 | * substantially larger, the test below will cause |
| 6365 | * the array to appear non-idle, and resync will slow |
| 6366 | * down. |
| 6367 | * If there is a lot of outstanding resync activity when |
| 6368 | * we set last_event to curr_events, then all that activity |
| 6369 | * completing might cause the array to appear non-idle |
| 6370 | * and resync will be slowed down even though there might |
| 6371 | * not have been non-resync activity. This will only |
| 6372 | * happen once though. 'last_events' will soon reflect |
| 6373 | * the state where there is little or no outstanding |
| 6374 | * resync requests, and further resync activity will |
| 6375 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6376 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6378 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | rdev->last_events = curr_events; |
| 6380 | idle = 0; |
| 6381 | } |
| 6382 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6383 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | return idle; |
| 6385 | } |
| 6386 | |
| 6387 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6388 | { |
| 6389 | /* another "blocks" (512byte) blocks have been synced */ |
| 6390 | atomic_sub(blocks, &mddev->recovery_active); |
| 6391 | wake_up(&mddev->recovery_wait); |
| 6392 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6393 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | md_wakeup_thread(mddev->thread); |
| 6395 | // stop recovery, signal do_sync .... |
| 6396 | } |
| 6397 | } |
| 6398 | |
| 6399 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6400 | /* md_write_start(mddev, bi) |
| 6401 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6402 | * in superblock) before writing, schedule a superblock update |
| 6403 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6404 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6405 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6407 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6408 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6409 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6410 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6411 | BUG_ON(mddev->ro == 1); |
| 6412 | if (mddev->ro == 2) { |
| 6413 | /* need to switch to read/write */ |
| 6414 | mddev->ro = 0; |
| 6415 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6416 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6417 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6418 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6419 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6420 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6421 | if (mddev->safemode == 1) |
| 6422 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6423 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6424 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6425 | if (mddev->in_sync) { |
| 6426 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6427 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6428 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6429 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6430 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6431 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6432 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6433 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6434 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6435 | wait_event(mddev->sb_wait, |
| 6436 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6437 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | void md_write_end(mddev_t *mddev) |
| 6441 | { |
| 6442 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6443 | if (mddev->safemode == 2) |
| 6444 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6445 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6447 | } |
| 6448 | } |
| 6449 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6450 | /* md_allow_write(mddev) |
| 6451 | * Calling this ensures that the array is marked 'active' so that writes |
| 6452 | * may proceed without blocking. It is important to call this before |
| 6453 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6454 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6455 | * |
| 6456 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6457 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6458 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6459 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6460 | { |
| 6461 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6462 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6463 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6464 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6465 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6466 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6467 | |
| 6468 | spin_lock_irq(&mddev->write_lock); |
| 6469 | if (mddev->in_sync) { |
| 6470 | mddev->in_sync = 0; |
| 6471 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6472 | if (mddev->safemode_delay && |
| 6473 | mddev->safemode == 0) |
| 6474 | mddev->safemode = 1; |
| 6475 | spin_unlock_irq(&mddev->write_lock); |
| 6476 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6477 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6478 | } else |
| 6479 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6480 | |
| 6481 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6482 | return -EAGAIN; |
| 6483 | else |
| 6484 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6485 | } |
| 6486 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | #define SYNC_MARKS 10 |
| 6489 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6490 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | { |
| 6492 | mddev_t *mddev2; |
| 6493 | unsigned int currspeed = 0, |
| 6494 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6495 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | unsigned long mark[SYNC_MARKS]; |
| 6497 | sector_t mark_cnt[SYNC_MARKS]; |
| 6498 | int last_mark,m; |
| 6499 | struct list_head *tmp; |
| 6500 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6501 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6502 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6503 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | |
| 6505 | /* just incase thread restarts... */ |
| 6506 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6507 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6508 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6509 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6511 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6512 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6513 | desc = "data-check"; |
| 6514 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6515 | desc = "requested-resync"; |
| 6516 | else |
| 6517 | desc = "resync"; |
| 6518 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6519 | desc = "reshape"; |
| 6520 | else |
| 6521 | desc = "recovery"; |
| 6522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | /* we overload curr_resync somewhat here. |
| 6524 | * 0 == not engaged in resync at all |
| 6525 | * 2 == checking that there is no conflict with another sync |
| 6526 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6527 | * commense |
| 6528 | * other == active in resync - this many blocks |
| 6529 | * |
| 6530 | * Before starting a resync we must have set curr_resync to |
| 6531 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6532 | * less than ours. When we find one that is the same or higher |
| 6533 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6534 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6535 | * This will mean we have to start checking from the beginning again. |
| 6536 | * |
| 6537 | */ |
| 6538 | |
| 6539 | do { |
| 6540 | mddev->curr_resync = 2; |
| 6541 | |
| 6542 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6543 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6544 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6545 | |
| 6546 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6548 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | if (mddev2 == mddev) |
| 6550 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6551 | if (!mddev->parallel_resync |
| 6552 | && mddev2->curr_resync |
| 6553 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | DEFINE_WAIT(wq); |
| 6555 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6556 | /* arbitrarily yield */ |
| 6557 | mddev->curr_resync = 1; |
| 6558 | wake_up(&resync_wait); |
| 6559 | } |
| 6560 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6561 | /* no need to wait here, we can wait the next |
| 6562 | * time 'round when curr_resync == 2 |
| 6563 | */ |
| 6564 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6565 | /* We need to wait 'interruptible' so as not to |
| 6566 | * contribute to the load average, and not to |
| 6567 | * be caught by 'softlockup' |
| 6568 | */ |
| 6569 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6570 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6571 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6572 | printk(KERN_INFO "md: delaying %s of %s" |
| 6573 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6575 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6577 | if (signal_pending(current)) |
| 6578 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | schedule(); |
| 6580 | finish_wait(&resync_wait, &wq); |
| 6581 | goto try_again; |
| 6582 | } |
| 6583 | finish_wait(&resync_wait, &wq); |
| 6584 | } |
| 6585 | } |
| 6586 | } while (mddev->curr_resync < 2); |
| 6587 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6588 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6589 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6591 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | */ |
| 6593 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6594 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6595 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6596 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6597 | j = mddev->resync_min; |
| 6598 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6599 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6600 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6601 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6602 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6603 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6605 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6606 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6607 | rcu_read_lock(); |
| 6608 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6609 | if (rdev->raid_disk >= 0 && |
| 6610 | !test_bit(Faulty, &rdev->flags) && |
| 6611 | !test_bit(In_sync, &rdev->flags) && |
| 6612 | rdev->recovery_offset < j) |
| 6613 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6614 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6615 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6617 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6618 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6619 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6620 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6621 | "(but not more than %d KB/sec) for %s.\n", |
| 6622 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6623 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6624 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6625 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6626 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6628 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6629 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | } |
| 6631 | last_mark = 0; |
| 6632 | mddev->resync_mark = mark[last_mark]; |
| 6633 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6634 | |
| 6635 | /* |
| 6636 | * Tune reconstruction: |
| 6637 | */ |
| 6638 | window = 32*(PAGE_SIZE/512); |
| 6639 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6640 | window/2,(unsigned long long) max_sectors/2); |
| 6641 | |
| 6642 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | last_check = 0; |
| 6644 | |
| 6645 | if (j>2) { |
| 6646 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6647 | "md: resuming %s of %s from checkpoint.\n", |
| 6648 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | mddev->curr_resync = j; |
| 6650 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6651 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | |
| 6653 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6654 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6656 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6657 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6658 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6659 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6660 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6661 | > (max_sectors >> 4)) || |
| 6662 | (j - mddev->curr_resync_completed)*2 |
| 6663 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6664 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6665 | /* time to update curr_resync_completed */ |
| 6666 | blk_unplug(mddev->queue); |
| 6667 | wait_event(mddev->recovery_wait, |
| 6668 | atomic_read(&mddev->recovery_active) == 0); |
| 6669 | mddev->curr_resync_completed = |
| 6670 | mddev->curr_resync; |
| 6671 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6672 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6673 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6674 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6675 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6676 | /* As this condition is controlled by user-space, |
| 6677 | * we can block indefinitely, so use '_interruptible' |
| 6678 | * to avoid triggering warnings. |
| 6679 | */ |
| 6680 | flush_signals(current); /* just in case */ |
| 6681 | wait_event_interruptible(mddev->recovery_wait, |
| 6682 | mddev->resync_max > j |
| 6683 | || kthread_should_stop()); |
| 6684 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6685 | |
| 6686 | if (kthread_should_stop()) |
| 6687 | goto interrupted; |
| 6688 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6689 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6690 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6691 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6692 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6693 | goto out; |
| 6694 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6695 | |
| 6696 | if (!skipped) { /* actual IO requested */ |
| 6697 | io_sectors += sectors; |
| 6698 | atomic_add(sectors, &mddev->recovery_active); |
| 6699 | } |
| 6700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | j += sectors; |
| 6702 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6703 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6704 | if (last_check == 0) |
| 6705 | /* this is the earliers that rebuilt will be |
| 6706 | * visible in /proc/mdstat |
| 6707 | */ |
| 6708 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6709 | |
| 6710 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | continue; |
| 6712 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6713 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6715 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | break; |
| 6717 | |
| 6718 | repeat: |
| 6719 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6720 | /* step marks */ |
| 6721 | int next = (last_mark+1) % SYNC_MARKS; |
| 6722 | |
| 6723 | mddev->resync_mark = mark[next]; |
| 6724 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6725 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6726 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | last_mark = next; |
| 6728 | } |
| 6729 | |
| 6730 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6731 | if (kthread_should_stop()) |
| 6732 | goto interrupted; |
| 6733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | |
| 6735 | /* |
| 6736 | * this loop exits only if either when we are slower than |
| 6737 | * the 'hard' speed limit, or the system was IO-idle for |
| 6738 | * a jiffy. |
| 6739 | * the system might be non-idle CPU-wise, but we only care |
| 6740 | * about not overloading the IO subsystem. (things like an |
| 6741 | * e2fsck being done on the RAID array should execute fast) |
| 6742 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6743 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | cond_resched(); |
| 6745 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6746 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6747 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6749 | if (currspeed > speed_min(mddev)) { |
| 6750 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6751 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6752 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | goto repeat; |
| 6754 | } |
| 6755 | } |
| 6756 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6757 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | /* |
| 6759 | * this also signals 'finished resyncing' to md_stop |
| 6760 | */ |
| 6761 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6762 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | |
| 6764 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6765 | |
| 6766 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6767 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6769 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6770 | mddev->curr_resync > 2) { |
| 6771 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6772 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6773 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6774 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6775 | "md: checkpointing %s of %s.\n", |
| 6776 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6777 | mddev->recovery_cp = mddev->curr_resync; |
| 6778 | } |
| 6779 | } else |
| 6780 | mddev->recovery_cp = MaxSector; |
| 6781 | } else { |
| 6782 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6783 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6784 | rcu_read_lock(); |
| 6785 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6786 | if (rdev->raid_disk >= 0 && |
| 6787 | !test_bit(Faulty, &rdev->flags) && |
| 6788 | !test_bit(In_sync, &rdev->flags) && |
| 6789 | rdev->recovery_offset < mddev->curr_resync) |
| 6790 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6791 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6794 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6797 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6798 | /* We completed so min/max setting can be forgotten if used. */ |
| 6799 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6800 | mddev->resync_min = 0; |
| 6801 | mddev->resync_max = MaxSector; |
| 6802 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6803 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6805 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6806 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6807 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | wake_up(&resync_wait); |
| 6809 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6810 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6811 | return; |
| 6812 | |
| 6813 | interrupted: |
| 6814 | /* |
| 6815 | * got a signal, exit. |
| 6816 | */ |
| 6817 | printk(KERN_INFO |
| 6818 | "md: md_do_sync() got signal ... exiting\n"); |
| 6819 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6820 | goto out; |
| 6821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6823 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | |
| 6825 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6826 | static int remove_and_add_spares(mddev_t *mddev) |
| 6827 | { |
| 6828 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6829 | int spares = 0; |
| 6830 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6831 | mddev->curr_resync_completed = 0; |
| 6832 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6833 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6834 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6835 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6836 | (test_bit(Faulty, &rdev->flags) || |
| 6837 | ! test_bit(In_sync, &rdev->flags)) && |
| 6838 | atomic_read(&rdev->nr_pending)==0) { |
| 6839 | if (mddev->pers->hot_remove_disk( |
| 6840 | mddev, rdev->raid_disk)==0) { |
| 6841 | char nm[20]; |
| 6842 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6843 | sysfs_remove_link(&mddev->kobj, nm); |
| 6844 | rdev->raid_disk = -1; |
| 6845 | } |
| 6846 | } |
| 6847 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6848 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6849 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6850 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6851 | !test_bit(In_sync, &rdev->flags) && |
| 6852 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6853 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6854 | if (rdev->raid_disk < 0 |
| 6855 | && !test_bit(Faulty, &rdev->flags)) { |
| 6856 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6857 | if (mddev->pers-> |
| 6858 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6859 | char nm[20]; |
| 6860 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6861 | if (sysfs_create_link(&mddev->kobj, |
| 6862 | &rdev->kobj, nm)) |
| 6863 | printk(KERN_WARNING |
| 6864 | "md: cannot register " |
| 6865 | "%s for %s\n", |
| 6866 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6867 | spares++; |
| 6868 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6869 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6870 | } else |
| 6871 | break; |
| 6872 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6873 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6874 | } |
| 6875 | return spares; |
| 6876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | /* |
| 6878 | * This routine is regularly called by all per-raid-array threads to |
| 6879 | * deal with generic issues like resync and super-block update. |
| 6880 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6881 | * need this as they never do any recovery or update the superblock. |
| 6882 | * |
| 6883 | * It does not do any resync itself, but rather "forks" off other threads |
| 6884 | * to do that as needed. |
| 6885 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6886 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6887 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | * and wakeups up this thread which will reap the thread and finish up. |
| 6889 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6890 | * |
| 6891 | * The overall approach is: |
| 6892 | * 1/ if the superblock needs updating, update it. |
| 6893 | * 2/ If a recovery thread is running, don't do anything else. |
| 6894 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6895 | * 4/ If there are any faulty devices, remove them. |
| 6896 | * 5/ If array is degraded, try to add spares devices |
| 6897 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6898 | */ |
| 6899 | void md_check_recovery(mddev_t *mddev) |
| 6900 | { |
| 6901 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | |
| 6903 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6904 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6905 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | |
| 6907 | if (mddev->ro) |
| 6908 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6909 | |
| 6910 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6911 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6912 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6913 | mdname(mddev)); |
| 6914 | mddev->safemode = 2; |
| 6915 | } |
| 6916 | flush_signals(current); |
| 6917 | } |
| 6918 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6919 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6920 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6922 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6924 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6925 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6926 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6927 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | )) |
| 6929 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6930 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6931 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6932 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6933 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6934 | if (mddev->ro) { |
| 6935 | /* Only thing we do on a ro array is remove |
| 6936 | * failed devices. |
| 6937 | */ |
| 6938 | remove_and_add_spares(mddev); |
| 6939 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6940 | goto unlock; |
| 6941 | } |
| 6942 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6943 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6944 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6945 | spin_lock_irq(&mddev->write_lock); |
| 6946 | if (mddev->safemode && |
| 6947 | !atomic_read(&mddev->writes_pending) && |
| 6948 | !mddev->in_sync && |
| 6949 | mddev->recovery_cp == MaxSector) { |
| 6950 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6951 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6952 | if (mddev->persistent) |
| 6953 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6954 | } |
| 6955 | if (mddev->safemode == 1) |
| 6956 | mddev->safemode = 0; |
| 6957 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6958 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6959 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6960 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6961 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6962 | if (mddev->flags) |
| 6963 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6966 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6967 | /* resync/recovery still happening */ |
| 6968 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6969 | goto unlock; |
| 6970 | } |
| 6971 | if (mddev->sync_thread) { |
| 6972 | /* resync has finished, collect result */ |
| 6973 | md_unregister_thread(mddev->sync_thread); |
| 6974 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6975 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6976 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | /* success...*/ |
| 6978 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6979 | if (mddev->pers->spare_active(mddev)) |
| 6980 | sysfs_notify(&mddev->kobj, NULL, |
| 6981 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6983 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6984 | mddev->pers->finish_reshape) |
| 6985 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6986 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6987 | |
| 6988 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6989 | * information must be scrapped |
| 6990 | */ |
| 6991 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6992 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6993 | rdev->saved_raid_disk = -1; |
| 6994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6995 | mddev->recovery = 0; |
| 6996 | /* flag recovery needed just to double check */ |
| 6997 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6998 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6999 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | goto unlock; |
| 7001 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7002 | /* Set RUNNING before clearing NEEDED to avoid |
| 7003 | * any transients in the value of "sync_action". |
| 7004 | */ |
| 7005 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7006 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7007 | /* Clear some bits that don't mean anything, but |
| 7008 | * might be left set |
| 7009 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7010 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7011 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7013 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7014 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | /* no recovery is running. |
| 7016 | * remove any failed drives, then |
| 7017 | * add spares if possible. |
| 7018 | * Spare are also removed and re-added, to allow |
| 7019 | * the personality to fail the re-add. |
| 7020 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7022 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7023 | if (mddev->pers->check_reshape == NULL || |
| 7024 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7025 | /* Cannot proceed */ |
| 7026 | goto unlock; |
| 7027 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7028 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7029 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7030 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7031 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7032 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7033 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7034 | } else if (mddev->recovery_cp < MaxSector) { |
| 7035 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7036 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7037 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7038 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7042 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7043 | /* We are adding a device or devices to an array |
| 7044 | * which has the bitmap stored on all devices. |
| 7045 | * So make sure all bitmap pages get written |
| 7046 | */ |
| 7047 | bitmap_write_all(mddev->bitmap); |
| 7048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7050 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7051 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | if (!mddev->sync_thread) { |
| 7053 | printk(KERN_ERR "%s: could not start resync" |
| 7054 | " thread...\n", |
| 7055 | mdname(mddev)); |
| 7056 | /* leave the spares where they are, it shouldn't hurt */ |
| 7057 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7058 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7060 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7061 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | } |
| 7063 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7064 | if (!mddev->sync_thread) { |
| 7065 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7066 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7067 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7068 | if (mddev->sysfs_action) |
| 7069 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7071 | mddev_unlock(mddev); |
| 7072 | } |
| 7073 | } |
| 7074 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7075 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7076 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7077 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7078 | wait_event_timeout(rdev->blocked_wait, |
| 7079 | !test_bit(Blocked, &rdev->flags), |
| 7080 | msecs_to_jiffies(5000)); |
| 7081 | rdev_dec_pending(rdev, mddev); |
| 7082 | } |
| 7083 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7084 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7085 | static int md_notify_reboot(struct notifier_block *this, |
| 7086 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | { |
| 7088 | struct list_head *tmp; |
| 7089 | mddev_t *mddev; |
| 7090 | |
| 7091 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7092 | |
| 7093 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7094 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7095 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7096 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7097 | /* Force a switch to readonly even array |
| 7098 | * appears to still be in use. Hence |
| 7099 | * the '100'. |
| 7100 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7101 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7102 | mddev_unlock(mddev); |
| 7103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | /* |
| 7105 | * certain more exotic SCSI devices are known to be |
| 7106 | * volatile wrt too early system reboots. While the |
| 7107 | * right place to handle this issue is the given |
| 7108 | * driver, we do want to have a safe RAID driver ... |
| 7109 | */ |
| 7110 | mdelay(1000*1); |
| 7111 | } |
| 7112 | return NOTIFY_DONE; |
| 7113 | } |
| 7114 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7115 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | .notifier_call = md_notify_reboot, |
| 7117 | .next = NULL, |
| 7118 | .priority = INT_MAX, /* before any real devices */ |
| 7119 | }; |
| 7120 | |
| 7121 | static void md_geninit(void) |
| 7122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7124 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7125 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | } |
| 7127 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7128 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7130 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | return -1; |
| 7132 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7133 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | return -1; |
| 7135 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7136 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7137 | md_probe, NULL, NULL); |
| 7138 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | md_probe, NULL, NULL); |
| 7140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7142 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | |
| 7144 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7145 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | |
| 7149 | #ifndef MODULE |
| 7150 | |
| 7151 | /* |
| 7152 | * Searches all registered partitions for autorun RAID arrays |
| 7153 | * at boot time. |
| 7154 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7155 | |
| 7156 | static LIST_HEAD(all_detected_devices); |
| 7157 | struct detected_devices_node { |
| 7158 | struct list_head list; |
| 7159 | dev_t dev; |
| 7160 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | |
| 7162 | void md_autodetect_dev(dev_t dev) |
| 7163 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7164 | struct detected_devices_node *node_detected_dev; |
| 7165 | |
| 7166 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7167 | if (node_detected_dev) { |
| 7168 | node_detected_dev->dev = dev; |
| 7169 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7170 | } else { |
| 7171 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7172 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | } |
| 7175 | |
| 7176 | |
| 7177 | static void autostart_arrays(int part) |
| 7178 | { |
| 7179 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7180 | struct detected_devices_node *node_detected_dev; |
| 7181 | dev_t dev; |
| 7182 | int i_scanned, i_passed; |
| 7183 | |
| 7184 | i_scanned = 0; |
| 7185 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | |
| 7187 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7188 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7189 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7190 | i_scanned++; |
| 7191 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7192 | struct detected_devices_node, list); |
| 7193 | list_del(&node_detected_dev->list); |
| 7194 | dev = node_detected_dev->dev; |
| 7195 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7196 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | if (IS_ERR(rdev)) |
| 7198 | continue; |
| 7199 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7200 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | MD_BUG(); |
| 7202 | continue; |
| 7203 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7204 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7206 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7208 | |
| 7209 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7210 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | |
| 7212 | autorun_devices(part); |
| 7213 | } |
| 7214 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7215 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7216 | |
| 7217 | static __exit void md_exit(void) |
| 7218 | { |
| 7219 | mddev_t *mddev; |
| 7220 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7221 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7222 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7223 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7225 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | unregister_blkdev(mdp_major, "mdp"); |
| 7227 | unregister_reboot_notifier(&md_notifier); |
| 7228 | unregister_sysctl_table(raid_table_header); |
| 7229 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7230 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7232 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | } |
| 7234 | } |
| 7235 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7236 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | module_exit(md_exit) |
| 7238 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7239 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7240 | { |
| 7241 | return sprintf(buffer, "%d", start_readonly); |
| 7242 | } |
| 7243 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7244 | { |
| 7245 | char *e; |
| 7246 | int num = simple_strtoul(val, &e, 10); |
| 7247 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7248 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7249 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7250 | } |
| 7251 | return -EINVAL; |
| 7252 | } |
| 7253 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7254 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7255 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7256 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7257 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | EXPORT_SYMBOL(register_md_personality); |
| 7260 | EXPORT_SYMBOL(unregister_md_personality); |
| 7261 | EXPORT_SYMBOL(md_error); |
| 7262 | EXPORT_SYMBOL(md_done_sync); |
| 7263 | EXPORT_SYMBOL(md_write_start); |
| 7264 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | EXPORT_SYMBOL(md_register_thread); |
| 7266 | EXPORT_SYMBOL(md_unregister_thread); |
| 7267 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | EXPORT_SYMBOL(md_check_recovery); |
| 7269 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7270 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7271 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7272 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |