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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame^] | 3020 | if (mddev->pers->sync_request == NULL && |
| 3021 | mddev->external) { |
| 3022 | /* We are converting from a no-redundancy array |
| 3023 | * to a redundancy array and metadata is managed |
| 3024 | * externally so we need to be sure that writes |
| 3025 | * won't block due to a need to transition |
| 3026 | * clean->dirty |
| 3027 | * until external management is started. |
| 3028 | */ |
| 3029 | mddev->in_sync = 0; |
| 3030 | mddev->safemode_delay = 0; |
| 3031 | mddev->safemode = 0; |
| 3032 | } |
| 3033 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3034 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3035 | /* Invalidate devices that are now superfluous */ |
| 3036 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3037 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 3038 | rdev->raid_disk = -1; |
| 3039 | clear_bit(In_sync, &rdev->flags); |
| 3040 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3041 | mddev->pers = pers; |
| 3042 | mddev->private = priv; |
| 3043 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3044 | mddev->level = mddev->new_level; |
| 3045 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3046 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3047 | mddev->delta_disks = 0; |
| 3048 | pers->run(mddev); |
| 3049 | mddev_resume(mddev); |
| 3050 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3051 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3052 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3053 | return rv; |
| 3054 | } |
| 3055 | |
| 3056 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3057 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3058 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3059 | |
| 3060 | static ssize_t |
| 3061 | layout_show(mddev_t *mddev, char *page) |
| 3062 | { |
| 3063 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3064 | if (mddev->reshape_position != MaxSector && |
| 3065 | mddev->layout != mddev->new_layout) |
| 3066 | return sprintf(page, "%d (%d)\n", |
| 3067 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3068 | return sprintf(page, "%d\n", mddev->layout); |
| 3069 | } |
| 3070 | |
| 3071 | static ssize_t |
| 3072 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3073 | { |
| 3074 | char *e; |
| 3075 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3076 | |
| 3077 | if (!*buf || (*e && *e != '\n')) |
| 3078 | return -EINVAL; |
| 3079 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3080 | if (mddev->pers) { |
| 3081 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3082 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3083 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3084 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3085 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3086 | if (err) { |
| 3087 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3088 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3089 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3090 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3091 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3092 | if (mddev->reshape_position == MaxSector) |
| 3093 | mddev->layout = n; |
| 3094 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3095 | return len; |
| 3096 | } |
| 3097 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3098 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3099 | |
| 3100 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3101 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3102 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3103 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3104 | if (mddev->raid_disks == 0) |
| 3105 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3106 | if (mddev->reshape_position != MaxSector && |
| 3107 | mddev->delta_disks != 0) |
| 3108 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3109 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3110 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3111 | } |
| 3112 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3113 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3114 | |
| 3115 | static ssize_t |
| 3116 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3117 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3118 | char *e; |
| 3119 | int rv = 0; |
| 3120 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3121 | |
| 3122 | if (!*buf || (*e && *e != '\n')) |
| 3123 | return -EINVAL; |
| 3124 | |
| 3125 | if (mddev->pers) |
| 3126 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3127 | else if (mddev->reshape_position != MaxSector) { |
| 3128 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3129 | mddev->delta_disks = n - olddisks; |
| 3130 | mddev->raid_disks = n; |
| 3131 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3132 | mddev->raid_disks = n; |
| 3133 | return rv ? rv : len; |
| 3134 | } |
| 3135 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3136 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3137 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3138 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3139 | chunk_size_show(mddev_t *mddev, char *page) |
| 3140 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3141 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3142 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3143 | return sprintf(page, "%d (%d)\n", |
| 3144 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3145 | mddev->chunk_sectors << 9); |
| 3146 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | static ssize_t |
| 3150 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3151 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3152 | char *e; |
| 3153 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3154 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3155 | if (!*buf || (*e && *e != '\n')) |
| 3156 | return -EINVAL; |
| 3157 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3158 | if (mddev->pers) { |
| 3159 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3160 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3161 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3162 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3163 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3164 | if (err) { |
| 3165 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3166 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3167 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3168 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3169 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3170 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3171 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3172 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3173 | return len; |
| 3174 | } |
| 3175 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3176 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3177 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3178 | static ssize_t |
| 3179 | resync_start_show(mddev_t *mddev, char *page) |
| 3180 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3181 | if (mddev->recovery_cp == MaxSector) |
| 3182 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3183 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3184 | } |
| 3185 | |
| 3186 | static ssize_t |
| 3187 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3188 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3189 | char *e; |
| 3190 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3191 | |
| 3192 | if (mddev->pers) |
| 3193 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3194 | if (cmd_match(buf, "none")) |
| 3195 | n = MaxSector; |
| 3196 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3197 | return -EINVAL; |
| 3198 | |
| 3199 | mddev->recovery_cp = n; |
| 3200 | return len; |
| 3201 | } |
| 3202 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3203 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3204 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * The array state can be: |
| 3207 | * |
| 3208 | * clear |
| 3209 | * No devices, no size, no level |
| 3210 | * Equivalent to STOP_ARRAY ioctl |
| 3211 | * inactive |
| 3212 | * May have some settings, but array is not active |
| 3213 | * all IO results in error |
| 3214 | * When written, doesn't tear down array, but just stops it |
| 3215 | * suspended (not supported yet) |
| 3216 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3217 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3218 | * readonly |
| 3219 | * no resync can happen. no superblocks get written. |
| 3220 | * write requests fail |
| 3221 | * read-auto |
| 3222 | * like readonly, but behaves like 'clean' on a write request. |
| 3223 | * |
| 3224 | * clean - no pending writes, but otherwise active. |
| 3225 | * When written to inactive array, starts without resync |
| 3226 | * If a write request arrives then |
| 3227 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3228 | * if not known, block and switch to write-pending |
| 3229 | * If written to an active array that has pending writes, then fails. |
| 3230 | * active |
| 3231 | * fully active: IO and resync can be happening. |
| 3232 | * When written to inactive array, starts with resync |
| 3233 | * |
| 3234 | * write-pending |
| 3235 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3236 | * |
| 3237 | * active-idle |
| 3238 | * like active, but no writes have been seen for a while (100msec). |
| 3239 | * |
| 3240 | */ |
| 3241 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3242 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3243 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3244 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3245 | "write-pending", "active-idle", NULL }; |
| 3246 | |
| 3247 | static int match_word(const char *word, char **list) |
| 3248 | { |
| 3249 | int n; |
| 3250 | for (n=0; list[n]; n++) |
| 3251 | if (cmd_match(word, list[n])) |
| 3252 | break; |
| 3253 | return n; |
| 3254 | } |
| 3255 | |
| 3256 | static ssize_t |
| 3257 | array_state_show(mddev_t *mddev, char *page) |
| 3258 | { |
| 3259 | enum array_state st = inactive; |
| 3260 | |
| 3261 | if (mddev->pers) |
| 3262 | switch(mddev->ro) { |
| 3263 | case 1: |
| 3264 | st = readonly; |
| 3265 | break; |
| 3266 | case 2: |
| 3267 | st = read_auto; |
| 3268 | break; |
| 3269 | case 0: |
| 3270 | if (mddev->in_sync) |
| 3271 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3272 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3273 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3274 | else if (mddev->safemode) |
| 3275 | st = active_idle; |
| 3276 | else |
| 3277 | st = active; |
| 3278 | } |
| 3279 | else { |
| 3280 | if (list_empty(&mddev->disks) && |
| 3281 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3282 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3283 | st = clear; |
| 3284 | else |
| 3285 | st = inactive; |
| 3286 | } |
| 3287 | return sprintf(page, "%s\n", array_states[st]); |
| 3288 | } |
| 3289 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3290 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3291 | static int do_md_run(mddev_t * mddev); |
| 3292 | static int restart_array(mddev_t *mddev); |
| 3293 | |
| 3294 | static ssize_t |
| 3295 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3296 | { |
| 3297 | int err = -EINVAL; |
| 3298 | enum array_state st = match_word(buf, array_states); |
| 3299 | switch(st) { |
| 3300 | case bad_word: |
| 3301 | break; |
| 3302 | case clear: |
| 3303 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3304 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3305 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3306 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3307 | break; |
| 3308 | case inactive: |
| 3309 | /* stopping an active array */ |
| 3310 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3311 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3312 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3313 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3314 | } else |
| 3315 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3316 | break; |
| 3317 | case suspended: |
| 3318 | break; /* not supported yet */ |
| 3319 | case readonly: |
| 3320 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3321 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3322 | else { |
| 3323 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3324 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3325 | err = do_md_run(mddev); |
| 3326 | } |
| 3327 | break; |
| 3328 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3329 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3330 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3331 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3332 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3333 | err = restart_array(mddev); |
| 3334 | if (err == 0) { |
| 3335 | mddev->ro = 2; |
| 3336 | set_disk_ro(mddev->gendisk, 0); |
| 3337 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3338 | } else { |
| 3339 | mddev->ro = 2; |
| 3340 | err = do_md_run(mddev); |
| 3341 | } |
| 3342 | break; |
| 3343 | case clean: |
| 3344 | if (mddev->pers) { |
| 3345 | restart_array(mddev); |
| 3346 | spin_lock_irq(&mddev->write_lock); |
| 3347 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3348 | if (mddev->in_sync == 0) { |
| 3349 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3350 | if (mddev->safemode == 1) |
| 3351 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3352 | if (mddev->persistent) |
| 3353 | set_bit(MD_CHANGE_CLEAN, |
| 3354 | &mddev->flags); |
| 3355 | } |
| 3356 | err = 0; |
| 3357 | } else |
| 3358 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3359 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3360 | } else |
| 3361 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3362 | break; |
| 3363 | case active: |
| 3364 | if (mddev->pers) { |
| 3365 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3366 | if (mddev->external) |
| 3367 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3368 | wake_up(&mddev->sb_wait); |
| 3369 | err = 0; |
| 3370 | } else { |
| 3371 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3372 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3373 | err = do_md_run(mddev); |
| 3374 | } |
| 3375 | break; |
| 3376 | case write_pending: |
| 3377 | case active_idle: |
| 3378 | /* these cannot be set */ |
| 3379 | break; |
| 3380 | } |
| 3381 | if (err) |
| 3382 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3383 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3384 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3385 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3386 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3387 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3388 | static struct md_sysfs_entry md_array_state = |
| 3389 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3390 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3391 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3392 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3393 | return sprintf(page, "%d\n", |
| 3394 | atomic_read(&mddev->max_corr_read_errors)); |
| 3395 | } |
| 3396 | |
| 3397 | static ssize_t |
| 3398 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3399 | { |
| 3400 | char *e; |
| 3401 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3402 | |
| 3403 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3404 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3405 | return len; |
| 3406 | } |
| 3407 | return -EINVAL; |
| 3408 | } |
| 3409 | |
| 3410 | static struct md_sysfs_entry max_corr_read_errors = |
| 3411 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3412 | max_corrected_read_errors_store); |
| 3413 | |
| 3414 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3415 | null_show(mddev_t *mddev, char *page) |
| 3416 | { |
| 3417 | return -EINVAL; |
| 3418 | } |
| 3419 | |
| 3420 | static ssize_t |
| 3421 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3422 | { |
| 3423 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3424 | /* The new device is added to the array. |
| 3425 | * If the array has a persistent superblock, we read the |
| 3426 | * superblock to initialise info and check validity. |
| 3427 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3428 | * which mainly checks size. |
| 3429 | */ |
| 3430 | char *e; |
| 3431 | int major = simple_strtoul(buf, &e, 10); |
| 3432 | int minor; |
| 3433 | dev_t dev; |
| 3434 | mdk_rdev_t *rdev; |
| 3435 | int err; |
| 3436 | |
| 3437 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3438 | return -EINVAL; |
| 3439 | minor = simple_strtoul(e+1, &e, 10); |
| 3440 | if (*e && *e != '\n') |
| 3441 | return -EINVAL; |
| 3442 | dev = MKDEV(major, minor); |
| 3443 | if (major != MAJOR(dev) || |
| 3444 | minor != MINOR(dev)) |
| 3445 | return -EOVERFLOW; |
| 3446 | |
| 3447 | |
| 3448 | if (mddev->persistent) { |
| 3449 | rdev = md_import_device(dev, mddev->major_version, |
| 3450 | mddev->minor_version); |
| 3451 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3452 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3453 | mdk_rdev_t, same_set); |
| 3454 | err = super_types[mddev->major_version] |
| 3455 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3456 | if (err < 0) |
| 3457 | goto out; |
| 3458 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3459 | } else if (mddev->external) |
| 3460 | rdev = md_import_device(dev, -2, -1); |
| 3461 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3462 | rdev = md_import_device(dev, -1, -1); |
| 3463 | |
| 3464 | if (IS_ERR(rdev)) |
| 3465 | return PTR_ERR(rdev); |
| 3466 | err = bind_rdev_to_array(rdev, mddev); |
| 3467 | out: |
| 3468 | if (err) |
| 3469 | export_rdev(rdev); |
| 3470 | return err ? err : len; |
| 3471 | } |
| 3472 | |
| 3473 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3474 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3475 | |
| 3476 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3477 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3478 | { |
| 3479 | char *end; |
| 3480 | unsigned long chunk, end_chunk; |
| 3481 | |
| 3482 | if (!mddev->bitmap) |
| 3483 | goto out; |
| 3484 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3485 | while (*buf) { |
| 3486 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3487 | if (buf == end) break; |
| 3488 | if (*end == '-') { /* range */ |
| 3489 | buf = end + 1; |
| 3490 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3491 | if (buf == end) break; |
| 3492 | } |
| 3493 | if (*end && !isspace(*end)) break; |
| 3494 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3495 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3496 | } |
| 3497 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3498 | out: |
| 3499 | return len; |
| 3500 | } |
| 3501 | |
| 3502 | static struct md_sysfs_entry md_bitmap = |
| 3503 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3504 | |
| 3505 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3506 | size_show(mddev_t *mddev, char *page) |
| 3507 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3508 | return sprintf(page, "%llu\n", |
| 3509 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3510 | } |
| 3511 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3512 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3513 | |
| 3514 | static ssize_t |
| 3515 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3516 | { |
| 3517 | /* If array is inactive, we can reduce the component size, but |
| 3518 | * not increase it (except from 0). |
| 3519 | * If array is active, we can try an on-line resize |
| 3520 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3521 | sector_t sectors; |
| 3522 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3523 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3524 | if (err < 0) |
| 3525 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3526 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3527 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3528 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3529 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3530 | if (mddev->dev_sectors == 0 || |
| 3531 | mddev->dev_sectors > sectors) |
| 3532 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3533 | else |
| 3534 | err = -ENOSPC; |
| 3535 | } |
| 3536 | return err ? err : len; |
| 3537 | } |
| 3538 | |
| 3539 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3540 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3541 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3542 | |
| 3543 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3544 | * This is one of |
| 3545 | * 'none' for arrays with no metadata (good luck...) |
| 3546 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3547 | * or N.M for internally known formats |
| 3548 | */ |
| 3549 | static ssize_t |
| 3550 | metadata_show(mddev_t *mddev, char *page) |
| 3551 | { |
| 3552 | if (mddev->persistent) |
| 3553 | return sprintf(page, "%d.%d\n", |
| 3554 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3555 | else if (mddev->external) |
| 3556 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3557 | else |
| 3558 | return sprintf(page, "none\n"); |
| 3559 | } |
| 3560 | |
| 3561 | static ssize_t |
| 3562 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3563 | { |
| 3564 | int major, minor; |
| 3565 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3566 | /* Changing the details of 'external' metadata is |
| 3567 | * always permitted. Otherwise there must be |
| 3568 | * no devices attached to the array. |
| 3569 | */ |
| 3570 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3571 | ; |
| 3572 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3573 | return -EBUSY; |
| 3574 | |
| 3575 | if (cmd_match(buf, "none")) { |
| 3576 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3577 | mddev->external = 0; |
| 3578 | mddev->major_version = 0; |
| 3579 | mddev->minor_version = 90; |
| 3580 | return len; |
| 3581 | } |
| 3582 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3583 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3584 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3585 | namelen = sizeof(mddev->metadata_type)-1; |
| 3586 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3587 | mddev->metadata_type[namelen] = 0; |
| 3588 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3589 | mddev->metadata_type[--namelen] = 0; |
| 3590 | mddev->persistent = 0; |
| 3591 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3592 | mddev->major_version = 0; |
| 3593 | mddev->minor_version = 90; |
| 3594 | return len; |
| 3595 | } |
| 3596 | major = simple_strtoul(buf, &e, 10); |
| 3597 | if (e==buf || *e != '.') |
| 3598 | return -EINVAL; |
| 3599 | buf = e+1; |
| 3600 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3601 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3602 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3603 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3604 | return -ENOENT; |
| 3605 | mddev->major_version = major; |
| 3606 | mddev->minor_version = minor; |
| 3607 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3608 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3609 | return len; |
| 3610 | } |
| 3611 | |
| 3612 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3613 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3614 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3615 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3616 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3617 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3618 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3619 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3620 | type = "frozen"; |
| 3621 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3622 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3623 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3624 | type = "reshape"; |
| 3625 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3626 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3627 | type = "resync"; |
| 3628 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3629 | type = "check"; |
| 3630 | else |
| 3631 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3632 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3633 | type = "recover"; |
| 3634 | } |
| 3635 | return sprintf(page, "%s\n", type); |
| 3636 | } |
| 3637 | |
| 3638 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3639 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3640 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3641 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3642 | return -EINVAL; |
| 3643 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3644 | if (cmd_match(page, "frozen")) |
| 3645 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3646 | else |
| 3647 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3648 | |
| 3649 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3650 | if (mddev->sync_thread) { |
| 3651 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3652 | md_unregister_thread(mddev->sync_thread); |
| 3653 | mddev->sync_thread = NULL; |
| 3654 | mddev->recovery = 0; |
| 3655 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3656 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3657 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3658 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3659 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3660 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3661 | else if (cmd_match(page, "recover")) { |
| 3662 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3663 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3664 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3665 | int err; |
| 3666 | if (mddev->pers->start_reshape == NULL) |
| 3667 | return -EINVAL; |
| 3668 | err = mddev->pers->start_reshape(mddev); |
| 3669 | if (err) |
| 3670 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3671 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3672 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3673 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3674 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3675 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3676 | return -EINVAL; |
| 3677 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3678 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3679 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3680 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3681 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3682 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3683 | return len; |
| 3684 | } |
| 3685 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3686 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3687 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3688 | { |
| 3689 | return sprintf(page, "%llu\n", |
| 3690 | (unsigned long long) mddev->resync_mismatches); |
| 3691 | } |
| 3692 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3693 | static struct md_sysfs_entry md_scan_mode = |
| 3694 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3695 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3696 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3697 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3698 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3699 | static ssize_t |
| 3700 | sync_min_show(mddev_t *mddev, char *page) |
| 3701 | { |
| 3702 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3703 | mddev->sync_speed_min ? "local": "system"); |
| 3704 | } |
| 3705 | |
| 3706 | static ssize_t |
| 3707 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3708 | { |
| 3709 | int min; |
| 3710 | char *e; |
| 3711 | if (strncmp(buf, "system", 6)==0) { |
| 3712 | mddev->sync_speed_min = 0; |
| 3713 | return len; |
| 3714 | } |
| 3715 | min = simple_strtoul(buf, &e, 10); |
| 3716 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3717 | return -EINVAL; |
| 3718 | mddev->sync_speed_min = min; |
| 3719 | return len; |
| 3720 | } |
| 3721 | |
| 3722 | static struct md_sysfs_entry md_sync_min = |
| 3723 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3724 | |
| 3725 | static ssize_t |
| 3726 | sync_max_show(mddev_t *mddev, char *page) |
| 3727 | { |
| 3728 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3729 | mddev->sync_speed_max ? "local": "system"); |
| 3730 | } |
| 3731 | |
| 3732 | static ssize_t |
| 3733 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3734 | { |
| 3735 | int max; |
| 3736 | char *e; |
| 3737 | if (strncmp(buf, "system", 6)==0) { |
| 3738 | mddev->sync_speed_max = 0; |
| 3739 | return len; |
| 3740 | } |
| 3741 | max = simple_strtoul(buf, &e, 10); |
| 3742 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3743 | return -EINVAL; |
| 3744 | mddev->sync_speed_max = max; |
| 3745 | return len; |
| 3746 | } |
| 3747 | |
| 3748 | static struct md_sysfs_entry md_sync_max = |
| 3749 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3750 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3751 | static ssize_t |
| 3752 | degraded_show(mddev_t *mddev, char *page) |
| 3753 | { |
| 3754 | return sprintf(page, "%d\n", mddev->degraded); |
| 3755 | } |
| 3756 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3757 | |
| 3758 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3759 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3760 | { |
| 3761 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3762 | } |
| 3763 | |
| 3764 | static ssize_t |
| 3765 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3766 | { |
| 3767 | long n; |
| 3768 | |
| 3769 | if (strict_strtol(buf, 10, &n)) |
| 3770 | return -EINVAL; |
| 3771 | |
| 3772 | if (n != 0 && n != 1) |
| 3773 | return -EINVAL; |
| 3774 | |
| 3775 | mddev->parallel_resync = n; |
| 3776 | |
| 3777 | if (mddev->sync_thread) |
| 3778 | wake_up(&resync_wait); |
| 3779 | |
| 3780 | return len; |
| 3781 | } |
| 3782 | |
| 3783 | /* force parallel resync, even with shared block devices */ |
| 3784 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3785 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3786 | sync_force_parallel_show, sync_force_parallel_store); |
| 3787 | |
| 3788 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3789 | sync_speed_show(mddev_t *mddev, char *page) |
| 3790 | { |
| 3791 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3792 | if (mddev->curr_resync == 0) |
| 3793 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3794 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3795 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3796 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3797 | db = resync - mddev->resync_mark_cnt; |
| 3798 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3801 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3802 | |
| 3803 | static ssize_t |
| 3804 | sync_completed_show(mddev_t *mddev, char *page) |
| 3805 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3806 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3807 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3808 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3809 | return sprintf(page, "none\n"); |
| 3810 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3811 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3812 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3813 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3814 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3815 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3816 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3817 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3818 | } |
| 3819 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3820 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3821 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3822 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3823 | min_sync_show(mddev_t *mddev, char *page) |
| 3824 | { |
| 3825 | return sprintf(page, "%llu\n", |
| 3826 | (unsigned long long)mddev->resync_min); |
| 3827 | } |
| 3828 | static ssize_t |
| 3829 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3830 | { |
| 3831 | unsigned long long min; |
| 3832 | if (strict_strtoull(buf, 10, &min)) |
| 3833 | return -EINVAL; |
| 3834 | if (min > mddev->resync_max) |
| 3835 | return -EINVAL; |
| 3836 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3837 | return -EBUSY; |
| 3838 | |
| 3839 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3840 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3841 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3842 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3843 | return -EINVAL; |
| 3844 | } |
| 3845 | mddev->resync_min = min; |
| 3846 | |
| 3847 | return len; |
| 3848 | } |
| 3849 | |
| 3850 | static struct md_sysfs_entry md_min_sync = |
| 3851 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3852 | |
| 3853 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3854 | max_sync_show(mddev_t *mddev, char *page) |
| 3855 | { |
| 3856 | if (mddev->resync_max == MaxSector) |
| 3857 | return sprintf(page, "max\n"); |
| 3858 | else |
| 3859 | return sprintf(page, "%llu\n", |
| 3860 | (unsigned long long)mddev->resync_max); |
| 3861 | } |
| 3862 | static ssize_t |
| 3863 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3864 | { |
| 3865 | if (strncmp(buf, "max", 3) == 0) |
| 3866 | mddev->resync_max = MaxSector; |
| 3867 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3868 | unsigned long long max; |
| 3869 | if (strict_strtoull(buf, 10, &max)) |
| 3870 | return -EINVAL; |
| 3871 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3872 | return -EINVAL; |
| 3873 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3874 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3875 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3876 | return -EBUSY; |
| 3877 | |
| 3878 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3879 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3880 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3881 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3882 | return -EINVAL; |
| 3883 | } |
| 3884 | mddev->resync_max = max; |
| 3885 | } |
| 3886 | wake_up(&mddev->recovery_wait); |
| 3887 | return len; |
| 3888 | } |
| 3889 | |
| 3890 | static struct md_sysfs_entry md_max_sync = |
| 3891 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3892 | |
| 3893 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3894 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3895 | { |
| 3896 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3897 | } |
| 3898 | |
| 3899 | static ssize_t |
| 3900 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3901 | { |
| 3902 | char *e; |
| 3903 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3904 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3905 | if (mddev->pers == NULL || |
| 3906 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3907 | return -EINVAL; |
| 3908 | if (buf == e || (*e && *e != '\n')) |
| 3909 | return -EINVAL; |
| 3910 | if (new >= mddev->suspend_hi || |
| 3911 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3912 | mddev->suspend_lo = new; |
| 3913 | mddev->pers->quiesce(mddev, 2); |
| 3914 | return len; |
| 3915 | } else |
| 3916 | return -EINVAL; |
| 3917 | } |
| 3918 | static struct md_sysfs_entry md_suspend_lo = |
| 3919 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3920 | |
| 3921 | |
| 3922 | static ssize_t |
| 3923 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3924 | { |
| 3925 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3926 | } |
| 3927 | |
| 3928 | static ssize_t |
| 3929 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3930 | { |
| 3931 | char *e; |
| 3932 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3933 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3934 | if (mddev->pers == NULL || |
| 3935 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3936 | return -EINVAL; |
| 3937 | if (buf == e || (*e && *e != '\n')) |
| 3938 | return -EINVAL; |
| 3939 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3940 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3941 | mddev->suspend_hi = new; |
| 3942 | mddev->pers->quiesce(mddev, 1); |
| 3943 | mddev->pers->quiesce(mddev, 0); |
| 3944 | return len; |
| 3945 | } else |
| 3946 | return -EINVAL; |
| 3947 | } |
| 3948 | static struct md_sysfs_entry md_suspend_hi = |
| 3949 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3950 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3951 | static ssize_t |
| 3952 | reshape_position_show(mddev_t *mddev, char *page) |
| 3953 | { |
| 3954 | if (mddev->reshape_position != MaxSector) |
| 3955 | return sprintf(page, "%llu\n", |
| 3956 | (unsigned long long)mddev->reshape_position); |
| 3957 | strcpy(page, "none\n"); |
| 3958 | return 5; |
| 3959 | } |
| 3960 | |
| 3961 | static ssize_t |
| 3962 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3963 | { |
| 3964 | char *e; |
| 3965 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3966 | if (mddev->pers) |
| 3967 | return -EBUSY; |
| 3968 | if (buf == e || (*e && *e != '\n')) |
| 3969 | return -EINVAL; |
| 3970 | mddev->reshape_position = new; |
| 3971 | mddev->delta_disks = 0; |
| 3972 | mddev->new_level = mddev->level; |
| 3973 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3974 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3975 | return len; |
| 3976 | } |
| 3977 | |
| 3978 | static struct md_sysfs_entry md_reshape_position = |
| 3979 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3980 | reshape_position_store); |
| 3981 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3982 | static ssize_t |
| 3983 | array_size_show(mddev_t *mddev, char *page) |
| 3984 | { |
| 3985 | if (mddev->external_size) |
| 3986 | return sprintf(page, "%llu\n", |
| 3987 | (unsigned long long)mddev->array_sectors/2); |
| 3988 | else |
| 3989 | return sprintf(page, "default\n"); |
| 3990 | } |
| 3991 | |
| 3992 | static ssize_t |
| 3993 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3994 | { |
| 3995 | sector_t sectors; |
| 3996 | |
| 3997 | if (strncmp(buf, "default", 7) == 0) { |
| 3998 | if (mddev->pers) |
| 3999 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4000 | else |
| 4001 | sectors = mddev->array_sectors; |
| 4002 | |
| 4003 | mddev->external_size = 0; |
| 4004 | } else { |
| 4005 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4006 | return -EINVAL; |
| 4007 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4008 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4009 | |
| 4010 | mddev->external_size = 1; |
| 4011 | } |
| 4012 | |
| 4013 | mddev->array_sectors = sectors; |
| 4014 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4015 | if (mddev->pers) |
| 4016 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4017 | |
| 4018 | return len; |
| 4019 | } |
| 4020 | |
| 4021 | static struct md_sysfs_entry md_array_size = |
| 4022 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4023 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4024 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4025 | static struct attribute *md_default_attrs[] = { |
| 4026 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4027 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4028 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4029 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4030 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4031 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4032 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4033 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4034 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4035 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4036 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4037 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4038 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4039 | NULL, |
| 4040 | }; |
| 4041 | |
| 4042 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4043 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4044 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4045 | &md_sync_min.attr, |
| 4046 | &md_sync_max.attr, |
| 4047 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4048 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4049 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4050 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4051 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4052 | &md_suspend_lo.attr, |
| 4053 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4054 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4055 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4056 | NULL, |
| 4057 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4058 | static struct attribute_group md_redundancy_group = { |
| 4059 | .name = NULL, |
| 4060 | .attrs = md_redundancy_attrs, |
| 4061 | }; |
| 4062 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4063 | |
| 4064 | static ssize_t |
| 4065 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4066 | { |
| 4067 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4068 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4069 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4070 | |
| 4071 | if (!entry->show) |
| 4072 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4073 | rv = mddev_lock(mddev); |
| 4074 | if (!rv) { |
| 4075 | rv = entry->show(mddev, page); |
| 4076 | mddev_unlock(mddev); |
| 4077 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4078 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | static ssize_t |
| 4082 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4083 | const char *page, size_t length) |
| 4084 | { |
| 4085 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4086 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4087 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4088 | |
| 4089 | if (!entry->store) |
| 4090 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4091 | if (!capable(CAP_SYS_ADMIN)) |
| 4092 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4093 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4094 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4095 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4096 | if (!rv) { |
| 4097 | rv = entry->store(mddev, page, length); |
| 4098 | mddev_unlock(mddev); |
| 4099 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4100 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | static void md_free(struct kobject *ko) |
| 4104 | { |
| 4105 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4106 | |
| 4107 | if (mddev->sysfs_state) |
| 4108 | sysfs_put(mddev->sysfs_state); |
| 4109 | |
| 4110 | if (mddev->gendisk) { |
| 4111 | del_gendisk(mddev->gendisk); |
| 4112 | put_disk(mddev->gendisk); |
| 4113 | } |
| 4114 | if (mddev->queue) |
| 4115 | blk_cleanup_queue(mddev->queue); |
| 4116 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4117 | kfree(mddev); |
| 4118 | } |
| 4119 | |
| 4120 | static struct sysfs_ops md_sysfs_ops = { |
| 4121 | .show = md_attr_show, |
| 4122 | .store = md_attr_store, |
| 4123 | }; |
| 4124 | static struct kobj_type md_ktype = { |
| 4125 | .release = md_free, |
| 4126 | .sysfs_ops = &md_sysfs_ops, |
| 4127 | .default_attrs = md_default_attrs, |
| 4128 | }; |
| 4129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | int mdp_major = 0; |
| 4131 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4132 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4133 | { |
| 4134 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4135 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4136 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4137 | kobject_del(&mddev->kobj); |
| 4138 | kobject_put(&mddev->kobj); |
| 4139 | } |
| 4140 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4141 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4143 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | mddev_t *mddev = mddev_find(dev); |
| 4145 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4146 | int partitioned; |
| 4147 | int shift; |
| 4148 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4149 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | |
| 4151 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4152 | return -ENODEV; |
| 4153 | |
| 4154 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4155 | shift = partitioned ? MdpMinorShift : 0; |
| 4156 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4158 | /* wait for any previous instance if this device |
| 4159 | * to be completed removed (mddev_delayed_delete). |
| 4160 | */ |
| 4161 | flush_scheduled_work(); |
| 4162 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4163 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4164 | error = -EEXIST; |
| 4165 | if (mddev->gendisk) |
| 4166 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4167 | |
| 4168 | if (name) { |
| 4169 | /* Need to ensure that 'name' is not a duplicate. |
| 4170 | */ |
| 4171 | mddev_t *mddev2; |
| 4172 | spin_lock(&all_mddevs_lock); |
| 4173 | |
| 4174 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4175 | if (mddev2->gendisk && |
| 4176 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4177 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4178 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4179 | } |
| 4180 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4182 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4183 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4184 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4185 | if (!mddev->queue) |
| 4186 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4187 | mddev->queue->queuedata = mddev; |
| 4188 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4189 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4190 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4191 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4192 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | disk = alloc_disk(1 << shift); |
| 4195 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4196 | blk_cleanup_queue(mddev->queue); |
| 4197 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4198 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4200 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4202 | if (name) |
| 4203 | strcpy(disk->disk_name, name); |
| 4204 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4206 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | disk->fops = &md_fops; |
| 4209 | disk->private_data = mddev; |
| 4210 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4211 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4212 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4213 | * remove it now. |
| 4214 | */ |
| 4215 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | add_disk(disk); |
| 4217 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4218 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4219 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4220 | if (error) { |
| 4221 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4222 | * __must_check, we must do something with the result |
| 4223 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4224 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4225 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4226 | error = 0; |
| 4227 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4228 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4229 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4230 | abort: |
| 4231 | mutex_unlock(&disks_mutex); |
| 4232 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4233 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4234 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4235 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4236 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4237 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4241 | { |
| 4242 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | return NULL; |
| 4244 | } |
| 4245 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4246 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4247 | { |
| 4248 | /* val must be "md_*" where * is not all digits. |
| 4249 | * We allocate an array with a large free minor number, and |
| 4250 | * set the name to val. val must not already be an active name. |
| 4251 | */ |
| 4252 | int len = strlen(val); |
| 4253 | char buf[DISK_NAME_LEN]; |
| 4254 | |
| 4255 | while (len && val[len-1] == '\n') |
| 4256 | len--; |
| 4257 | if (len >= DISK_NAME_LEN) |
| 4258 | return -E2BIG; |
| 4259 | strlcpy(buf, val, len+1); |
| 4260 | if (strncmp(buf, "md_", 3) != 0) |
| 4261 | return -EINVAL; |
| 4262 | return md_alloc(0, buf); |
| 4263 | } |
| 4264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | static void md_safemode_timeout(unsigned long data) |
| 4266 | { |
| 4267 | mddev_t *mddev = (mddev_t *) data; |
| 4268 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4269 | if (!atomic_read(&mddev->writes_pending)) { |
| 4270 | mddev->safemode = 1; |
| 4271 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4272 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | md_wakeup_thread(mddev->thread); |
| 4275 | } |
| 4276 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4277 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | |
| 4279 | static int do_md_run(mddev_t * mddev) |
| 4280 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4281 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | mdk_rdev_t *rdev; |
| 4283 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4284 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4286 | if (list_empty(&mddev->disks)) |
| 4287 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | |
| 4290 | if (mddev->pers) |
| 4291 | return -EBUSY; |
| 4292 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4293 | /* These two calls synchronise us with the |
| 4294 | * sysfs_remove_group calls in mddev_unlock, |
| 4295 | * so they must have completed. |
| 4296 | */ |
| 4297 | mutex_lock(&mddev->open_mutex); |
| 4298 | mutex_unlock(&mddev->open_mutex); |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | /* |
| 4301 | * Analyze all RAID superblock(s) |
| 4302 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4303 | if (!mddev->raid_disks) { |
| 4304 | if (!mddev->persistent) |
| 4305 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4306 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4309 | if (mddev->level != LEVEL_NONE) |
| 4310 | request_module("md-level-%d", mddev->level); |
| 4311 | else if (mddev->clevel[0]) |
| 4312 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | |
| 4314 | /* |
| 4315 | * Drop all container device buffers, from now on |
| 4316 | * the only valid external interface is through the md |
| 4317 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4319 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4320 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | continue; |
| 4322 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4323 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4324 | |
| 4325 | /* perform some consistency tests on the device. |
| 4326 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4327 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4328 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4329 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4330 | if (mddev->dev_sectors && |
| 4331 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4332 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4333 | printk("md: %s: data overlaps metadata\n", |
| 4334 | mdname(mddev)); |
| 4335 | return -EINVAL; |
| 4336 | } |
| 4337 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4338 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4339 | > rdev->data_offset) { |
| 4340 | printk("md: %s: metadata overlaps data\n", |
| 4341 | mdname(mddev)); |
| 4342 | return -EINVAL; |
| 4343 | } |
| 4344 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4345 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | } |
| 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | |
| 4350 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4351 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4352 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4354 | if (mddev->level != LEVEL_NONE) |
| 4355 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4356 | mddev->level); |
| 4357 | else |
| 4358 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4359 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | return -EINVAL; |
| 4361 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4362 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4364 | if (mddev->level != pers->level) { |
| 4365 | mddev->level = pers->level; |
| 4366 | mddev->new_level = pers->level; |
| 4367 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4368 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4370 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4371 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4372 | /* This personality cannot handle reshaping... */ |
| 4373 | mddev->pers = NULL; |
| 4374 | module_put(pers->owner); |
| 4375 | return -EINVAL; |
| 4376 | } |
| 4377 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4378 | if (pers->sync_request) { |
| 4379 | /* Warn if this is a potentially silly |
| 4380 | * configuration. |
| 4381 | */ |
| 4382 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4383 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4384 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4385 | |
| 4386 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4387 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4388 | if (rdev < rdev2 && |
| 4389 | rdev->bdev->bd_contains == |
| 4390 | rdev2->bdev->bd_contains) { |
| 4391 | printk(KERN_WARNING |
| 4392 | "%s: WARNING: %s appears to be" |
| 4393 | " on the same physical disk as" |
| 4394 | " %s.\n", |
| 4395 | mdname(mddev), |
| 4396 | bdevname(rdev->bdev,b), |
| 4397 | bdevname(rdev2->bdev,b2)); |
| 4398 | warned = 1; |
| 4399 | } |
| 4400 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4401 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4402 | if (warned) |
| 4403 | printk(KERN_WARNING |
| 4404 | "True protection against single-disk" |
| 4405 | " failure might be compromised.\n"); |
| 4406 | } |
| 4407 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4408 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4409 | /* may be over-ridden by personality */ |
| 4410 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4411 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4412 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4413 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4415 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4416 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4417 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4418 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4419 | if (err) |
| 4420 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4421 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4422 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4423 | " but 'external_size' not in effect?\n", __func__); |
| 4424 | printk(KERN_ERR |
| 4425 | "md: invalid array_size %llu > default size %llu\n", |
| 4426 | (unsigned long long)mddev->array_sectors / 2, |
| 4427 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4428 | err = -EINVAL; |
| 4429 | mddev->pers->stop(mddev); |
| 4430 | } |
| 4431 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4432 | err = bitmap_create(mddev); |
| 4433 | if (err) { |
| 4434 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4435 | mdname(mddev), err); |
| 4436 | mddev->pers->stop(mddev); |
| 4437 | } |
| 4438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | module_put(mddev->pers->owner); |
| 4441 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4442 | bitmap_destroy(mddev); |
| 4443 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4445 | if (mddev->pers->sync_request) { |
| 4446 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4447 | printk(KERN_WARNING |
| 4448 | "md: cannot register extra attributes for %s\n", |
| 4449 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4450 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4451 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4452 | mddev->ro = 0; |
| 4453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4455 | atomic_set(&mddev->max_corr_read_errors, |
| 4456 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | mddev->safemode = 0; |
| 4458 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4459 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4460 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4462 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4463 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4464 | if (rdev->raid_disk >= 0) { |
| 4465 | char nm[20]; |
| 4466 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4467 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4468 | printk("md: cannot register %s for %s\n", |
| 4469 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | |
| 4472 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4473 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4474 | if (mddev->flags) |
| 4475 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4477 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4479 | md_wakeup_thread(mddev->thread); |
| 4480 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4481 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4482 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4483 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4484 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4485 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4486 | if (mddev->sysfs_action) |
| 4487 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4488 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4489 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | return 0; |
| 4491 | } |
| 4492 | |
| 4493 | static int restart_array(mddev_t *mddev) |
| 4494 | { |
| 4495 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4497 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4499 | return -ENXIO; |
| 4500 | if (!mddev->pers) |
| 4501 | return -EINVAL; |
| 4502 | if (!mddev->ro) |
| 4503 | return -EBUSY; |
| 4504 | mddev->safemode = 0; |
| 4505 | mddev->ro = 0; |
| 4506 | set_disk_ro(disk, 0); |
| 4507 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4508 | mdname(mddev)); |
| 4509 | /* Kick recovery or resync if necessary */ |
| 4510 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4511 | md_wakeup_thread(mddev->thread); |
| 4512 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4513 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4514 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | } |
| 4516 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4517 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4518 | * to the file ourselves */ |
| 4519 | static int deny_bitmap_write_access(struct file * file) |
| 4520 | { |
| 4521 | struct inode *inode = file->f_mapping->host; |
| 4522 | |
| 4523 | spin_lock(&inode->i_lock); |
| 4524 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4525 | spin_unlock(&inode->i_lock); |
| 4526 | return -ETXTBSY; |
| 4527 | } |
| 4528 | atomic_set(&inode->i_writecount, -1); |
| 4529 | spin_unlock(&inode->i_lock); |
| 4530 | |
| 4531 | return 0; |
| 4532 | } |
| 4533 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4534 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4535 | { |
| 4536 | struct inode *inode = file->f_mapping->host; |
| 4537 | |
| 4538 | spin_lock(&inode->i_lock); |
| 4539 | atomic_set(&inode->i_writecount, 1); |
| 4540 | spin_unlock(&inode->i_lock); |
| 4541 | } |
| 4542 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4543 | /* mode: |
| 4544 | * 0 - completely stop and dis-assemble array |
| 4545 | * 1 - switch to readonly |
| 4546 | * 2 - stop but do not disassemble array |
| 4547 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4548 | 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] | 4549 | { |
| 4550 | int err = 0; |
| 4551 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4552 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4554 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4555 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4556 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4557 | err = -EBUSY; |
| 4558 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | |
| 4560 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4561 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4563 | md_unregister_thread(mddev->sync_thread); |
| 4564 | mddev->sync_thread = NULL; |
| 4565 | } |
| 4566 | |
| 4567 | del_timer_sync(&mddev->safemode_timer); |
| 4568 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4569 | switch(mode) { |
| 4570 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4572 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | goto out; |
| 4574 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4575 | break; |
| 4576 | case 0: /* disassemble */ |
| 4577 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4578 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4579 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | if (mddev->ro) |
| 4581 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4584 | mddev->queue->merge_bvec_fn = NULL; |
| 4585 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4586 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | module_put(mddev->pers->owner); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 4588 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4589 | mddev->to_remove = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4591 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4592 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4593 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4594 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4595 | if (rdev->raid_disk >= 0) { |
| 4596 | char nm[20]; |
| 4597 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4598 | sysfs_remove_link(&mddev->kobj, nm); |
| 4599 | } |
| 4600 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4601 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4602 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | if (mddev->ro) |
| 4605 | mddev->ro = 0; |
| 4606 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4607 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | /* mark array as shutdown cleanly */ |
| 4609 | mddev->in_sync = 1; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4610 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4612 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4614 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4615 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4617 | out: |
| 4618 | mutex_unlock(&mddev->open_mutex); |
| 4619 | if (err) |
| 4620 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | /* |
| 4622 | * Free resources if final stop |
| 4623 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4624 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4627 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4628 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4629 | if (mddev->bitmap_info.file) { |
| 4630 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4631 | fput(mddev->bitmap_info.file); |
| 4632 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4633 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4634 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | export_array(mddev); |
| 4637 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4638 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4639 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4640 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4641 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4642 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4643 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4644 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4645 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4646 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4647 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4648 | mddev->level = LEVEL_NONE; |
| 4649 | mddev->clevel[0] = 0; |
| 4650 | mddev->flags = 0; |
| 4651 | mddev->ro = 0; |
| 4652 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4653 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4654 | mddev->ctime = mddev->utime = 0; |
| 4655 | mddev->layout = 0; |
| 4656 | mddev->max_disks = 0; |
| 4657 | mddev->events = 0; |
| 4658 | mddev->delta_disks = 0; |
| 4659 | mddev->new_level = LEVEL_NONE; |
| 4660 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4661 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4662 | mddev->curr_resync = 0; |
| 4663 | mddev->resync_mismatches = 0; |
| 4664 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4665 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4666 | mddev->recovery = 0; |
| 4667 | mddev->in_sync = 0; |
| 4668 | mddev->changed = 0; |
| 4669 | mddev->degraded = 0; |
| 4670 | mddev->barriers_work = 0; |
| 4671 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4672 | mddev->bitmap_info.offset = 0; |
| 4673 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 4674 | mddev->bitmap_info.chunksize = 0; |
| 4675 | mddev->bitmap_info.daemon_sleep = 0; |
| 4676 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4677 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4678 | if (mddev->hold_active == UNTIL_STOP) |
| 4679 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4680 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4681 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4683 | mdname(mddev)); |
| 4684 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4685 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4686 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4687 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | return err; |
| 4689 | } |
| 4690 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4691 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | static void autorun_array(mddev_t *mddev) |
| 4693 | { |
| 4694 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | int err; |
| 4696 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4697 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | |
| 4700 | printk(KERN_INFO "md: running: "); |
| 4701 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4702 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | char b[BDEVNAME_SIZE]; |
| 4704 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4705 | } |
| 4706 | printk("\n"); |
| 4707 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4708 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | if (err) { |
| 4710 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4711 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | /* |
| 4716 | * lets try to run arrays based on all disks that have arrived |
| 4717 | * until now. (those are in pending_raid_disks) |
| 4718 | * |
| 4719 | * the method: pick the first pending disk, collect all disks with |
| 4720 | * the same UUID, remove all from the pending list and put them into |
| 4721 | * the 'same_array' list. Then order this list based on superblock |
| 4722 | * update time (freshest comes first), kick out 'old' disks and |
| 4723 | * compare superblocks. If everything's fine then run it. |
| 4724 | * |
| 4725 | * If "unit" is allocated, then bump its reference count |
| 4726 | */ |
| 4727 | static void autorun_devices(int part) |
| 4728 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4729 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | mddev_t *mddev; |
| 4731 | char b[BDEVNAME_SIZE]; |
| 4732 | |
| 4733 | printk(KERN_INFO "md: autorun ...\n"); |
| 4734 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4735 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4737 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | rdev0 = list_entry(pending_raid_disks.next, |
| 4739 | mdk_rdev_t, same_set); |
| 4740 | |
| 4741 | printk(KERN_INFO "md: considering %s ...\n", |
| 4742 | bdevname(rdev0->bdev,b)); |
| 4743 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4744 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4746 | printk(KERN_INFO "md: adding %s ...\n", |
| 4747 | bdevname(rdev->bdev,b)); |
| 4748 | list_move(&rdev->same_set, &candidates); |
| 4749 | } |
| 4750 | /* |
| 4751 | * now we have a set of devices, with all of them having |
| 4752 | * mostly sane superblocks. It's time to allocate the |
| 4753 | * mddev. |
| 4754 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4755 | if (part) { |
| 4756 | dev = MKDEV(mdp_major, |
| 4757 | rdev0->preferred_minor << MdpMinorShift); |
| 4758 | unit = MINOR(dev) >> MdpMinorShift; |
| 4759 | } else { |
| 4760 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4761 | unit = MINOR(dev); |
| 4762 | } |
| 4763 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4765 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4766 | break; |
| 4767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | |
| 4769 | md_probe(dev, NULL, NULL); |
| 4770 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4771 | if (!mddev || !mddev->gendisk) { |
| 4772 | if (mddev) |
| 4773 | mddev_put(mddev); |
| 4774 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | "md: cannot allocate memory for md drive.\n"); |
| 4776 | break; |
| 4777 | } |
| 4778 | if (mddev_lock(mddev)) |
| 4779 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4780 | mdname(mddev)); |
| 4781 | else if (mddev->raid_disks || mddev->major_version |
| 4782 | || !list_empty(&mddev->disks)) { |
| 4783 | printk(KERN_WARNING |
| 4784 | "md: %s already running, cannot run %s\n", |
| 4785 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4786 | mddev_unlock(mddev); |
| 4787 | } else { |
| 4788 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4789 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4790 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | list_del_init(&rdev->same_set); |
| 4792 | if (bind_rdev_to_array(rdev, mddev)) |
| 4793 | export_rdev(rdev); |
| 4794 | } |
| 4795 | autorun_array(mddev); |
| 4796 | mddev_unlock(mddev); |
| 4797 | } |
| 4798 | /* on success, candidates will be empty, on error |
| 4799 | * it won't... |
| 4800 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4801 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4802 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | mddev_put(mddev); |
| 4806 | } |
| 4807 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4808 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4809 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | static int get_version(void __user * arg) |
| 4812 | { |
| 4813 | mdu_version_t ver; |
| 4814 | |
| 4815 | ver.major = MD_MAJOR_VERSION; |
| 4816 | ver.minor = MD_MINOR_VERSION; |
| 4817 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4818 | |
| 4819 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4820 | return -EFAULT; |
| 4821 | |
| 4822 | return 0; |
| 4823 | } |
| 4824 | |
| 4825 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4826 | { |
| 4827 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4828 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4831 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4832 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4834 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | failed++; |
| 4836 | else { |
| 4837 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4838 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4839 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | else |
| 4841 | spare++; |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | info.major_version = mddev->major_version; |
| 4846 | info.minor_version = mddev->minor_version; |
| 4847 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4848 | info.ctime = mddev->ctime; |
| 4849 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4850 | info.size = mddev->dev_sectors / 2; |
| 4851 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4852 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | info.nr_disks = nr; |
| 4854 | info.raid_disks = mddev->raid_disks; |
| 4855 | info.md_minor = mddev->md_minor; |
| 4856 | info.not_persistent= !mddev->persistent; |
| 4857 | |
| 4858 | info.utime = mddev->utime; |
| 4859 | info.state = 0; |
| 4860 | if (mddev->in_sync) |
| 4861 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4862 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4863 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4864 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | info.working_disks = working; |
| 4866 | info.failed_disks = failed; |
| 4867 | info.spare_disks = spare; |
| 4868 | |
| 4869 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4870 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
| 4872 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4873 | return -EFAULT; |
| 4874 | |
| 4875 | return 0; |
| 4876 | } |
| 4877 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4878 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4879 | { |
| 4880 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4881 | char *ptr, *buf = NULL; |
| 4882 | int err = -ENOMEM; |
| 4883 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4884 | if (md_allow_write(mddev)) |
| 4885 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4886 | else |
| 4887 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4888 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4889 | if (!file) |
| 4890 | goto out; |
| 4891 | |
| 4892 | /* bitmap disabled, zero the first byte and copy out */ |
| 4893 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4894 | file->pathname[0] = '\0'; |
| 4895 | goto copy_out; |
| 4896 | } |
| 4897 | |
| 4898 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4899 | if (!buf) |
| 4900 | goto out; |
| 4901 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4902 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4903 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4904 | goto out; |
| 4905 | |
| 4906 | strcpy(file->pathname, ptr); |
| 4907 | |
| 4908 | copy_out: |
| 4909 | err = 0; |
| 4910 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4911 | err = -EFAULT; |
| 4912 | out: |
| 4913 | kfree(buf); |
| 4914 | kfree(file); |
| 4915 | return err; |
| 4916 | } |
| 4917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4919 | { |
| 4920 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | mdk_rdev_t *rdev; |
| 4922 | |
| 4923 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4924 | return -EFAULT; |
| 4925 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4926 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | if (rdev) { |
| 4928 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4929 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4930 | info.raid_disk = rdev->raid_disk; |
| 4931 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4932 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4934 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4936 | info.state |= (1<<MD_DISK_SYNC); |
| 4937 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4938 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4939 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | } else { |
| 4941 | info.major = info.minor = 0; |
| 4942 | info.raid_disk = -1; |
| 4943 | info.state = (1<<MD_DISK_REMOVED); |
| 4944 | } |
| 4945 | |
| 4946 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4947 | return -EFAULT; |
| 4948 | |
| 4949 | return 0; |
| 4950 | } |
| 4951 | |
| 4952 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4953 | { |
| 4954 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4955 | mdk_rdev_t *rdev; |
| 4956 | dev_t dev = MKDEV(info->major,info->minor); |
| 4957 | |
| 4958 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4959 | return -EOVERFLOW; |
| 4960 | |
| 4961 | if (!mddev->raid_disks) { |
| 4962 | int err; |
| 4963 | /* expecting a device which has a superblock */ |
| 4964 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4965 | if (IS_ERR(rdev)) { |
| 4966 | printk(KERN_WARNING |
| 4967 | "md: md_import_device returned %ld\n", |
| 4968 | PTR_ERR(rdev)); |
| 4969 | return PTR_ERR(rdev); |
| 4970 | } |
| 4971 | if (!list_empty(&mddev->disks)) { |
| 4972 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4973 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4974 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4976 | if (err < 0) { |
| 4977 | printk(KERN_WARNING |
| 4978 | "md: %s has different UUID to %s\n", |
| 4979 | bdevname(rdev->bdev,b), |
| 4980 | bdevname(rdev0->bdev,b2)); |
| 4981 | export_rdev(rdev); |
| 4982 | return -EINVAL; |
| 4983 | } |
| 4984 | } |
| 4985 | err = bind_rdev_to_array(rdev, mddev); |
| 4986 | if (err) |
| 4987 | export_rdev(rdev); |
| 4988 | return err; |
| 4989 | } |
| 4990 | |
| 4991 | /* |
| 4992 | * add_new_disk can be used once the array is assembled |
| 4993 | * to add "hot spares". They must already have a superblock |
| 4994 | * written |
| 4995 | */ |
| 4996 | if (mddev->pers) { |
| 4997 | int err; |
| 4998 | if (!mddev->pers->hot_add_disk) { |
| 4999 | printk(KERN_WARNING |
| 5000 | "%s: personality does not support diskops!\n", |
| 5001 | mdname(mddev)); |
| 5002 | return -EINVAL; |
| 5003 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5004 | if (mddev->persistent) |
| 5005 | rdev = md_import_device(dev, mddev->major_version, |
| 5006 | mddev->minor_version); |
| 5007 | else |
| 5008 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | if (IS_ERR(rdev)) { |
| 5010 | printk(KERN_WARNING |
| 5011 | "md: md_import_device returned %ld\n", |
| 5012 | PTR_ERR(rdev)); |
| 5013 | return PTR_ERR(rdev); |
| 5014 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5015 | /* set save_raid_disk if appropriate */ |
| 5016 | if (!mddev->persistent) { |
| 5017 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5018 | info->raid_disk < mddev->raid_disks) |
| 5019 | rdev->raid_disk = info->raid_disk; |
| 5020 | else |
| 5021 | rdev->raid_disk = -1; |
| 5022 | } else |
| 5023 | super_types[mddev->major_version]. |
| 5024 | validate_super(mddev, rdev); |
| 5025 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5026 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5027 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5028 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5029 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5030 | else |
| 5031 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | rdev->raid_disk = -1; |
| 5034 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5035 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5036 | /* If there is hot_add_disk but no hot_remove_disk |
| 5037 | * then added disks for geometry changes, |
| 5038 | * and should be added immediately. |
| 5039 | */ |
| 5040 | super_types[mddev->major_version]. |
| 5041 | validate_super(mddev, rdev); |
| 5042 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5043 | if (err) |
| 5044 | unbind_rdev_from_array(rdev); |
| 5045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | if (err) |
| 5047 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5048 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5049 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5050 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5051 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5052 | if (mddev->degraded) |
| 5053 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5054 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5055 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | return err; |
| 5057 | } |
| 5058 | |
| 5059 | /* otherwise, add_new_disk is only allowed |
| 5060 | * for major_version==0 superblocks |
| 5061 | */ |
| 5062 | if (mddev->major_version != 0) { |
| 5063 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5064 | mdname(mddev)); |
| 5065 | return -EINVAL; |
| 5066 | } |
| 5067 | |
| 5068 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5069 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5070 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | if (IS_ERR(rdev)) { |
| 5072 | printk(KERN_WARNING |
| 5073 | "md: error, md_import_device() returned %ld\n", |
| 5074 | PTR_ERR(rdev)); |
| 5075 | return PTR_ERR(rdev); |
| 5076 | } |
| 5077 | rdev->desc_nr = info->number; |
| 5078 | if (info->raid_disk < mddev->raid_disks) |
| 5079 | rdev->raid_disk = info->raid_disk; |
| 5080 | else |
| 5081 | rdev->raid_disk = -1; |
| 5082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5084 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5085 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5087 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5088 | set_bit(WriteMostly, &rdev->flags); |
| 5089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | if (!mddev->persistent) { |
| 5091 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5092 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5094 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5095 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5097 | err = bind_rdev_to_array(rdev, mddev); |
| 5098 | if (err) { |
| 5099 | export_rdev(rdev); |
| 5100 | return err; |
| 5101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | return 0; |
| 5105 | } |
| 5106 | |
| 5107 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5108 | { |
| 5109 | char b[BDEVNAME_SIZE]; |
| 5110 | mdk_rdev_t *rdev; |
| 5111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | rdev = find_rdev(mddev, dev); |
| 5113 | if (!rdev) |
| 5114 | return -ENXIO; |
| 5115 | |
| 5116 | if (rdev->raid_disk >= 0) |
| 5117 | goto busy; |
| 5118 | |
| 5119 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5120 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5121 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | |
| 5123 | return 0; |
| 5124 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5125 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5127 | return -EBUSY; |
| 5128 | } |
| 5129 | |
| 5130 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5131 | { |
| 5132 | char b[BDEVNAME_SIZE]; |
| 5133 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | mdk_rdev_t *rdev; |
| 5135 | |
| 5136 | if (!mddev->pers) |
| 5137 | return -ENODEV; |
| 5138 | |
| 5139 | if (mddev->major_version != 0) { |
| 5140 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5141 | " version-0 superblocks.\n", |
| 5142 | mdname(mddev)); |
| 5143 | return -EINVAL; |
| 5144 | } |
| 5145 | if (!mddev->pers->hot_add_disk) { |
| 5146 | printk(KERN_WARNING |
| 5147 | "%s: personality does not support diskops!\n", |
| 5148 | mdname(mddev)); |
| 5149 | return -EINVAL; |
| 5150 | } |
| 5151 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5152 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | if (IS_ERR(rdev)) { |
| 5154 | printk(KERN_WARNING |
| 5155 | "md: error, md_import_device() returned %ld\n", |
| 5156 | PTR_ERR(rdev)); |
| 5157 | return -EINVAL; |
| 5158 | } |
| 5159 | |
| 5160 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5161 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5163 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5165 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5167 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | printk(KERN_WARNING |
| 5169 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5170 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5171 | err = -EINVAL; |
| 5172 | goto abort_export; |
| 5173 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5174 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5176 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5177 | err = bind_rdev_to_array(rdev, mddev); |
| 5178 | if (err) |
| 5179 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
| 5181 | /* |
| 5182 | * The rest should better be atomic, we can have disk failures |
| 5183 | * noticed in interrupt contexts ... |
| 5184 | */ |
| 5185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | rdev->raid_disk = -1; |
| 5187 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5188 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | |
| 5190 | /* |
| 5191 | * Kick recovery, maybe this spare has to be added to the |
| 5192 | * array immediately. |
| 5193 | */ |
| 5194 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5195 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5196 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | return 0; |
| 5198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | abort_export: |
| 5200 | export_rdev(rdev); |
| 5201 | return err; |
| 5202 | } |
| 5203 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5204 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5205 | { |
| 5206 | int err; |
| 5207 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5208 | if (mddev->pers) { |
| 5209 | if (!mddev->pers->quiesce) |
| 5210 | return -EBUSY; |
| 5211 | if (mddev->recovery || mddev->sync_thread) |
| 5212 | return -EBUSY; |
| 5213 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5214 | } |
| 5215 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5216 | |
| 5217 | if (fd >= 0) { |
| 5218 | if (mddev->bitmap) |
| 5219 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5220 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5221 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5222 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5223 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5224 | mdname(mddev)); |
| 5225 | return -EBADF; |
| 5226 | } |
| 5227 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5228 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5229 | if (err) { |
| 5230 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5231 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5232 | fput(mddev->bitmap_info.file); |
| 5233 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5234 | return err; |
| 5235 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5236 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5237 | } else if (mddev->bitmap == NULL) |
| 5238 | return -ENOENT; /* cannot remove what isn't there */ |
| 5239 | err = 0; |
| 5240 | if (mddev->pers) { |
| 5241 | mddev->pers->quiesce(mddev, 1); |
| 5242 | if (fd >= 0) |
| 5243 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5244 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5245 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5246 | fd = -1; /* make sure to put the file */ |
| 5247 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5248 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5249 | } |
| 5250 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5251 | if (mddev->bitmap_info.file) { |
| 5252 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5253 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5254 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5255 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5258 | return err; |
| 5259 | } |
| 5260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | /* |
| 5262 | * set_array_info is used two different ways |
| 5263 | * The original usage is when creating a new array. |
| 5264 | * In this usage, raid_disks is > 0 and it together with |
| 5265 | * level, size, not_persistent,layout,chunksize determine the |
| 5266 | * shape of the array. |
| 5267 | * This will always create an array with a type-0.90.0 superblock. |
| 5268 | * The newer usage is when assembling an array. |
| 5269 | * In this case raid_disks will be 0, and the major_version field is |
| 5270 | * use to determine which style super-blocks are to be found on the devices. |
| 5271 | * The minor and patch _version numbers are also kept incase the |
| 5272 | * super_block handler wishes to interpret them. |
| 5273 | */ |
| 5274 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5275 | { |
| 5276 | |
| 5277 | if (info->raid_disks == 0) { |
| 5278 | /* just setting version number for superblock loading */ |
| 5279 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5280 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | super_types[info->major_version].name == NULL) { |
| 5282 | /* maybe try to auto-load a module? */ |
| 5283 | printk(KERN_INFO |
| 5284 | "md: superblock version %d not known\n", |
| 5285 | info->major_version); |
| 5286 | return -EINVAL; |
| 5287 | } |
| 5288 | mddev->major_version = info->major_version; |
| 5289 | mddev->minor_version = info->minor_version; |
| 5290 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5291 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5292 | /* ensure mddev_put doesn't delete this now that there |
| 5293 | * is some minimal configuration. |
| 5294 | */ |
| 5295 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | return 0; |
| 5297 | } |
| 5298 | mddev->major_version = MD_MAJOR_VERSION; |
| 5299 | mddev->minor_version = MD_MINOR_VERSION; |
| 5300 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5301 | mddev->ctime = get_seconds(); |
| 5302 | |
| 5303 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5304 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5305 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | mddev->raid_disks = info->raid_disks; |
| 5307 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5308 | * openned |
| 5309 | */ |
| 5310 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5311 | mddev->recovery_cp = MaxSector; |
| 5312 | else |
| 5313 | mddev->recovery_cp = 0; |
| 5314 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5315 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | |
| 5317 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5318 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | |
| 5320 | mddev->max_disks = MD_SB_DISKS; |
| 5321 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5322 | if (mddev->persistent) |
| 5323 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5324 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5326 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5327 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5328 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5329 | mddev->reshape_position = MaxSector; |
| 5330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | /* |
| 5332 | * Generate a 128 bit UUID |
| 5333 | */ |
| 5334 | get_random_bytes(mddev->uuid, 16); |
| 5335 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5336 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5337 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5338 | mddev->new_layout = mddev->layout; |
| 5339 | mddev->delta_disks = 0; |
| 5340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | return 0; |
| 5342 | } |
| 5343 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5344 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5345 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5346 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5347 | |
| 5348 | if (mddev->external_size) |
| 5349 | return; |
| 5350 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5351 | mddev->array_sectors = array_sectors; |
| 5352 | } |
| 5353 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5354 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5355 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5356 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5357 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5358 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5359 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5360 | |
| 5361 | if (mddev->pers->resize == NULL) |
| 5362 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5363 | /* The "num_sectors" is the number of sectors of each device that |
| 5364 | * is used. This can only make sense for arrays with redundancy. |
| 5365 | * linear and raid0 always use whatever space is available. We can only |
| 5366 | * consider changing this number if no resync or reconstruction is |
| 5367 | * 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] | 5368 | * 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] | 5369 | * of each device. If num_sectors is zero, we find the largest size |
| 5370 | * that fits. |
| 5371 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5372 | */ |
| 5373 | if (mddev->sync_thread) |
| 5374 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5375 | if (mddev->bitmap) |
| 5376 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5377 | * grow, and re-add. |
| 5378 | */ |
| 5379 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5380 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5381 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5382 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5383 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5384 | num_sectors = avail; |
| 5385 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5386 | return -ENOSPC; |
| 5387 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5388 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5389 | if (!rv) |
| 5390 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5391 | return rv; |
| 5392 | } |
| 5393 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5394 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5395 | { |
| 5396 | int rv; |
| 5397 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5398 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5399 | return -EINVAL; |
| 5400 | if (raid_disks <= 0 || |
| 5401 | raid_disks >= mddev->max_disks) |
| 5402 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5403 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5404 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5405 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5406 | |
| 5407 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5408 | return rv; |
| 5409 | } |
| 5410 | |
| 5411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | /* |
| 5413 | * update_array_info is used to change the configuration of an |
| 5414 | * on-line array. |
| 5415 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5416 | * fields in the info are checked against the array. |
| 5417 | * Any differences that cannot be handled will cause an error. |
| 5418 | * Normally, only one change can be managed at a time. |
| 5419 | */ |
| 5420 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5421 | { |
| 5422 | int rv = 0; |
| 5423 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5424 | int state = 0; |
| 5425 | |
| 5426 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5427 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5428 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | |
| 5430 | if (mddev->major_version != info->major_version || |
| 5431 | mddev->minor_version != info->minor_version || |
| 5432 | /* mddev->patch_version != info->patch_version || */ |
| 5433 | mddev->ctime != info->ctime || |
| 5434 | mddev->level != info->level || |
| 5435 | /* mddev->layout != info->layout || */ |
| 5436 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5437 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5438 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5439 | ((state^info->state) & 0xfffffe00) |
| 5440 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | return -EINVAL; |
| 5442 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5443 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5444 | cnt++; |
| 5445 | if (mddev->raid_disks != info->raid_disks) |
| 5446 | cnt++; |
| 5447 | if (mddev->layout != info->layout) |
| 5448 | cnt++; |
| 5449 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5450 | cnt++; |
| 5451 | if (cnt == 0) |
| 5452 | return 0; |
| 5453 | if (cnt > 1) |
| 5454 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | |
| 5456 | if (mddev->layout != info->layout) { |
| 5457 | /* Change layout |
| 5458 | * we don't need to do anything at the md level, the |
| 5459 | * personality will take care of it all. |
| 5460 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5461 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5463 | else { |
| 5464 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5465 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5466 | if (rv) |
| 5467 | mddev->new_layout = mddev->layout; |
| 5468 | return rv; |
| 5469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5471 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5472 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5474 | if (mddev->raid_disks != info->raid_disks) |
| 5475 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5477 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5478 | if (mddev->pers->quiesce == NULL) |
| 5479 | return -EINVAL; |
| 5480 | if (mddev->recovery || mddev->sync_thread) |
| 5481 | return -EBUSY; |
| 5482 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5483 | /* add the bitmap */ |
| 5484 | if (mddev->bitmap) |
| 5485 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5486 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5487 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5488 | mddev->bitmap_info.offset = |
| 5489 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5490 | mddev->pers->quiesce(mddev, 1); |
| 5491 | rv = bitmap_create(mddev); |
| 5492 | if (rv) |
| 5493 | bitmap_destroy(mddev); |
| 5494 | mddev->pers->quiesce(mddev, 0); |
| 5495 | } else { |
| 5496 | /* remove the bitmap */ |
| 5497 | if (!mddev->bitmap) |
| 5498 | return -ENOENT; |
| 5499 | if (mddev->bitmap->file) |
| 5500 | return -EINVAL; |
| 5501 | mddev->pers->quiesce(mddev, 1); |
| 5502 | bitmap_destroy(mddev); |
| 5503 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5504 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5505 | } |
| 5506 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5507 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | return rv; |
| 5509 | } |
| 5510 | |
| 5511 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5512 | { |
| 5513 | mdk_rdev_t *rdev; |
| 5514 | |
| 5515 | if (mddev->pers == NULL) |
| 5516 | return -ENODEV; |
| 5517 | |
| 5518 | rdev = find_rdev(mddev, dev); |
| 5519 | if (!rdev) |
| 5520 | return -ENODEV; |
| 5521 | |
| 5522 | md_error(mddev, rdev); |
| 5523 | return 0; |
| 5524 | } |
| 5525 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5526 | /* |
| 5527 | * We have a problem here : there is no easy way to give a CHS |
| 5528 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5529 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5530 | * dosfs just mad... ;-) |
| 5531 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5532 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5533 | { |
| 5534 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5535 | |
| 5536 | geo->heads = 2; |
| 5537 | geo->sectors = 4; |
| 5538 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5539 | return 0; |
| 5540 | } |
| 5541 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5542 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | unsigned int cmd, unsigned long arg) |
| 5544 | { |
| 5545 | int err = 0; |
| 5546 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5548 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | |
| 5550 | if (!capable(CAP_SYS_ADMIN)) |
| 5551 | return -EACCES; |
| 5552 | |
| 5553 | /* |
| 5554 | * Commands dealing with the RAID driver but not any |
| 5555 | * particular array: |
| 5556 | */ |
| 5557 | switch (cmd) |
| 5558 | { |
| 5559 | case RAID_VERSION: |
| 5560 | err = get_version(argp); |
| 5561 | goto done; |
| 5562 | |
| 5563 | case PRINT_RAID_DEBUG: |
| 5564 | err = 0; |
| 5565 | md_print_devices(); |
| 5566 | goto done; |
| 5567 | |
| 5568 | #ifndef MODULE |
| 5569 | case RAID_AUTORUN: |
| 5570 | err = 0; |
| 5571 | autostart_arrays(arg); |
| 5572 | goto done; |
| 5573 | #endif |
| 5574 | default:; |
| 5575 | } |
| 5576 | |
| 5577 | /* |
| 5578 | * Commands creating/starting a new array: |
| 5579 | */ |
| 5580 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5581 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | |
| 5583 | if (!mddev) { |
| 5584 | BUG(); |
| 5585 | goto abort; |
| 5586 | } |
| 5587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | err = mddev_lock(mddev); |
| 5589 | if (err) { |
| 5590 | printk(KERN_INFO |
| 5591 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5592 | err, cmd); |
| 5593 | goto abort; |
| 5594 | } |
| 5595 | |
| 5596 | switch (cmd) |
| 5597 | { |
| 5598 | case SET_ARRAY_INFO: |
| 5599 | { |
| 5600 | mdu_array_info_t info; |
| 5601 | if (!arg) |
| 5602 | memset(&info, 0, sizeof(info)); |
| 5603 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5604 | err = -EFAULT; |
| 5605 | goto abort_unlock; |
| 5606 | } |
| 5607 | if (mddev->pers) { |
| 5608 | err = update_array_info(mddev, &info); |
| 5609 | if (err) { |
| 5610 | printk(KERN_WARNING "md: couldn't update" |
| 5611 | " array info. %d\n", err); |
| 5612 | goto abort_unlock; |
| 5613 | } |
| 5614 | goto done_unlock; |
| 5615 | } |
| 5616 | if (!list_empty(&mddev->disks)) { |
| 5617 | printk(KERN_WARNING |
| 5618 | "md: array %s already has disks!\n", |
| 5619 | mdname(mddev)); |
| 5620 | err = -EBUSY; |
| 5621 | goto abort_unlock; |
| 5622 | } |
| 5623 | if (mddev->raid_disks) { |
| 5624 | printk(KERN_WARNING |
| 5625 | "md: array %s already initialised!\n", |
| 5626 | mdname(mddev)); |
| 5627 | err = -EBUSY; |
| 5628 | goto abort_unlock; |
| 5629 | } |
| 5630 | err = set_array_info(mddev, &info); |
| 5631 | if (err) { |
| 5632 | printk(KERN_WARNING "md: couldn't set" |
| 5633 | " array info. %d\n", err); |
| 5634 | goto abort_unlock; |
| 5635 | } |
| 5636 | } |
| 5637 | goto done_unlock; |
| 5638 | |
| 5639 | default:; |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * Commands querying/configuring an existing array: |
| 5644 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5645 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5646 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5647 | if ((!mddev->raid_disks && !mddev->external) |
| 5648 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5649 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5650 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | err = -ENODEV; |
| 5652 | goto abort_unlock; |
| 5653 | } |
| 5654 | |
| 5655 | /* |
| 5656 | * Commands even a read-only array can execute: |
| 5657 | */ |
| 5658 | switch (cmd) |
| 5659 | { |
| 5660 | case GET_ARRAY_INFO: |
| 5661 | err = get_array_info(mddev, argp); |
| 5662 | goto done_unlock; |
| 5663 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5664 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5665 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5666 | goto done_unlock; |
| 5667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | case GET_DISK_INFO: |
| 5669 | err = get_disk_info(mddev, argp); |
| 5670 | goto done_unlock; |
| 5671 | |
| 5672 | case RESTART_ARRAY_RW: |
| 5673 | err = restart_array(mddev); |
| 5674 | goto done_unlock; |
| 5675 | |
| 5676 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5677 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | goto done_unlock; |
| 5679 | |
| 5680 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5681 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | goto done_unlock; |
| 5683 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5684 | case BLKROSET: |
| 5685 | if (get_user(ro, (int __user *)(arg))) { |
| 5686 | err = -EFAULT; |
| 5687 | goto done_unlock; |
| 5688 | } |
| 5689 | err = -EINVAL; |
| 5690 | |
| 5691 | /* if the bdev is going readonly the value of mddev->ro |
| 5692 | * does not matter, no writes are coming |
| 5693 | */ |
| 5694 | if (ro) |
| 5695 | goto done_unlock; |
| 5696 | |
| 5697 | /* are we are already prepared for writes? */ |
| 5698 | if (mddev->ro != 1) |
| 5699 | goto done_unlock; |
| 5700 | |
| 5701 | /* transitioning to readauto need only happen for |
| 5702 | * arrays that call md_write_start |
| 5703 | */ |
| 5704 | if (mddev->pers) { |
| 5705 | err = restart_array(mddev); |
| 5706 | if (err == 0) { |
| 5707 | mddev->ro = 2; |
| 5708 | set_disk_ro(mddev->gendisk, 0); |
| 5709 | } |
| 5710 | } |
| 5711 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | /* |
| 5715 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5716 | * superblock, so we do not allow them on read-only arrays. |
| 5717 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5718 | * here and hit the 'default' below, so only disallow |
| 5719 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5721 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5722 | if (mddev->ro == 2) { |
| 5723 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5724 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5725 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5726 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5727 | } else { |
| 5728 | err = -EROFS; |
| 5729 | goto abort_unlock; |
| 5730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | } |
| 5732 | |
| 5733 | switch (cmd) |
| 5734 | { |
| 5735 | case ADD_NEW_DISK: |
| 5736 | { |
| 5737 | mdu_disk_info_t info; |
| 5738 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5739 | err = -EFAULT; |
| 5740 | else |
| 5741 | err = add_new_disk(mddev, &info); |
| 5742 | goto done_unlock; |
| 5743 | } |
| 5744 | |
| 5745 | case HOT_REMOVE_DISK: |
| 5746 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5747 | goto done_unlock; |
| 5748 | |
| 5749 | case HOT_ADD_DISK: |
| 5750 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5751 | goto done_unlock; |
| 5752 | |
| 5753 | case SET_DISK_FAULTY: |
| 5754 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5755 | goto done_unlock; |
| 5756 | |
| 5757 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5758 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | goto done_unlock; |
| 5760 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5761 | case SET_BITMAP_FILE: |
| 5762 | err = set_bitmap_file(mddev, (int)arg); |
| 5763 | goto done_unlock; |
| 5764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | err = -EINVAL; |
| 5767 | goto abort_unlock; |
| 5768 | } |
| 5769 | |
| 5770 | done_unlock: |
| 5771 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5772 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5773 | err != -EINVAL) |
| 5774 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | mddev_unlock(mddev); |
| 5776 | |
| 5777 | return err; |
| 5778 | done: |
| 5779 | if (err) |
| 5780 | MD_BUG(); |
| 5781 | abort: |
| 5782 | return err; |
| 5783 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5784 | #ifdef CONFIG_COMPAT |
| 5785 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5786 | unsigned int cmd, unsigned long arg) |
| 5787 | { |
| 5788 | switch (cmd) { |
| 5789 | case HOT_REMOVE_DISK: |
| 5790 | case HOT_ADD_DISK: |
| 5791 | case SET_DISK_FAULTY: |
| 5792 | case SET_BITMAP_FILE: |
| 5793 | /* These take in integer arg, do not convert */ |
| 5794 | break; |
| 5795 | default: |
| 5796 | arg = (unsigned long)compat_ptr(arg); |
| 5797 | break; |
| 5798 | } |
| 5799 | |
| 5800 | return md_ioctl(bdev, mode, cmd, arg); |
| 5801 | } |
| 5802 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5804 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | { |
| 5806 | /* |
| 5807 | * Succeed if we can lock the mddev, which confirms that |
| 5808 | * it isn't being stopped right now. |
| 5809 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5810 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | int err; |
| 5812 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5813 | if (mddev->gendisk != bdev->bd_disk) { |
| 5814 | /* we are racing with mddev_put which is discarding this |
| 5815 | * bd_disk. |
| 5816 | */ |
| 5817 | mddev_put(mddev); |
| 5818 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5819 | flush_scheduled_work(); |
| 5820 | /* Then retry the open from the top */ |
| 5821 | return -ERESTARTSYS; |
| 5822 | } |
| 5823 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5824 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5825 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | goto out; |
| 5827 | |
| 5828 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5829 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5830 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5832 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | out: |
| 5834 | return err; |
| 5835 | } |
| 5836 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5837 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5839 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5841 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5842 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | mddev_put(mddev); |
| 5844 | |
| 5845 | return 0; |
| 5846 | } |
| 5847 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5848 | static int md_media_changed(struct gendisk *disk) |
| 5849 | { |
| 5850 | mddev_t *mddev = disk->private_data; |
| 5851 | |
| 5852 | return mddev->changed; |
| 5853 | } |
| 5854 | |
| 5855 | static int md_revalidate(struct gendisk *disk) |
| 5856 | { |
| 5857 | mddev_t *mddev = disk->private_data; |
| 5858 | |
| 5859 | mddev->changed = 0; |
| 5860 | return 0; |
| 5861 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5862 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | { |
| 5864 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5865 | .open = md_open, |
| 5866 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5867 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5868 | #ifdef CONFIG_COMPAT |
| 5869 | .compat_ioctl = md_compat_ioctl, |
| 5870 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5871 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5872 | .media_changed = md_media_changed, |
| 5873 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | }; |
| 5875 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5876 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | { |
| 5878 | mdk_thread_t *thread = arg; |
| 5879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | /* |
| 5881 | * md_thread is a 'system-thread', it's priority should be very |
| 5882 | * high. We avoid resource deadlocks individually in each |
| 5883 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5884 | * the very same RT priority as kswapd, thus we will never get |
| 5885 | * into a priority inversion deadlock. |
| 5886 | * |
| 5887 | * we definitely have to have equal or higher priority than |
| 5888 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5889 | * many dirty RAID5 blocks. |
| 5890 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5892 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5893 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5895 | /* We need to wait INTERRUPTIBLE so that |
| 5896 | * we don't add to the load-average. |
| 5897 | * That means we need to be sure no signals are |
| 5898 | * pending |
| 5899 | */ |
| 5900 | if (signal_pending(current)) |
| 5901 | flush_signals(current); |
| 5902 | |
| 5903 | wait_event_interruptible_timeout |
| 5904 | (thread->wqueue, |
| 5905 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5906 | || kthread_should_stop(), |
| 5907 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | |
| 5909 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5910 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5911 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | return 0; |
| 5915 | } |
| 5916 | |
| 5917 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5918 | { |
| 5919 | if (thread) { |
| 5920 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5921 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5922 | wake_up(&thread->wqueue); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5927 | const char *name) |
| 5928 | { |
| 5929 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5931 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | if (!thread) |
| 5933 | return NULL; |
| 5934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | init_waitqueue_head(&thread->wqueue); |
| 5936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | thread->run = run; |
| 5938 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5939 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5940 | thread->tsk = kthread_run(md_thread, thread, |
| 5941 | "%s_%s", |
| 5942 | mdname(thread->mddev), |
| 5943 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5944 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | kfree(thread); |
| 5946 | return NULL; |
| 5947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | return thread; |
| 5949 | } |
| 5950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | void md_unregister_thread(mdk_thread_t *thread) |
| 5952 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5953 | if (!thread) |
| 5954 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5955 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5956 | |
| 5957 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | kfree(thread); |
| 5959 | } |
| 5960 | |
| 5961 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5962 | { |
| 5963 | if (!mddev) { |
| 5964 | MD_BUG(); |
| 5965 | return; |
| 5966 | } |
| 5967 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5968 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5970 | |
| 5971 | if (mddev->external) |
| 5972 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5973 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5975 | mdname(mddev), |
| 5976 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5977 | __builtin_return_address(0),__builtin_return_address(1), |
| 5978 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5979 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5980 | if (!mddev->pers) |
| 5981 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | if (!mddev->pers->error_handler) |
| 5983 | return; |
| 5984 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5985 | if (mddev->degraded) |
| 5986 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 5987 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5989 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5990 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5991 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | /* seq_file implementation /proc/mdstat */ |
| 5995 | |
| 5996 | static void status_unused(struct seq_file *seq) |
| 5997 | { |
| 5998 | int i = 0; |
| 5999 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | |
| 6001 | seq_printf(seq, "unused devices: "); |
| 6002 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6003 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | char b[BDEVNAME_SIZE]; |
| 6005 | i++; |
| 6006 | seq_printf(seq, "%s ", |
| 6007 | bdevname(rdev->bdev,b)); |
| 6008 | } |
| 6009 | if (!i) |
| 6010 | seq_printf(seq, "<none>"); |
| 6011 | |
| 6012 | seq_printf(seq, "\n"); |
| 6013 | } |
| 6014 | |
| 6015 | |
| 6016 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6017 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6018 | sector_t max_sectors, resync, res; |
| 6019 | unsigned long dt, db; |
| 6020 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6021 | int scale; |
| 6022 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6024 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | |
| 6026 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6027 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6029 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | |
| 6031 | /* |
| 6032 | * Should not happen. |
| 6033 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6034 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | MD_BUG(); |
| 6036 | return; |
| 6037 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6038 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6039 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6040 | * u32, as those are the requirements for sector_div. |
| 6041 | * Thus 'scale' must be at least 10 |
| 6042 | */ |
| 6043 | scale = 10; |
| 6044 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6045 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6046 | scale++; |
| 6047 | } |
| 6048 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6049 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6050 | |
| 6051 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6053 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | seq_printf(seq, "["); |
| 6055 | for (i = 0; i < x; i++) |
| 6056 | seq_printf(seq, "="); |
| 6057 | seq_printf(seq, ">"); |
| 6058 | for (i = 0; i < y; i++) |
| 6059 | seq_printf(seq, "."); |
| 6060 | seq_printf(seq, "] "); |
| 6061 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6062 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6063 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6064 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6065 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6066 | "check" : |
| 6067 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6068 | "resync" : "recovery"))), |
| 6069 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6070 | (unsigned long long) resync/2, |
| 6071 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | |
| 6073 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | * dt: time from mark until now |
| 6075 | * db: blocks written from mark until now |
| 6076 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6077 | * |
| 6078 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6079 | * So we divide before multiply in case it is 32bit and close |
| 6080 | * to the limit. |
| 6081 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6082 | * near the end of resync when the number of remaining sectors |
| 6083 | * is close to 'db'. |
| 6084 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6085 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | */ |
| 6087 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6088 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6089 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6090 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6092 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6093 | sector_div(rt, db/32+1); |
| 6094 | rt *= dt; |
| 6095 | rt >>= 5; |
| 6096 | |
| 6097 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6098 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6100 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6104 | { |
| 6105 | struct list_head *tmp; |
| 6106 | loff_t l = *pos; |
| 6107 | mddev_t *mddev; |
| 6108 | |
| 6109 | if (l >= 0x10000) |
| 6110 | return NULL; |
| 6111 | if (!l--) |
| 6112 | /* header */ |
| 6113 | return (void*)1; |
| 6114 | |
| 6115 | spin_lock(&all_mddevs_lock); |
| 6116 | list_for_each(tmp,&all_mddevs) |
| 6117 | if (!l--) { |
| 6118 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6119 | mddev_get(mddev); |
| 6120 | spin_unlock(&all_mddevs_lock); |
| 6121 | return mddev; |
| 6122 | } |
| 6123 | spin_unlock(&all_mddevs_lock); |
| 6124 | if (!l--) |
| 6125 | return (void*)2;/* tail */ |
| 6126 | return NULL; |
| 6127 | } |
| 6128 | |
| 6129 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6130 | { |
| 6131 | struct list_head *tmp; |
| 6132 | mddev_t *next_mddev, *mddev = v; |
| 6133 | |
| 6134 | ++*pos; |
| 6135 | if (v == (void*)2) |
| 6136 | return NULL; |
| 6137 | |
| 6138 | spin_lock(&all_mddevs_lock); |
| 6139 | if (v == (void*)1) |
| 6140 | tmp = all_mddevs.next; |
| 6141 | else |
| 6142 | tmp = mddev->all_mddevs.next; |
| 6143 | if (tmp != &all_mddevs) |
| 6144 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6145 | else { |
| 6146 | next_mddev = (void*)2; |
| 6147 | *pos = 0x10000; |
| 6148 | } |
| 6149 | spin_unlock(&all_mddevs_lock); |
| 6150 | |
| 6151 | if (v != (void*)1) |
| 6152 | mddev_put(mddev); |
| 6153 | return next_mddev; |
| 6154 | |
| 6155 | } |
| 6156 | |
| 6157 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6158 | { |
| 6159 | mddev_t *mddev = v; |
| 6160 | |
| 6161 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6162 | mddev_put(mddev); |
| 6163 | } |
| 6164 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6165 | struct mdstat_info { |
| 6166 | int event; |
| 6167 | }; |
| 6168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6170 | { |
| 6171 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6172 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6174 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6175 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | |
| 6177 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6178 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | seq_printf(seq, "Personalities : "); |
| 6180 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6181 | list_for_each_entry(pers, &pers_list, list) |
| 6182 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | |
| 6184 | spin_unlock(&pers_lock); |
| 6185 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6186 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | return 0; |
| 6188 | } |
| 6189 | if (v == (void*)2) { |
| 6190 | status_unused(seq); |
| 6191 | return 0; |
| 6192 | } |
| 6193 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6194 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6198 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6199 | mddev->pers ? "" : "in"); |
| 6200 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6201 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6203 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6204 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | seq_printf(seq, " %s", mddev->pers->name); |
| 6206 | } |
| 6207 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6208 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6209 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | char b[BDEVNAME_SIZE]; |
| 6211 | seq_printf(seq, " %s[%d]", |
| 6212 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6213 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6214 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6215 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | seq_printf(seq, "(F)"); |
| 6217 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6218 | } else if (rdev->raid_disk < 0) |
| 6219 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6220 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | if (!list_empty(&mddev->disks)) { |
| 6224 | if (mddev->pers) |
| 6225 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6226 | (unsigned long long) |
| 6227 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | else |
| 6229 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6230 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6232 | if (mddev->persistent) { |
| 6233 | if (mddev->major_version != 0 || |
| 6234 | mddev->minor_version != 90) { |
| 6235 | seq_printf(seq," super %d.%d", |
| 6236 | mddev->major_version, |
| 6237 | mddev->minor_version); |
| 6238 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6239 | } else if (mddev->external) |
| 6240 | seq_printf(seq, " super external:%s", |
| 6241 | mddev->metadata_type); |
| 6242 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6243 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | |
| 6245 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6246 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6248 | if (mddev->pers->sync_request) { |
| 6249 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6250 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6251 | seq_printf(seq, "\n "); |
| 6252 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6253 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6254 | else if (mddev->recovery_cp < MaxSector) |
| 6255 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6256 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6257 | } else |
| 6258 | seq_printf(seq, "\n "); |
| 6259 | |
| 6260 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6261 | unsigned long chunk_kb; |
| 6262 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6263 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6264 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6265 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6266 | "%lu%s chunk", |
| 6267 | bitmap->pages - bitmap->missing_pages, |
| 6268 | bitmap->pages, |
| 6269 | (bitmap->pages - bitmap->missing_pages) |
| 6270 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6271 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6272 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6273 | if (bitmap->file) { |
| 6274 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6275 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6276 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6277 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6278 | seq_printf(seq, "\n"); |
| 6279 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | seq_printf(seq, "\n"); |
| 6283 | } |
| 6284 | mddev_unlock(mddev); |
| 6285 | |
| 6286 | return 0; |
| 6287 | } |
| 6288 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6289 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | .start = md_seq_start, |
| 6291 | .next = md_seq_next, |
| 6292 | .stop = md_seq_stop, |
| 6293 | .show = md_seq_show, |
| 6294 | }; |
| 6295 | |
| 6296 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6297 | { |
| 6298 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6299 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6300 | if (mi == NULL) |
| 6301 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | |
| 6303 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6304 | if (error) |
| 6305 | kfree(mi); |
| 6306 | else { |
| 6307 | struct seq_file *p = file->private_data; |
| 6308 | p->private = mi; |
| 6309 | mi->event = atomic_read(&md_event_count); |
| 6310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | return error; |
| 6312 | } |
| 6313 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6314 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6315 | { |
| 6316 | struct seq_file *m = filp->private_data; |
| 6317 | struct mdstat_info *mi = m->private; |
| 6318 | int mask; |
| 6319 | |
| 6320 | poll_wait(filp, &md_event_waiters, wait); |
| 6321 | |
| 6322 | /* always allow read */ |
| 6323 | mask = POLLIN | POLLRDNORM; |
| 6324 | |
| 6325 | if (mi->event != atomic_read(&md_event_count)) |
| 6326 | mask |= POLLERR | POLLPRI; |
| 6327 | return mask; |
| 6328 | } |
| 6329 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6330 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6331 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | .open = md_seq_open, |
| 6333 | .read = seq_read, |
| 6334 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6335 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6336 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | }; |
| 6338 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6339 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6342 | list_add_tail(&p->list, &pers_list); |
| 6343 | 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] | 6344 | spin_unlock(&pers_lock); |
| 6345 | return 0; |
| 6346 | } |
| 6347 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6348 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6349 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6350 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6352 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | spin_unlock(&pers_lock); |
| 6354 | return 0; |
| 6355 | } |
| 6356 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6357 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | { |
| 6359 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6361 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | |
| 6363 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6364 | rcu_read_lock(); |
| 6365 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6367 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6368 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6369 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6370 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6371 | * as sync_io is counted when a request starts, and |
| 6372 | * disk_stats is counted when it completes. |
| 6373 | * So resync activity will cause curr_events to be smaller than |
| 6374 | * when there was no such activity. |
| 6375 | * non-sync IO will cause disk_stat to increase without |
| 6376 | * increasing sync_io so curr_events will (eventually) |
| 6377 | * be larger than it was before. Once it becomes |
| 6378 | * substantially larger, the test below will cause |
| 6379 | * the array to appear non-idle, and resync will slow |
| 6380 | * down. |
| 6381 | * If there is a lot of outstanding resync activity when |
| 6382 | * we set last_event to curr_events, then all that activity |
| 6383 | * completing might cause the array to appear non-idle |
| 6384 | * and resync will be slowed down even though there might |
| 6385 | * not have been non-resync activity. This will only |
| 6386 | * happen once though. 'last_events' will soon reflect |
| 6387 | * the state where there is little or no outstanding |
| 6388 | * resync requests, and further resync activity will |
| 6389 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6390 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6392 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | rdev->last_events = curr_events; |
| 6394 | idle = 0; |
| 6395 | } |
| 6396 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6397 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | return idle; |
| 6399 | } |
| 6400 | |
| 6401 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6402 | { |
| 6403 | /* another "blocks" (512byte) blocks have been synced */ |
| 6404 | atomic_sub(blocks, &mddev->recovery_active); |
| 6405 | wake_up(&mddev->recovery_wait); |
| 6406 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6407 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | md_wakeup_thread(mddev->thread); |
| 6409 | // stop recovery, signal do_sync .... |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6414 | /* md_write_start(mddev, bi) |
| 6415 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6416 | * in superblock) before writing, schedule a superblock update |
| 6417 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6418 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6419 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6421 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6422 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6423 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6424 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6425 | BUG_ON(mddev->ro == 1); |
| 6426 | if (mddev->ro == 2) { |
| 6427 | /* need to switch to read/write */ |
| 6428 | mddev->ro = 0; |
| 6429 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6430 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6431 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6432 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6433 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6434 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6435 | if (mddev->safemode == 1) |
| 6436 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6437 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6438 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6439 | if (mddev->in_sync) { |
| 6440 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6441 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6442 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6443 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6444 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6445 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6446 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6447 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6448 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6449 | wait_event(mddev->sb_wait, |
| 6450 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6451 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | void md_write_end(mddev_t *mddev) |
| 6455 | { |
| 6456 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6457 | if (mddev->safemode == 2) |
| 6458 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6459 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6461 | } |
| 6462 | } |
| 6463 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6464 | /* md_allow_write(mddev) |
| 6465 | * Calling this ensures that the array is marked 'active' so that writes |
| 6466 | * may proceed without blocking. It is important to call this before |
| 6467 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6468 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6469 | * |
| 6470 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6471 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6472 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6473 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6474 | { |
| 6475 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6476 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6477 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6478 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6479 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6480 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6481 | |
| 6482 | spin_lock_irq(&mddev->write_lock); |
| 6483 | if (mddev->in_sync) { |
| 6484 | mddev->in_sync = 0; |
| 6485 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6486 | if (mddev->safemode_delay && |
| 6487 | mddev->safemode == 0) |
| 6488 | mddev->safemode = 1; |
| 6489 | spin_unlock_irq(&mddev->write_lock); |
| 6490 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6491 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6492 | } else |
| 6493 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6494 | |
| 6495 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6496 | return -EAGAIN; |
| 6497 | else |
| 6498 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6499 | } |
| 6500 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | #define SYNC_MARKS 10 |
| 6503 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6504 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | { |
| 6506 | mddev_t *mddev2; |
| 6507 | unsigned int currspeed = 0, |
| 6508 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6509 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | unsigned long mark[SYNC_MARKS]; |
| 6511 | sector_t mark_cnt[SYNC_MARKS]; |
| 6512 | int last_mark,m; |
| 6513 | struct list_head *tmp; |
| 6514 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6515 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6516 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6517 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | |
| 6519 | /* just incase thread restarts... */ |
| 6520 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6521 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6522 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6523 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6525 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6526 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6527 | desc = "data-check"; |
| 6528 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6529 | desc = "requested-resync"; |
| 6530 | else |
| 6531 | desc = "resync"; |
| 6532 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6533 | desc = "reshape"; |
| 6534 | else |
| 6535 | desc = "recovery"; |
| 6536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | /* we overload curr_resync somewhat here. |
| 6538 | * 0 == not engaged in resync at all |
| 6539 | * 2 == checking that there is no conflict with another sync |
| 6540 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6541 | * commense |
| 6542 | * other == active in resync - this many blocks |
| 6543 | * |
| 6544 | * Before starting a resync we must have set curr_resync to |
| 6545 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6546 | * less than ours. When we find one that is the same or higher |
| 6547 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6548 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6549 | * This will mean we have to start checking from the beginning again. |
| 6550 | * |
| 6551 | */ |
| 6552 | |
| 6553 | do { |
| 6554 | mddev->curr_resync = 2; |
| 6555 | |
| 6556 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6557 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6558 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6559 | |
| 6560 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6562 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6563 | if (mddev2 == mddev) |
| 6564 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6565 | if (!mddev->parallel_resync |
| 6566 | && mddev2->curr_resync |
| 6567 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | DEFINE_WAIT(wq); |
| 6569 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6570 | /* arbitrarily yield */ |
| 6571 | mddev->curr_resync = 1; |
| 6572 | wake_up(&resync_wait); |
| 6573 | } |
| 6574 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6575 | /* no need to wait here, we can wait the next |
| 6576 | * time 'round when curr_resync == 2 |
| 6577 | */ |
| 6578 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6579 | /* We need to wait 'interruptible' so as not to |
| 6580 | * contribute to the load average, and not to |
| 6581 | * be caught by 'softlockup' |
| 6582 | */ |
| 6583 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6584 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6585 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6586 | printk(KERN_INFO "md: delaying %s of %s" |
| 6587 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6589 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6591 | if (signal_pending(current)) |
| 6592 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | schedule(); |
| 6594 | finish_wait(&resync_wait, &wq); |
| 6595 | goto try_again; |
| 6596 | } |
| 6597 | finish_wait(&resync_wait, &wq); |
| 6598 | } |
| 6599 | } |
| 6600 | } while (mddev->curr_resync < 2); |
| 6601 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6602 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6603 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6605 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6606 | */ |
| 6607 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6608 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6609 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6610 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6611 | j = mddev->resync_min; |
| 6612 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6613 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6614 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6615 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6616 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6617 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6619 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6620 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6621 | rcu_read_lock(); |
| 6622 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6623 | if (rdev->raid_disk >= 0 && |
| 6624 | !test_bit(Faulty, &rdev->flags) && |
| 6625 | !test_bit(In_sync, &rdev->flags) && |
| 6626 | rdev->recovery_offset < j) |
| 6627 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6628 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6631 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6632 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6633 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6634 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6635 | "(but not more than %d KB/sec) for %s.\n", |
| 6636 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6638 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6639 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6640 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6642 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6643 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | } |
| 6645 | last_mark = 0; |
| 6646 | mddev->resync_mark = mark[last_mark]; |
| 6647 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6648 | |
| 6649 | /* |
| 6650 | * Tune reconstruction: |
| 6651 | */ |
| 6652 | window = 32*(PAGE_SIZE/512); |
| 6653 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6654 | window/2,(unsigned long long) max_sectors/2); |
| 6655 | |
| 6656 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | last_check = 0; |
| 6658 | |
| 6659 | if (j>2) { |
| 6660 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6661 | "md: resuming %s of %s from checkpoint.\n", |
| 6662 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | mddev->curr_resync = j; |
| 6664 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6665 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6666 | |
| 6667 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6668 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6670 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6671 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6672 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6673 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6674 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6675 | > (max_sectors >> 4)) || |
| 6676 | (j - mddev->curr_resync_completed)*2 |
| 6677 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6678 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6679 | /* time to update curr_resync_completed */ |
| 6680 | blk_unplug(mddev->queue); |
| 6681 | wait_event(mddev->recovery_wait, |
| 6682 | atomic_read(&mddev->recovery_active) == 0); |
| 6683 | mddev->curr_resync_completed = |
| 6684 | mddev->curr_resync; |
| 6685 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6686 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6687 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6688 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6689 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6690 | /* As this condition is controlled by user-space, |
| 6691 | * we can block indefinitely, so use '_interruptible' |
| 6692 | * to avoid triggering warnings. |
| 6693 | */ |
| 6694 | flush_signals(current); /* just in case */ |
| 6695 | wait_event_interruptible(mddev->recovery_wait, |
| 6696 | mddev->resync_max > j |
| 6697 | || kthread_should_stop()); |
| 6698 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6699 | |
| 6700 | if (kthread_should_stop()) |
| 6701 | goto interrupted; |
| 6702 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6703 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6704 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6705 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6706 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | goto out; |
| 6708 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6709 | |
| 6710 | if (!skipped) { /* actual IO requested */ |
| 6711 | io_sectors += sectors; |
| 6712 | atomic_add(sectors, &mddev->recovery_active); |
| 6713 | } |
| 6714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | j += sectors; |
| 6716 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6717 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6718 | if (last_check == 0) |
| 6719 | /* this is the earliers that rebuilt will be |
| 6720 | * visible in /proc/mdstat |
| 6721 | */ |
| 6722 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6723 | |
| 6724 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | continue; |
| 6726 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6727 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6729 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | break; |
| 6731 | |
| 6732 | repeat: |
| 6733 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6734 | /* step marks */ |
| 6735 | int next = (last_mark+1) % SYNC_MARKS; |
| 6736 | |
| 6737 | mddev->resync_mark = mark[next]; |
| 6738 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6739 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6740 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | last_mark = next; |
| 6742 | } |
| 6743 | |
| 6744 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6745 | if (kthread_should_stop()) |
| 6746 | goto interrupted; |
| 6747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | |
| 6749 | /* |
| 6750 | * this loop exits only if either when we are slower than |
| 6751 | * the 'hard' speed limit, or the system was IO-idle for |
| 6752 | * a jiffy. |
| 6753 | * the system might be non-idle CPU-wise, but we only care |
| 6754 | * about not overloading the IO subsystem. (things like an |
| 6755 | * e2fsck being done on the RAID array should execute fast) |
| 6756 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6757 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | cond_resched(); |
| 6759 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6760 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6761 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6763 | if (currspeed > speed_min(mddev)) { |
| 6764 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6765 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6766 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | goto repeat; |
| 6768 | } |
| 6769 | } |
| 6770 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6771 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | /* |
| 6773 | * this also signals 'finished resyncing' to md_stop |
| 6774 | */ |
| 6775 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6776 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | |
| 6778 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6779 | |
| 6780 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6781 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6783 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6784 | mddev->curr_resync > 2) { |
| 6785 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6786 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6787 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6788 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6789 | "md: checkpointing %s of %s.\n", |
| 6790 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6791 | mddev->recovery_cp = mddev->curr_resync; |
| 6792 | } |
| 6793 | } else |
| 6794 | mddev->recovery_cp = MaxSector; |
| 6795 | } else { |
| 6796 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6797 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6798 | rcu_read_lock(); |
| 6799 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6800 | if (rdev->raid_disk >= 0 && |
| 6801 | !test_bit(Faulty, &rdev->flags) && |
| 6802 | !test_bit(In_sync, &rdev->flags) && |
| 6803 | rdev->recovery_offset < mddev->curr_resync) |
| 6804 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6805 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6808 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6811 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6812 | /* We completed so min/max setting can be forgotten if used. */ |
| 6813 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6814 | mddev->resync_min = 0; |
| 6815 | mddev->resync_max = MaxSector; |
| 6816 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6817 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6819 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6820 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6821 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | wake_up(&resync_wait); |
| 6823 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6824 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6825 | return; |
| 6826 | |
| 6827 | interrupted: |
| 6828 | /* |
| 6829 | * got a signal, exit. |
| 6830 | */ |
| 6831 | printk(KERN_INFO |
| 6832 | "md: md_do_sync() got signal ... exiting\n"); |
| 6833 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6834 | goto out; |
| 6835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6837 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
| 6839 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6840 | static int remove_and_add_spares(mddev_t *mddev) |
| 6841 | { |
| 6842 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6843 | int spares = 0; |
| 6844 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6845 | mddev->curr_resync_completed = 0; |
| 6846 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6847 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6848 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6849 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6850 | (test_bit(Faulty, &rdev->flags) || |
| 6851 | ! test_bit(In_sync, &rdev->flags)) && |
| 6852 | atomic_read(&rdev->nr_pending)==0) { |
| 6853 | if (mddev->pers->hot_remove_disk( |
| 6854 | mddev, rdev->raid_disk)==0) { |
| 6855 | char nm[20]; |
| 6856 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6857 | sysfs_remove_link(&mddev->kobj, nm); |
| 6858 | rdev->raid_disk = -1; |
| 6859 | } |
| 6860 | } |
| 6861 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6862 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6863 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6864 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6865 | !test_bit(In_sync, &rdev->flags) && |
| 6866 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6867 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6868 | if (rdev->raid_disk < 0 |
| 6869 | && !test_bit(Faulty, &rdev->flags)) { |
| 6870 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6871 | if (mddev->pers-> |
| 6872 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6873 | char nm[20]; |
| 6874 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6875 | if (sysfs_create_link(&mddev->kobj, |
| 6876 | &rdev->kobj, nm)) |
| 6877 | printk(KERN_WARNING |
| 6878 | "md: cannot register " |
| 6879 | "%s for %s\n", |
| 6880 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6881 | spares++; |
| 6882 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6883 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6884 | } else |
| 6885 | break; |
| 6886 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6887 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6888 | } |
| 6889 | return spares; |
| 6890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | /* |
| 6892 | * This routine is regularly called by all per-raid-array threads to |
| 6893 | * deal with generic issues like resync and super-block update. |
| 6894 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6895 | * need this as they never do any recovery or update the superblock. |
| 6896 | * |
| 6897 | * It does not do any resync itself, but rather "forks" off other threads |
| 6898 | * to do that as needed. |
| 6899 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6900 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6901 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | * and wakeups up this thread which will reap the thread and finish up. |
| 6903 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6904 | * |
| 6905 | * The overall approach is: |
| 6906 | * 1/ if the superblock needs updating, update it. |
| 6907 | * 2/ If a recovery thread is running, don't do anything else. |
| 6908 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6909 | * 4/ If there are any faulty devices, remove them. |
| 6910 | * 5/ If array is degraded, try to add spares devices |
| 6911 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6912 | */ |
| 6913 | void md_check_recovery(mddev_t *mddev) |
| 6914 | { |
| 6915 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | |
| 6917 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6918 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6919 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | |
| 6921 | if (mddev->ro) |
| 6922 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6923 | |
| 6924 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6925 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6926 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6927 | mdname(mddev)); |
| 6928 | mddev->safemode = 2; |
| 6929 | } |
| 6930 | flush_signals(current); |
| 6931 | } |
| 6932 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6933 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6934 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6936 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6938 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6939 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6940 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6941 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | )) |
| 6943 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6944 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6945 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6946 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6947 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6948 | if (mddev->ro) { |
| 6949 | /* Only thing we do on a ro array is remove |
| 6950 | * failed devices. |
| 6951 | */ |
| 6952 | remove_and_add_spares(mddev); |
| 6953 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6954 | goto unlock; |
| 6955 | } |
| 6956 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6957 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6958 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6959 | spin_lock_irq(&mddev->write_lock); |
| 6960 | if (mddev->safemode && |
| 6961 | !atomic_read(&mddev->writes_pending) && |
| 6962 | !mddev->in_sync && |
| 6963 | mddev->recovery_cp == MaxSector) { |
| 6964 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6965 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6966 | if (mddev->persistent) |
| 6967 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6968 | } |
| 6969 | if (mddev->safemode == 1) |
| 6970 | mddev->safemode = 0; |
| 6971 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6972 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6973 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6974 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6975 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6976 | if (mddev->flags) |
| 6977 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6980 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6981 | /* resync/recovery still happening */ |
| 6982 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6983 | goto unlock; |
| 6984 | } |
| 6985 | if (mddev->sync_thread) { |
| 6986 | /* resync has finished, collect result */ |
| 6987 | md_unregister_thread(mddev->sync_thread); |
| 6988 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6989 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6990 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | /* success...*/ |
| 6992 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6993 | if (mddev->pers->spare_active(mddev)) |
| 6994 | sysfs_notify(&mddev->kobj, NULL, |
| 6995 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6997 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6998 | mddev->pers->finish_reshape) |
| 6999 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7000 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7001 | |
| 7002 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7003 | * information must be scrapped |
| 7004 | */ |
| 7005 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7006 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7007 | rdev->saved_raid_disk = -1; |
| 7008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | mddev->recovery = 0; |
| 7010 | /* flag recovery needed just to double check */ |
| 7011 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7012 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7013 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | goto unlock; |
| 7015 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7016 | /* Set RUNNING before clearing NEEDED to avoid |
| 7017 | * any transients in the value of "sync_action". |
| 7018 | */ |
| 7019 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7020 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7021 | /* Clear some bits that don't mean anything, but |
| 7022 | * might be left set |
| 7023 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7024 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7025 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7027 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7028 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | /* no recovery is running. |
| 7030 | * remove any failed drives, then |
| 7031 | * add spares if possible. |
| 7032 | * Spare are also removed and re-added, to allow |
| 7033 | * the personality to fail the re-add. |
| 7034 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7036 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7037 | if (mddev->pers->check_reshape == NULL || |
| 7038 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7039 | /* Cannot proceed */ |
| 7040 | goto unlock; |
| 7041 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7042 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7043 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7044 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7045 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7046 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7047 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7048 | } else if (mddev->recovery_cp < MaxSector) { |
| 7049 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7050 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7051 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7052 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7056 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7057 | /* We are adding a device or devices to an array |
| 7058 | * which has the bitmap stored on all devices. |
| 7059 | * So make sure all bitmap pages get written |
| 7060 | */ |
| 7061 | bitmap_write_all(mddev->bitmap); |
| 7062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7064 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7065 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | if (!mddev->sync_thread) { |
| 7067 | printk(KERN_ERR "%s: could not start resync" |
| 7068 | " thread...\n", |
| 7069 | mdname(mddev)); |
| 7070 | /* leave the spares where they are, it shouldn't hurt */ |
| 7071 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7072 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7074 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7075 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | } |
| 7077 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7078 | if (!mddev->sync_thread) { |
| 7079 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7080 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7081 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7082 | if (mddev->sysfs_action) |
| 7083 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | mddev_unlock(mddev); |
| 7086 | } |
| 7087 | } |
| 7088 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7089 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7090 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7091 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7092 | wait_event_timeout(rdev->blocked_wait, |
| 7093 | !test_bit(Blocked, &rdev->flags), |
| 7094 | msecs_to_jiffies(5000)); |
| 7095 | rdev_dec_pending(rdev, mddev); |
| 7096 | } |
| 7097 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7098 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7099 | static int md_notify_reboot(struct notifier_block *this, |
| 7100 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | { |
| 7102 | struct list_head *tmp; |
| 7103 | mddev_t *mddev; |
| 7104 | |
| 7105 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7106 | |
| 7107 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7108 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7109 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7110 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7111 | /* Force a switch to readonly even array |
| 7112 | * appears to still be in use. Hence |
| 7113 | * the '100'. |
| 7114 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7115 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7116 | mddev_unlock(mddev); |
| 7117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | /* |
| 7119 | * certain more exotic SCSI devices are known to be |
| 7120 | * volatile wrt too early system reboots. While the |
| 7121 | * right place to handle this issue is the given |
| 7122 | * driver, we do want to have a safe RAID driver ... |
| 7123 | */ |
| 7124 | mdelay(1000*1); |
| 7125 | } |
| 7126 | return NOTIFY_DONE; |
| 7127 | } |
| 7128 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7129 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | .notifier_call = md_notify_reboot, |
| 7131 | .next = NULL, |
| 7132 | .priority = INT_MAX, /* before any real devices */ |
| 7133 | }; |
| 7134 | |
| 7135 | static void md_geninit(void) |
| 7136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7138 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7139 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | } |
| 7141 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7142 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7144 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | return -1; |
| 7146 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7147 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | return -1; |
| 7149 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7150 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7151 | md_probe, NULL, NULL); |
| 7152 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | md_probe, NULL, NULL); |
| 7154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7156 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7157 | |
| 7158 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7159 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | } |
| 7161 | |
| 7162 | |
| 7163 | #ifndef MODULE |
| 7164 | |
| 7165 | /* |
| 7166 | * Searches all registered partitions for autorun RAID arrays |
| 7167 | * at boot time. |
| 7168 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7169 | |
| 7170 | static LIST_HEAD(all_detected_devices); |
| 7171 | struct detected_devices_node { |
| 7172 | struct list_head list; |
| 7173 | dev_t dev; |
| 7174 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | |
| 7176 | void md_autodetect_dev(dev_t dev) |
| 7177 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7178 | struct detected_devices_node *node_detected_dev; |
| 7179 | |
| 7180 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7181 | if (node_detected_dev) { |
| 7182 | node_detected_dev->dev = dev; |
| 7183 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7184 | } else { |
| 7185 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7186 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | |
| 7191 | static void autostart_arrays(int part) |
| 7192 | { |
| 7193 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7194 | struct detected_devices_node *node_detected_dev; |
| 7195 | dev_t dev; |
| 7196 | int i_scanned, i_passed; |
| 7197 | |
| 7198 | i_scanned = 0; |
| 7199 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | |
| 7201 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7202 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7203 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7204 | i_scanned++; |
| 7205 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7206 | struct detected_devices_node, list); |
| 7207 | list_del(&node_detected_dev->list); |
| 7208 | dev = node_detected_dev->dev; |
| 7209 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7210 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | if (IS_ERR(rdev)) |
| 7212 | continue; |
| 7213 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7214 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | MD_BUG(); |
| 7216 | continue; |
| 7217 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7218 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7220 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7221 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7222 | |
| 7223 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7224 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | |
| 7226 | autorun_devices(part); |
| 7227 | } |
| 7228 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7229 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | |
| 7231 | static __exit void md_exit(void) |
| 7232 | { |
| 7233 | mddev_t *mddev; |
| 7234 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7235 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7236 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7237 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7239 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | unregister_blkdev(mdp_major, "mdp"); |
| 7241 | unregister_reboot_notifier(&md_notifier); |
| 7242 | unregister_sysctl_table(raid_table_header); |
| 7243 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7244 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7246 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7247 | } |
| 7248 | } |
| 7249 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7250 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7251 | module_exit(md_exit) |
| 7252 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7253 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7254 | { |
| 7255 | return sprintf(buffer, "%d", start_readonly); |
| 7256 | } |
| 7257 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7258 | { |
| 7259 | char *e; |
| 7260 | int num = simple_strtoul(val, &e, 10); |
| 7261 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7262 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7263 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7264 | } |
| 7265 | return -EINVAL; |
| 7266 | } |
| 7267 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7268 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7269 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7270 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7271 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | EXPORT_SYMBOL(register_md_personality); |
| 7274 | EXPORT_SYMBOL(unregister_md_personality); |
| 7275 | EXPORT_SYMBOL(md_error); |
| 7276 | EXPORT_SYMBOL(md_done_sync); |
| 7277 | EXPORT_SYMBOL(md_write_start); |
| 7278 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | EXPORT_SYMBOL(md_register_thread); |
| 7280 | EXPORT_SYMBOL(md_unregister_thread); |
| 7281 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | EXPORT_SYMBOL(md_check_recovery); |
| 7283 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7284 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7285 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7286 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |