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 | |
| 510 | static inline void mddev_unlock(mddev_t * mddev) |
| 511 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 512 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 514 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 517 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 519 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 521 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (rdev->desc_nr == nr) |
| 523 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 529 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | mdk_rdev_t *rdev; |
| 531 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 532 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (rdev->bdev->bd_dev == dev) |
| 534 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
| 538 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 539 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 540 | { |
| 541 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 542 | list_for_each_entry(pers, &pers_list, list) { |
| 543 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 544 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 545 | if (strcmp(pers->name, clevel)==0) |
| 546 | return pers; |
| 547 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 548 | return NULL; |
| 549 | } |
| 550 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 551 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 552 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 554 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 555 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 559 | { |
| 560 | if (rdev->sb_page) |
| 561 | MD_BUG(); |
| 562 | |
| 563 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 564 | if (!rdev->sb_page) { |
| 565 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 566 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 573 | { |
| 574 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 575 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | rdev->sb_loaded = 0; |
| 577 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 578 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 579 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 584 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 585 | { |
| 586 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 587 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 588 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 589 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 590 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 591 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 592 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 593 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 594 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 595 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 596 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 597 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 598 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 599 | } |
| 600 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 601 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 602 | { |
| 603 | struct bio *bio2 = bio->bi_private; |
| 604 | mdk_rdev_t *rdev = bio2->bi_private; |
| 605 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 606 | |
| 607 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 608 | error == -EOPNOTSUPP) { |
| 609 | unsigned long flags; |
| 610 | /* barriers don't appear to be supported :-( */ |
| 611 | set_bit(BarriersNotsupp, &rdev->flags); |
| 612 | mddev->barriers_work = 0; |
| 613 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 614 | bio2->bi_next = mddev->biolist; |
| 615 | mddev->biolist = bio2; |
| 616 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 617 | wake_up(&mddev->sb_wait); |
| 618 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 619 | } else { |
| 620 | bio_put(bio2); |
| 621 | bio->bi_private = rdev; |
| 622 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 623 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 624 | } |
| 625 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 626 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 627 | sector_t sector, int size, struct page *page) |
| 628 | { |
| 629 | /* write first size bytes of page to sector of rdev |
| 630 | * Increment mddev->pending_writes before returning |
| 631 | * and decrement it on completion, waking up sb_wait |
| 632 | * if zero is reached. |
| 633 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 634 | * |
| 635 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 636 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 637 | */ |
| 638 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 639 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 640 | |
| 641 | bio->bi_bdev = rdev->bdev; |
| 642 | bio->bi_sector = sector; |
| 643 | bio_add_page(bio, page, size, 0); |
| 644 | bio->bi_private = rdev; |
| 645 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 646 | bio->bi_rw = rw; |
| 647 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 648 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 649 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 650 | struct bio *rbio; |
| 651 | rw |= (1<<BIO_RW_BARRIER); |
| 652 | rbio = bio_clone(bio, GFP_NOIO); |
| 653 | rbio->bi_private = bio; |
| 654 | rbio->bi_end_io = super_written_barrier; |
| 655 | submit_bio(rw, rbio); |
| 656 | } else |
| 657 | submit_bio(rw, bio); |
| 658 | } |
| 659 | |
| 660 | void md_super_wait(mddev_t *mddev) |
| 661 | { |
| 662 | /* wait for all superblock writes that were scheduled to complete. |
| 663 | * if any had to be retried (due to BARRIER problems), retry them |
| 664 | */ |
| 665 | DEFINE_WAIT(wq); |
| 666 | for(;;) { |
| 667 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 668 | if (atomic_read(&mddev->pending_writes)==0) |
| 669 | break; |
| 670 | while (mddev->biolist) { |
| 671 | struct bio *bio; |
| 672 | spin_lock_irq(&mddev->write_lock); |
| 673 | bio = mddev->biolist; |
| 674 | mddev->biolist = bio->bi_next ; |
| 675 | bio->bi_next = NULL; |
| 676 | spin_unlock_irq(&mddev->write_lock); |
| 677 | submit_bio(bio->bi_rw, bio); |
| 678 | } |
| 679 | schedule(); |
| 680 | } |
| 681 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 682 | } |
| 683 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 684 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 689 | 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] | 690 | struct page *page, int rw) |
| 691 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 692 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | struct completion event; |
| 694 | int ret; |
| 695 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 696 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | bio->bi_bdev = bdev; |
| 699 | bio->bi_sector = sector; |
| 700 | bio_add_page(bio, page, size, 0); |
| 701 | init_completion(&event); |
| 702 | bio->bi_private = &event; |
| 703 | bio->bi_end_io = bi_complete; |
| 704 | submit_bio(rw, bio); |
| 705 | wait_for_completion(&event); |
| 706 | |
| 707 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 708 | bio_put(bio); |
| 709 | return ret; |
| 710 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 711 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 713 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | char b[BDEVNAME_SIZE]; |
| 716 | if (!rdev->sb_page) { |
| 717 | MD_BUG(); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | if (rdev->sb_loaded) |
| 721 | return 0; |
| 722 | |
| 723 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 724 | 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] | 725 | goto fail; |
| 726 | rdev->sb_loaded = 1; |
| 727 | return 0; |
| 728 | |
| 729 | fail: |
| 730 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 731 | bdevname(rdev->bdev,b)); |
| 732 | return -EINVAL; |
| 733 | } |
| 734 | |
| 735 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 736 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 737 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 738 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 739 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 740 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 744 | { |
| 745 | int ret; |
| 746 | mdp_super_t *tmp1, *tmp2; |
| 747 | |
| 748 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 749 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 750 | |
| 751 | if (!tmp1 || !tmp2) { |
| 752 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 753 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | goto abort; |
| 755 | } |
| 756 | |
| 757 | *tmp1 = *sb1; |
| 758 | *tmp2 = *sb2; |
| 759 | |
| 760 | /* |
| 761 | * nr_disks is not constant |
| 762 | */ |
| 763 | tmp1->nr_disks = 0; |
| 764 | tmp2->nr_disks = 0; |
| 765 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 766 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 768 | kfree(tmp1); |
| 769 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return ret; |
| 771 | } |
| 772 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 773 | |
| 774 | static u32 md_csum_fold(u32 csum) |
| 775 | { |
| 776 | csum = (csum & 0xffff) + (csum >> 16); |
| 777 | return (csum & 0xffff) + (csum >> 16); |
| 778 | } |
| 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 781 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 782 | u64 newcsum = 0; |
| 783 | u32 *sb32 = (u32*)sb; |
| 784 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | unsigned int disk_csum, csum; |
| 786 | |
| 787 | disk_csum = sb->sb_csum; |
| 788 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 789 | |
| 790 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 791 | newcsum += sb32[i]; |
| 792 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 793 | |
| 794 | |
| 795 | #ifdef CONFIG_ALPHA |
| 796 | /* This used to use csum_partial, which was wrong for several |
| 797 | * reasons including that different results are returned on |
| 798 | * different architectures. It isn't critical that we get exactly |
| 799 | * the same return value as before (we always csum_fold before |
| 800 | * testing, and that removes any differences). However as we |
| 801 | * know that csum_partial always returned a 16bit value on |
| 802 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 803 | */ |
| 804 | sb->sb_csum = md_csum_fold(disk_csum); |
| 805 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 807 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return csum; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | /* |
| 813 | * Handle superblock details. |
| 814 | * We want to be able to handle multiple superblock formats |
| 815 | * so we have a common interface to them all, and an array of |
| 816 | * different handlers. |
| 817 | * We rely on user-space to write the initial superblock, and support |
| 818 | * reading and updating of superblocks. |
| 819 | * Interface methods are: |
| 820 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 821 | * loads and validates a superblock on dev. |
| 822 | * if refdev != NULL, compare superblocks on both devices |
| 823 | * Return: |
| 824 | * 0 - dev has a superblock that is compatible with refdev |
| 825 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 826 | * so dev should be used as the refdev in future |
| 827 | * -EINVAL superblock incompatible or invalid |
| 828 | * -othererror e.g. -EIO |
| 829 | * |
| 830 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 831 | * Verify that dev is acceptable into mddev. |
| 832 | * The first time, mddev->raid_disks will be 0, and data from |
| 833 | * dev should be merged in. Subsequent calls check that dev |
| 834 | * is new enough. Return 0 or -EINVAL |
| 835 | * |
| 836 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 837 | * Update the superblock for rdev with data in mddev |
| 838 | * This does not write to disc. |
| 839 | * |
| 840 | */ |
| 841 | |
| 842 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 843 | char *name; |
| 844 | struct module *owner; |
| 845 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 846 | int minor_version); |
| 847 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 848 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 849 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 850 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 854 | * Check that the given mddev has no bitmap. |
| 855 | * |
| 856 | * This function is called from the run method of all personalities that do not |
| 857 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 858 | * has a bitmap. Otherwise, it returns 0. |
| 859 | * |
| 860 | */ |
| 861 | int md_check_no_bitmap(mddev_t *mddev) |
| 862 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 863 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 864 | return 0; |
| 865 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 866 | mdname(mddev), mddev->pers->name); |
| 867 | return 1; |
| 868 | } |
| 869 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 870 | |
| 871 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | * load_super for 0.90.0 |
| 873 | */ |
| 874 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 875 | { |
| 876 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 877 | mdp_super_t *sb; |
| 878 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
| 880 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 881 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | * it's at the end of the disk. |
| 883 | * |
| 884 | * It also happens to be a multiple of 4Kb. |
| 885 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 886 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 888 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | if (ret) return ret; |
| 890 | |
| 891 | ret = -EINVAL; |
| 892 | |
| 893 | bdevname(rdev->bdev, b); |
| 894 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 895 | |
| 896 | if (sb->md_magic != MD_SB_MAGIC) { |
| 897 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 898 | b); |
| 899 | goto abort; |
| 900 | } |
| 901 | |
| 902 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 903 | sb->minor_version < 90 || |
| 904 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 906 | sb->major_version, sb->minor_version, |
| 907 | b); |
| 908 | goto abort; |
| 909 | } |
| 910 | |
| 911 | if (sb->raid_disks <= 0) |
| 912 | goto abort; |
| 913 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 914 | 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] | 915 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 916 | b); |
| 917 | goto abort; |
| 918 | } |
| 919 | |
| 920 | rdev->preferred_minor = sb->md_minor; |
| 921 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 922 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | if (sb->level == LEVEL_MULTIPATH) |
| 925 | rdev->desc_nr = -1; |
| 926 | else |
| 927 | rdev->desc_nr = sb->this_disk.number; |
| 928 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 929 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 931 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | __u64 ev1, ev2; |
| 933 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 934 | if (!uuid_equal(refsb, sb)) { |
| 935 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 936 | b, bdevname(refdev->bdev,b2)); |
| 937 | goto abort; |
| 938 | } |
| 939 | if (!sb_equal(refsb, sb)) { |
| 940 | printk(KERN_WARNING "md: %s has same UUID" |
| 941 | " but different superblock to %s\n", |
| 942 | b, bdevname(refdev->bdev, b2)); |
| 943 | goto abort; |
| 944 | } |
| 945 | ev1 = md_event(sb); |
| 946 | ev2 = md_event(refsb); |
| 947 | if (ev1 > ev2) |
| 948 | ret = 1; |
| 949 | else |
| 950 | ret = 0; |
| 951 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 952 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 954 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 955 | /* "this cannot possibly happen" ... */ |
| 956 | ret = -EINVAL; |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | abort: |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * validate_super for 0.90.0 |
| 964 | */ |
| 965 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 966 | { |
| 967 | mdp_disk_t *desc; |
| 968 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 969 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 971 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 972 | clear_bit(Faulty, &rdev->flags); |
| 973 | clear_bit(In_sync, &rdev->flags); |
| 974 | clear_bit(WriteMostly, &rdev->flags); |
| 975 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (mddev->raid_disks == 0) { |
| 978 | mddev->major_version = 0; |
| 979 | mddev->minor_version = sb->minor_version; |
| 980 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 981 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 982 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | mddev->ctime = sb->ctime; |
| 984 | mddev->utime = sb->utime; |
| 985 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 986 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | mddev->layout = sb->layout; |
| 988 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 989 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 990 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 991 | mddev->bitmap_info.offset = 0; |
| 992 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 994 | if (mddev->minor_version >= 91) { |
| 995 | mddev->reshape_position = sb->reshape_position; |
| 996 | mddev->delta_disks = sb->delta_disks; |
| 997 | mddev->new_level = sb->new_level; |
| 998 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 999 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1000 | } else { |
| 1001 | mddev->reshape_position = MaxSector; |
| 1002 | mddev->delta_disks = 0; |
| 1003 | mddev->new_level = mddev->level; |
| 1004 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1005 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1009 | mddev->recovery_cp = MaxSector; |
| 1010 | else { |
| 1011 | if (sb->events_hi == sb->cp_events_hi && |
| 1012 | sb->events_lo == sb->cp_events_lo) { |
| 1013 | mddev->recovery_cp = sb->recovery_cp; |
| 1014 | } else |
| 1015 | mddev->recovery_cp = 0; |
| 1016 | } |
| 1017 | |
| 1018 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1019 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1020 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1021 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1022 | |
| 1023 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1026 | mddev->bitmap_info.file == NULL) |
| 1027 | mddev->bitmap_info.offset = |
| 1028 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1029 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1030 | } else if (mddev->pers == NULL) { |
| 1031 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | ++ev1; |
| 1033 | if (ev1 < mddev->events) |
| 1034 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1035 | } else if (mddev->bitmap) { |
| 1036 | /* if adding to array with a bitmap, then we can accept an |
| 1037 | * older device ... but not too old. |
| 1038 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1039 | if (ev1 < mddev->bitmap->events_cleared) |
| 1040 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1041 | } else { |
| 1042 | if (ev1 < mddev->events) |
| 1043 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1044 | return 0; |
| 1045 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | desc = sb->disks + rdev->desc_nr; |
| 1049 | |
| 1050 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1051 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1052 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1053 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1054 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1056 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1057 | /* active but not in sync implies recovery up to |
| 1058 | * reshape position. We don't know exactly where |
| 1059 | * that is, so set to zero for now */ |
| 1060 | if (mddev->minor_version >= 91) { |
| 1061 | rdev->recovery_offset = 0; |
| 1062 | rdev->raid_disk = desc->raid_disk; |
| 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1065 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1066 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1067 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1068 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * sync_super for 0.90.0 |
| 1074 | */ |
| 1075 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1076 | { |
| 1077 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | mdk_rdev_t *rdev2; |
| 1079 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
| 1082 | /* make rdev->sb match mddev data.. |
| 1083 | * |
| 1084 | * 1/ zero out disks |
| 1085 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1086 | * 3/ any empty disks < next_spare become removed |
| 1087 | * |
| 1088 | * disks[0] gets initialised to REMOVED because |
| 1089 | * we cannot be sure from other fields if it has |
| 1090 | * been initialised or not. |
| 1091 | */ |
| 1092 | int i; |
| 1093 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1094 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1095 | rdev->sb_size = MD_SB_BYTES; |
| 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1098 | |
| 1099 | memset(sb, 0, sizeof(*sb)); |
| 1100 | |
| 1101 | sb->md_magic = MD_SB_MAGIC; |
| 1102 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | sb->patch_version = mddev->patch_version; |
| 1104 | sb->gvalid_words = 0; /* ignored */ |
| 1105 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1106 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1107 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1108 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1109 | |
| 1110 | sb->ctime = mddev->ctime; |
| 1111 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1112 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | sb->raid_disks = mddev->raid_disks; |
| 1114 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1115 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | sb->utime = mddev->utime; |
| 1117 | sb->state = 0; |
| 1118 | sb->events_hi = (mddev->events>>32); |
| 1119 | sb->events_lo = (u32)mddev->events; |
| 1120 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1121 | if (mddev->reshape_position == MaxSector) |
| 1122 | sb->minor_version = 90; |
| 1123 | else { |
| 1124 | sb->minor_version = 91; |
| 1125 | sb->reshape_position = mddev->reshape_position; |
| 1126 | sb->new_level = mddev->new_level; |
| 1127 | sb->delta_disks = mddev->delta_disks; |
| 1128 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1129 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1130 | } |
| 1131 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | if (mddev->in_sync) |
| 1133 | { |
| 1134 | sb->recovery_cp = mddev->recovery_cp; |
| 1135 | sb->cp_events_hi = (mddev->events>>32); |
| 1136 | sb->cp_events_lo = (u32)mddev->events; |
| 1137 | if (mddev->recovery_cp == MaxSector) |
| 1138 | sb->state = (1<< MD_SB_CLEAN); |
| 1139 | } else |
| 1140 | sb->recovery_cp = 0; |
| 1141 | |
| 1142 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1143 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1145 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1146 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1149 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1151 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1152 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1153 | |
| 1154 | if (rdev2->raid_disk >= 0 && |
| 1155 | sb->minor_version >= 91) |
| 1156 | /* we have nowhere to store the recovery_offset, |
| 1157 | * but if it is not below the reshape_position, |
| 1158 | * we can piggy-back on that. |
| 1159 | */ |
| 1160 | is_active = 1; |
| 1161 | if (rdev2->raid_disk < 0 || |
| 1162 | test_bit(Faulty, &rdev2->flags)) |
| 1163 | is_active = 0; |
| 1164 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1165 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1167 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1168 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | d = &sb->disks[rdev2->desc_nr]; |
| 1170 | nr_disks++; |
| 1171 | d->number = rdev2->desc_nr; |
| 1172 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1173 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1174 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | d->raid_disk = rdev2->raid_disk; |
| 1176 | else |
| 1177 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1178 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1180 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1182 | if (test_bit(In_sync, &rdev2->flags)) |
| 1183 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | active++; |
| 1185 | working++; |
| 1186 | } else { |
| 1187 | d->state = 0; |
| 1188 | spare++; |
| 1189 | working++; |
| 1190 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1191 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1192 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1195 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1196 | mdp_disk_t *d = &sb->disks[i]; |
| 1197 | if (d->state == 0 && d->number == 0) { |
| 1198 | d->number = i; |
| 1199 | d->raid_disk = i; |
| 1200 | d->state = (1<<MD_DISK_REMOVED); |
| 1201 | d->state |= (1<<MD_DISK_FAULTY); |
| 1202 | failed++; |
| 1203 | } |
| 1204 | } |
| 1205 | sb->nr_disks = nr_disks; |
| 1206 | sb->active_disks = active; |
| 1207 | sb->working_disks = working; |
| 1208 | sb->failed_disks = failed; |
| 1209 | sb->spare_disks = spare; |
| 1210 | |
| 1211 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1212 | sb->sb_csum = calc_sb_csum(sb); |
| 1213 | } |
| 1214 | |
| 1215 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1216 | * rdev_size_change for 0.90.0 |
| 1217 | */ |
| 1218 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1219 | 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] | 1220 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1221 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1222 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1223 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1224 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1225 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1226 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1227 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1228 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1229 | rdev->sb_page); |
| 1230 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1231 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | * version 1 superblock |
| 1237 | */ |
| 1238 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1239 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1241 | __le32 disk_csum; |
| 1242 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | unsigned long long newcsum; |
| 1244 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1245 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | int i; |
| 1247 | |
| 1248 | disk_csum = sb->sb_csum; |
| 1249 | sb->sb_csum = 0; |
| 1250 | newcsum = 0; |
| 1251 | for (i=0; size>=4; size -= 4 ) |
| 1252 | newcsum += le32_to_cpu(*isuper++); |
| 1253 | |
| 1254 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1255 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
| 1257 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1258 | sb->sb_csum = disk_csum; |
| 1259 | return cpu_to_le32(csum); |
| 1260 | } |
| 1261 | |
| 1262 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1263 | { |
| 1264 | struct mdp_superblock_1 *sb; |
| 1265 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1266 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1268 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
| 1270 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1271 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | * It is always aligned to a 4K boundary and |
| 1273 | * depeding on minor_version, it can be: |
| 1274 | * 0: At least 8K, but less than 12K, from end of device |
| 1275 | * 1: At start of device |
| 1276 | * 2: 4K from start of device. |
| 1277 | */ |
| 1278 | switch(minor_version) { |
| 1279 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1280 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1281 | sb_start -= 8*2; |
| 1282 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | break; |
| 1284 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1285 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | break; |
| 1287 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1288 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | break; |
| 1290 | default: |
| 1291 | return -EINVAL; |
| 1292 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1293 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1295 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1296 | * and it is safe to read 4k, so we do that |
| 1297 | */ |
| 1298 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | if (ret) return ret; |
| 1300 | |
| 1301 | |
| 1302 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1303 | |
| 1304 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1305 | sb->major_version != cpu_to_le32(1) || |
| 1306 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1307 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1308 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | return -EINVAL; |
| 1310 | |
| 1311 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1312 | printk("md: invalid superblock checksum on %s\n", |
| 1313 | bdevname(rdev->bdev,b)); |
| 1314 | return -EINVAL; |
| 1315 | } |
| 1316 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1317 | printk("md: data_size too small on %s\n", |
| 1318 | bdevname(rdev->bdev,b)); |
| 1319 | return -EINVAL; |
| 1320 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | rdev->preferred_minor = 0xffff; |
| 1323 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1324 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1326 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1327 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1328 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1329 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1330 | |
| 1331 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1332 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1333 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1334 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1335 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1336 | rdev->desc_nr = -1; |
| 1337 | else |
| 1338 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1339 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1340 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1341 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1342 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | __u64 ev1, ev2; |
| 1344 | struct mdp_superblock_1 *refsb = |
| 1345 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1346 | |
| 1347 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1348 | sb->level != refsb->level || |
| 1349 | sb->layout != refsb->layout || |
| 1350 | sb->chunksize != refsb->chunksize) { |
| 1351 | printk(KERN_WARNING "md: %s has strangely different" |
| 1352 | " superblock to %s\n", |
| 1353 | bdevname(rdev->bdev,b), |
| 1354 | bdevname(refdev->bdev,b2)); |
| 1355 | return -EINVAL; |
| 1356 | } |
| 1357 | ev1 = le64_to_cpu(sb->events); |
| 1358 | ev2 = le64_to_cpu(refsb->events); |
| 1359 | |
| 1360 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1361 | ret = 1; |
| 1362 | else |
| 1363 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1365 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1366 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1367 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1369 | rdev->sectors = rdev->sb_start; |
| 1370 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1372 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1373 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1374 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1375 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1379 | { |
| 1380 | 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] | 1381 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1383 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1384 | clear_bit(Faulty, &rdev->flags); |
| 1385 | clear_bit(In_sync, &rdev->flags); |
| 1386 | clear_bit(WriteMostly, &rdev->flags); |
| 1387 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | if (mddev->raid_disks == 0) { |
| 1390 | mddev->major_version = 1; |
| 1391 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1392 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1393 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1395 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1396 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1397 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | mddev->layout = le32_to_cpu(sb->layout); |
| 1399 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1400 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1401 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1402 | mddev->bitmap_info.offset = 0; |
| 1403 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1406 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1407 | |
| 1408 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1409 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1410 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1411 | mddev->bitmap_info.file == NULL ) |
| 1412 | mddev->bitmap_info.offset = |
| 1413 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1414 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1415 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1416 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1417 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1418 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1419 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1420 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1421 | } else { |
| 1422 | mddev->reshape_position = MaxSector; |
| 1423 | mddev->delta_disks = 0; |
| 1424 | mddev->new_level = mddev->level; |
| 1425 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1426 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1429 | } else if (mddev->pers == NULL) { |
| 1430 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | ++ev1; |
| 1432 | if (ev1 < mddev->events) |
| 1433 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1434 | } else if (mddev->bitmap) { |
| 1435 | /* If adding to array with a bitmap, then we can accept an |
| 1436 | * older device, but not too old. |
| 1437 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1438 | if (ev1 < mddev->bitmap->events_cleared) |
| 1439 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1440 | } else { |
| 1441 | if (ev1 < mddev->events) |
| 1442 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1443 | return 0; |
| 1444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1446 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1447 | if (rdev->desc_nr < 0 || |
| 1448 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1449 | role = 0xffff; |
| 1450 | rdev->desc_nr = -1; |
| 1451 | } else |
| 1452 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | switch(role) { |
| 1454 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | break; |
| 1456 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1457 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | break; |
| 1459 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1460 | if ((le32_to_cpu(sb->feature_map) & |
| 1461 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1462 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1463 | else |
| 1464 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | rdev->raid_disk = role; |
| 1466 | break; |
| 1467 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1468 | if (sb->devflags & WriteMostly1) |
| 1469 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1470 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1471 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1477 | { |
| 1478 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | mdk_rdev_t *rdev2; |
| 1480 | int max_dev, i; |
| 1481 | /* make rdev->sb match mddev and rdev data. */ |
| 1482 | |
| 1483 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1484 | |
| 1485 | sb->feature_map = 0; |
| 1486 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1487 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1489 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1490 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1491 | |
| 1492 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1493 | sb->events = cpu_to_le64(mddev->events); |
| 1494 | if (mddev->in_sync) |
| 1495 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1496 | else |
| 1497 | sb->resync_offset = cpu_to_le64(0); |
| 1498 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1499 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1500 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1501 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1502 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1503 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1504 | sb->level = cpu_to_le32(mddev->level); |
| 1505 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1506 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1507 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1508 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1509 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1510 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1511 | |
| 1512 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1513 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1514 | sb->feature_map |= |
| 1515 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1516 | sb->recovery_offset = |
| 1517 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1520 | if (mddev->reshape_position != MaxSector) { |
| 1521 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1522 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1523 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1524 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1525 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1526 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1527 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1530 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | if (rdev2->desc_nr+1 > max_dev) |
| 1532 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1533 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1534 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1535 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1536 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1537 | rdev->sb_size = max_dev * 2 + 256; |
| 1538 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1539 | if (rdev->sb_size & bmask) |
| 1540 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | for (i=0; i<max_dev;i++) |
| 1543 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1544 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1545 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1547 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1549 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1551 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1552 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | else |
| 1554 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1555 | } |
| 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1558 | } |
| 1559 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1560 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1561 | 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] | 1562 | { |
| 1563 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1564 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1565 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1566 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1567 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1568 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1569 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1570 | max_sectors -= rdev->data_offset; |
| 1571 | if (!num_sectors || num_sectors > max_sectors) |
| 1572 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1573 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1574 | /* minor version 0 with bitmap we can't move */ |
| 1575 | return 0; |
| 1576 | } else { |
| 1577 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1578 | sector_t sb_start; |
| 1579 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1580 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1581 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1582 | if (!num_sectors || num_sectors > max_sectors) |
| 1583 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1584 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1585 | } |
| 1586 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1587 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1588 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1589 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1590 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1591 | rdev->sb_page); |
| 1592 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1593 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1596 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | [0] = { |
| 1598 | .name = "0.90.0", |
| 1599 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1600 | .load_super = super_90_load, |
| 1601 | .validate_super = super_90_validate, |
| 1602 | .sync_super = super_90_sync, |
| 1603 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | }, |
| 1605 | [1] = { |
| 1606 | .name = "md-1", |
| 1607 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1608 | .load_super = super_1_load, |
| 1609 | .validate_super = super_1_validate, |
| 1610 | .sync_super = super_1_sync, |
| 1611 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | }, |
| 1613 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
| 1615 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1616 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1617 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1619 | rcu_read_lock(); |
| 1620 | rdev_for_each_rcu(rdev, mddev1) |
| 1621 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1622 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1623 | rdev2->bdev->bd_contains) { |
| 1624 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1625 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1626 | } |
| 1627 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | static LIST_HEAD(pending_raid_disks); |
| 1632 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1633 | /* |
| 1634 | * Try to register data integrity profile for an mddev |
| 1635 | * |
| 1636 | * This is called when an array is started and after a disk has been kicked |
| 1637 | * from the array. It only succeeds if all working and active component devices |
| 1638 | * are integrity capable with matching profiles. |
| 1639 | */ |
| 1640 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1641 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1642 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1643 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1644 | if (list_empty(&mddev->disks)) |
| 1645 | return 0; /* nothing to do */ |
| 1646 | if (blk_get_integrity(mddev->gendisk)) |
| 1647 | return 0; /* already registered */ |
| 1648 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1649 | /* skip spares and non-functional disks */ |
| 1650 | if (test_bit(Faulty, &rdev->flags)) |
| 1651 | continue; |
| 1652 | if (rdev->raid_disk < 0) |
| 1653 | continue; |
| 1654 | /* |
| 1655 | * If at least one rdev is not integrity capable, we can not |
| 1656 | * enable data integrity for the md device. |
| 1657 | */ |
| 1658 | if (!bdev_get_integrity(rdev->bdev)) |
| 1659 | return -EINVAL; |
| 1660 | if (!reference) { |
| 1661 | /* Use the first rdev as the reference */ |
| 1662 | reference = rdev; |
| 1663 | continue; |
| 1664 | } |
| 1665 | /* does this rdev's profile match the reference profile? */ |
| 1666 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1667 | rdev->bdev->bd_disk) < 0) |
| 1668 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1669 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1670 | /* |
| 1671 | * All component devices are integrity capable and have matching |
| 1672 | * profiles, register the common profile for the md device. |
| 1673 | */ |
| 1674 | if (blk_integrity_register(mddev->gendisk, |
| 1675 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1676 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1677 | mdname(mddev)); |
| 1678 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1679 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1680 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1681 | mdname(mddev)); |
| 1682 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1683 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1684 | EXPORT_SYMBOL(md_integrity_register); |
| 1685 | |
| 1686 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1687 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1688 | { |
| 1689 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1690 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1691 | |
| 1692 | if (!bi_mddev) /* nothing to do */ |
| 1693 | return; |
| 1694 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1695 | return; |
| 1696 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1697 | rdev->bdev->bd_disk) >= 0) |
| 1698 | return; |
| 1699 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1700 | blk_integrity_unregister(mddev->gendisk); |
| 1701 | } |
| 1702 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1705 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1706 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1707 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1708 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1709 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
| 1711 | if (rdev->mddev) { |
| 1712 | MD_BUG(); |
| 1713 | return -EINVAL; |
| 1714 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1715 | |
| 1716 | /* prevent duplicates */ |
| 1717 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1718 | return -EEXIST; |
| 1719 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1720 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1721 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1722 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1723 | if (mddev->pers) { |
| 1724 | /* Cannot change size, so fail |
| 1725 | * If mddev->level <= 0, then we don't care |
| 1726 | * about aligning sizes (e.g. linear) |
| 1727 | */ |
| 1728 | if (mddev->level > 0) |
| 1729 | return -ENOSPC; |
| 1730 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1731 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
| 1734 | /* Verify rdev->desc_nr is unique. |
| 1735 | * If it is -1, assign a free number, else |
| 1736 | * check number is not in use |
| 1737 | */ |
| 1738 | if (rdev->desc_nr < 0) { |
| 1739 | int choice = 0; |
| 1740 | if (mddev->pers) choice = mddev->raid_disks; |
| 1741 | while (find_rdev_nr(mddev, choice)) |
| 1742 | choice++; |
| 1743 | rdev->desc_nr = choice; |
| 1744 | } else { |
| 1745 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1746 | return -EBUSY; |
| 1747 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1748 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1749 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1750 | mdname(mddev), mddev->max_disks); |
| 1751 | return -EBUSY; |
| 1752 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1753 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1754 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1755 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1758 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1759 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1760 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1761 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1762 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1763 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1764 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1765 | kobject_del(&rdev->kobj); |
| 1766 | goto fail; |
| 1767 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1768 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1769 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1770 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1771 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1772 | |
| 1773 | /* May as well allow recovery to be retried once */ |
| 1774 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1777 | |
| 1778 | fail: |
| 1779 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1780 | b, mdname(mddev)); |
| 1781 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1784 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1785 | { |
| 1786 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1787 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1788 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1792 | { |
| 1793 | char b[BDEVNAME_SIZE]; |
| 1794 | if (!rdev->mddev) { |
| 1795 | MD_BUG(); |
| 1796 | return; |
| 1797 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1798 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1799 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1801 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1802 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1803 | sysfs_put(rdev->sysfs_state); |
| 1804 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1805 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1806 | * writing to 'remove' to "dev/state". We also need |
| 1807 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1808 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1809 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1810 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1811 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1812 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /* |
| 1816 | * prevent the device from being mounted, repartitioned or |
| 1817 | * otherwise reused by a RAID array (or any other kernel |
| 1818 | * subsystem), by bd_claiming the device. |
| 1819 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 1822 | int err = 0; |
| 1823 | struct block_device *bdev; |
| 1824 | char b[BDEVNAME_SIZE]; |
| 1825 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1826 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | if (IS_ERR(bdev)) { |
| 1828 | printk(KERN_ERR "md: could not open %s.\n", |
| 1829 | __bdevname(dev, b)); |
| 1830 | return PTR_ERR(bdev); |
| 1831 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1832 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | if (err) { |
| 1834 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1835 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1836 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | return err; |
| 1838 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1839 | if (!shared) |
| 1840 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | rdev->bdev = bdev; |
| 1842 | return err; |
| 1843 | } |
| 1844 | |
| 1845 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1846 | { |
| 1847 | struct block_device *bdev = rdev->bdev; |
| 1848 | rdev->bdev = NULL; |
| 1849 | if (!bdev) |
| 1850 | MD_BUG(); |
| 1851 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1852 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | void md_autodetect_dev(dev_t dev); |
| 1856 | |
| 1857 | static void export_rdev(mdk_rdev_t * rdev) |
| 1858 | { |
| 1859 | char b[BDEVNAME_SIZE]; |
| 1860 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1861 | bdevname(rdev->bdev,b)); |
| 1862 | if (rdev->mddev) |
| 1863 | MD_BUG(); |
| 1864 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1866 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1867 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | #endif |
| 1869 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1870 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1874 | { |
| 1875 | unbind_rdev_from_array(rdev); |
| 1876 | export_rdev(rdev); |
| 1877 | } |
| 1878 | |
| 1879 | static void export_array(mddev_t *mddev) |
| 1880 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1881 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1883 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | if (!rdev->mddev) { |
| 1885 | MD_BUG(); |
| 1886 | continue; |
| 1887 | } |
| 1888 | kick_rdev_from_array(rdev); |
| 1889 | } |
| 1890 | if (!list_empty(&mddev->disks)) |
| 1891 | MD_BUG(); |
| 1892 | mddev->raid_disks = 0; |
| 1893 | mddev->major_version = 0; |
| 1894 | } |
| 1895 | |
| 1896 | static void print_desc(mdp_disk_t *desc) |
| 1897 | { |
| 1898 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1899 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1900 | } |
| 1901 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1902 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | { |
| 1904 | int i; |
| 1905 | |
| 1906 | printk(KERN_INFO |
| 1907 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1908 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1909 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1910 | sb->ctime); |
| 1911 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1912 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1913 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1914 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1915 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1916 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1917 | sb->failed_disks, sb->spare_disks, |
| 1918 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1919 | |
| 1920 | printk(KERN_INFO); |
| 1921 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1922 | mdp_disk_t *desc; |
| 1923 | |
| 1924 | desc = sb->disks + i; |
| 1925 | if (desc->number || desc->major || desc->minor || |
| 1926 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1927 | printk(" D %2d: ", i); |
| 1928 | print_desc(desc); |
| 1929 | } |
| 1930 | } |
| 1931 | printk(KERN_INFO "md: THIS: "); |
| 1932 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1935 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1936 | { |
| 1937 | __u8 *uuid; |
| 1938 | |
| 1939 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1940 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1941 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1942 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1943 | le32_to_cpu(sb->major_version), |
| 1944 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1945 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1946 | sb->set_name, |
| 1947 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1948 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1949 | |
| 1950 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1951 | printk(KERN_INFO |
| 1952 | "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] | 1953 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1954 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1955 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1956 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1957 | le32_to_cpu(sb->level), |
| 1958 | (unsigned long long)le64_to_cpu(sb->size), |
| 1959 | le32_to_cpu(sb->raid_disks), |
| 1960 | le32_to_cpu(sb->layout), |
| 1961 | le32_to_cpu(sb->chunksize), |
| 1962 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1963 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1964 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1965 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1966 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1967 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1968 | sb->devflags, |
| 1969 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1970 | (unsigned long long)le64_to_cpu(sb->events), |
| 1971 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1972 | le32_to_cpu(sb->sb_csum), |
| 1973 | le32_to_cpu(sb->max_dev) |
| 1974 | ); |
| 1975 | } |
| 1976 | |
| 1977 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | { |
| 1979 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1980 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1981 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1982 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1983 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1985 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1986 | switch (major_version) { |
| 1987 | case 0: |
| 1988 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1989 | break; |
| 1990 | case 1: |
| 1991 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1992 | break; |
| 1993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | } else |
| 1995 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1996 | } |
| 1997 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1998 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2000 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | mdk_rdev_t *rdev; |
| 2002 | mddev_t *mddev; |
| 2003 | char b[BDEVNAME_SIZE]; |
| 2004 | |
| 2005 | printk("\n"); |
| 2006 | printk("md: **********************************\n"); |
| 2007 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2008 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2009 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2011 | if (mddev->bitmap) |
| 2012 | bitmap_print_sb(mddev->bitmap); |
| 2013 | else |
| 2014 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2015 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2017 | printk("\n"); |
| 2018 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2019 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2020 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
| 2022 | printk("md: **********************************\n"); |
| 2023 | printk("\n"); |
| 2024 | } |
| 2025 | |
| 2026 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2027 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2029 | /* Update each superblock (in-memory image), but |
| 2030 | * if we are allowed to, skip spares which already |
| 2031 | * have the right event counter, or have one earlier |
| 2032 | * (which would mean they aren't being marked as dirty |
| 2033 | * with the rest of the array) |
| 2034 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2037 | /* First make sure individual recovery_offsets are correct */ |
| 2038 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2039 | if (rdev->raid_disk >= 0 && |
| 2040 | !test_bit(In_sync, &rdev->flags) && |
| 2041 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2042 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2043 | |
| 2044 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2045 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2046 | if (rdev->sb_events == mddev->events || |
| 2047 | (nospares && |
| 2048 | rdev->raid_disk < 0 && |
| 2049 | (rdev->sb_events&1)==0 && |
| 2050 | rdev->sb_events+1 == mddev->events)) { |
| 2051 | /* Don't update this superblock */ |
| 2052 | rdev->sb_loaded = 2; |
| 2053 | } else { |
| 2054 | super_types[mddev->major_version]. |
| 2055 | sync_super(mddev, rdev); |
| 2056 | rdev->sb_loaded = 1; |
| 2057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2061 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2064 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2065 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2067 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2068 | if (mddev->external) |
| 2069 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2071 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2072 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2073 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2074 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2075 | force_change = 1; |
| 2076 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2077 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2078 | * though if events isn't the right even/odd, we will have to do |
| 2079 | * spares after all |
| 2080 | */ |
| 2081 | nospares = 1; |
| 2082 | if (force_change) |
| 2083 | nospares = 0; |
| 2084 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2085 | /* If the array is degraded, then skipping spares is both |
| 2086 | * dangerous and fairly pointless. |
| 2087 | * Dangerous because a device that was removed from the array |
| 2088 | * might have a event_count that still looks up-to-date, |
| 2089 | * so it can be re-added without a resync. |
| 2090 | * Pointless because if there are any spares to skip, |
| 2091 | * then a recovery will happen and soon that array won't |
| 2092 | * be degraded any more and the spare can go back to sleep then. |
| 2093 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2094 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2095 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2096 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2097 | |
| 2098 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2099 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2100 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2101 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2102 | && (mddev->events & 1) |
| 2103 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2104 | mddev->events--; |
| 2105 | else { |
| 2106 | /* otherwise we have to go forward and ... */ |
| 2107 | mddev->events ++; |
| 2108 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2109 | /* .. if the array isn't clean, an 'even' event must also go |
| 2110 | * to spares. */ |
| 2111 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2112 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2113 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2114 | /* otherwise an 'odd' event must go to spares */ |
| 2115 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2116 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | if (!mddev->events) { |
| 2121 | /* |
| 2122 | * oops, this 64-bit counter should never wrap. |
| 2123 | * Either we are in around ~1 trillion A.C., assuming |
| 2124 | * 1 reboot per second, or we have a bug: |
| 2125 | */ |
| 2126 | MD_BUG(); |
| 2127 | mddev->events --; |
| 2128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | /* |
| 2131 | * do not write anything to disk if using |
| 2132 | * nonpersistent superblocks |
| 2133 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2134 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2135 | if (!mddev->external) |
| 2136 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2137 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2138 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2139 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2141 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2142 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2143 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | |
| 2145 | dprintk(KERN_INFO |
| 2146 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2147 | mdname(mddev),mddev->in_sync); |
| 2148 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2149 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2150 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | char b[BDEVNAME_SIZE]; |
| 2152 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2153 | if (rdev->sb_loaded != 1) |
| 2154 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2155 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | dprintk("(skipping faulty "); |
| 2157 | |
| 2158 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2159 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2160 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2161 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2162 | rdev->sb_page); |
| 2163 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2164 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2165 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2166 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } else |
| 2169 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2170 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | /* only need to write one superblock... */ |
| 2172 | break; |
| 2173 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2174 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2175 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2176 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2177 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2178 | if (mddev->in_sync != sync_req || |
| 2179 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2180 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2181 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2182 | goto repeat; |
| 2183 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2184 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2185 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2186 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2187 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2188 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2192 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2193 | * We want to accept with case. For this we use cmd_match. |
| 2194 | */ |
| 2195 | static int cmd_match(const char *cmd, const char *str) |
| 2196 | { |
| 2197 | /* See if cmd, written into a sysfs file, matches |
| 2198 | * str. They must either be the same, or cmd can |
| 2199 | * have a trailing newline |
| 2200 | */ |
| 2201 | while (*cmd && *str && *cmd == *str) { |
| 2202 | cmd++; |
| 2203 | str++; |
| 2204 | } |
| 2205 | if (*cmd == '\n') |
| 2206 | cmd++; |
| 2207 | if (*str || *cmd) |
| 2208 | return 0; |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2212 | struct rdev_sysfs_entry { |
| 2213 | struct attribute attr; |
| 2214 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2215 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2216 | }; |
| 2217 | |
| 2218 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2219 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2220 | { |
| 2221 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2222 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2223 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2224 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2225 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2226 | sep = ","; |
| 2227 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2228 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2229 | len += sprintf(page+len, "%sin_sync",sep); |
| 2230 | sep = ","; |
| 2231 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2232 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2233 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2234 | sep = ","; |
| 2235 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2236 | if (test_bit(Blocked, &rdev->flags)) { |
| 2237 | len += sprintf(page+len, "%sblocked", sep); |
| 2238 | sep = ","; |
| 2239 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2240 | if (!test_bit(Faulty, &rdev->flags) && |
| 2241 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2242 | len += sprintf(page+len, "%sspare", sep); |
| 2243 | sep = ","; |
| 2244 | } |
| 2245 | return len+sprintf(page+len, "\n"); |
| 2246 | } |
| 2247 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2248 | static ssize_t |
| 2249 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2250 | { |
| 2251 | /* can write |
| 2252 | * faulty - simulates and error |
| 2253 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2254 | * writemostly - sets write_mostly |
| 2255 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2256 | * blocked - sets the Blocked flag |
| 2257 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2258 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2259 | */ |
| 2260 | int err = -EINVAL; |
| 2261 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2262 | md_error(rdev->mddev, rdev); |
| 2263 | err = 0; |
| 2264 | } else if (cmd_match(buf, "remove")) { |
| 2265 | if (rdev->raid_disk >= 0) |
| 2266 | err = -EBUSY; |
| 2267 | else { |
| 2268 | mddev_t *mddev = rdev->mddev; |
| 2269 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2270 | if (mddev->pers) |
| 2271 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2272 | md_new_event(mddev); |
| 2273 | err = 0; |
| 2274 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2275 | } else if (cmd_match(buf, "writemostly")) { |
| 2276 | set_bit(WriteMostly, &rdev->flags); |
| 2277 | err = 0; |
| 2278 | } else if (cmd_match(buf, "-writemostly")) { |
| 2279 | clear_bit(WriteMostly, &rdev->flags); |
| 2280 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2281 | } else if (cmd_match(buf, "blocked")) { |
| 2282 | set_bit(Blocked, &rdev->flags); |
| 2283 | err = 0; |
| 2284 | } else if (cmd_match(buf, "-blocked")) { |
| 2285 | clear_bit(Blocked, &rdev->flags); |
| 2286 | wake_up(&rdev->blocked_wait); |
| 2287 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2288 | md_wakeup_thread(rdev->mddev->thread); |
| 2289 | |
| 2290 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2291 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2292 | set_bit(In_sync, &rdev->flags); |
| 2293 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2294 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2295 | if (!err && rdev->sysfs_state) |
| 2296 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2297 | return err ? err : len; |
| 2298 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2299 | static struct rdev_sysfs_entry rdev_state = |
| 2300 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2301 | |
| 2302 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2303 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2304 | { |
| 2305 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2306 | } |
| 2307 | |
| 2308 | static ssize_t |
| 2309 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2310 | { |
| 2311 | char *e; |
| 2312 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2313 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2314 | atomic_set(&rdev->corrected_errors, n); |
| 2315 | return len; |
| 2316 | } |
| 2317 | return -EINVAL; |
| 2318 | } |
| 2319 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2320 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2321 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2322 | static ssize_t |
| 2323 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2324 | { |
| 2325 | if (rdev->raid_disk < 0) |
| 2326 | return sprintf(page, "none\n"); |
| 2327 | else |
| 2328 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2329 | } |
| 2330 | |
| 2331 | static ssize_t |
| 2332 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2333 | { |
| 2334 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2335 | int err; |
| 2336 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2337 | int slot = simple_strtoul(buf, &e, 10); |
| 2338 | if (strncmp(buf, "none", 4)==0) |
| 2339 | slot = -1; |
| 2340 | else if (e==buf || (*e && *e!= '\n')) |
| 2341 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2342 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2343 | /* Setting 'slot' on an active array requires also |
| 2344 | * updating the 'rd%d' link, and communicating |
| 2345 | * with the personality with ->hot_*_disk. |
| 2346 | * For now we only support removing |
| 2347 | * failed/spare devices. This normally happens automatically, |
| 2348 | * but not when the metadata is externally managed. |
| 2349 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2350 | if (rdev->raid_disk == -1) |
| 2351 | return -EEXIST; |
| 2352 | /* personality does all needed checks */ |
| 2353 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2354 | return -EINVAL; |
| 2355 | err = rdev->mddev->pers-> |
| 2356 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2357 | if (err) |
| 2358 | return err; |
| 2359 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2360 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2361 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2362 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2363 | } else if (rdev->mddev->pers) { |
| 2364 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2365 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2366 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2367 | */ |
| 2368 | |
| 2369 | if (rdev->raid_disk != -1) |
| 2370 | return -EBUSY; |
| 2371 | |
| 2372 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2373 | return -EINVAL; |
| 2374 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2375 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2376 | if (rdev2->raid_disk == slot) |
| 2377 | return -EEXIST; |
| 2378 | |
| 2379 | rdev->raid_disk = slot; |
| 2380 | if (test_bit(In_sync, &rdev->flags)) |
| 2381 | rdev->saved_raid_disk = slot; |
| 2382 | else |
| 2383 | rdev->saved_raid_disk = -1; |
| 2384 | err = rdev->mddev->pers-> |
| 2385 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2386 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2387 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2388 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2389 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2390 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2391 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2392 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2393 | printk(KERN_WARNING |
| 2394 | "md: cannot register " |
| 2395 | "%s for %s\n", |
| 2396 | nm, mdname(rdev->mddev)); |
| 2397 | |
| 2398 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2399 | } else { |
| 2400 | if (slot >= rdev->mddev->raid_disks) |
| 2401 | return -ENOSPC; |
| 2402 | rdev->raid_disk = slot; |
| 2403 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2404 | clear_bit(Faulty, &rdev->flags); |
| 2405 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2406 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2407 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2408 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2409 | return len; |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2414 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2415 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2416 | static ssize_t |
| 2417 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2418 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2419 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | static ssize_t |
| 2423 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2424 | { |
| 2425 | char *e; |
| 2426 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2427 | if (e==buf || (*e && *e != '\n')) |
| 2428 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2429 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2430 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2431 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2432 | /* Must set offset before size, so overlap checks |
| 2433 | * can be sane */ |
| 2434 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2435 | rdev->data_offset = offset; |
| 2436 | return len; |
| 2437 | } |
| 2438 | |
| 2439 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2440 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2441 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2442 | static ssize_t |
| 2443 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2444 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2445 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2446 | } |
| 2447 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2448 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2449 | { |
| 2450 | /* check if two start/length pairs overlap */ |
| 2451 | if (s1+l1 <= s2) |
| 2452 | return 0; |
| 2453 | if (s2+l2 <= s1) |
| 2454 | return 0; |
| 2455 | return 1; |
| 2456 | } |
| 2457 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2458 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2459 | { |
| 2460 | unsigned long long blocks; |
| 2461 | sector_t new; |
| 2462 | |
| 2463 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2464 | return -EINVAL; |
| 2465 | |
| 2466 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2467 | return -EINVAL; /* sector conversion overflow */ |
| 2468 | |
| 2469 | new = blocks * 2; |
| 2470 | if (new != blocks * 2) |
| 2471 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2472 | |
| 2473 | *sectors = new; |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2477 | static ssize_t |
| 2478 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2479 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2480 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2481 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2482 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2483 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2484 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2485 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2486 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2487 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2488 | sectors = super_types[my_mddev->major_version]. |
| 2489 | rdev_size_change(rdev, sectors); |
| 2490 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2491 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2492 | } else if (!sectors) |
| 2493 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2494 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2495 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2496 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2497 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2498 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2499 | rdev->sectors = sectors; |
| 2500 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2501 | /* need to check that all other rdevs with the same ->bdev |
| 2502 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2503 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2504 | * we have to change it back, we will have the lock again. |
| 2505 | */ |
| 2506 | mddev_t *mddev; |
| 2507 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2508 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2509 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2510 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2511 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2512 | mdk_rdev_t *rdev2; |
| 2513 | |
| 2514 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2515 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2516 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2517 | (rdev->bdev == rdev2->bdev && |
| 2518 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2519 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2520 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2521 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2522 | overlap = 1; |
| 2523 | break; |
| 2524 | } |
| 2525 | mddev_unlock(mddev); |
| 2526 | if (overlap) { |
| 2527 | mddev_put(mddev); |
| 2528 | break; |
| 2529 | } |
| 2530 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2531 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2532 | if (overlap) { |
| 2533 | /* Someone else could have slipped in a size |
| 2534 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2535 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2536 | * safe, and trust userspace not to race with |
| 2537 | * itself |
| 2538 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2539 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2540 | return -EBUSY; |
| 2541 | } |
| 2542 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2543 | return len; |
| 2544 | } |
| 2545 | |
| 2546 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2547 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2548 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2549 | |
| 2550 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2551 | { |
| 2552 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2553 | |
| 2554 | if (test_bit(In_sync, &rdev->flags) || |
| 2555 | recovery_start == MaxSector) |
| 2556 | return sprintf(page, "none\n"); |
| 2557 | |
| 2558 | return sprintf(page, "%llu\n", recovery_start); |
| 2559 | } |
| 2560 | |
| 2561 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2562 | { |
| 2563 | unsigned long long recovery_start; |
| 2564 | |
| 2565 | if (cmd_match(buf, "none")) |
| 2566 | recovery_start = MaxSector; |
| 2567 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2568 | return -EINVAL; |
| 2569 | |
| 2570 | if (rdev->mddev->pers && |
| 2571 | rdev->raid_disk >= 0) |
| 2572 | return -EBUSY; |
| 2573 | |
| 2574 | rdev->recovery_offset = recovery_start; |
| 2575 | if (recovery_start == MaxSector) |
| 2576 | set_bit(In_sync, &rdev->flags); |
| 2577 | else |
| 2578 | clear_bit(In_sync, &rdev->flags); |
| 2579 | return len; |
| 2580 | } |
| 2581 | |
| 2582 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2583 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2584 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2585 | static struct attribute *rdev_default_attrs[] = { |
| 2586 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2587 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2588 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2589 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2590 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2591 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2592 | NULL, |
| 2593 | }; |
| 2594 | static ssize_t |
| 2595 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2596 | { |
| 2597 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2598 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2599 | mddev_t *mddev = rdev->mddev; |
| 2600 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2601 | |
| 2602 | if (!entry->show) |
| 2603 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2604 | |
| 2605 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2606 | if (!rv) { |
| 2607 | if (rdev->mddev == NULL) |
| 2608 | rv = -EBUSY; |
| 2609 | else |
| 2610 | rv = entry->show(rdev, page); |
| 2611 | mddev_unlock(mddev); |
| 2612 | } |
| 2613 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | static ssize_t |
| 2617 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2618 | const char *page, size_t length) |
| 2619 | { |
| 2620 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2621 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2622 | ssize_t rv; |
| 2623 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2624 | |
| 2625 | if (!entry->store) |
| 2626 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2627 | if (!capable(CAP_SYS_ADMIN)) |
| 2628 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2629 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2630 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2631 | if (rdev->mddev == NULL) |
| 2632 | rv = -EBUSY; |
| 2633 | else |
| 2634 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2635 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2636 | } |
| 2637 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | static void rdev_free(struct kobject *ko) |
| 2641 | { |
| 2642 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2643 | kfree(rdev); |
| 2644 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2645 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2646 | .show = rdev_attr_show, |
| 2647 | .store = rdev_attr_store, |
| 2648 | }; |
| 2649 | static struct kobj_type rdev_ktype = { |
| 2650 | .release = rdev_free, |
| 2651 | .sysfs_ops = &rdev_sysfs_ops, |
| 2652 | .default_attrs = rdev_default_attrs, |
| 2653 | }; |
| 2654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | /* |
| 2656 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2657 | * |
| 2658 | * mark the device faulty if: |
| 2659 | * |
| 2660 | * - the device is nonexistent (zero size) |
| 2661 | * - the device has no valid superblock |
| 2662 | * |
| 2663 | * a faulty rdev _never_ has rdev->sb set. |
| 2664 | */ |
| 2665 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2666 | { |
| 2667 | char b[BDEVNAME_SIZE]; |
| 2668 | int err; |
| 2669 | mdk_rdev_t *rdev; |
| 2670 | sector_t size; |
| 2671 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2672 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | if (!rdev) { |
| 2674 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2675 | return ERR_PTR(-ENOMEM); |
| 2676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | |
| 2678 | if ((err = alloc_disk_sb(rdev))) |
| 2679 | goto abort_free; |
| 2680 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2681 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | if (err) |
| 2683 | goto abort_free; |
| 2684 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2685 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2688 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2689 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2690 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2692 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2693 | rdev->last_read_error.tv_sec = 0; |
| 2694 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2696 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2697 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | |
| 2699 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2700 | if (!size) { |
| 2701 | printk(KERN_WARNING |
| 2702 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2703 | bdevname(rdev->bdev,b)); |
| 2704 | err = -EINVAL; |
| 2705 | goto abort_free; |
| 2706 | } |
| 2707 | |
| 2708 | if (super_format >= 0) { |
| 2709 | err = super_types[super_format]. |
| 2710 | load_super(rdev, NULL, super_minor); |
| 2711 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2712 | printk(KERN_WARNING |
| 2713 | "md: %s does not have a valid v%d.%d " |
| 2714 | "superblock, not importing!\n", |
| 2715 | bdevname(rdev->bdev,b), |
| 2716 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | goto abort_free; |
| 2718 | } |
| 2719 | if (err < 0) { |
| 2720 | printk(KERN_WARNING |
| 2721 | "md: could not read %s's sb, not importing!\n", |
| 2722 | bdevname(rdev->bdev,b)); |
| 2723 | goto abort_free; |
| 2724 | } |
| 2725 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2728 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | |
| 2730 | return rdev; |
| 2731 | |
| 2732 | abort_free: |
| 2733 | if (rdev->sb_page) { |
| 2734 | if (rdev->bdev) |
| 2735 | unlock_rdev(rdev); |
| 2736 | free_disk_sb(rdev); |
| 2737 | } |
| 2738 | kfree(rdev); |
| 2739 | return ERR_PTR(err); |
| 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | * Check a full RAID array for plausibility |
| 2744 | */ |
| 2745 | |
| 2746 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2747 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | { |
| 2749 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2750 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | char b[BDEVNAME_SIZE]; |
| 2752 | |
| 2753 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2754 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | switch (super_types[mddev->major_version]. |
| 2756 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2757 | case 1: |
| 2758 | freshest = rdev; |
| 2759 | break; |
| 2760 | case 0: |
| 2761 | break; |
| 2762 | default: |
| 2763 | printk( KERN_ERR \ |
| 2764 | "md: fatal superblock inconsistency in %s" |
| 2765 | " -- removing from array\n", |
| 2766 | bdevname(rdev->bdev,b)); |
| 2767 | kick_rdev_from_array(rdev); |
| 2768 | } |
| 2769 | |
| 2770 | |
| 2771 | super_types[mddev->major_version]. |
| 2772 | validate_super(mddev, freshest); |
| 2773 | |
| 2774 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2775 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2776 | if (rdev->desc_nr >= mddev->max_disks || |
| 2777 | i > mddev->max_disks) { |
| 2778 | printk(KERN_WARNING |
| 2779 | "md: %s: %s: only %d devices permitted\n", |
| 2780 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2781 | mddev->max_disks); |
| 2782 | kick_rdev_from_array(rdev); |
| 2783 | continue; |
| 2784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | if (rdev != freshest) |
| 2786 | if (super_types[mddev->major_version]. |
| 2787 | validate_super(mddev, rdev)) { |
| 2788 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2789 | " from array!\n", |
| 2790 | bdevname(rdev->bdev,b)); |
| 2791 | kick_rdev_from_array(rdev); |
| 2792 | continue; |
| 2793 | } |
| 2794 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2795 | rdev->desc_nr = i++; |
| 2796 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2797 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2798 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2799 | rdev->raid_disk = -1; |
| 2800 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | } |
| 2802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | } |
| 2804 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2805 | /* Read a fixed-point number. |
| 2806 | * Numbers in sysfs attributes should be in "standard" units where |
| 2807 | * possible, so time should be in seconds. |
| 2808 | * However we internally use a a much smaller unit such as |
| 2809 | * milliseconds or jiffies. |
| 2810 | * This function takes a decimal number with a possible fractional |
| 2811 | * component, and produces an integer which is the result of |
| 2812 | * multiplying that number by 10^'scale'. |
| 2813 | * all without any floating-point arithmetic. |
| 2814 | */ |
| 2815 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2816 | { |
| 2817 | unsigned long result = 0; |
| 2818 | long decimals = -1; |
| 2819 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2820 | if (*cp == '.') |
| 2821 | decimals = 0; |
| 2822 | else if (decimals < scale) { |
| 2823 | unsigned int value; |
| 2824 | value = *cp - '0'; |
| 2825 | result = result * 10 + value; |
| 2826 | if (decimals >= 0) |
| 2827 | decimals++; |
| 2828 | } |
| 2829 | cp++; |
| 2830 | } |
| 2831 | if (*cp == '\n') |
| 2832 | cp++; |
| 2833 | if (*cp) |
| 2834 | return -EINVAL; |
| 2835 | if (decimals < 0) |
| 2836 | decimals = 0; |
| 2837 | while (decimals < scale) { |
| 2838 | result *= 10; |
| 2839 | decimals ++; |
| 2840 | } |
| 2841 | *res = result; |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2846 | static void md_safemode_timeout(unsigned long data); |
| 2847 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2848 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2849 | safe_delay_show(mddev_t *mddev, char *page) |
| 2850 | { |
| 2851 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2852 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2853 | } |
| 2854 | static ssize_t |
| 2855 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2856 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2857 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2858 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2859 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2860 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2861 | if (msec == 0) |
| 2862 | mddev->safemode_delay = 0; |
| 2863 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2864 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2865 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2866 | if (mddev->safemode_delay == 0) |
| 2867 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2868 | if (mddev->safemode_delay < old_delay) |
| 2869 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2870 | } |
| 2871 | return len; |
| 2872 | } |
| 2873 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2874 | __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] | 2875 | |
| 2876 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2877 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2878 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2879 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2880 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2881 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2882 | else if (mddev->clevel[0]) |
| 2883 | return sprintf(page, "%s\n", mddev->clevel); |
| 2884 | else if (mddev->level != LEVEL_NONE) |
| 2885 | return sprintf(page, "%d\n", mddev->level); |
| 2886 | else |
| 2887 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2888 | } |
| 2889 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2890 | static ssize_t |
| 2891 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2892 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2893 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2894 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2895 | struct mdk_personality *pers; |
| 2896 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2897 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2898 | |
| 2899 | if (mddev->pers == NULL) { |
| 2900 | if (len == 0) |
| 2901 | return 0; |
| 2902 | if (len >= sizeof(mddev->clevel)) |
| 2903 | return -ENOSPC; |
| 2904 | strncpy(mddev->clevel, buf, len); |
| 2905 | if (mddev->clevel[len-1] == '\n') |
| 2906 | len--; |
| 2907 | mddev->clevel[len] = 0; |
| 2908 | mddev->level = LEVEL_NONE; |
| 2909 | return rv; |
| 2910 | } |
| 2911 | |
| 2912 | /* request to change the personality. Need to ensure: |
| 2913 | * - array is not engaged in resync/recovery/reshape |
| 2914 | * - old personality can be suspended |
| 2915 | * - new personality will access other array. |
| 2916 | */ |
| 2917 | |
| 2918 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2919 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2920 | |
| 2921 | if (!mddev->pers->quiesce) { |
| 2922 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2923 | mdname(mddev), mddev->pers->name); |
| 2924 | return -EINVAL; |
| 2925 | } |
| 2926 | |
| 2927 | /* Now find the new personality */ |
| 2928 | if (len == 0 || len >= sizeof(level)) |
| 2929 | return -EINVAL; |
| 2930 | strncpy(level, buf, len); |
| 2931 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2932 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2933 | level[len] = 0; |
| 2934 | |
| 2935 | request_module("md-%s", level); |
| 2936 | spin_lock(&pers_lock); |
| 2937 | pers = find_pers(LEVEL_NONE, level); |
| 2938 | if (!pers || !try_module_get(pers->owner)) { |
| 2939 | spin_unlock(&pers_lock); |
| 2940 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2941 | return -EINVAL; |
| 2942 | } |
| 2943 | spin_unlock(&pers_lock); |
| 2944 | |
| 2945 | if (pers == mddev->pers) { |
| 2946 | /* Nothing to do! */ |
| 2947 | module_put(pers->owner); |
| 2948 | return rv; |
| 2949 | } |
| 2950 | if (!pers->takeover) { |
| 2951 | module_put(pers->owner); |
| 2952 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2953 | mdname(mddev), level); |
| 2954 | return -EINVAL; |
| 2955 | } |
| 2956 | |
| 2957 | /* ->takeover must set new_* and/or delta_disks |
| 2958 | * if it succeeds, and may set them when it fails. |
| 2959 | */ |
| 2960 | priv = pers->takeover(mddev); |
| 2961 | if (IS_ERR(priv)) { |
| 2962 | mddev->new_level = mddev->level; |
| 2963 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2964 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2965 | mddev->raid_disks -= mddev->delta_disks; |
| 2966 | mddev->delta_disks = 0; |
| 2967 | module_put(pers->owner); |
| 2968 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2969 | mdname(mddev), level); |
| 2970 | return PTR_ERR(priv); |
| 2971 | } |
| 2972 | |
| 2973 | /* Looks like we have a winner */ |
| 2974 | mddev_suspend(mddev); |
| 2975 | mddev->pers->stop(mddev); |
| 2976 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2977 | /* Invalidate devices that are now superfluous */ |
| 2978 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2979 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2980 | rdev->raid_disk = -1; |
| 2981 | clear_bit(In_sync, &rdev->flags); |
| 2982 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2983 | mddev->pers = pers; |
| 2984 | mddev->private = priv; |
| 2985 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2986 | mddev->level = mddev->new_level; |
| 2987 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2988 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2989 | mddev->delta_disks = 0; |
| 2990 | pers->run(mddev); |
| 2991 | mddev_resume(mddev); |
| 2992 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2993 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2994 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2995 | return rv; |
| 2996 | } |
| 2997 | |
| 2998 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2999 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3000 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3001 | |
| 3002 | static ssize_t |
| 3003 | layout_show(mddev_t *mddev, char *page) |
| 3004 | { |
| 3005 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3006 | if (mddev->reshape_position != MaxSector && |
| 3007 | mddev->layout != mddev->new_layout) |
| 3008 | return sprintf(page, "%d (%d)\n", |
| 3009 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3010 | return sprintf(page, "%d\n", mddev->layout); |
| 3011 | } |
| 3012 | |
| 3013 | static ssize_t |
| 3014 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3015 | { |
| 3016 | char *e; |
| 3017 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3018 | |
| 3019 | if (!*buf || (*e && *e != '\n')) |
| 3020 | return -EINVAL; |
| 3021 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3022 | if (mddev->pers) { |
| 3023 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3024 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3025 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3026 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3027 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3028 | if (err) { |
| 3029 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3030 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3031 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3032 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3033 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3034 | if (mddev->reshape_position == MaxSector) |
| 3035 | mddev->layout = n; |
| 3036 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3037 | return len; |
| 3038 | } |
| 3039 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3040 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3041 | |
| 3042 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3043 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3044 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3045 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3046 | if (mddev->raid_disks == 0) |
| 3047 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3048 | if (mddev->reshape_position != MaxSector && |
| 3049 | mddev->delta_disks != 0) |
| 3050 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3051 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3052 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3053 | } |
| 3054 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3055 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3056 | |
| 3057 | static ssize_t |
| 3058 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3059 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3060 | char *e; |
| 3061 | int rv = 0; |
| 3062 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3063 | |
| 3064 | if (!*buf || (*e && *e != '\n')) |
| 3065 | return -EINVAL; |
| 3066 | |
| 3067 | if (mddev->pers) |
| 3068 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3069 | else if (mddev->reshape_position != MaxSector) { |
| 3070 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3071 | mddev->delta_disks = n - olddisks; |
| 3072 | mddev->raid_disks = n; |
| 3073 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3074 | mddev->raid_disks = n; |
| 3075 | return rv ? rv : len; |
| 3076 | } |
| 3077 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3078 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3079 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3080 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3081 | chunk_size_show(mddev_t *mddev, char *page) |
| 3082 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3083 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3084 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3085 | return sprintf(page, "%d (%d)\n", |
| 3086 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3087 | mddev->chunk_sectors << 9); |
| 3088 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | static ssize_t |
| 3092 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3093 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3094 | char *e; |
| 3095 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3096 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3097 | if (!*buf || (*e && *e != '\n')) |
| 3098 | return -EINVAL; |
| 3099 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3100 | if (mddev->pers) { |
| 3101 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3102 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3103 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3104 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3105 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3106 | if (err) { |
| 3107 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3108 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3109 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3110 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3111 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3112 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3113 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3114 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3115 | return len; |
| 3116 | } |
| 3117 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3118 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3119 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3120 | static ssize_t |
| 3121 | resync_start_show(mddev_t *mddev, char *page) |
| 3122 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3123 | if (mddev->recovery_cp == MaxSector) |
| 3124 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3125 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3126 | } |
| 3127 | |
| 3128 | static ssize_t |
| 3129 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3130 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3131 | char *e; |
| 3132 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3133 | |
| 3134 | if (mddev->pers) |
| 3135 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3136 | if (cmd_match(buf, "none")) |
| 3137 | n = MaxSector; |
| 3138 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3139 | return -EINVAL; |
| 3140 | |
| 3141 | mddev->recovery_cp = n; |
| 3142 | return len; |
| 3143 | } |
| 3144 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3145 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3146 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3147 | /* |
| 3148 | * The array state can be: |
| 3149 | * |
| 3150 | * clear |
| 3151 | * No devices, no size, no level |
| 3152 | * Equivalent to STOP_ARRAY ioctl |
| 3153 | * inactive |
| 3154 | * May have some settings, but array is not active |
| 3155 | * all IO results in error |
| 3156 | * When written, doesn't tear down array, but just stops it |
| 3157 | * suspended (not supported yet) |
| 3158 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3159 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3160 | * readonly |
| 3161 | * no resync can happen. no superblocks get written. |
| 3162 | * write requests fail |
| 3163 | * read-auto |
| 3164 | * like readonly, but behaves like 'clean' on a write request. |
| 3165 | * |
| 3166 | * clean - no pending writes, but otherwise active. |
| 3167 | * When written to inactive array, starts without resync |
| 3168 | * If a write request arrives then |
| 3169 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3170 | * if not known, block and switch to write-pending |
| 3171 | * If written to an active array that has pending writes, then fails. |
| 3172 | * active |
| 3173 | * fully active: IO and resync can be happening. |
| 3174 | * When written to inactive array, starts with resync |
| 3175 | * |
| 3176 | * write-pending |
| 3177 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3178 | * |
| 3179 | * active-idle |
| 3180 | * like active, but no writes have been seen for a while (100msec). |
| 3181 | * |
| 3182 | */ |
| 3183 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3184 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3185 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3186 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3187 | "write-pending", "active-idle", NULL }; |
| 3188 | |
| 3189 | static int match_word(const char *word, char **list) |
| 3190 | { |
| 3191 | int n; |
| 3192 | for (n=0; list[n]; n++) |
| 3193 | if (cmd_match(word, list[n])) |
| 3194 | break; |
| 3195 | return n; |
| 3196 | } |
| 3197 | |
| 3198 | static ssize_t |
| 3199 | array_state_show(mddev_t *mddev, char *page) |
| 3200 | { |
| 3201 | enum array_state st = inactive; |
| 3202 | |
| 3203 | if (mddev->pers) |
| 3204 | switch(mddev->ro) { |
| 3205 | case 1: |
| 3206 | st = readonly; |
| 3207 | break; |
| 3208 | case 2: |
| 3209 | st = read_auto; |
| 3210 | break; |
| 3211 | case 0: |
| 3212 | if (mddev->in_sync) |
| 3213 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3214 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3215 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3216 | else if (mddev->safemode) |
| 3217 | st = active_idle; |
| 3218 | else |
| 3219 | st = active; |
| 3220 | } |
| 3221 | else { |
| 3222 | if (list_empty(&mddev->disks) && |
| 3223 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3224 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3225 | st = clear; |
| 3226 | else |
| 3227 | st = inactive; |
| 3228 | } |
| 3229 | return sprintf(page, "%s\n", array_states[st]); |
| 3230 | } |
| 3231 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3232 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3233 | static int do_md_run(mddev_t * mddev); |
| 3234 | static int restart_array(mddev_t *mddev); |
| 3235 | |
| 3236 | static ssize_t |
| 3237 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3238 | { |
| 3239 | int err = -EINVAL; |
| 3240 | enum array_state st = match_word(buf, array_states); |
| 3241 | switch(st) { |
| 3242 | case bad_word: |
| 3243 | break; |
| 3244 | case clear: |
| 3245 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3246 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3247 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3248 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3249 | break; |
| 3250 | case inactive: |
| 3251 | /* stopping an active array */ |
| 3252 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3253 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3254 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3255 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3256 | } else |
| 3257 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3258 | break; |
| 3259 | case suspended: |
| 3260 | break; /* not supported yet */ |
| 3261 | case readonly: |
| 3262 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3263 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3264 | else { |
| 3265 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3266 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3267 | err = do_md_run(mddev); |
| 3268 | } |
| 3269 | break; |
| 3270 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3271 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3272 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3273 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3274 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3275 | err = restart_array(mddev); |
| 3276 | if (err == 0) { |
| 3277 | mddev->ro = 2; |
| 3278 | set_disk_ro(mddev->gendisk, 0); |
| 3279 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3280 | } else { |
| 3281 | mddev->ro = 2; |
| 3282 | err = do_md_run(mddev); |
| 3283 | } |
| 3284 | break; |
| 3285 | case clean: |
| 3286 | if (mddev->pers) { |
| 3287 | restart_array(mddev); |
| 3288 | spin_lock_irq(&mddev->write_lock); |
| 3289 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3290 | if (mddev->in_sync == 0) { |
| 3291 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3292 | if (mddev->safemode == 1) |
| 3293 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3294 | if (mddev->persistent) |
| 3295 | set_bit(MD_CHANGE_CLEAN, |
| 3296 | &mddev->flags); |
| 3297 | } |
| 3298 | err = 0; |
| 3299 | } else |
| 3300 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3301 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3302 | } else |
| 3303 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3304 | break; |
| 3305 | case active: |
| 3306 | if (mddev->pers) { |
| 3307 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3308 | if (mddev->external) |
| 3309 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3310 | wake_up(&mddev->sb_wait); |
| 3311 | err = 0; |
| 3312 | } else { |
| 3313 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3314 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3315 | err = do_md_run(mddev); |
| 3316 | } |
| 3317 | break; |
| 3318 | case write_pending: |
| 3319 | case active_idle: |
| 3320 | /* these cannot be set */ |
| 3321 | break; |
| 3322 | } |
| 3323 | if (err) |
| 3324 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3325 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3326 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3327 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3328 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3329 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3330 | static struct md_sysfs_entry md_array_state = |
| 3331 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3332 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3333 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3334 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3335 | return sprintf(page, "%d\n", |
| 3336 | atomic_read(&mddev->max_corr_read_errors)); |
| 3337 | } |
| 3338 | |
| 3339 | static ssize_t |
| 3340 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3341 | { |
| 3342 | char *e; |
| 3343 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3344 | |
| 3345 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3346 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3347 | return len; |
| 3348 | } |
| 3349 | return -EINVAL; |
| 3350 | } |
| 3351 | |
| 3352 | static struct md_sysfs_entry max_corr_read_errors = |
| 3353 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3354 | max_corrected_read_errors_store); |
| 3355 | |
| 3356 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3357 | null_show(mddev_t *mddev, char *page) |
| 3358 | { |
| 3359 | return -EINVAL; |
| 3360 | } |
| 3361 | |
| 3362 | static ssize_t |
| 3363 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3364 | { |
| 3365 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3366 | /* The new device is added to the array. |
| 3367 | * If the array has a persistent superblock, we read the |
| 3368 | * superblock to initialise info and check validity. |
| 3369 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3370 | * which mainly checks size. |
| 3371 | */ |
| 3372 | char *e; |
| 3373 | int major = simple_strtoul(buf, &e, 10); |
| 3374 | int minor; |
| 3375 | dev_t dev; |
| 3376 | mdk_rdev_t *rdev; |
| 3377 | int err; |
| 3378 | |
| 3379 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3380 | return -EINVAL; |
| 3381 | minor = simple_strtoul(e+1, &e, 10); |
| 3382 | if (*e && *e != '\n') |
| 3383 | return -EINVAL; |
| 3384 | dev = MKDEV(major, minor); |
| 3385 | if (major != MAJOR(dev) || |
| 3386 | minor != MINOR(dev)) |
| 3387 | return -EOVERFLOW; |
| 3388 | |
| 3389 | |
| 3390 | if (mddev->persistent) { |
| 3391 | rdev = md_import_device(dev, mddev->major_version, |
| 3392 | mddev->minor_version); |
| 3393 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3394 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3395 | mdk_rdev_t, same_set); |
| 3396 | err = super_types[mddev->major_version] |
| 3397 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3398 | if (err < 0) |
| 3399 | goto out; |
| 3400 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3401 | } else if (mddev->external) |
| 3402 | rdev = md_import_device(dev, -2, -1); |
| 3403 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3404 | rdev = md_import_device(dev, -1, -1); |
| 3405 | |
| 3406 | if (IS_ERR(rdev)) |
| 3407 | return PTR_ERR(rdev); |
| 3408 | err = bind_rdev_to_array(rdev, mddev); |
| 3409 | out: |
| 3410 | if (err) |
| 3411 | export_rdev(rdev); |
| 3412 | return err ? err : len; |
| 3413 | } |
| 3414 | |
| 3415 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3416 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3417 | |
| 3418 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3419 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3420 | { |
| 3421 | char *end; |
| 3422 | unsigned long chunk, end_chunk; |
| 3423 | |
| 3424 | if (!mddev->bitmap) |
| 3425 | goto out; |
| 3426 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3427 | while (*buf) { |
| 3428 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3429 | if (buf == end) break; |
| 3430 | if (*end == '-') { /* range */ |
| 3431 | buf = end + 1; |
| 3432 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3433 | if (buf == end) break; |
| 3434 | } |
| 3435 | if (*end && !isspace(*end)) break; |
| 3436 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3437 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3438 | } |
| 3439 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3440 | out: |
| 3441 | return len; |
| 3442 | } |
| 3443 | |
| 3444 | static struct md_sysfs_entry md_bitmap = |
| 3445 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3446 | |
| 3447 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3448 | size_show(mddev_t *mddev, char *page) |
| 3449 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3450 | return sprintf(page, "%llu\n", |
| 3451 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3454 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3455 | |
| 3456 | static ssize_t |
| 3457 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3458 | { |
| 3459 | /* If array is inactive, we can reduce the component size, but |
| 3460 | * not increase it (except from 0). |
| 3461 | * If array is active, we can try an on-line resize |
| 3462 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3463 | sector_t sectors; |
| 3464 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3465 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3466 | if (err < 0) |
| 3467 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3468 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3469 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3470 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3471 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3472 | if (mddev->dev_sectors == 0 || |
| 3473 | mddev->dev_sectors > sectors) |
| 3474 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3475 | else |
| 3476 | err = -ENOSPC; |
| 3477 | } |
| 3478 | return err ? err : len; |
| 3479 | } |
| 3480 | |
| 3481 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3482 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3483 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3484 | |
| 3485 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3486 | * This is one of |
| 3487 | * 'none' for arrays with no metadata (good luck...) |
| 3488 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3489 | * or N.M for internally known formats |
| 3490 | */ |
| 3491 | static ssize_t |
| 3492 | metadata_show(mddev_t *mddev, char *page) |
| 3493 | { |
| 3494 | if (mddev->persistent) |
| 3495 | return sprintf(page, "%d.%d\n", |
| 3496 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3497 | else if (mddev->external) |
| 3498 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3499 | else |
| 3500 | return sprintf(page, "none\n"); |
| 3501 | } |
| 3502 | |
| 3503 | static ssize_t |
| 3504 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3505 | { |
| 3506 | int major, minor; |
| 3507 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3508 | /* Changing the details of 'external' metadata is |
| 3509 | * always permitted. Otherwise there must be |
| 3510 | * no devices attached to the array. |
| 3511 | */ |
| 3512 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3513 | ; |
| 3514 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3515 | return -EBUSY; |
| 3516 | |
| 3517 | if (cmd_match(buf, "none")) { |
| 3518 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3519 | mddev->external = 0; |
| 3520 | mddev->major_version = 0; |
| 3521 | mddev->minor_version = 90; |
| 3522 | return len; |
| 3523 | } |
| 3524 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3525 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3526 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3527 | namelen = sizeof(mddev->metadata_type)-1; |
| 3528 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3529 | mddev->metadata_type[namelen] = 0; |
| 3530 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3531 | mddev->metadata_type[--namelen] = 0; |
| 3532 | mddev->persistent = 0; |
| 3533 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3534 | mddev->major_version = 0; |
| 3535 | mddev->minor_version = 90; |
| 3536 | return len; |
| 3537 | } |
| 3538 | major = simple_strtoul(buf, &e, 10); |
| 3539 | if (e==buf || *e != '.') |
| 3540 | return -EINVAL; |
| 3541 | buf = e+1; |
| 3542 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3543 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3544 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3545 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3546 | return -ENOENT; |
| 3547 | mddev->major_version = major; |
| 3548 | mddev->minor_version = minor; |
| 3549 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3550 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3551 | return len; |
| 3552 | } |
| 3553 | |
| 3554 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3555 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3556 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3557 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3558 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3559 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3560 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3561 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3562 | type = "frozen"; |
| 3563 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3564 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3565 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3566 | type = "reshape"; |
| 3567 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3568 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3569 | type = "resync"; |
| 3570 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3571 | type = "check"; |
| 3572 | else |
| 3573 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3574 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3575 | type = "recover"; |
| 3576 | } |
| 3577 | return sprintf(page, "%s\n", type); |
| 3578 | } |
| 3579 | |
| 3580 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3581 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3582 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3583 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3584 | return -EINVAL; |
| 3585 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3586 | if (cmd_match(page, "frozen")) |
| 3587 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3588 | else |
| 3589 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3590 | |
| 3591 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3592 | if (mddev->sync_thread) { |
| 3593 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3594 | md_unregister_thread(mddev->sync_thread); |
| 3595 | mddev->sync_thread = NULL; |
| 3596 | mddev->recovery = 0; |
| 3597 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3598 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3599 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3600 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3601 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3602 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3603 | else if (cmd_match(page, "recover")) { |
| 3604 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3605 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3606 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3607 | int err; |
| 3608 | if (mddev->pers->start_reshape == NULL) |
| 3609 | return -EINVAL; |
| 3610 | err = mddev->pers->start_reshape(mddev); |
| 3611 | if (err) |
| 3612 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3613 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3614 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3615 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3616 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3617 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3618 | return -EINVAL; |
| 3619 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3620 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3621 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3622 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3623 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3624 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3625 | return len; |
| 3626 | } |
| 3627 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3628 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3629 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3630 | { |
| 3631 | return sprintf(page, "%llu\n", |
| 3632 | (unsigned long long) mddev->resync_mismatches); |
| 3633 | } |
| 3634 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3635 | static struct md_sysfs_entry md_scan_mode = |
| 3636 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3637 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3638 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3639 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3640 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3641 | static ssize_t |
| 3642 | sync_min_show(mddev_t *mddev, char *page) |
| 3643 | { |
| 3644 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3645 | mddev->sync_speed_min ? "local": "system"); |
| 3646 | } |
| 3647 | |
| 3648 | static ssize_t |
| 3649 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3650 | { |
| 3651 | int min; |
| 3652 | char *e; |
| 3653 | if (strncmp(buf, "system", 6)==0) { |
| 3654 | mddev->sync_speed_min = 0; |
| 3655 | return len; |
| 3656 | } |
| 3657 | min = simple_strtoul(buf, &e, 10); |
| 3658 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3659 | return -EINVAL; |
| 3660 | mddev->sync_speed_min = min; |
| 3661 | return len; |
| 3662 | } |
| 3663 | |
| 3664 | static struct md_sysfs_entry md_sync_min = |
| 3665 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3666 | |
| 3667 | static ssize_t |
| 3668 | sync_max_show(mddev_t *mddev, char *page) |
| 3669 | { |
| 3670 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3671 | mddev->sync_speed_max ? "local": "system"); |
| 3672 | } |
| 3673 | |
| 3674 | static ssize_t |
| 3675 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3676 | { |
| 3677 | int max; |
| 3678 | char *e; |
| 3679 | if (strncmp(buf, "system", 6)==0) { |
| 3680 | mddev->sync_speed_max = 0; |
| 3681 | return len; |
| 3682 | } |
| 3683 | max = simple_strtoul(buf, &e, 10); |
| 3684 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3685 | return -EINVAL; |
| 3686 | mddev->sync_speed_max = max; |
| 3687 | return len; |
| 3688 | } |
| 3689 | |
| 3690 | static struct md_sysfs_entry md_sync_max = |
| 3691 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3692 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3693 | static ssize_t |
| 3694 | degraded_show(mddev_t *mddev, char *page) |
| 3695 | { |
| 3696 | return sprintf(page, "%d\n", mddev->degraded); |
| 3697 | } |
| 3698 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3699 | |
| 3700 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3701 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3702 | { |
| 3703 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3704 | } |
| 3705 | |
| 3706 | static ssize_t |
| 3707 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3708 | { |
| 3709 | long n; |
| 3710 | |
| 3711 | if (strict_strtol(buf, 10, &n)) |
| 3712 | return -EINVAL; |
| 3713 | |
| 3714 | if (n != 0 && n != 1) |
| 3715 | return -EINVAL; |
| 3716 | |
| 3717 | mddev->parallel_resync = n; |
| 3718 | |
| 3719 | if (mddev->sync_thread) |
| 3720 | wake_up(&resync_wait); |
| 3721 | |
| 3722 | return len; |
| 3723 | } |
| 3724 | |
| 3725 | /* force parallel resync, even with shared block devices */ |
| 3726 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3727 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3728 | sync_force_parallel_show, sync_force_parallel_store); |
| 3729 | |
| 3730 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3731 | sync_speed_show(mddev_t *mddev, char *page) |
| 3732 | { |
| 3733 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3734 | if (mddev->curr_resync == 0) |
| 3735 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3736 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3737 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3738 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3739 | db = resync - mddev->resync_mark_cnt; |
| 3740 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3741 | } |
| 3742 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3743 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3744 | |
| 3745 | static ssize_t |
| 3746 | sync_completed_show(mddev_t *mddev, char *page) |
| 3747 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3748 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3749 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3750 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3751 | return sprintf(page, "none\n"); |
| 3752 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3753 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3754 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3755 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3756 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3757 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3758 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3759 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3762 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3763 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3764 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3765 | min_sync_show(mddev_t *mddev, char *page) |
| 3766 | { |
| 3767 | return sprintf(page, "%llu\n", |
| 3768 | (unsigned long long)mddev->resync_min); |
| 3769 | } |
| 3770 | static ssize_t |
| 3771 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3772 | { |
| 3773 | unsigned long long min; |
| 3774 | if (strict_strtoull(buf, 10, &min)) |
| 3775 | return -EINVAL; |
| 3776 | if (min > mddev->resync_max) |
| 3777 | return -EINVAL; |
| 3778 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3779 | return -EBUSY; |
| 3780 | |
| 3781 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3782 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3783 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3784 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3785 | return -EINVAL; |
| 3786 | } |
| 3787 | mddev->resync_min = min; |
| 3788 | |
| 3789 | return len; |
| 3790 | } |
| 3791 | |
| 3792 | static struct md_sysfs_entry md_min_sync = |
| 3793 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3794 | |
| 3795 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3796 | max_sync_show(mddev_t *mddev, char *page) |
| 3797 | { |
| 3798 | if (mddev->resync_max == MaxSector) |
| 3799 | return sprintf(page, "max\n"); |
| 3800 | else |
| 3801 | return sprintf(page, "%llu\n", |
| 3802 | (unsigned long long)mddev->resync_max); |
| 3803 | } |
| 3804 | static ssize_t |
| 3805 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3806 | { |
| 3807 | if (strncmp(buf, "max", 3) == 0) |
| 3808 | mddev->resync_max = MaxSector; |
| 3809 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3810 | unsigned long long max; |
| 3811 | if (strict_strtoull(buf, 10, &max)) |
| 3812 | return -EINVAL; |
| 3813 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3814 | return -EINVAL; |
| 3815 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3816 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3817 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3818 | return -EBUSY; |
| 3819 | |
| 3820 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3821 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3822 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3823 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3824 | return -EINVAL; |
| 3825 | } |
| 3826 | mddev->resync_max = max; |
| 3827 | } |
| 3828 | wake_up(&mddev->recovery_wait); |
| 3829 | return len; |
| 3830 | } |
| 3831 | |
| 3832 | static struct md_sysfs_entry md_max_sync = |
| 3833 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3834 | |
| 3835 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3836 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3837 | { |
| 3838 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3839 | } |
| 3840 | |
| 3841 | static ssize_t |
| 3842 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3843 | { |
| 3844 | char *e; |
| 3845 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3846 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3847 | if (mddev->pers == NULL || |
| 3848 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3849 | return -EINVAL; |
| 3850 | if (buf == e || (*e && *e != '\n')) |
| 3851 | return -EINVAL; |
| 3852 | if (new >= mddev->suspend_hi || |
| 3853 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3854 | mddev->suspend_lo = new; |
| 3855 | mddev->pers->quiesce(mddev, 2); |
| 3856 | return len; |
| 3857 | } else |
| 3858 | return -EINVAL; |
| 3859 | } |
| 3860 | static struct md_sysfs_entry md_suspend_lo = |
| 3861 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3862 | |
| 3863 | |
| 3864 | static ssize_t |
| 3865 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3866 | { |
| 3867 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3868 | } |
| 3869 | |
| 3870 | static ssize_t |
| 3871 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3872 | { |
| 3873 | char *e; |
| 3874 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3875 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3876 | if (mddev->pers == NULL || |
| 3877 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3878 | return -EINVAL; |
| 3879 | if (buf == e || (*e && *e != '\n')) |
| 3880 | return -EINVAL; |
| 3881 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3882 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3883 | mddev->suspend_hi = new; |
| 3884 | mddev->pers->quiesce(mddev, 1); |
| 3885 | mddev->pers->quiesce(mddev, 0); |
| 3886 | return len; |
| 3887 | } else |
| 3888 | return -EINVAL; |
| 3889 | } |
| 3890 | static struct md_sysfs_entry md_suspend_hi = |
| 3891 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3892 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3893 | static ssize_t |
| 3894 | reshape_position_show(mddev_t *mddev, char *page) |
| 3895 | { |
| 3896 | if (mddev->reshape_position != MaxSector) |
| 3897 | return sprintf(page, "%llu\n", |
| 3898 | (unsigned long long)mddev->reshape_position); |
| 3899 | strcpy(page, "none\n"); |
| 3900 | return 5; |
| 3901 | } |
| 3902 | |
| 3903 | static ssize_t |
| 3904 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3905 | { |
| 3906 | char *e; |
| 3907 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3908 | if (mddev->pers) |
| 3909 | return -EBUSY; |
| 3910 | if (buf == e || (*e && *e != '\n')) |
| 3911 | return -EINVAL; |
| 3912 | mddev->reshape_position = new; |
| 3913 | mddev->delta_disks = 0; |
| 3914 | mddev->new_level = mddev->level; |
| 3915 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3916 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3917 | return len; |
| 3918 | } |
| 3919 | |
| 3920 | static struct md_sysfs_entry md_reshape_position = |
| 3921 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3922 | reshape_position_store); |
| 3923 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3924 | static ssize_t |
| 3925 | array_size_show(mddev_t *mddev, char *page) |
| 3926 | { |
| 3927 | if (mddev->external_size) |
| 3928 | return sprintf(page, "%llu\n", |
| 3929 | (unsigned long long)mddev->array_sectors/2); |
| 3930 | else |
| 3931 | return sprintf(page, "default\n"); |
| 3932 | } |
| 3933 | |
| 3934 | static ssize_t |
| 3935 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3936 | { |
| 3937 | sector_t sectors; |
| 3938 | |
| 3939 | if (strncmp(buf, "default", 7) == 0) { |
| 3940 | if (mddev->pers) |
| 3941 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3942 | else |
| 3943 | sectors = mddev->array_sectors; |
| 3944 | |
| 3945 | mddev->external_size = 0; |
| 3946 | } else { |
| 3947 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3948 | return -EINVAL; |
| 3949 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3950 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3951 | |
| 3952 | mddev->external_size = 1; |
| 3953 | } |
| 3954 | |
| 3955 | mddev->array_sectors = sectors; |
| 3956 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3957 | if (mddev->pers) |
| 3958 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3959 | |
| 3960 | return len; |
| 3961 | } |
| 3962 | |
| 3963 | static struct md_sysfs_entry md_array_size = |
| 3964 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3965 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3966 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3967 | static struct attribute *md_default_attrs[] = { |
| 3968 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3969 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3970 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3971 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3972 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3973 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3974 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3975 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3976 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3978 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3979 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3980 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3981 | NULL, |
| 3982 | }; |
| 3983 | |
| 3984 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3985 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3986 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3987 | &md_sync_min.attr, |
| 3988 | &md_sync_max.attr, |
| 3989 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3990 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3991 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3992 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3993 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3994 | &md_suspend_lo.attr, |
| 3995 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3996 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3997 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3998 | NULL, |
| 3999 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4000 | static struct attribute_group md_redundancy_group = { |
| 4001 | .name = NULL, |
| 4002 | .attrs = md_redundancy_attrs, |
| 4003 | }; |
| 4004 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4005 | |
| 4006 | static ssize_t |
| 4007 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4008 | { |
| 4009 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4010 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4011 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4012 | |
| 4013 | if (!entry->show) |
| 4014 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4015 | rv = mddev_lock(mddev); |
| 4016 | if (!rv) { |
| 4017 | rv = entry->show(mddev, page); |
| 4018 | mddev_unlock(mddev); |
| 4019 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4020 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | static ssize_t |
| 4024 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4025 | const char *page, size_t length) |
| 4026 | { |
| 4027 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4028 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4029 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4030 | |
| 4031 | if (!entry->store) |
| 4032 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4033 | if (!capable(CAP_SYS_ADMIN)) |
| 4034 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4035 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4036 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4037 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4038 | if (!rv) { |
| 4039 | rv = entry->store(mddev, page, length); |
| 4040 | mddev_unlock(mddev); |
| 4041 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4042 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | static void md_free(struct kobject *ko) |
| 4046 | { |
| 4047 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4048 | |
| 4049 | if (mddev->sysfs_state) |
| 4050 | sysfs_put(mddev->sysfs_state); |
| 4051 | |
| 4052 | if (mddev->gendisk) { |
| 4053 | del_gendisk(mddev->gendisk); |
| 4054 | put_disk(mddev->gendisk); |
| 4055 | } |
| 4056 | if (mddev->queue) |
| 4057 | blk_cleanup_queue(mddev->queue); |
| 4058 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4059 | kfree(mddev); |
| 4060 | } |
| 4061 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4062 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4063 | .show = md_attr_show, |
| 4064 | .store = md_attr_store, |
| 4065 | }; |
| 4066 | static struct kobj_type md_ktype = { |
| 4067 | .release = md_free, |
| 4068 | .sysfs_ops = &md_sysfs_ops, |
| 4069 | .default_attrs = md_default_attrs, |
| 4070 | }; |
| 4071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | int mdp_major = 0; |
| 4073 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4074 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4075 | { |
| 4076 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4077 | |
NeilBrown | ef286f6 | 2010-02-09 16:34:14 +1100 | [diff] [blame] | 4078 | if (mddev->private) { |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4079 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
NeilBrown | ef286f6 | 2010-02-09 16:34:14 +1100 | [diff] [blame] | 4080 | if (mddev->private != (void*)1) |
| 4081 | sysfs_remove_group(&mddev->kobj, mddev->private); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4082 | if (mddev->sysfs_action) |
| 4083 | sysfs_put(mddev->sysfs_action); |
| 4084 | mddev->sysfs_action = NULL; |
| 4085 | mddev->private = NULL; |
| 4086 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4087 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4088 | kobject_del(&mddev->kobj); |
| 4089 | kobject_put(&mddev->kobj); |
| 4090 | } |
| 4091 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4092 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4094 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | mddev_t *mddev = mddev_find(dev); |
| 4096 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4097 | int partitioned; |
| 4098 | int shift; |
| 4099 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4100 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | |
| 4102 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4103 | return -ENODEV; |
| 4104 | |
| 4105 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4106 | shift = partitioned ? MdpMinorShift : 0; |
| 4107 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4109 | /* wait for any previous instance if this device |
| 4110 | * to be completed removed (mddev_delayed_delete). |
| 4111 | */ |
| 4112 | flush_scheduled_work(); |
| 4113 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4114 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4115 | error = -EEXIST; |
| 4116 | if (mddev->gendisk) |
| 4117 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4118 | |
| 4119 | if (name) { |
| 4120 | /* Need to ensure that 'name' is not a duplicate. |
| 4121 | */ |
| 4122 | mddev_t *mddev2; |
| 4123 | spin_lock(&all_mddevs_lock); |
| 4124 | |
| 4125 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4126 | if (mddev2->gendisk && |
| 4127 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4128 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4129 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4130 | } |
| 4131 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4133 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4134 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4135 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4136 | if (!mddev->queue) |
| 4137 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4138 | mddev->queue->queuedata = mddev; |
| 4139 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4140 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4141 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4142 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4143 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | disk = alloc_disk(1 << shift); |
| 4146 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4147 | blk_cleanup_queue(mddev->queue); |
| 4148 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4149 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4151 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4153 | if (name) |
| 4154 | strcpy(disk->disk_name, name); |
| 4155 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4157 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | disk->fops = &md_fops; |
| 4160 | disk->private_data = mddev; |
| 4161 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4162 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4163 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4164 | * remove it now. |
| 4165 | */ |
| 4166 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | add_disk(disk); |
| 4168 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4169 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4170 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4171 | if (error) { |
| 4172 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4173 | * __must_check, we must do something with the result |
| 4174 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4175 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4176 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4177 | error = 0; |
| 4178 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4179 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4180 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4181 | abort: |
| 4182 | mutex_unlock(&disks_mutex); |
| 4183 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4184 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4185 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4186 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4187 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4188 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4192 | { |
| 4193 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | return NULL; |
| 4195 | } |
| 4196 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4197 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4198 | { |
| 4199 | /* val must be "md_*" where * is not all digits. |
| 4200 | * We allocate an array with a large free minor number, and |
| 4201 | * set the name to val. val must not already be an active name. |
| 4202 | */ |
| 4203 | int len = strlen(val); |
| 4204 | char buf[DISK_NAME_LEN]; |
| 4205 | |
| 4206 | while (len && val[len-1] == '\n') |
| 4207 | len--; |
| 4208 | if (len >= DISK_NAME_LEN) |
| 4209 | return -E2BIG; |
| 4210 | strlcpy(buf, val, len+1); |
| 4211 | if (strncmp(buf, "md_", 3) != 0) |
| 4212 | return -EINVAL; |
| 4213 | return md_alloc(0, buf); |
| 4214 | } |
| 4215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | static void md_safemode_timeout(unsigned long data) |
| 4217 | { |
| 4218 | mddev_t *mddev = (mddev_t *) data; |
| 4219 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4220 | if (!atomic_read(&mddev->writes_pending)) { |
| 4221 | mddev->safemode = 1; |
| 4222 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4223 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | md_wakeup_thread(mddev->thread); |
| 4226 | } |
| 4227 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4228 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | |
| 4230 | static int do_md_run(mddev_t * mddev) |
| 4231 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4232 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | mdk_rdev_t *rdev; |
| 4234 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4235 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4237 | if (list_empty(&mddev->disks)) |
| 4238 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | |
| 4241 | if (mddev->pers) |
| 4242 | return -EBUSY; |
| 4243 | |
| 4244 | /* |
| 4245 | * Analyze all RAID superblock(s) |
| 4246 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4247 | if (!mddev->raid_disks) { |
| 4248 | if (!mddev->persistent) |
| 4249 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4250 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4253 | if (mddev->level != LEVEL_NONE) |
| 4254 | request_module("md-level-%d", mddev->level); |
| 4255 | else if (mddev->clevel[0]) |
| 4256 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
| 4258 | /* |
| 4259 | * Drop all container device buffers, from now on |
| 4260 | * the only valid external interface is through the md |
| 4261 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4263 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4264 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | continue; |
| 4266 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4267 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4268 | |
| 4269 | /* perform some consistency tests on the device. |
| 4270 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4271 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4272 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4273 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4274 | if (mddev->dev_sectors && |
| 4275 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4276 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4277 | printk("md: %s: data overlaps metadata\n", |
| 4278 | mdname(mddev)); |
| 4279 | return -EINVAL; |
| 4280 | } |
| 4281 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4282 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4283 | > rdev->data_offset) { |
| 4284 | printk("md: %s: metadata overlaps data\n", |
| 4285 | mdname(mddev)); |
| 4286 | return -EINVAL; |
| 4287 | } |
| 4288 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4289 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | |
| 4294 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4295 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4296 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4298 | if (mddev->level != LEVEL_NONE) |
| 4299 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4300 | mddev->level); |
| 4301 | else |
| 4302 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4303 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | return -EINVAL; |
| 4305 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4306 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4308 | if (mddev->level != pers->level) { |
| 4309 | mddev->level = pers->level; |
| 4310 | mddev->new_level = pers->level; |
| 4311 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4312 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4314 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4315 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4316 | /* This personality cannot handle reshaping... */ |
| 4317 | mddev->pers = NULL; |
| 4318 | module_put(pers->owner); |
| 4319 | return -EINVAL; |
| 4320 | } |
| 4321 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4322 | if (pers->sync_request) { |
| 4323 | /* Warn if this is a potentially silly |
| 4324 | * configuration. |
| 4325 | */ |
| 4326 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4327 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4328 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4329 | |
| 4330 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4331 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4332 | if (rdev < rdev2 && |
| 4333 | rdev->bdev->bd_contains == |
| 4334 | rdev2->bdev->bd_contains) { |
| 4335 | printk(KERN_WARNING |
| 4336 | "%s: WARNING: %s appears to be" |
| 4337 | " on the same physical disk as" |
| 4338 | " %s.\n", |
| 4339 | mdname(mddev), |
| 4340 | bdevname(rdev->bdev,b), |
| 4341 | bdevname(rdev2->bdev,b2)); |
| 4342 | warned = 1; |
| 4343 | } |
| 4344 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4345 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4346 | if (warned) |
| 4347 | printk(KERN_WARNING |
| 4348 | "True protection against single-disk" |
| 4349 | " failure might be compromised.\n"); |
| 4350 | } |
| 4351 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4352 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4353 | /* may be over-ridden by personality */ |
| 4354 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4355 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4356 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4357 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4359 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4360 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4361 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4362 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4363 | if (err) |
| 4364 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4365 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4366 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4367 | " but 'external_size' not in effect?\n", __func__); |
| 4368 | printk(KERN_ERR |
| 4369 | "md: invalid array_size %llu > default size %llu\n", |
| 4370 | (unsigned long long)mddev->array_sectors / 2, |
| 4371 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4372 | err = -EINVAL; |
| 4373 | mddev->pers->stop(mddev); |
| 4374 | } |
| 4375 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4376 | err = bitmap_create(mddev); |
| 4377 | if (err) { |
| 4378 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4379 | mdname(mddev), err); |
| 4380 | mddev->pers->stop(mddev); |
| 4381 | } |
| 4382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | module_put(mddev->pers->owner); |
| 4385 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4386 | bitmap_destroy(mddev); |
| 4387 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4389 | if (mddev->pers->sync_request) { |
| 4390 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4391 | printk(KERN_WARNING |
| 4392 | "md: cannot register extra attributes for %s\n", |
| 4393 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4394 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4395 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4396 | mddev->ro = 0; |
| 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4399 | atomic_set(&mddev->max_corr_read_errors, |
| 4400 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | mddev->safemode = 0; |
| 4402 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4403 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4404 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4406 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4407 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4408 | if (rdev->raid_disk >= 0) { |
| 4409 | char nm[20]; |
| 4410 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4411 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4412 | printk("md: cannot register %s for %s\n", |
| 4413 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | |
| 4416 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4417 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4418 | if (mddev->flags) |
| 4419 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4421 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4423 | md_wakeup_thread(mddev->thread); |
| 4424 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4425 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4426 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4427 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4428 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4429 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4430 | if (mddev->sysfs_action) |
| 4431 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4432 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4433 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | static int restart_array(mddev_t *mddev) |
| 4438 | { |
| 4439 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4441 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4443 | return -ENXIO; |
| 4444 | if (!mddev->pers) |
| 4445 | return -EINVAL; |
| 4446 | if (!mddev->ro) |
| 4447 | return -EBUSY; |
| 4448 | mddev->safemode = 0; |
| 4449 | mddev->ro = 0; |
| 4450 | set_disk_ro(disk, 0); |
| 4451 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4452 | mdname(mddev)); |
| 4453 | /* Kick recovery or resync if necessary */ |
| 4454 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4455 | md_wakeup_thread(mddev->thread); |
| 4456 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4457 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4458 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4461 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4462 | * to the file ourselves */ |
| 4463 | static int deny_bitmap_write_access(struct file * file) |
| 4464 | { |
| 4465 | struct inode *inode = file->f_mapping->host; |
| 4466 | |
| 4467 | spin_lock(&inode->i_lock); |
| 4468 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4469 | spin_unlock(&inode->i_lock); |
| 4470 | return -ETXTBSY; |
| 4471 | } |
| 4472 | atomic_set(&inode->i_writecount, -1); |
| 4473 | spin_unlock(&inode->i_lock); |
| 4474 | |
| 4475 | return 0; |
| 4476 | } |
| 4477 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4478 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4479 | { |
| 4480 | struct inode *inode = file->f_mapping->host; |
| 4481 | |
| 4482 | spin_lock(&inode->i_lock); |
| 4483 | atomic_set(&inode->i_writecount, 1); |
| 4484 | spin_unlock(&inode->i_lock); |
| 4485 | } |
| 4486 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4487 | /* mode: |
| 4488 | * 0 - completely stop and dis-assemble array |
| 4489 | * 1 - switch to readonly |
| 4490 | * 2 - stop but do not disassemble array |
| 4491 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4492 | 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] | 4493 | { |
| 4494 | int err = 0; |
| 4495 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4496 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4498 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4499 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4500 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4501 | err = -EBUSY; |
| 4502 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
| 4504 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4505 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4507 | md_unregister_thread(mddev->sync_thread); |
| 4508 | mddev->sync_thread = NULL; |
| 4509 | } |
| 4510 | |
| 4511 | del_timer_sync(&mddev->safemode_timer); |
| 4512 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4513 | switch(mode) { |
| 4514 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4516 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | goto out; |
| 4518 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4519 | break; |
| 4520 | case 0: /* disassemble */ |
| 4521 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4522 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4523 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | if (mddev->ro) |
| 4525 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4528 | mddev->queue->merge_bvec_fn = NULL; |
| 4529 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4530 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | module_put(mddev->pers->owner); |
NeilBrown | ef286f6 | 2010-02-09 16:34:14 +1100 | [diff] [blame] | 4532 | if (mddev->pers->sync_request && mddev->private == NULL) |
| 4533 | mddev->private = (void*)1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4535 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4536 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4537 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4538 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4539 | if (rdev->raid_disk >= 0) { |
| 4540 | char nm[20]; |
| 4541 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4542 | sysfs_remove_link(&mddev->kobj, nm); |
| 4543 | } |
| 4544 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4545 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4546 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | if (mddev->ro) |
| 4549 | mddev->ro = 0; |
| 4550 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4551 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | /* mark array as shutdown cleanly */ |
| 4553 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4554 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4556 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4558 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4559 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4561 | out: |
| 4562 | mutex_unlock(&mddev->open_mutex); |
| 4563 | if (err) |
| 4564 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | /* |
| 4566 | * Free resources if final stop |
| 4567 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4568 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4571 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4572 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4573 | if (mddev->bitmap_info.file) { |
| 4574 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4575 | fput(mddev->bitmap_info.file); |
| 4576 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4577 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4578 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | export_array(mddev); |
| 4581 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4582 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4583 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4584 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4585 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4586 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4587 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4588 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4589 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4590 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4591 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4592 | mddev->level = LEVEL_NONE; |
| 4593 | mddev->clevel[0] = 0; |
| 4594 | mddev->flags = 0; |
| 4595 | mddev->ro = 0; |
| 4596 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4597 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4598 | mddev->ctime = mddev->utime = 0; |
| 4599 | mddev->layout = 0; |
| 4600 | mddev->max_disks = 0; |
| 4601 | mddev->events = 0; |
| 4602 | mddev->delta_disks = 0; |
| 4603 | mddev->new_level = LEVEL_NONE; |
| 4604 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4605 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4606 | mddev->curr_resync = 0; |
| 4607 | mddev->resync_mismatches = 0; |
| 4608 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4609 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4610 | mddev->recovery = 0; |
| 4611 | mddev->in_sync = 0; |
| 4612 | mddev->changed = 0; |
| 4613 | mddev->degraded = 0; |
| 4614 | mddev->barriers_work = 0; |
| 4615 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4616 | mddev->bitmap_info.offset = 0; |
| 4617 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 4618 | mddev->bitmap_info.chunksize = 0; |
| 4619 | mddev->bitmap_info.daemon_sleep = 0; |
| 4620 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4621 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4622 | if (mddev->hold_active == UNTIL_STOP) |
| 4623 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4624 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4625 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4627 | mdname(mddev)); |
| 4628 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4629 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4630 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4631 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | return err; |
| 4633 | } |
| 4634 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4635 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | static void autorun_array(mddev_t *mddev) |
| 4637 | { |
| 4638 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | int err; |
| 4640 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4641 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | |
| 4644 | printk(KERN_INFO "md: running: "); |
| 4645 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4646 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | char b[BDEVNAME_SIZE]; |
| 4648 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4649 | } |
| 4650 | printk("\n"); |
| 4651 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4652 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | if (err) { |
| 4654 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4655 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | /* |
| 4660 | * lets try to run arrays based on all disks that have arrived |
| 4661 | * until now. (those are in pending_raid_disks) |
| 4662 | * |
| 4663 | * the method: pick the first pending disk, collect all disks with |
| 4664 | * the same UUID, remove all from the pending list and put them into |
| 4665 | * the 'same_array' list. Then order this list based on superblock |
| 4666 | * update time (freshest comes first), kick out 'old' disks and |
| 4667 | * compare superblocks. If everything's fine then run it. |
| 4668 | * |
| 4669 | * If "unit" is allocated, then bump its reference count |
| 4670 | */ |
| 4671 | static void autorun_devices(int part) |
| 4672 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4673 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | mddev_t *mddev; |
| 4675 | char b[BDEVNAME_SIZE]; |
| 4676 | |
| 4677 | printk(KERN_INFO "md: autorun ...\n"); |
| 4678 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4679 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4681 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | rdev0 = list_entry(pending_raid_disks.next, |
| 4683 | mdk_rdev_t, same_set); |
| 4684 | |
| 4685 | printk(KERN_INFO "md: considering %s ...\n", |
| 4686 | bdevname(rdev0->bdev,b)); |
| 4687 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4688 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4690 | printk(KERN_INFO "md: adding %s ...\n", |
| 4691 | bdevname(rdev->bdev,b)); |
| 4692 | list_move(&rdev->same_set, &candidates); |
| 4693 | } |
| 4694 | /* |
| 4695 | * now we have a set of devices, with all of them having |
| 4696 | * mostly sane superblocks. It's time to allocate the |
| 4697 | * mddev. |
| 4698 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4699 | if (part) { |
| 4700 | dev = MKDEV(mdp_major, |
| 4701 | rdev0->preferred_minor << MdpMinorShift); |
| 4702 | unit = MINOR(dev) >> MdpMinorShift; |
| 4703 | } else { |
| 4704 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4705 | unit = MINOR(dev); |
| 4706 | } |
| 4707 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4709 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4710 | break; |
| 4711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | |
| 4713 | md_probe(dev, NULL, NULL); |
| 4714 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4715 | if (!mddev || !mddev->gendisk) { |
| 4716 | if (mddev) |
| 4717 | mddev_put(mddev); |
| 4718 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | "md: cannot allocate memory for md drive.\n"); |
| 4720 | break; |
| 4721 | } |
| 4722 | if (mddev_lock(mddev)) |
| 4723 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4724 | mdname(mddev)); |
| 4725 | else if (mddev->raid_disks || mddev->major_version |
| 4726 | || !list_empty(&mddev->disks)) { |
| 4727 | printk(KERN_WARNING |
| 4728 | "md: %s already running, cannot run %s\n", |
| 4729 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4730 | mddev_unlock(mddev); |
| 4731 | } else { |
| 4732 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4733 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4734 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | list_del_init(&rdev->same_set); |
| 4736 | if (bind_rdev_to_array(rdev, mddev)) |
| 4737 | export_rdev(rdev); |
| 4738 | } |
| 4739 | autorun_array(mddev); |
| 4740 | mddev_unlock(mddev); |
| 4741 | } |
| 4742 | /* on success, candidates will be empty, on error |
| 4743 | * it won't... |
| 4744 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4745 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4746 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | mddev_put(mddev); |
| 4750 | } |
| 4751 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4752 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4753 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | static int get_version(void __user * arg) |
| 4756 | { |
| 4757 | mdu_version_t ver; |
| 4758 | |
| 4759 | ver.major = MD_MAJOR_VERSION; |
| 4760 | ver.minor = MD_MINOR_VERSION; |
| 4761 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4762 | |
| 4763 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4764 | return -EFAULT; |
| 4765 | |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4770 | { |
| 4771 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4772 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4775 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4776 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4778 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | failed++; |
| 4780 | else { |
| 4781 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4782 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4783 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | else |
| 4785 | spare++; |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | info.major_version = mddev->major_version; |
| 4790 | info.minor_version = mddev->minor_version; |
| 4791 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4792 | info.ctime = mddev->ctime; |
| 4793 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4794 | info.size = mddev->dev_sectors / 2; |
| 4795 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4796 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | info.nr_disks = nr; |
| 4798 | info.raid_disks = mddev->raid_disks; |
| 4799 | info.md_minor = mddev->md_minor; |
| 4800 | info.not_persistent= !mddev->persistent; |
| 4801 | |
| 4802 | info.utime = mddev->utime; |
| 4803 | info.state = 0; |
| 4804 | if (mddev->in_sync) |
| 4805 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4806 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4807 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4808 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | info.working_disks = working; |
| 4810 | info.failed_disks = failed; |
| 4811 | info.spare_disks = spare; |
| 4812 | |
| 4813 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4814 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | |
| 4816 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4817 | return -EFAULT; |
| 4818 | |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4822 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4823 | { |
| 4824 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4825 | char *ptr, *buf = NULL; |
| 4826 | int err = -ENOMEM; |
| 4827 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4828 | if (md_allow_write(mddev)) |
| 4829 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4830 | else |
| 4831 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4832 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4833 | if (!file) |
| 4834 | goto out; |
| 4835 | |
| 4836 | /* bitmap disabled, zero the first byte and copy out */ |
| 4837 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4838 | file->pathname[0] = '\0'; |
| 4839 | goto copy_out; |
| 4840 | } |
| 4841 | |
| 4842 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4843 | if (!buf) |
| 4844 | goto out; |
| 4845 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4846 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4847 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4848 | goto out; |
| 4849 | |
| 4850 | strcpy(file->pathname, ptr); |
| 4851 | |
| 4852 | copy_out: |
| 4853 | err = 0; |
| 4854 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4855 | err = -EFAULT; |
| 4856 | out: |
| 4857 | kfree(buf); |
| 4858 | kfree(file); |
| 4859 | return err; |
| 4860 | } |
| 4861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4863 | { |
| 4864 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | mdk_rdev_t *rdev; |
| 4866 | |
| 4867 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4868 | return -EFAULT; |
| 4869 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4870 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | if (rdev) { |
| 4872 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4873 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4874 | info.raid_disk = rdev->raid_disk; |
| 4875 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4876 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4878 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4880 | info.state |= (1<<MD_DISK_SYNC); |
| 4881 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4882 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4883 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | } else { |
| 4885 | info.major = info.minor = 0; |
| 4886 | info.raid_disk = -1; |
| 4887 | info.state = (1<<MD_DISK_REMOVED); |
| 4888 | } |
| 4889 | |
| 4890 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4891 | return -EFAULT; |
| 4892 | |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4897 | { |
| 4898 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4899 | mdk_rdev_t *rdev; |
| 4900 | dev_t dev = MKDEV(info->major,info->minor); |
| 4901 | |
| 4902 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4903 | return -EOVERFLOW; |
| 4904 | |
| 4905 | if (!mddev->raid_disks) { |
| 4906 | int err; |
| 4907 | /* expecting a device which has a superblock */ |
| 4908 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4909 | if (IS_ERR(rdev)) { |
| 4910 | printk(KERN_WARNING |
| 4911 | "md: md_import_device returned %ld\n", |
| 4912 | PTR_ERR(rdev)); |
| 4913 | return PTR_ERR(rdev); |
| 4914 | } |
| 4915 | if (!list_empty(&mddev->disks)) { |
| 4916 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4917 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4918 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4920 | if (err < 0) { |
| 4921 | printk(KERN_WARNING |
| 4922 | "md: %s has different UUID to %s\n", |
| 4923 | bdevname(rdev->bdev,b), |
| 4924 | bdevname(rdev0->bdev,b2)); |
| 4925 | export_rdev(rdev); |
| 4926 | return -EINVAL; |
| 4927 | } |
| 4928 | } |
| 4929 | err = bind_rdev_to_array(rdev, mddev); |
| 4930 | if (err) |
| 4931 | export_rdev(rdev); |
| 4932 | return err; |
| 4933 | } |
| 4934 | |
| 4935 | /* |
| 4936 | * add_new_disk can be used once the array is assembled |
| 4937 | * to add "hot spares". They must already have a superblock |
| 4938 | * written |
| 4939 | */ |
| 4940 | if (mddev->pers) { |
| 4941 | int err; |
| 4942 | if (!mddev->pers->hot_add_disk) { |
| 4943 | printk(KERN_WARNING |
| 4944 | "%s: personality does not support diskops!\n", |
| 4945 | mdname(mddev)); |
| 4946 | return -EINVAL; |
| 4947 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4948 | if (mddev->persistent) |
| 4949 | rdev = md_import_device(dev, mddev->major_version, |
| 4950 | mddev->minor_version); |
| 4951 | else |
| 4952 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | if (IS_ERR(rdev)) { |
| 4954 | printk(KERN_WARNING |
| 4955 | "md: md_import_device returned %ld\n", |
| 4956 | PTR_ERR(rdev)); |
| 4957 | return PTR_ERR(rdev); |
| 4958 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4959 | /* set save_raid_disk if appropriate */ |
| 4960 | if (!mddev->persistent) { |
| 4961 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4962 | info->raid_disk < mddev->raid_disks) |
| 4963 | rdev->raid_disk = info->raid_disk; |
| 4964 | else |
| 4965 | rdev->raid_disk = -1; |
| 4966 | } else |
| 4967 | super_types[mddev->major_version]. |
| 4968 | validate_super(mddev, rdev); |
| 4969 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4970 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4971 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4972 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4973 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4974 | else |
| 4975 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | rdev->raid_disk = -1; |
| 4978 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4979 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4980 | /* If there is hot_add_disk but no hot_remove_disk |
| 4981 | * then added disks for geometry changes, |
| 4982 | * and should be added immediately. |
| 4983 | */ |
| 4984 | super_types[mddev->major_version]. |
| 4985 | validate_super(mddev, rdev); |
| 4986 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4987 | if (err) |
| 4988 | unbind_rdev_from_array(rdev); |
| 4989 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | if (err) |
| 4991 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4992 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4993 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4994 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4995 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4996 | if (mddev->degraded) |
| 4997 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4998 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4999 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | return err; |
| 5001 | } |
| 5002 | |
| 5003 | /* otherwise, add_new_disk is only allowed |
| 5004 | * for major_version==0 superblocks |
| 5005 | */ |
| 5006 | if (mddev->major_version != 0) { |
| 5007 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5008 | mdname(mddev)); |
| 5009 | return -EINVAL; |
| 5010 | } |
| 5011 | |
| 5012 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5013 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5014 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | if (IS_ERR(rdev)) { |
| 5016 | printk(KERN_WARNING |
| 5017 | "md: error, md_import_device() returned %ld\n", |
| 5018 | PTR_ERR(rdev)); |
| 5019 | return PTR_ERR(rdev); |
| 5020 | } |
| 5021 | rdev->desc_nr = info->number; |
| 5022 | if (info->raid_disk < mddev->raid_disks) |
| 5023 | rdev->raid_disk = info->raid_disk; |
| 5024 | else |
| 5025 | rdev->raid_disk = -1; |
| 5026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5028 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5029 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5031 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5032 | set_bit(WriteMostly, &rdev->flags); |
| 5033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | if (!mddev->persistent) { |
| 5035 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5036 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5038 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5039 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5041 | err = bind_rdev_to_array(rdev, mddev); |
| 5042 | if (err) { |
| 5043 | export_rdev(rdev); |
| 5044 | return err; |
| 5045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | return 0; |
| 5049 | } |
| 5050 | |
| 5051 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5052 | { |
| 5053 | char b[BDEVNAME_SIZE]; |
| 5054 | mdk_rdev_t *rdev; |
| 5055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | rdev = find_rdev(mddev, dev); |
| 5057 | if (!rdev) |
| 5058 | return -ENXIO; |
| 5059 | |
| 5060 | if (rdev->raid_disk >= 0) |
| 5061 | goto busy; |
| 5062 | |
| 5063 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5064 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5065 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
| 5067 | return 0; |
| 5068 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5069 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5071 | return -EBUSY; |
| 5072 | } |
| 5073 | |
| 5074 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5075 | { |
| 5076 | char b[BDEVNAME_SIZE]; |
| 5077 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | mdk_rdev_t *rdev; |
| 5079 | |
| 5080 | if (!mddev->pers) |
| 5081 | return -ENODEV; |
| 5082 | |
| 5083 | if (mddev->major_version != 0) { |
| 5084 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5085 | " version-0 superblocks.\n", |
| 5086 | mdname(mddev)); |
| 5087 | return -EINVAL; |
| 5088 | } |
| 5089 | if (!mddev->pers->hot_add_disk) { |
| 5090 | printk(KERN_WARNING |
| 5091 | "%s: personality does not support diskops!\n", |
| 5092 | mdname(mddev)); |
| 5093 | return -EINVAL; |
| 5094 | } |
| 5095 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5096 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | if (IS_ERR(rdev)) { |
| 5098 | printk(KERN_WARNING |
| 5099 | "md: error, md_import_device() returned %ld\n", |
| 5100 | PTR_ERR(rdev)); |
| 5101 | return -EINVAL; |
| 5102 | } |
| 5103 | |
| 5104 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5105 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5107 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5109 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5111 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | printk(KERN_WARNING |
| 5113 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5114 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5115 | err = -EINVAL; |
| 5116 | goto abort_export; |
| 5117 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5118 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5120 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5121 | err = bind_rdev_to_array(rdev, mddev); |
| 5122 | if (err) |
| 5123 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | |
| 5125 | /* |
| 5126 | * The rest should better be atomic, we can have disk failures |
| 5127 | * noticed in interrupt contexts ... |
| 5128 | */ |
| 5129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | rdev->raid_disk = -1; |
| 5131 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5132 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | |
| 5134 | /* |
| 5135 | * Kick recovery, maybe this spare has to be added to the |
| 5136 | * array immediately. |
| 5137 | */ |
| 5138 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5139 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5140 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | return 0; |
| 5142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | abort_export: |
| 5144 | export_rdev(rdev); |
| 5145 | return err; |
| 5146 | } |
| 5147 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5148 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5149 | { |
| 5150 | int err; |
| 5151 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5152 | if (mddev->pers) { |
| 5153 | if (!mddev->pers->quiesce) |
| 5154 | return -EBUSY; |
| 5155 | if (mddev->recovery || mddev->sync_thread) |
| 5156 | return -EBUSY; |
| 5157 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5158 | } |
| 5159 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5160 | |
| 5161 | if (fd >= 0) { |
| 5162 | if (mddev->bitmap) |
| 5163 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5164 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5165 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5166 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5167 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5168 | mdname(mddev)); |
| 5169 | return -EBADF; |
| 5170 | } |
| 5171 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5172 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5173 | if (err) { |
| 5174 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5175 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5176 | fput(mddev->bitmap_info.file); |
| 5177 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5178 | return err; |
| 5179 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5180 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5181 | } else if (mddev->bitmap == NULL) |
| 5182 | return -ENOENT; /* cannot remove what isn't there */ |
| 5183 | err = 0; |
| 5184 | if (mddev->pers) { |
| 5185 | mddev->pers->quiesce(mddev, 1); |
| 5186 | if (fd >= 0) |
| 5187 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5188 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5189 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5190 | fd = -1; /* make sure to put the file */ |
| 5191 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5192 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5193 | } |
| 5194 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5195 | if (mddev->bitmap_info.file) { |
| 5196 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5197 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5198 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5199 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5200 | } |
| 5201 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5202 | return err; |
| 5203 | } |
| 5204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | /* |
| 5206 | * set_array_info is used two different ways |
| 5207 | * The original usage is when creating a new array. |
| 5208 | * In this usage, raid_disks is > 0 and it together with |
| 5209 | * level, size, not_persistent,layout,chunksize determine the |
| 5210 | * shape of the array. |
| 5211 | * This will always create an array with a type-0.90.0 superblock. |
| 5212 | * The newer usage is when assembling an array. |
| 5213 | * In this case raid_disks will be 0, and the major_version field is |
| 5214 | * use to determine which style super-blocks are to be found on the devices. |
| 5215 | * The minor and patch _version numbers are also kept incase the |
| 5216 | * super_block handler wishes to interpret them. |
| 5217 | */ |
| 5218 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5219 | { |
| 5220 | |
| 5221 | if (info->raid_disks == 0) { |
| 5222 | /* just setting version number for superblock loading */ |
| 5223 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5224 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | super_types[info->major_version].name == NULL) { |
| 5226 | /* maybe try to auto-load a module? */ |
| 5227 | printk(KERN_INFO |
| 5228 | "md: superblock version %d not known\n", |
| 5229 | info->major_version); |
| 5230 | return -EINVAL; |
| 5231 | } |
| 5232 | mddev->major_version = info->major_version; |
| 5233 | mddev->minor_version = info->minor_version; |
| 5234 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5235 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5236 | /* ensure mddev_put doesn't delete this now that there |
| 5237 | * is some minimal configuration. |
| 5238 | */ |
| 5239 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | return 0; |
| 5241 | } |
| 5242 | mddev->major_version = MD_MAJOR_VERSION; |
| 5243 | mddev->minor_version = MD_MINOR_VERSION; |
| 5244 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5245 | mddev->ctime = get_seconds(); |
| 5246 | |
| 5247 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5248 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5249 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | mddev->raid_disks = info->raid_disks; |
| 5251 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5252 | * openned |
| 5253 | */ |
| 5254 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5255 | mddev->recovery_cp = MaxSector; |
| 5256 | else |
| 5257 | mddev->recovery_cp = 0; |
| 5258 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5259 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | |
| 5261 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5262 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | |
| 5264 | mddev->max_disks = MD_SB_DISKS; |
| 5265 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5266 | if (mddev->persistent) |
| 5267 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5268 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5270 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5271 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5272 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5273 | mddev->reshape_position = MaxSector; |
| 5274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | /* |
| 5276 | * Generate a 128 bit UUID |
| 5277 | */ |
| 5278 | get_random_bytes(mddev->uuid, 16); |
| 5279 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5280 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5281 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5282 | mddev->new_layout = mddev->layout; |
| 5283 | mddev->delta_disks = 0; |
| 5284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | return 0; |
| 5286 | } |
| 5287 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5288 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5289 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5290 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5291 | |
| 5292 | if (mddev->external_size) |
| 5293 | return; |
| 5294 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5295 | mddev->array_sectors = array_sectors; |
| 5296 | } |
| 5297 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5298 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5299 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5300 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5301 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5302 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5303 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5304 | |
| 5305 | if (mddev->pers->resize == NULL) |
| 5306 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5307 | /* The "num_sectors" is the number of sectors of each device that |
| 5308 | * is used. This can only make sense for arrays with redundancy. |
| 5309 | * linear and raid0 always use whatever space is available. We can only |
| 5310 | * consider changing this number if no resync or reconstruction is |
| 5311 | * 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] | 5312 | * 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] | 5313 | * of each device. If num_sectors is zero, we find the largest size |
| 5314 | * that fits. |
| 5315 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5316 | */ |
| 5317 | if (mddev->sync_thread) |
| 5318 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5319 | if (mddev->bitmap) |
| 5320 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5321 | * grow, and re-add. |
| 5322 | */ |
| 5323 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5324 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5325 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5326 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5327 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5328 | num_sectors = avail; |
| 5329 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5330 | return -ENOSPC; |
| 5331 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5332 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5333 | if (!rv) |
| 5334 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5335 | return rv; |
| 5336 | } |
| 5337 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5338 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5339 | { |
| 5340 | int rv; |
| 5341 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5342 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5343 | return -EINVAL; |
| 5344 | if (raid_disks <= 0 || |
| 5345 | raid_disks >= mddev->max_disks) |
| 5346 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5347 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5348 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5349 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5350 | |
| 5351 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5352 | return rv; |
| 5353 | } |
| 5354 | |
| 5355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | /* |
| 5357 | * update_array_info is used to change the configuration of an |
| 5358 | * on-line array. |
| 5359 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5360 | * fields in the info are checked against the array. |
| 5361 | * Any differences that cannot be handled will cause an error. |
| 5362 | * Normally, only one change can be managed at a time. |
| 5363 | */ |
| 5364 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5365 | { |
| 5366 | int rv = 0; |
| 5367 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5368 | int state = 0; |
| 5369 | |
| 5370 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5371 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5372 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | |
| 5374 | if (mddev->major_version != info->major_version || |
| 5375 | mddev->minor_version != info->minor_version || |
| 5376 | /* mddev->patch_version != info->patch_version || */ |
| 5377 | mddev->ctime != info->ctime || |
| 5378 | mddev->level != info->level || |
| 5379 | /* mddev->layout != info->layout || */ |
| 5380 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5381 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5382 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5383 | ((state^info->state) & 0xfffffe00) |
| 5384 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | return -EINVAL; |
| 5386 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5387 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5388 | cnt++; |
| 5389 | if (mddev->raid_disks != info->raid_disks) |
| 5390 | cnt++; |
| 5391 | if (mddev->layout != info->layout) |
| 5392 | cnt++; |
| 5393 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5394 | cnt++; |
| 5395 | if (cnt == 0) |
| 5396 | return 0; |
| 5397 | if (cnt > 1) |
| 5398 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | |
| 5400 | if (mddev->layout != info->layout) { |
| 5401 | /* Change layout |
| 5402 | * we don't need to do anything at the md level, the |
| 5403 | * personality will take care of it all. |
| 5404 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5405 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5407 | else { |
| 5408 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5409 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5410 | if (rv) |
| 5411 | mddev->new_layout = mddev->layout; |
| 5412 | return rv; |
| 5413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5415 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5416 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5418 | if (mddev->raid_disks != info->raid_disks) |
| 5419 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5421 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5422 | if (mddev->pers->quiesce == NULL) |
| 5423 | return -EINVAL; |
| 5424 | if (mddev->recovery || mddev->sync_thread) |
| 5425 | return -EBUSY; |
| 5426 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5427 | /* add the bitmap */ |
| 5428 | if (mddev->bitmap) |
| 5429 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5430 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5431 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5432 | mddev->bitmap_info.offset = |
| 5433 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5434 | mddev->pers->quiesce(mddev, 1); |
| 5435 | rv = bitmap_create(mddev); |
| 5436 | if (rv) |
| 5437 | bitmap_destroy(mddev); |
| 5438 | mddev->pers->quiesce(mddev, 0); |
| 5439 | } else { |
| 5440 | /* remove the bitmap */ |
| 5441 | if (!mddev->bitmap) |
| 5442 | return -ENOENT; |
| 5443 | if (mddev->bitmap->file) |
| 5444 | return -EINVAL; |
| 5445 | mddev->pers->quiesce(mddev, 1); |
| 5446 | bitmap_destroy(mddev); |
| 5447 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5448 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5449 | } |
| 5450 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5451 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | return rv; |
| 5453 | } |
| 5454 | |
| 5455 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5456 | { |
| 5457 | mdk_rdev_t *rdev; |
| 5458 | |
| 5459 | if (mddev->pers == NULL) |
| 5460 | return -ENODEV; |
| 5461 | |
| 5462 | rdev = find_rdev(mddev, dev); |
| 5463 | if (!rdev) |
| 5464 | return -ENODEV; |
| 5465 | |
| 5466 | md_error(mddev, rdev); |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5470 | /* |
| 5471 | * We have a problem here : there is no easy way to give a CHS |
| 5472 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5473 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5474 | * dosfs just mad... ;-) |
| 5475 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5476 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5477 | { |
| 5478 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5479 | |
| 5480 | geo->heads = 2; |
| 5481 | geo->sectors = 4; |
| 5482 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5483 | return 0; |
| 5484 | } |
| 5485 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5486 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | unsigned int cmd, unsigned long arg) |
| 5488 | { |
| 5489 | int err = 0; |
| 5490 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | mddev_t *mddev = NULL; |
| 5492 | |
| 5493 | if (!capable(CAP_SYS_ADMIN)) |
| 5494 | return -EACCES; |
| 5495 | |
| 5496 | /* |
| 5497 | * Commands dealing with the RAID driver but not any |
| 5498 | * particular array: |
| 5499 | */ |
| 5500 | switch (cmd) |
| 5501 | { |
| 5502 | case RAID_VERSION: |
| 5503 | err = get_version(argp); |
| 5504 | goto done; |
| 5505 | |
| 5506 | case PRINT_RAID_DEBUG: |
| 5507 | err = 0; |
| 5508 | md_print_devices(); |
| 5509 | goto done; |
| 5510 | |
| 5511 | #ifndef MODULE |
| 5512 | case RAID_AUTORUN: |
| 5513 | err = 0; |
| 5514 | autostart_arrays(arg); |
| 5515 | goto done; |
| 5516 | #endif |
| 5517 | default:; |
| 5518 | } |
| 5519 | |
| 5520 | /* |
| 5521 | * Commands creating/starting a new array: |
| 5522 | */ |
| 5523 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5524 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | |
| 5526 | if (!mddev) { |
| 5527 | BUG(); |
| 5528 | goto abort; |
| 5529 | } |
| 5530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | err = mddev_lock(mddev); |
| 5532 | if (err) { |
| 5533 | printk(KERN_INFO |
| 5534 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5535 | err, cmd); |
| 5536 | goto abort; |
| 5537 | } |
| 5538 | |
| 5539 | switch (cmd) |
| 5540 | { |
| 5541 | case SET_ARRAY_INFO: |
| 5542 | { |
| 5543 | mdu_array_info_t info; |
| 5544 | if (!arg) |
| 5545 | memset(&info, 0, sizeof(info)); |
| 5546 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5547 | err = -EFAULT; |
| 5548 | goto abort_unlock; |
| 5549 | } |
| 5550 | if (mddev->pers) { |
| 5551 | err = update_array_info(mddev, &info); |
| 5552 | if (err) { |
| 5553 | printk(KERN_WARNING "md: couldn't update" |
| 5554 | " array info. %d\n", err); |
| 5555 | goto abort_unlock; |
| 5556 | } |
| 5557 | goto done_unlock; |
| 5558 | } |
| 5559 | if (!list_empty(&mddev->disks)) { |
| 5560 | printk(KERN_WARNING |
| 5561 | "md: array %s already has disks!\n", |
| 5562 | mdname(mddev)); |
| 5563 | err = -EBUSY; |
| 5564 | goto abort_unlock; |
| 5565 | } |
| 5566 | if (mddev->raid_disks) { |
| 5567 | printk(KERN_WARNING |
| 5568 | "md: array %s already initialised!\n", |
| 5569 | mdname(mddev)); |
| 5570 | err = -EBUSY; |
| 5571 | goto abort_unlock; |
| 5572 | } |
| 5573 | err = set_array_info(mddev, &info); |
| 5574 | if (err) { |
| 5575 | printk(KERN_WARNING "md: couldn't set" |
| 5576 | " array info. %d\n", err); |
| 5577 | goto abort_unlock; |
| 5578 | } |
| 5579 | } |
| 5580 | goto done_unlock; |
| 5581 | |
| 5582 | default:; |
| 5583 | } |
| 5584 | |
| 5585 | /* |
| 5586 | * Commands querying/configuring an existing array: |
| 5587 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5588 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5589 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5590 | if ((!mddev->raid_disks && !mddev->external) |
| 5591 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5592 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5593 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | err = -ENODEV; |
| 5595 | goto abort_unlock; |
| 5596 | } |
| 5597 | |
| 5598 | /* |
| 5599 | * Commands even a read-only array can execute: |
| 5600 | */ |
| 5601 | switch (cmd) |
| 5602 | { |
| 5603 | case GET_ARRAY_INFO: |
| 5604 | err = get_array_info(mddev, argp); |
| 5605 | goto done_unlock; |
| 5606 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5607 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5608 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5609 | goto done_unlock; |
| 5610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | case GET_DISK_INFO: |
| 5612 | err = get_disk_info(mddev, argp); |
| 5613 | goto done_unlock; |
| 5614 | |
| 5615 | case RESTART_ARRAY_RW: |
| 5616 | err = restart_array(mddev); |
| 5617 | goto done_unlock; |
| 5618 | |
| 5619 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5620 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | goto done_unlock; |
| 5622 | |
| 5623 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5624 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | goto done_unlock; |
| 5626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | /* |
| 5630 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5631 | * superblock, so we do not allow them on read-only arrays. |
| 5632 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5633 | * here and hit the 'default' below, so only disallow |
| 5634 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5636 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5637 | if (mddev->ro == 2) { |
| 5638 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5639 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5640 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5641 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5642 | } else { |
| 5643 | err = -EROFS; |
| 5644 | goto abort_unlock; |
| 5645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | switch (cmd) |
| 5649 | { |
| 5650 | case ADD_NEW_DISK: |
| 5651 | { |
| 5652 | mdu_disk_info_t info; |
| 5653 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5654 | err = -EFAULT; |
| 5655 | else |
| 5656 | err = add_new_disk(mddev, &info); |
| 5657 | goto done_unlock; |
| 5658 | } |
| 5659 | |
| 5660 | case HOT_REMOVE_DISK: |
| 5661 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5662 | goto done_unlock; |
| 5663 | |
| 5664 | case HOT_ADD_DISK: |
| 5665 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5666 | goto done_unlock; |
| 5667 | |
| 5668 | case SET_DISK_FAULTY: |
| 5669 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5670 | goto done_unlock; |
| 5671 | |
| 5672 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5673 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | goto done_unlock; |
| 5675 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5676 | case SET_BITMAP_FILE: |
| 5677 | err = set_bitmap_file(mddev, (int)arg); |
| 5678 | goto done_unlock; |
| 5679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | err = -EINVAL; |
| 5682 | goto abort_unlock; |
| 5683 | } |
| 5684 | |
| 5685 | done_unlock: |
| 5686 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5687 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5688 | err != -EINVAL) |
| 5689 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | mddev_unlock(mddev); |
| 5691 | |
| 5692 | return err; |
| 5693 | done: |
| 5694 | if (err) |
| 5695 | MD_BUG(); |
| 5696 | abort: |
| 5697 | return err; |
| 5698 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5699 | #ifdef CONFIG_COMPAT |
| 5700 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5701 | unsigned int cmd, unsigned long arg) |
| 5702 | { |
| 5703 | switch (cmd) { |
| 5704 | case HOT_REMOVE_DISK: |
| 5705 | case HOT_ADD_DISK: |
| 5706 | case SET_DISK_FAULTY: |
| 5707 | case SET_BITMAP_FILE: |
| 5708 | /* These take in integer arg, do not convert */ |
| 5709 | break; |
| 5710 | default: |
| 5711 | arg = (unsigned long)compat_ptr(arg); |
| 5712 | break; |
| 5713 | } |
| 5714 | |
| 5715 | return md_ioctl(bdev, mode, cmd, arg); |
| 5716 | } |
| 5717 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5719 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | { |
| 5721 | /* |
| 5722 | * Succeed if we can lock the mddev, which confirms that |
| 5723 | * it isn't being stopped right now. |
| 5724 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5725 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | int err; |
| 5727 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5728 | if (mddev->gendisk != bdev->bd_disk) { |
| 5729 | /* we are racing with mddev_put which is discarding this |
| 5730 | * bd_disk. |
| 5731 | */ |
| 5732 | mddev_put(mddev); |
| 5733 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5734 | flush_scheduled_work(); |
| 5735 | /* Then retry the open from the top */ |
| 5736 | return -ERESTARTSYS; |
| 5737 | } |
| 5738 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5739 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5740 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | goto out; |
| 5742 | |
| 5743 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5744 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5745 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5747 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | out: |
| 5749 | return err; |
| 5750 | } |
| 5751 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5752 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5754 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5756 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5757 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | mddev_put(mddev); |
| 5759 | |
| 5760 | return 0; |
| 5761 | } |
| 5762 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5763 | static int md_media_changed(struct gendisk *disk) |
| 5764 | { |
| 5765 | mddev_t *mddev = disk->private_data; |
| 5766 | |
| 5767 | return mddev->changed; |
| 5768 | } |
| 5769 | |
| 5770 | static int md_revalidate(struct gendisk *disk) |
| 5771 | { |
| 5772 | mddev_t *mddev = disk->private_data; |
| 5773 | |
| 5774 | mddev->changed = 0; |
| 5775 | return 0; |
| 5776 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5777 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | { |
| 5779 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5780 | .open = md_open, |
| 5781 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5782 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5783 | #ifdef CONFIG_COMPAT |
| 5784 | .compat_ioctl = md_compat_ioctl, |
| 5785 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5786 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5787 | .media_changed = md_media_changed, |
| 5788 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | }; |
| 5790 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5791 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | { |
| 5793 | mdk_thread_t *thread = arg; |
| 5794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | /* |
| 5796 | * md_thread is a 'system-thread', it's priority should be very |
| 5797 | * high. We avoid resource deadlocks individually in each |
| 5798 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5799 | * the very same RT priority as kswapd, thus we will never get |
| 5800 | * into a priority inversion deadlock. |
| 5801 | * |
| 5802 | * we definitely have to have equal or higher priority than |
| 5803 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5804 | * many dirty RAID5 blocks. |
| 5805 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5807 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5808 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5810 | /* We need to wait INTERRUPTIBLE so that |
| 5811 | * we don't add to the load-average. |
| 5812 | * That means we need to be sure no signals are |
| 5813 | * pending |
| 5814 | */ |
| 5815 | if (signal_pending(current)) |
| 5816 | flush_signals(current); |
| 5817 | |
| 5818 | wait_event_interruptible_timeout |
| 5819 | (thread->wqueue, |
| 5820 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5821 | || kthread_should_stop(), |
| 5822 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | |
| 5824 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5825 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5826 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | return 0; |
| 5830 | } |
| 5831 | |
| 5832 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5833 | { |
| 5834 | if (thread) { |
| 5835 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5836 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5837 | wake_up(&thread->wqueue); |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5842 | const char *name) |
| 5843 | { |
| 5844 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5846 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | if (!thread) |
| 5848 | return NULL; |
| 5849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | init_waitqueue_head(&thread->wqueue); |
| 5851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | thread->run = run; |
| 5853 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5854 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5855 | thread->tsk = kthread_run(md_thread, thread, |
| 5856 | "%s_%s", |
| 5857 | mdname(thread->mddev), |
| 5858 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5859 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | kfree(thread); |
| 5861 | return NULL; |
| 5862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | return thread; |
| 5864 | } |
| 5865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | void md_unregister_thread(mdk_thread_t *thread) |
| 5867 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5868 | if (!thread) |
| 5869 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5870 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5871 | |
| 5872 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | kfree(thread); |
| 5874 | } |
| 5875 | |
| 5876 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5877 | { |
| 5878 | if (!mddev) { |
| 5879 | MD_BUG(); |
| 5880 | return; |
| 5881 | } |
| 5882 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5883 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5885 | |
| 5886 | if (mddev->external) |
| 5887 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5888 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5890 | mdname(mddev), |
| 5891 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5892 | __builtin_return_address(0),__builtin_return_address(1), |
| 5893 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5894 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5895 | if (!mddev->pers) |
| 5896 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | if (!mddev->pers->error_handler) |
| 5898 | return; |
| 5899 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5900 | if (mddev->degraded) |
| 5901 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5902 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5904 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5905 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5906 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | } |
| 5908 | |
| 5909 | /* seq_file implementation /proc/mdstat */ |
| 5910 | |
| 5911 | static void status_unused(struct seq_file *seq) |
| 5912 | { |
| 5913 | int i = 0; |
| 5914 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | |
| 5916 | seq_printf(seq, "unused devices: "); |
| 5917 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5918 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | char b[BDEVNAME_SIZE]; |
| 5920 | i++; |
| 5921 | seq_printf(seq, "%s ", |
| 5922 | bdevname(rdev->bdev,b)); |
| 5923 | } |
| 5924 | if (!i) |
| 5925 | seq_printf(seq, "<none>"); |
| 5926 | |
| 5927 | seq_printf(seq, "\n"); |
| 5928 | } |
| 5929 | |
| 5930 | |
| 5931 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5932 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5933 | sector_t max_sectors, resync, res; |
| 5934 | unsigned long dt, db; |
| 5935 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5936 | int scale; |
| 5937 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5939 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | |
| 5941 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5942 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5944 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | |
| 5946 | /* |
| 5947 | * Should not happen. |
| 5948 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5949 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | MD_BUG(); |
| 5951 | return; |
| 5952 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5953 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5954 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5955 | * u32, as those are the requirements for sector_div. |
| 5956 | * Thus 'scale' must be at least 10 |
| 5957 | */ |
| 5958 | scale = 10; |
| 5959 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5960 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5961 | scale++; |
| 5962 | } |
| 5963 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5964 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5965 | |
| 5966 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5968 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | seq_printf(seq, "["); |
| 5970 | for (i = 0; i < x; i++) |
| 5971 | seq_printf(seq, "="); |
| 5972 | seq_printf(seq, ">"); |
| 5973 | for (i = 0; i < y; i++) |
| 5974 | seq_printf(seq, "."); |
| 5975 | seq_printf(seq, "] "); |
| 5976 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5977 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5978 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5979 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5980 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5981 | "check" : |
| 5982 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5983 | "resync" : "recovery"))), |
| 5984 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5985 | (unsigned long long) resync/2, |
| 5986 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | |
| 5988 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | * dt: time from mark until now |
| 5990 | * db: blocks written from mark until now |
| 5991 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5992 | * |
| 5993 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5994 | * So we divide before multiply in case it is 32bit and close |
| 5995 | * to the limit. |
| 5996 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5997 | * near the end of resync when the number of remaining sectors |
| 5998 | * is close to 'db'. |
| 5999 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6000 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | */ |
| 6002 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6003 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6004 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6005 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6007 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6008 | sector_div(rt, db/32+1); |
| 6009 | rt *= dt; |
| 6010 | rt >>= 5; |
| 6011 | |
| 6012 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6013 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6015 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6019 | { |
| 6020 | struct list_head *tmp; |
| 6021 | loff_t l = *pos; |
| 6022 | mddev_t *mddev; |
| 6023 | |
| 6024 | if (l >= 0x10000) |
| 6025 | return NULL; |
| 6026 | if (!l--) |
| 6027 | /* header */ |
| 6028 | return (void*)1; |
| 6029 | |
| 6030 | spin_lock(&all_mddevs_lock); |
| 6031 | list_for_each(tmp,&all_mddevs) |
| 6032 | if (!l--) { |
| 6033 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6034 | mddev_get(mddev); |
| 6035 | spin_unlock(&all_mddevs_lock); |
| 6036 | return mddev; |
| 6037 | } |
| 6038 | spin_unlock(&all_mddevs_lock); |
| 6039 | if (!l--) |
| 6040 | return (void*)2;/* tail */ |
| 6041 | return NULL; |
| 6042 | } |
| 6043 | |
| 6044 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6045 | { |
| 6046 | struct list_head *tmp; |
| 6047 | mddev_t *next_mddev, *mddev = v; |
| 6048 | |
| 6049 | ++*pos; |
| 6050 | if (v == (void*)2) |
| 6051 | return NULL; |
| 6052 | |
| 6053 | spin_lock(&all_mddevs_lock); |
| 6054 | if (v == (void*)1) |
| 6055 | tmp = all_mddevs.next; |
| 6056 | else |
| 6057 | tmp = mddev->all_mddevs.next; |
| 6058 | if (tmp != &all_mddevs) |
| 6059 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6060 | else { |
| 6061 | next_mddev = (void*)2; |
| 6062 | *pos = 0x10000; |
| 6063 | } |
| 6064 | spin_unlock(&all_mddevs_lock); |
| 6065 | |
| 6066 | if (v != (void*)1) |
| 6067 | mddev_put(mddev); |
| 6068 | return next_mddev; |
| 6069 | |
| 6070 | } |
| 6071 | |
| 6072 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6073 | { |
| 6074 | mddev_t *mddev = v; |
| 6075 | |
| 6076 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6077 | mddev_put(mddev); |
| 6078 | } |
| 6079 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6080 | struct mdstat_info { |
| 6081 | int event; |
| 6082 | }; |
| 6083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6085 | { |
| 6086 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6087 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6089 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6090 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
| 6092 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6093 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | seq_printf(seq, "Personalities : "); |
| 6095 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6096 | list_for_each_entry(pers, &pers_list, list) |
| 6097 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | |
| 6099 | spin_unlock(&pers_lock); |
| 6100 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6101 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | return 0; |
| 6103 | } |
| 6104 | if (v == (void*)2) { |
| 6105 | status_unused(seq); |
| 6106 | return 0; |
| 6107 | } |
| 6108 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6109 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6113 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6114 | mddev->pers ? "" : "in"); |
| 6115 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6116 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6118 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6119 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | seq_printf(seq, " %s", mddev->pers->name); |
| 6121 | } |
| 6122 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6123 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6124 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | char b[BDEVNAME_SIZE]; |
| 6126 | seq_printf(seq, " %s[%d]", |
| 6127 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6128 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6129 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6130 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | seq_printf(seq, "(F)"); |
| 6132 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6133 | } else if (rdev->raid_disk < 0) |
| 6134 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6135 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | if (!list_empty(&mddev->disks)) { |
| 6139 | if (mddev->pers) |
| 6140 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6141 | (unsigned long long) |
| 6142 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | else |
| 6144 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6145 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6147 | if (mddev->persistent) { |
| 6148 | if (mddev->major_version != 0 || |
| 6149 | mddev->minor_version != 90) { |
| 6150 | seq_printf(seq," super %d.%d", |
| 6151 | mddev->major_version, |
| 6152 | mddev->minor_version); |
| 6153 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6154 | } else if (mddev->external) |
| 6155 | seq_printf(seq, " super external:%s", |
| 6156 | mddev->metadata_type); |
| 6157 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6158 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | |
| 6160 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6161 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6163 | if (mddev->pers->sync_request) { |
| 6164 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6165 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6166 | seq_printf(seq, "\n "); |
| 6167 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6168 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6169 | else if (mddev->recovery_cp < MaxSector) |
| 6170 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6171 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6172 | } else |
| 6173 | seq_printf(seq, "\n "); |
| 6174 | |
| 6175 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6176 | unsigned long chunk_kb; |
| 6177 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6178 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6179 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6180 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6181 | "%lu%s chunk", |
| 6182 | bitmap->pages - bitmap->missing_pages, |
| 6183 | bitmap->pages, |
| 6184 | (bitmap->pages - bitmap->missing_pages) |
| 6185 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6186 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6187 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6188 | if (bitmap->file) { |
| 6189 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6190 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6191 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6192 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6193 | seq_printf(seq, "\n"); |
| 6194 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | seq_printf(seq, "\n"); |
| 6198 | } |
| 6199 | mddev_unlock(mddev); |
| 6200 | |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6204 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | .start = md_seq_start, |
| 6206 | .next = md_seq_next, |
| 6207 | .stop = md_seq_stop, |
| 6208 | .show = md_seq_show, |
| 6209 | }; |
| 6210 | |
| 6211 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6212 | { |
| 6213 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6214 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6215 | if (mi == NULL) |
| 6216 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | |
| 6218 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6219 | if (error) |
| 6220 | kfree(mi); |
| 6221 | else { |
| 6222 | struct seq_file *p = file->private_data; |
| 6223 | p->private = mi; |
| 6224 | mi->event = atomic_read(&md_event_count); |
| 6225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | return error; |
| 6227 | } |
| 6228 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6229 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6230 | { |
| 6231 | struct seq_file *m = filp->private_data; |
| 6232 | struct mdstat_info *mi = m->private; |
| 6233 | int mask; |
| 6234 | |
| 6235 | poll_wait(filp, &md_event_waiters, wait); |
| 6236 | |
| 6237 | /* always allow read */ |
| 6238 | mask = POLLIN | POLLRDNORM; |
| 6239 | |
| 6240 | if (mi->event != atomic_read(&md_event_count)) |
| 6241 | mask |= POLLERR | POLLPRI; |
| 6242 | return mask; |
| 6243 | } |
| 6244 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6245 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6246 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | .open = md_seq_open, |
| 6248 | .read = seq_read, |
| 6249 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6250 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6251 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | }; |
| 6253 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6254 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6257 | list_add_tail(&p->list, &pers_list); |
| 6258 | 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] | 6259 | spin_unlock(&pers_lock); |
| 6260 | return 0; |
| 6261 | } |
| 6262 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6263 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6265 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6267 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | spin_unlock(&pers_lock); |
| 6269 | return 0; |
| 6270 | } |
| 6271 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6272 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | { |
| 6274 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6276 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | |
| 6278 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6279 | rcu_read_lock(); |
| 6280 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6282 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6283 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6284 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6285 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6286 | * as sync_io is counted when a request starts, and |
| 6287 | * disk_stats is counted when it completes. |
| 6288 | * So resync activity will cause curr_events to be smaller than |
| 6289 | * when there was no such activity. |
| 6290 | * non-sync IO will cause disk_stat to increase without |
| 6291 | * increasing sync_io so curr_events will (eventually) |
| 6292 | * be larger than it was before. Once it becomes |
| 6293 | * substantially larger, the test below will cause |
| 6294 | * the array to appear non-idle, and resync will slow |
| 6295 | * down. |
| 6296 | * If there is a lot of outstanding resync activity when |
| 6297 | * we set last_event to curr_events, then all that activity |
| 6298 | * completing might cause the array to appear non-idle |
| 6299 | * and resync will be slowed down even though there might |
| 6300 | * not have been non-resync activity. This will only |
| 6301 | * happen once though. 'last_events' will soon reflect |
| 6302 | * the state where there is little or no outstanding |
| 6303 | * resync requests, and further resync activity will |
| 6304 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6305 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6307 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | rdev->last_events = curr_events; |
| 6309 | idle = 0; |
| 6310 | } |
| 6311 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6312 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | return idle; |
| 6314 | } |
| 6315 | |
| 6316 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6317 | { |
| 6318 | /* another "blocks" (512byte) blocks have been synced */ |
| 6319 | atomic_sub(blocks, &mddev->recovery_active); |
| 6320 | wake_up(&mddev->recovery_wait); |
| 6321 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6322 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | md_wakeup_thread(mddev->thread); |
| 6324 | // stop recovery, signal do_sync .... |
| 6325 | } |
| 6326 | } |
| 6327 | |
| 6328 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6329 | /* md_write_start(mddev, bi) |
| 6330 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6331 | * in superblock) before writing, schedule a superblock update |
| 6332 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6333 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6334 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6336 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6337 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6338 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6339 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6340 | BUG_ON(mddev->ro == 1); |
| 6341 | if (mddev->ro == 2) { |
| 6342 | /* need to switch to read/write */ |
| 6343 | mddev->ro = 0; |
| 6344 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6345 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6346 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6347 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6348 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6349 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6350 | if (mddev->safemode == 1) |
| 6351 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6352 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6353 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6354 | if (mddev->in_sync) { |
| 6355 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6356 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6357 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6358 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6359 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6360 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6361 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6362 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6363 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6364 | wait_event(mddev->sb_wait, |
| 6365 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6366 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | void md_write_end(mddev_t *mddev) |
| 6370 | { |
| 6371 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6372 | if (mddev->safemode == 2) |
| 6373 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6374 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6376 | } |
| 6377 | } |
| 6378 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6379 | /* md_allow_write(mddev) |
| 6380 | * Calling this ensures that the array is marked 'active' so that writes |
| 6381 | * may proceed without blocking. It is important to call this before |
| 6382 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6383 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6384 | * |
| 6385 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6386 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6387 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6388 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6389 | { |
| 6390 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6391 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6392 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6393 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6394 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6395 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6396 | |
| 6397 | spin_lock_irq(&mddev->write_lock); |
| 6398 | if (mddev->in_sync) { |
| 6399 | mddev->in_sync = 0; |
| 6400 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6401 | if (mddev->safemode_delay && |
| 6402 | mddev->safemode == 0) |
| 6403 | mddev->safemode = 1; |
| 6404 | spin_unlock_irq(&mddev->write_lock); |
| 6405 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6406 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6407 | } else |
| 6408 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6409 | |
| 6410 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6411 | return -EAGAIN; |
| 6412 | else |
| 6413 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6414 | } |
| 6415 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | #define SYNC_MARKS 10 |
| 6418 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6419 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | { |
| 6421 | mddev_t *mddev2; |
| 6422 | unsigned int currspeed = 0, |
| 6423 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6424 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | unsigned long mark[SYNC_MARKS]; |
| 6426 | sector_t mark_cnt[SYNC_MARKS]; |
| 6427 | int last_mark,m; |
| 6428 | struct list_head *tmp; |
| 6429 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6430 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6431 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6432 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | |
| 6434 | /* just incase thread restarts... */ |
| 6435 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6436 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6437 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6438 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6440 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6441 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6442 | desc = "data-check"; |
| 6443 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6444 | desc = "requested-resync"; |
| 6445 | else |
| 6446 | desc = "resync"; |
| 6447 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6448 | desc = "reshape"; |
| 6449 | else |
| 6450 | desc = "recovery"; |
| 6451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | /* we overload curr_resync somewhat here. |
| 6453 | * 0 == not engaged in resync at all |
| 6454 | * 2 == checking that there is no conflict with another sync |
| 6455 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6456 | * commense |
| 6457 | * other == active in resync - this many blocks |
| 6458 | * |
| 6459 | * Before starting a resync we must have set curr_resync to |
| 6460 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6461 | * less than ours. When we find one that is the same or higher |
| 6462 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6463 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6464 | * This will mean we have to start checking from the beginning again. |
| 6465 | * |
| 6466 | */ |
| 6467 | |
| 6468 | do { |
| 6469 | mddev->curr_resync = 2; |
| 6470 | |
| 6471 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6472 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6473 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6474 | |
| 6475 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6477 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | if (mddev2 == mddev) |
| 6479 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6480 | if (!mddev->parallel_resync |
| 6481 | && mddev2->curr_resync |
| 6482 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | DEFINE_WAIT(wq); |
| 6484 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6485 | /* arbitrarily yield */ |
| 6486 | mddev->curr_resync = 1; |
| 6487 | wake_up(&resync_wait); |
| 6488 | } |
| 6489 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6490 | /* no need to wait here, we can wait the next |
| 6491 | * time 'round when curr_resync == 2 |
| 6492 | */ |
| 6493 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6494 | /* We need to wait 'interruptible' so as not to |
| 6495 | * contribute to the load average, and not to |
| 6496 | * be caught by 'softlockup' |
| 6497 | */ |
| 6498 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6499 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6500 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6501 | printk(KERN_INFO "md: delaying %s of %s" |
| 6502 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6504 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6506 | if (signal_pending(current)) |
| 6507 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | schedule(); |
| 6509 | finish_wait(&resync_wait, &wq); |
| 6510 | goto try_again; |
| 6511 | } |
| 6512 | finish_wait(&resync_wait, &wq); |
| 6513 | } |
| 6514 | } |
| 6515 | } while (mddev->curr_resync < 2); |
| 6516 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6517 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6518 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6519 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6520 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | */ |
| 6522 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6523 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6524 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6525 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6526 | j = mddev->resync_min; |
| 6527 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6528 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6529 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6530 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6531 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6532 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6534 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6535 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6536 | rcu_read_lock(); |
| 6537 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6538 | if (rdev->raid_disk >= 0 && |
| 6539 | !test_bit(Faulty, &rdev->flags) && |
| 6540 | !test_bit(In_sync, &rdev->flags) && |
| 6541 | rdev->recovery_offset < j) |
| 6542 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6543 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6546 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6547 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6548 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6549 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6550 | "(but not more than %d KB/sec) for %s.\n", |
| 6551 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6553 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6554 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6555 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6557 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6558 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | } |
| 6560 | last_mark = 0; |
| 6561 | mddev->resync_mark = mark[last_mark]; |
| 6562 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6563 | |
| 6564 | /* |
| 6565 | * Tune reconstruction: |
| 6566 | */ |
| 6567 | window = 32*(PAGE_SIZE/512); |
| 6568 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6569 | window/2,(unsigned long long) max_sectors/2); |
| 6570 | |
| 6571 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | last_check = 0; |
| 6573 | |
| 6574 | if (j>2) { |
| 6575 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6576 | "md: resuming %s of %s from checkpoint.\n", |
| 6577 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | mddev->curr_resync = j; |
| 6579 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6580 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | |
| 6582 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6583 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6585 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6586 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6587 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6588 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6589 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6590 | > (max_sectors >> 4)) || |
| 6591 | (j - mddev->curr_resync_completed)*2 |
| 6592 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6593 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6594 | /* time to update curr_resync_completed */ |
| 6595 | blk_unplug(mddev->queue); |
| 6596 | wait_event(mddev->recovery_wait, |
| 6597 | atomic_read(&mddev->recovery_active) == 0); |
| 6598 | mddev->curr_resync_completed = |
| 6599 | mddev->curr_resync; |
| 6600 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6601 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6602 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6603 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6604 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6605 | /* As this condition is controlled by user-space, |
| 6606 | * we can block indefinitely, so use '_interruptible' |
| 6607 | * to avoid triggering warnings. |
| 6608 | */ |
| 6609 | flush_signals(current); /* just in case */ |
| 6610 | wait_event_interruptible(mddev->recovery_wait, |
| 6611 | mddev->resync_max > j |
| 6612 | || kthread_should_stop()); |
| 6613 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6614 | |
| 6615 | if (kthread_should_stop()) |
| 6616 | goto interrupted; |
| 6617 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6618 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6619 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6620 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6621 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | goto out; |
| 6623 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6624 | |
| 6625 | if (!skipped) { /* actual IO requested */ |
| 6626 | io_sectors += sectors; |
| 6627 | atomic_add(sectors, &mddev->recovery_active); |
| 6628 | } |
| 6629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | j += sectors; |
| 6631 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6632 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6633 | if (last_check == 0) |
| 6634 | /* this is the earliers that rebuilt will be |
| 6635 | * visible in /proc/mdstat |
| 6636 | */ |
| 6637 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6638 | |
| 6639 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | continue; |
| 6641 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6642 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6644 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | break; |
| 6646 | |
| 6647 | repeat: |
| 6648 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6649 | /* step marks */ |
| 6650 | int next = (last_mark+1) % SYNC_MARKS; |
| 6651 | |
| 6652 | mddev->resync_mark = mark[next]; |
| 6653 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6654 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6655 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | last_mark = next; |
| 6657 | } |
| 6658 | |
| 6659 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6660 | if (kthread_should_stop()) |
| 6661 | goto interrupted; |
| 6662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
| 6664 | /* |
| 6665 | * this loop exits only if either when we are slower than |
| 6666 | * the 'hard' speed limit, or the system was IO-idle for |
| 6667 | * a jiffy. |
| 6668 | * the system might be non-idle CPU-wise, but we only care |
| 6669 | * about not overloading the IO subsystem. (things like an |
| 6670 | * e2fsck being done on the RAID array should execute fast) |
| 6671 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6672 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | cond_resched(); |
| 6674 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6675 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6676 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6678 | if (currspeed > speed_min(mddev)) { |
| 6679 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6680 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6681 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | goto repeat; |
| 6683 | } |
| 6684 | } |
| 6685 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6686 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6687 | /* |
| 6688 | * this also signals 'finished resyncing' to md_stop |
| 6689 | */ |
| 6690 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6691 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | |
| 6693 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6694 | |
| 6695 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6696 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6698 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6699 | mddev->curr_resync > 2) { |
| 6700 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6701 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6702 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6703 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6704 | "md: checkpointing %s of %s.\n", |
| 6705 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6706 | mddev->recovery_cp = mddev->curr_resync; |
| 6707 | } |
| 6708 | } else |
| 6709 | mddev->recovery_cp = MaxSector; |
| 6710 | } else { |
| 6711 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6712 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6713 | rcu_read_lock(); |
| 6714 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6715 | if (rdev->raid_disk >= 0 && |
| 6716 | !test_bit(Faulty, &rdev->flags) && |
| 6717 | !test_bit(In_sync, &rdev->flags) && |
| 6718 | rdev->recovery_offset < mddev->curr_resync) |
| 6719 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6720 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6723 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6726 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6727 | /* We completed so min/max setting can be forgotten if used. */ |
| 6728 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6729 | mddev->resync_min = 0; |
| 6730 | mddev->resync_max = MaxSector; |
| 6731 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6732 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6734 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6735 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6736 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | wake_up(&resync_wait); |
| 6738 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6739 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6740 | return; |
| 6741 | |
| 6742 | interrupted: |
| 6743 | /* |
| 6744 | * got a signal, exit. |
| 6745 | */ |
| 6746 | printk(KERN_INFO |
| 6747 | "md: md_do_sync() got signal ... exiting\n"); |
| 6748 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6749 | goto out; |
| 6750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6751 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6752 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | |
| 6754 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6755 | static int remove_and_add_spares(mddev_t *mddev) |
| 6756 | { |
| 6757 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6758 | int spares = 0; |
| 6759 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6760 | mddev->curr_resync_completed = 0; |
| 6761 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6762 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6763 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6764 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6765 | (test_bit(Faulty, &rdev->flags) || |
| 6766 | ! test_bit(In_sync, &rdev->flags)) && |
| 6767 | atomic_read(&rdev->nr_pending)==0) { |
| 6768 | if (mddev->pers->hot_remove_disk( |
| 6769 | mddev, rdev->raid_disk)==0) { |
| 6770 | char nm[20]; |
| 6771 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6772 | sysfs_remove_link(&mddev->kobj, nm); |
| 6773 | rdev->raid_disk = -1; |
| 6774 | } |
| 6775 | } |
| 6776 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6777 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6778 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6779 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6780 | !test_bit(In_sync, &rdev->flags) && |
| 6781 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6782 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6783 | if (rdev->raid_disk < 0 |
| 6784 | && !test_bit(Faulty, &rdev->flags)) { |
| 6785 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6786 | if (mddev->pers-> |
| 6787 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6788 | char nm[20]; |
| 6789 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6790 | if (sysfs_create_link(&mddev->kobj, |
| 6791 | &rdev->kobj, nm)) |
| 6792 | printk(KERN_WARNING |
| 6793 | "md: cannot register " |
| 6794 | "%s for %s\n", |
| 6795 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6796 | spares++; |
| 6797 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6798 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6799 | } else |
| 6800 | break; |
| 6801 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6802 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6803 | } |
| 6804 | return spares; |
| 6805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | /* |
| 6807 | * This routine is regularly called by all per-raid-array threads to |
| 6808 | * deal with generic issues like resync and super-block update. |
| 6809 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6810 | * need this as they never do any recovery or update the superblock. |
| 6811 | * |
| 6812 | * It does not do any resync itself, but rather "forks" off other threads |
| 6813 | * to do that as needed. |
| 6814 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6815 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6816 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | * and wakeups up this thread which will reap the thread and finish up. |
| 6818 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6819 | * |
| 6820 | * The overall approach is: |
| 6821 | * 1/ if the superblock needs updating, update it. |
| 6822 | * 2/ If a recovery thread is running, don't do anything else. |
| 6823 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6824 | * 4/ If there are any faulty devices, remove them. |
| 6825 | * 5/ If array is degraded, try to add spares devices |
| 6826 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6827 | */ |
| 6828 | void md_check_recovery(mddev_t *mddev) |
| 6829 | { |
| 6830 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | |
| 6832 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6833 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6834 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | |
| 6836 | if (mddev->ro) |
| 6837 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6838 | |
| 6839 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6840 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6841 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6842 | mdname(mddev)); |
| 6843 | mddev->safemode = 2; |
| 6844 | } |
| 6845 | flush_signals(current); |
| 6846 | } |
| 6847 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6848 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6849 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6851 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6853 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6854 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6855 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6856 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | )) |
| 6858 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6859 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6860 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6861 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6862 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6863 | if (mddev->ro) { |
| 6864 | /* Only thing we do on a ro array is remove |
| 6865 | * failed devices. |
| 6866 | */ |
| 6867 | remove_and_add_spares(mddev); |
| 6868 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6869 | goto unlock; |
| 6870 | } |
| 6871 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6872 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6873 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6874 | spin_lock_irq(&mddev->write_lock); |
| 6875 | if (mddev->safemode && |
| 6876 | !atomic_read(&mddev->writes_pending) && |
| 6877 | !mddev->in_sync && |
| 6878 | mddev->recovery_cp == MaxSector) { |
| 6879 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6880 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6881 | if (mddev->persistent) |
| 6882 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6883 | } |
| 6884 | if (mddev->safemode == 1) |
| 6885 | mddev->safemode = 0; |
| 6886 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6887 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6888 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6889 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6890 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6891 | if (mddev->flags) |
| 6892 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6893 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6894 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6895 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6896 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6897 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6900 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6901 | /* resync/recovery still happening */ |
| 6902 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6903 | goto unlock; |
| 6904 | } |
| 6905 | if (mddev->sync_thread) { |
| 6906 | /* resync has finished, collect result */ |
| 6907 | md_unregister_thread(mddev->sync_thread); |
| 6908 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6909 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6910 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | /* success...*/ |
| 6912 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6913 | if (mddev->pers->spare_active(mddev)) |
| 6914 | sysfs_notify(&mddev->kobj, NULL, |
| 6915 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6917 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6918 | mddev->pers->finish_reshape) |
| 6919 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6920 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6921 | |
| 6922 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6923 | * information must be scrapped |
| 6924 | */ |
| 6925 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6926 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6927 | rdev->saved_raid_disk = -1; |
| 6928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | mddev->recovery = 0; |
| 6930 | /* flag recovery needed just to double check */ |
| 6931 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6932 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6933 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6934 | goto unlock; |
| 6935 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6936 | /* Set RUNNING before clearing NEEDED to avoid |
| 6937 | * any transients in the value of "sync_action". |
| 6938 | */ |
| 6939 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6940 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6941 | /* Clear some bits that don't mean anything, but |
| 6942 | * might be left set |
| 6943 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6944 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6945 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6947 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6948 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6949 | /* no recovery is running. |
| 6950 | * remove any failed drives, then |
| 6951 | * add spares if possible. |
| 6952 | * Spare are also removed and re-added, to allow |
| 6953 | * the personality to fail the re-add. |
| 6954 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6956 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6957 | if (mddev->pers->check_reshape == NULL || |
| 6958 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6959 | /* Cannot proceed */ |
| 6960 | goto unlock; |
| 6961 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6962 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6963 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6964 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6965 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6966 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6967 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6968 | } else if (mddev->recovery_cp < MaxSector) { |
| 6969 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6970 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6971 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6972 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6976 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6977 | /* We are adding a device or devices to an array |
| 6978 | * which has the bitmap stored on all devices. |
| 6979 | * So make sure all bitmap pages get written |
| 6980 | */ |
| 6981 | bitmap_write_all(mddev->bitmap); |
| 6982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6984 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6985 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | if (!mddev->sync_thread) { |
| 6987 | printk(KERN_ERR "%s: could not start resync" |
| 6988 | " thread...\n", |
| 6989 | mdname(mddev)); |
| 6990 | /* leave the spares where they are, it shouldn't hurt */ |
| 6991 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6992 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6994 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6995 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | } |
| 6997 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6998 | if (!mddev->sync_thread) { |
| 6999 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7000 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7001 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7002 | if (mddev->sysfs_action) |
| 7003 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | mddev_unlock(mddev); |
| 7006 | } |
| 7007 | } |
| 7008 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7009 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7010 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7011 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7012 | wait_event_timeout(rdev->blocked_wait, |
| 7013 | !test_bit(Blocked, &rdev->flags), |
| 7014 | msecs_to_jiffies(5000)); |
| 7015 | rdev_dec_pending(rdev, mddev); |
| 7016 | } |
| 7017 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7018 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7019 | static int md_notify_reboot(struct notifier_block *this, |
| 7020 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | { |
| 7022 | struct list_head *tmp; |
| 7023 | mddev_t *mddev; |
| 7024 | |
| 7025 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7026 | |
| 7027 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7028 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7029 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7030 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7031 | /* Force a switch to readonly even array |
| 7032 | * appears to still be in use. Hence |
| 7033 | * the '100'. |
| 7034 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7035 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7036 | mddev_unlock(mddev); |
| 7037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | /* |
| 7039 | * certain more exotic SCSI devices are known to be |
| 7040 | * volatile wrt too early system reboots. While the |
| 7041 | * right place to handle this issue is the given |
| 7042 | * driver, we do want to have a safe RAID driver ... |
| 7043 | */ |
| 7044 | mdelay(1000*1); |
| 7045 | } |
| 7046 | return NOTIFY_DONE; |
| 7047 | } |
| 7048 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7049 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7050 | .notifier_call = md_notify_reboot, |
| 7051 | .next = NULL, |
| 7052 | .priority = INT_MAX, /* before any real devices */ |
| 7053 | }; |
| 7054 | |
| 7055 | static void md_geninit(void) |
| 7056 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7057 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7058 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7059 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | } |
| 7061 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7062 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7064 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7065 | return -1; |
| 7066 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7067 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | return -1; |
| 7069 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7070 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7071 | md_probe, NULL, NULL); |
| 7072 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | md_probe, NULL, NULL); |
| 7074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7076 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | |
| 7078 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7079 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | } |
| 7081 | |
| 7082 | |
| 7083 | #ifndef MODULE |
| 7084 | |
| 7085 | /* |
| 7086 | * Searches all registered partitions for autorun RAID arrays |
| 7087 | * at boot time. |
| 7088 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7089 | |
| 7090 | static LIST_HEAD(all_detected_devices); |
| 7091 | struct detected_devices_node { |
| 7092 | struct list_head list; |
| 7093 | dev_t dev; |
| 7094 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | |
| 7096 | void md_autodetect_dev(dev_t dev) |
| 7097 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7098 | struct detected_devices_node *node_detected_dev; |
| 7099 | |
| 7100 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7101 | if (node_detected_dev) { |
| 7102 | node_detected_dev->dev = dev; |
| 7103 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7104 | } else { |
| 7105 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7106 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | } |
| 7109 | |
| 7110 | |
| 7111 | static void autostart_arrays(int part) |
| 7112 | { |
| 7113 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7114 | struct detected_devices_node *node_detected_dev; |
| 7115 | dev_t dev; |
| 7116 | int i_scanned, i_passed; |
| 7117 | |
| 7118 | i_scanned = 0; |
| 7119 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | |
| 7121 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7122 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7123 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7124 | i_scanned++; |
| 7125 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7126 | struct detected_devices_node, list); |
| 7127 | list_del(&node_detected_dev->list); |
| 7128 | dev = node_detected_dev->dev; |
| 7129 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7130 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | if (IS_ERR(rdev)) |
| 7132 | continue; |
| 7133 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7134 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | MD_BUG(); |
| 7136 | continue; |
| 7137 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7138 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7140 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7142 | |
| 7143 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7144 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | |
| 7146 | autorun_devices(part); |
| 7147 | } |
| 7148 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7149 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | |
| 7151 | static __exit void md_exit(void) |
| 7152 | { |
| 7153 | mddev_t *mddev; |
| 7154 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7155 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7156 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7157 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7159 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | unregister_blkdev(mdp_major, "mdp"); |
| 7161 | unregister_reboot_notifier(&md_notifier); |
| 7162 | unregister_sysctl_table(raid_table_header); |
| 7163 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7164 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7166 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | } |
| 7168 | } |
| 7169 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7170 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | module_exit(md_exit) |
| 7172 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7173 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7174 | { |
| 7175 | return sprintf(buffer, "%d", start_readonly); |
| 7176 | } |
| 7177 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7178 | { |
| 7179 | char *e; |
| 7180 | int num = simple_strtoul(val, &e, 10); |
| 7181 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7182 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7183 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7184 | } |
| 7185 | return -EINVAL; |
| 7186 | } |
| 7187 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7188 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7189 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7190 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7191 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | EXPORT_SYMBOL(register_md_personality); |
| 7194 | EXPORT_SYMBOL(unregister_md_personality); |
| 7195 | EXPORT_SYMBOL(md_error); |
| 7196 | EXPORT_SYMBOL(md_done_sync); |
| 7197 | EXPORT_SYMBOL(md_write_start); |
| 7198 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | EXPORT_SYMBOL(md_register_thread); |
| 7200 | EXPORT_SYMBOL(md_unregister_thread); |
| 7201 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | EXPORT_SYMBOL(md_check_recovery); |
| 7203 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7204 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7205 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7206 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |