Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 42 | #include <linux/hdreg.h> |
| 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/random.h> |
| 45 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 46 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 47 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 48 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 49 | #include <linux/raid/md_p.h> |
| 50 | #include <linux/raid/md_u.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 52 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #define DEBUG 0 |
| 55 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 56 | |
| 57 | |
| 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 62 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(pers_lock); |
| 64 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 65 | static void md_print_devices(void); |
| 66 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 67 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 72 | * Default number of read corrections we'll attempt on an rdev |
| 73 | * before ejecting it from the array. We divide the read error |
| 74 | * count by 2 for every hour elapsed between read errors. |
| 75 | */ |
| 76 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 77 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 79 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 80 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 81 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 83 | * speed limit - in case reconstruction slows down your system despite |
| 84 | * idle IO detection. |
| 85 | * |
| 86 | * 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] | 87 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | */ |
| 89 | |
| 90 | static int sysctl_speed_limit_min = 1000; |
| 91 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 92 | static inline int speed_min(mddev_t *mddev) |
| 93 | { |
| 94 | return mddev->sync_speed_min ? |
| 95 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 96 | } |
| 97 | |
| 98 | static inline int speed_max(mddev_t *mddev) |
| 99 | { |
| 100 | return mddev->sync_speed_max ? |
| 101 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | static struct ctl_table_header *raid_table_header; |
| 105 | |
| 106 | static ctl_table raid_table[] = { |
| 107 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .procname = "speed_limit_min", |
| 109 | .data = &sysctl_speed_limit_min, |
| 110 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 111 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 112 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }, |
| 114 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .procname = "speed_limit_max", |
| 116 | .data = &sysctl_speed_limit_max, |
| 117 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 118 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 119 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 121 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static ctl_table raid_dir_table[] = { |
| 125 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .procname = "raid", |
| 127 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 128 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .child = raid_table, |
| 130 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 131 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static ctl_table raid_root_table[] = { |
| 135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .procname = "dev", |
| 137 | .maxlen = 0, |
| 138 | .mode = 0555, |
| 139 | .child = raid_dir_table, |
| 140 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 141 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 144 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 146 | static int start_readonly; |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 149 | * We have a system wide 'event count' that is incremented |
| 150 | * on any 'interesting' event, and readers of /proc/mdstat |
| 151 | * can use 'poll' or 'select' to find out when the event |
| 152 | * count increases. |
| 153 | * |
| 154 | * Events are: |
| 155 | * start array, stop array, error, add device, remove device, |
| 156 | * start build, activate spare |
| 157 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 158 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 159 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 160 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 161 | { |
| 162 | atomic_inc(&md_event_count); |
| 163 | wake_up(&md_event_waiters); |
| 164 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 166 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 167 | /* Alternate version that can be called from interrupts |
| 168 | * when calling sysfs_notify isn't needed. |
| 169 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 170 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 171 | { |
| 172 | atomic_inc(&md_event_count); |
| 173 | wake_up(&md_event_waiters); |
| 174 | } |
| 175 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 176 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * Enables to iterate over all existing md arrays |
| 178 | * all_mddevs_lock protects this list. |
| 179 | */ |
| 180 | static LIST_HEAD(all_mddevs); |
| 181 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 182 | |
| 183 | |
| 184 | /* |
| 185 | * iterates through all used mddevs in the system. |
| 186 | * We take care to grab the all_mddevs_lock whenever navigating |
| 187 | * the list, and to always hold a refcount when unlocked. |
| 188 | * Any code which breaks out of this loop while own |
| 189 | * a reference to the current mddev and must mddev_put it. |
| 190 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 191 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | \ |
| 193 | for (({ spin_lock(&all_mddevs_lock); \ |
| 194 | tmp = all_mddevs.next; \ |
| 195 | mddev = NULL;}); \ |
| 196 | ({ if (tmp != &all_mddevs) \ |
| 197 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 198 | spin_unlock(&all_mddevs_lock); \ |
| 199 | if (mddev) mddev_put(mddev); \ |
| 200 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 201 | tmp != &all_mddevs;}); \ |
| 202 | ({ spin_lock(&all_mddevs_lock); \ |
| 203 | tmp = tmp->next;}) \ |
| 204 | ) |
| 205 | |
| 206 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 207 | /* Rather than calling directly into the personality make_request function, |
| 208 | * IO requests come here first so that we can check if the device is |
| 209 | * being suspended pending a reconfiguration. |
| 210 | * We hold a refcount over the call to ->make_request. By the time that |
| 211 | * call has finished, the bio has been linked into some internal structure |
| 212 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 213 | */ |
| 214 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 216 | mddev_t *mddev = q->queuedata; |
| 217 | int rv; |
| 218 | if (mddev == NULL || mddev->pers == NULL) { |
| 219 | bio_io_error(bio); |
| 220 | return 0; |
| 221 | } |
| 222 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 223 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 224 | DEFINE_WAIT(__wait); |
| 225 | for (;;) { |
| 226 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 227 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 228 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 229 | break; |
| 230 | rcu_read_unlock(); |
| 231 | schedule(); |
| 232 | rcu_read_lock(); |
| 233 | } |
| 234 | finish_wait(&mddev->sb_wait, &__wait); |
| 235 | } |
| 236 | atomic_inc(&mddev->active_io); |
| 237 | rcu_read_unlock(); |
| 238 | rv = mddev->pers->make_request(q, bio); |
| 239 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 240 | wake_up(&mddev->sb_wait); |
| 241 | |
| 242 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 245 | static void mddev_suspend(mddev_t *mddev) |
| 246 | { |
| 247 | BUG_ON(mddev->suspended); |
| 248 | mddev->suspended = 1; |
| 249 | synchronize_rcu(); |
| 250 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 251 | mddev->pers->quiesce(mddev, 1); |
| 252 | md_unregister_thread(mddev->thread); |
| 253 | mddev->thread = NULL; |
| 254 | /* we now know that no code is executing in the personality module, |
| 255 | * except possibly the tail end of a ->bi_end_io function, but that |
| 256 | * is certain to complete before the module has a chance to get |
| 257 | * unloaded |
| 258 | */ |
| 259 | } |
| 260 | |
| 261 | static void mddev_resume(mddev_t *mddev) |
| 262 | { |
| 263 | mddev->suspended = 0; |
| 264 | wake_up(&mddev->sb_wait); |
| 265 | mddev->pers->quiesce(mddev, 0); |
| 266 | } |
| 267 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 268 | int mddev_congested(mddev_t *mddev, int bits) |
| 269 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 270 | if (mddev->barrier) |
| 271 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 272 | return mddev->suspended; |
| 273 | } |
| 274 | EXPORT_SYMBOL(mddev_congested); |
| 275 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 276 | /* |
| 277 | * Generic barrier handling for md |
| 278 | */ |
| 279 | |
| 280 | #define POST_REQUEST_BARRIER ((void*)1) |
| 281 | |
| 282 | static void md_end_barrier(struct bio *bio, int err) |
| 283 | { |
| 284 | mdk_rdev_t *rdev = bio->bi_private; |
| 285 | mddev_t *mddev = rdev->mddev; |
| 286 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 287 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 288 | |
| 289 | rdev_dec_pending(rdev, mddev); |
| 290 | |
| 291 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 292 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 293 | /* This was a post-request barrier */ |
| 294 | mddev->barrier = NULL; |
| 295 | wake_up(&mddev->sb_wait); |
| 296 | } else |
| 297 | /* The pre-request barrier has finished */ |
| 298 | schedule_work(&mddev->barrier_work); |
| 299 | } |
| 300 | bio_put(bio); |
| 301 | } |
| 302 | |
| 303 | static void submit_barriers(mddev_t *mddev) |
| 304 | { |
| 305 | mdk_rdev_t *rdev; |
| 306 | |
| 307 | rcu_read_lock(); |
| 308 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 309 | if (rdev->raid_disk >= 0 && |
| 310 | !test_bit(Faulty, &rdev->flags)) { |
| 311 | /* Take two references, one is dropped |
| 312 | * when request finishes, one after |
| 313 | * we reclaim rcu_read_lock |
| 314 | */ |
| 315 | struct bio *bi; |
| 316 | atomic_inc(&rdev->nr_pending); |
| 317 | atomic_inc(&rdev->nr_pending); |
| 318 | rcu_read_unlock(); |
| 319 | bi = bio_alloc(GFP_KERNEL, 0); |
| 320 | bi->bi_end_io = md_end_barrier; |
| 321 | bi->bi_private = rdev; |
| 322 | bi->bi_bdev = rdev->bdev; |
| 323 | atomic_inc(&mddev->flush_pending); |
| 324 | submit_bio(WRITE_BARRIER, bi); |
| 325 | rcu_read_lock(); |
| 326 | rdev_dec_pending(rdev, mddev); |
| 327 | } |
| 328 | rcu_read_unlock(); |
| 329 | } |
| 330 | |
| 331 | static void md_submit_barrier(struct work_struct *ws) |
| 332 | { |
| 333 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 334 | struct bio *bio = mddev->barrier; |
| 335 | |
| 336 | atomic_set(&mddev->flush_pending, 1); |
| 337 | |
| 338 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 339 | bio_endio(bio, -EOPNOTSUPP); |
| 340 | else if (bio->bi_size == 0) |
| 341 | /* an empty barrier - all done */ |
| 342 | bio_endio(bio, 0); |
| 343 | else { |
| 344 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
| 345 | if (mddev->pers->make_request(mddev->queue, bio)) |
| 346 | generic_make_request(bio); |
| 347 | mddev->barrier = POST_REQUEST_BARRIER; |
| 348 | submit_barriers(mddev); |
| 349 | } |
| 350 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 351 | mddev->barrier = NULL; |
| 352 | wake_up(&mddev->sb_wait); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 357 | { |
| 358 | spin_lock_irq(&mddev->write_lock); |
| 359 | wait_event_lock_irq(mddev->sb_wait, |
| 360 | !mddev->barrier, |
| 361 | mddev->write_lock, /*nothing*/); |
| 362 | mddev->barrier = bio; |
| 363 | spin_unlock_irq(&mddev->write_lock); |
| 364 | |
| 365 | atomic_set(&mddev->flush_pending, 1); |
| 366 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 367 | |
| 368 | submit_barriers(mddev); |
| 369 | |
| 370 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 371 | schedule_work(&mddev->barrier_work); |
| 372 | } |
| 373 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 376 | { |
| 377 | atomic_inc(&mddev->active); |
| 378 | return mddev; |
| 379 | } |
| 380 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 381 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | static void mddev_put(mddev_t *mddev) |
| 384 | { |
| 385 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 386 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 387 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 388 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 390 | if (mddev->gendisk) { |
| 391 | /* we did a probe so need to clean up. |
| 392 | * Call schedule_work inside the spinlock |
| 393 | * so that flush_scheduled_work() after |
| 394 | * mddev_find will succeed in waiting for the |
| 395 | * work to be done. |
| 396 | */ |
| 397 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 398 | schedule_work(&mddev->del_work); |
| 399 | } else |
| 400 | kfree(mddev); |
| 401 | } |
| 402 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static mddev_t * mddev_find(dev_t unit) |
| 406 | { |
| 407 | mddev_t *mddev, *new = NULL; |
| 408 | |
| 409 | retry: |
| 410 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 412 | if (unit) { |
| 413 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 414 | if (mddev->unit == unit) { |
| 415 | mddev_get(mddev); |
| 416 | spin_unlock(&all_mddevs_lock); |
| 417 | kfree(new); |
| 418 | return mddev; |
| 419 | } |
| 420 | |
| 421 | if (new) { |
| 422 | list_add(&new->all_mddevs, &all_mddevs); |
| 423 | spin_unlock(&all_mddevs_lock); |
| 424 | new->hold_active = UNTIL_IOCTL; |
| 425 | return new; |
| 426 | } |
| 427 | } else if (new) { |
| 428 | /* find an unused unit number */ |
| 429 | static int next_minor = 512; |
| 430 | int start = next_minor; |
| 431 | int is_free = 0; |
| 432 | int dev = 0; |
| 433 | while (!is_free) { |
| 434 | dev = MKDEV(MD_MAJOR, next_minor); |
| 435 | next_minor++; |
| 436 | if (next_minor > MINORMASK) |
| 437 | next_minor = 0; |
| 438 | if (next_minor == start) { |
| 439 | /* Oh dear, all in use. */ |
| 440 | spin_unlock(&all_mddevs_lock); |
| 441 | kfree(new); |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | is_free = 1; |
| 446 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 447 | if (mddev->unit == dev) { |
| 448 | is_free = 0; |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | new->unit = dev; |
| 453 | new->md_minor = MINOR(dev); |
| 454 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | list_add(&new->all_mddevs, &all_mddevs); |
| 456 | spin_unlock(&all_mddevs_lock); |
| 457 | return new; |
| 458 | } |
| 459 | spin_unlock(&all_mddevs_lock); |
| 460 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 461 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (!new) |
| 463 | return NULL; |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | new->unit = unit; |
| 466 | if (MAJOR(unit) == MD_MAJOR) |
| 467 | new->md_minor = MINOR(unit); |
| 468 | else |
| 469 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 470 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 471 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 472 | mutex_init(&new->reconfig_mutex); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 473 | mutex_init(&new->bitmap_info.mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | INIT_LIST_HEAD(&new->disks); |
| 475 | INIT_LIST_HEAD(&new->all_mddevs); |
| 476 | init_timer(&new->safemode_timer); |
| 477 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 478 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 479 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 480 | spin_lock_init(&new->write_lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 481 | atomic_set(&new->flush_pending, 0); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 482 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 483 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 484 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 485 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 486 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 487 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | goto retry; |
| 490 | } |
| 491 | |
| 492 | static inline int mddev_lock(mddev_t * mddev) |
| 493 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 494 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 497 | static inline int mddev_is_locked(mddev_t *mddev) |
| 498 | { |
| 499 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | static inline int mddev_trylock(mddev_t * mddev) |
| 503 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 504 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static inline void mddev_unlock(mddev_t * mddev) |
| 508 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 509 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 511 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 514 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 516 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 518 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (rdev->desc_nr == nr) |
| 520 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 526 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | mdk_rdev_t *rdev; |
| 528 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 529 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (rdev->bdev->bd_dev == dev) |
| 531 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return NULL; |
| 534 | } |
| 535 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 536 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 537 | { |
| 538 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 539 | list_for_each_entry(pers, &pers_list, list) { |
| 540 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 541 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 542 | if (strcmp(pers->name, clevel)==0) |
| 543 | return pers; |
| 544 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 545 | return NULL; |
| 546 | } |
| 547 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 548 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 549 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 551 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 552 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 556 | { |
| 557 | if (rdev->sb_page) |
| 558 | MD_BUG(); |
| 559 | |
| 560 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 561 | if (!rdev->sb_page) { |
| 562 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 563 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 570 | { |
| 571 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 572 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | rdev->sb_loaded = 0; |
| 574 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 575 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 576 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 581 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 582 | { |
| 583 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 584 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 585 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 586 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 587 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 588 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 589 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 590 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 591 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 592 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 593 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 594 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 595 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 596 | } |
| 597 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 598 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 599 | { |
| 600 | struct bio *bio2 = bio->bi_private; |
| 601 | mdk_rdev_t *rdev = bio2->bi_private; |
| 602 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 603 | |
| 604 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 605 | error == -EOPNOTSUPP) { |
| 606 | unsigned long flags; |
| 607 | /* barriers don't appear to be supported :-( */ |
| 608 | set_bit(BarriersNotsupp, &rdev->flags); |
| 609 | mddev->barriers_work = 0; |
| 610 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 611 | bio2->bi_next = mddev->biolist; |
| 612 | mddev->biolist = bio2; |
| 613 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 614 | wake_up(&mddev->sb_wait); |
| 615 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 616 | } else { |
| 617 | bio_put(bio2); |
| 618 | bio->bi_private = rdev; |
| 619 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 620 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 621 | } |
| 622 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 623 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 624 | sector_t sector, int size, struct page *page) |
| 625 | { |
| 626 | /* write first size bytes of page to sector of rdev |
| 627 | * Increment mddev->pending_writes before returning |
| 628 | * and decrement it on completion, waking up sb_wait |
| 629 | * if zero is reached. |
| 630 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 631 | * |
| 632 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 633 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 634 | */ |
| 635 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 636 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 637 | |
| 638 | bio->bi_bdev = rdev->bdev; |
| 639 | bio->bi_sector = sector; |
| 640 | bio_add_page(bio, page, size, 0); |
| 641 | bio->bi_private = rdev; |
| 642 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 643 | bio->bi_rw = rw; |
| 644 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 645 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 646 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 647 | struct bio *rbio; |
| 648 | rw |= (1<<BIO_RW_BARRIER); |
| 649 | rbio = bio_clone(bio, GFP_NOIO); |
| 650 | rbio->bi_private = bio; |
| 651 | rbio->bi_end_io = super_written_barrier; |
| 652 | submit_bio(rw, rbio); |
| 653 | } else |
| 654 | submit_bio(rw, bio); |
| 655 | } |
| 656 | |
| 657 | void md_super_wait(mddev_t *mddev) |
| 658 | { |
| 659 | /* wait for all superblock writes that were scheduled to complete. |
| 660 | * if any had to be retried (due to BARRIER problems), retry them |
| 661 | */ |
| 662 | DEFINE_WAIT(wq); |
| 663 | for(;;) { |
| 664 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 665 | if (atomic_read(&mddev->pending_writes)==0) |
| 666 | break; |
| 667 | while (mddev->biolist) { |
| 668 | struct bio *bio; |
| 669 | spin_lock_irq(&mddev->write_lock); |
| 670 | bio = mddev->biolist; |
| 671 | mddev->biolist = bio->bi_next ; |
| 672 | bio->bi_next = NULL; |
| 673 | spin_unlock_irq(&mddev->write_lock); |
| 674 | submit_bio(bio->bi_rw, bio); |
| 675 | } |
| 676 | schedule(); |
| 677 | } |
| 678 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 679 | } |
| 680 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 681 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 686 | 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] | 687 | struct page *page, int rw) |
| 688 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 689 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | struct completion event; |
| 691 | int ret; |
| 692 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 693 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | bio->bi_bdev = bdev; |
| 696 | bio->bi_sector = sector; |
| 697 | bio_add_page(bio, page, size, 0); |
| 698 | init_completion(&event); |
| 699 | bio->bi_private = &event; |
| 700 | bio->bi_end_io = bi_complete; |
| 701 | submit_bio(rw, bio); |
| 702 | wait_for_completion(&event); |
| 703 | |
| 704 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 705 | bio_put(bio); |
| 706 | return ret; |
| 707 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 708 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 710 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | char b[BDEVNAME_SIZE]; |
| 713 | if (!rdev->sb_page) { |
| 714 | MD_BUG(); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | if (rdev->sb_loaded) |
| 718 | return 0; |
| 719 | |
| 720 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 721 | 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] | 722 | goto fail; |
| 723 | rdev->sb_loaded = 1; |
| 724 | return 0; |
| 725 | |
| 726 | fail: |
| 727 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 728 | bdevname(rdev->bdev,b)); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 733 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 734 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 735 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 736 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 737 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 741 | { |
| 742 | int ret; |
| 743 | mdp_super_t *tmp1, *tmp2; |
| 744 | |
| 745 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 746 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 747 | |
| 748 | if (!tmp1 || !tmp2) { |
| 749 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 750 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | goto abort; |
| 752 | } |
| 753 | |
| 754 | *tmp1 = *sb1; |
| 755 | *tmp2 = *sb2; |
| 756 | |
| 757 | /* |
| 758 | * nr_disks is not constant |
| 759 | */ |
| 760 | tmp1->nr_disks = 0; |
| 761 | tmp2->nr_disks = 0; |
| 762 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 763 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 765 | kfree(tmp1); |
| 766 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return ret; |
| 768 | } |
| 769 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 770 | |
| 771 | static u32 md_csum_fold(u32 csum) |
| 772 | { |
| 773 | csum = (csum & 0xffff) + (csum >> 16); |
| 774 | return (csum & 0xffff) + (csum >> 16); |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 778 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 779 | u64 newcsum = 0; |
| 780 | u32 *sb32 = (u32*)sb; |
| 781 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | unsigned int disk_csum, csum; |
| 783 | |
| 784 | disk_csum = sb->sb_csum; |
| 785 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 786 | |
| 787 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 788 | newcsum += sb32[i]; |
| 789 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 790 | |
| 791 | |
| 792 | #ifdef CONFIG_ALPHA |
| 793 | /* This used to use csum_partial, which was wrong for several |
| 794 | * reasons including that different results are returned on |
| 795 | * different architectures. It isn't critical that we get exactly |
| 796 | * the same return value as before (we always csum_fold before |
| 797 | * testing, and that removes any differences). However as we |
| 798 | * know that csum_partial always returned a 16bit value on |
| 799 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 800 | */ |
| 801 | sb->sb_csum = md_csum_fold(disk_csum); |
| 802 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 804 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return csum; |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /* |
| 810 | * Handle superblock details. |
| 811 | * We want to be able to handle multiple superblock formats |
| 812 | * so we have a common interface to them all, and an array of |
| 813 | * different handlers. |
| 814 | * We rely on user-space to write the initial superblock, and support |
| 815 | * reading and updating of superblocks. |
| 816 | * Interface methods are: |
| 817 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 818 | * loads and validates a superblock on dev. |
| 819 | * if refdev != NULL, compare superblocks on both devices |
| 820 | * Return: |
| 821 | * 0 - dev has a superblock that is compatible with refdev |
| 822 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 823 | * so dev should be used as the refdev in future |
| 824 | * -EINVAL superblock incompatible or invalid |
| 825 | * -othererror e.g. -EIO |
| 826 | * |
| 827 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 828 | * Verify that dev is acceptable into mddev. |
| 829 | * The first time, mddev->raid_disks will be 0, and data from |
| 830 | * dev should be merged in. Subsequent calls check that dev |
| 831 | * is new enough. Return 0 or -EINVAL |
| 832 | * |
| 833 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 834 | * Update the superblock for rdev with data in mddev |
| 835 | * This does not write to disc. |
| 836 | * |
| 837 | */ |
| 838 | |
| 839 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 840 | char *name; |
| 841 | struct module *owner; |
| 842 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 843 | int minor_version); |
| 844 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 845 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 846 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 847 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | }; |
| 849 | |
| 850 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 851 | * Check that the given mddev has no bitmap. |
| 852 | * |
| 853 | * This function is called from the run method of all personalities that do not |
| 854 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 855 | * has a bitmap. Otherwise, it returns 0. |
| 856 | * |
| 857 | */ |
| 858 | int md_check_no_bitmap(mddev_t *mddev) |
| 859 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 860 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 861 | return 0; |
| 862 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 863 | mdname(mddev), mddev->pers->name); |
| 864 | return 1; |
| 865 | } |
| 866 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 867 | |
| 868 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | * load_super for 0.90.0 |
| 870 | */ |
| 871 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 872 | { |
| 873 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 874 | mdp_super_t *sb; |
| 875 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
| 877 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 878 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | * it's at the end of the disk. |
| 880 | * |
| 881 | * It also happens to be a multiple of 4Kb. |
| 882 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 883 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 885 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (ret) return ret; |
| 887 | |
| 888 | ret = -EINVAL; |
| 889 | |
| 890 | bdevname(rdev->bdev, b); |
| 891 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 892 | |
| 893 | if (sb->md_magic != MD_SB_MAGIC) { |
| 894 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 895 | b); |
| 896 | goto abort; |
| 897 | } |
| 898 | |
| 899 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 900 | sb->minor_version < 90 || |
| 901 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 903 | sb->major_version, sb->minor_version, |
| 904 | b); |
| 905 | goto abort; |
| 906 | } |
| 907 | |
| 908 | if (sb->raid_disks <= 0) |
| 909 | goto abort; |
| 910 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 911 | 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] | 912 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 913 | b); |
| 914 | goto abort; |
| 915 | } |
| 916 | |
| 917 | rdev->preferred_minor = sb->md_minor; |
| 918 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 919 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
| 921 | if (sb->level == LEVEL_MULTIPATH) |
| 922 | rdev->desc_nr = -1; |
| 923 | else |
| 924 | rdev->desc_nr = sb->this_disk.number; |
| 925 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 926 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 928 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | __u64 ev1, ev2; |
| 930 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 931 | if (!uuid_equal(refsb, sb)) { |
| 932 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 933 | b, bdevname(refdev->bdev,b2)); |
| 934 | goto abort; |
| 935 | } |
| 936 | if (!sb_equal(refsb, sb)) { |
| 937 | printk(KERN_WARNING "md: %s has same UUID" |
| 938 | " but different superblock to %s\n", |
| 939 | b, bdevname(refdev->bdev, b2)); |
| 940 | goto abort; |
| 941 | } |
| 942 | ev1 = md_event(sb); |
| 943 | ev2 = md_event(refsb); |
| 944 | if (ev1 > ev2) |
| 945 | ret = 1; |
| 946 | else |
| 947 | ret = 0; |
| 948 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 949 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 951 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 952 | /* "this cannot possibly happen" ... */ |
| 953 | ret = -EINVAL; |
| 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | abort: |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * validate_super for 0.90.0 |
| 961 | */ |
| 962 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 963 | { |
| 964 | mdp_disk_t *desc; |
| 965 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 966 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 968 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 969 | clear_bit(Faulty, &rdev->flags); |
| 970 | clear_bit(In_sync, &rdev->flags); |
| 971 | clear_bit(WriteMostly, &rdev->flags); |
| 972 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (mddev->raid_disks == 0) { |
| 975 | mddev->major_version = 0; |
| 976 | mddev->minor_version = sb->minor_version; |
| 977 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 978 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 979 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | mddev->ctime = sb->ctime; |
| 981 | mddev->utime = sb->utime; |
| 982 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 983 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | mddev->layout = sb->layout; |
| 985 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 986 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 987 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 988 | mddev->bitmap_info.offset = 0; |
| 989 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 991 | if (mddev->minor_version >= 91) { |
| 992 | mddev->reshape_position = sb->reshape_position; |
| 993 | mddev->delta_disks = sb->delta_disks; |
| 994 | mddev->new_level = sb->new_level; |
| 995 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 996 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 997 | } else { |
| 998 | mddev->reshape_position = MaxSector; |
| 999 | mddev->delta_disks = 0; |
| 1000 | mddev->new_level = mddev->level; |
| 1001 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1002 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1006 | mddev->recovery_cp = MaxSector; |
| 1007 | else { |
| 1008 | if (sb->events_hi == sb->cp_events_hi && |
| 1009 | sb->events_lo == sb->cp_events_lo) { |
| 1010 | mddev->recovery_cp = sb->recovery_cp; |
| 1011 | } else |
| 1012 | mddev->recovery_cp = 0; |
| 1013 | } |
| 1014 | |
| 1015 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1016 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1017 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1018 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1019 | |
| 1020 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1023 | mddev->bitmap_info.file == NULL) |
| 1024 | mddev->bitmap_info.offset = |
| 1025 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1026 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1027 | } else if (mddev->pers == NULL) { |
| 1028 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | ++ev1; |
| 1030 | if (ev1 < mddev->events) |
| 1031 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1032 | } else if (mddev->bitmap) { |
| 1033 | /* if adding to array with a bitmap, then we can accept an |
| 1034 | * older device ... but not too old. |
| 1035 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1036 | if (ev1 < mddev->bitmap->events_cleared) |
| 1037 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1038 | } else { |
| 1039 | if (ev1 < mddev->events) |
| 1040 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1041 | return 0; |
| 1042 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | desc = sb->disks + rdev->desc_nr; |
| 1046 | |
| 1047 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1048 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1049 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1050 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1051 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1053 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1054 | /* active but not in sync implies recovery up to |
| 1055 | * reshape position. We don't know exactly where |
| 1056 | * that is, so set to zero for now */ |
| 1057 | if (mddev->minor_version >= 91) { |
| 1058 | rdev->recovery_offset = 0; |
| 1059 | rdev->raid_disk = desc->raid_disk; |
| 1060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1062 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1063 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1064 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1065 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * sync_super for 0.90.0 |
| 1071 | */ |
| 1072 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1073 | { |
| 1074 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | mdk_rdev_t *rdev2; |
| 1076 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | /* make rdev->sb match mddev data.. |
| 1080 | * |
| 1081 | * 1/ zero out disks |
| 1082 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1083 | * 3/ any empty disks < next_spare become removed |
| 1084 | * |
| 1085 | * disks[0] gets initialised to REMOVED because |
| 1086 | * we cannot be sure from other fields if it has |
| 1087 | * been initialised or not. |
| 1088 | */ |
| 1089 | int i; |
| 1090 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1091 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1092 | rdev->sb_size = MD_SB_BYTES; |
| 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1095 | |
| 1096 | memset(sb, 0, sizeof(*sb)); |
| 1097 | |
| 1098 | sb->md_magic = MD_SB_MAGIC; |
| 1099 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | sb->patch_version = mddev->patch_version; |
| 1101 | sb->gvalid_words = 0; /* ignored */ |
| 1102 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1103 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1104 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1105 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1106 | |
| 1107 | sb->ctime = mddev->ctime; |
| 1108 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1109 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | sb->raid_disks = mddev->raid_disks; |
| 1111 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1112 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | sb->utime = mddev->utime; |
| 1114 | sb->state = 0; |
| 1115 | sb->events_hi = (mddev->events>>32); |
| 1116 | sb->events_lo = (u32)mddev->events; |
| 1117 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1118 | if (mddev->reshape_position == MaxSector) |
| 1119 | sb->minor_version = 90; |
| 1120 | else { |
| 1121 | sb->minor_version = 91; |
| 1122 | sb->reshape_position = mddev->reshape_position; |
| 1123 | sb->new_level = mddev->new_level; |
| 1124 | sb->delta_disks = mddev->delta_disks; |
| 1125 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1126 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1127 | } |
| 1128 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (mddev->in_sync) |
| 1130 | { |
| 1131 | sb->recovery_cp = mddev->recovery_cp; |
| 1132 | sb->cp_events_hi = (mddev->events>>32); |
| 1133 | sb->cp_events_lo = (u32)mddev->events; |
| 1134 | if (mddev->recovery_cp == MaxSector) |
| 1135 | sb->state = (1<< MD_SB_CLEAN); |
| 1136 | } else |
| 1137 | sb->recovery_cp = 0; |
| 1138 | |
| 1139 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1140 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1142 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1143 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1146 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1148 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1149 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1150 | |
| 1151 | if (rdev2->raid_disk >= 0 && |
| 1152 | sb->minor_version >= 91) |
| 1153 | /* we have nowhere to store the recovery_offset, |
| 1154 | * but if it is not below the reshape_position, |
| 1155 | * we can piggy-back on that. |
| 1156 | */ |
| 1157 | is_active = 1; |
| 1158 | if (rdev2->raid_disk < 0 || |
| 1159 | test_bit(Faulty, &rdev2->flags)) |
| 1160 | is_active = 0; |
| 1161 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1162 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1164 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1165 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | d = &sb->disks[rdev2->desc_nr]; |
| 1167 | nr_disks++; |
| 1168 | d->number = rdev2->desc_nr; |
| 1169 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1170 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1171 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | d->raid_disk = rdev2->raid_disk; |
| 1173 | else |
| 1174 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1175 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1177 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1179 | if (test_bit(In_sync, &rdev2->flags)) |
| 1180 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | active++; |
| 1182 | working++; |
| 1183 | } else { |
| 1184 | d->state = 0; |
| 1185 | spare++; |
| 1186 | working++; |
| 1187 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1188 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1189 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1192 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1193 | mdp_disk_t *d = &sb->disks[i]; |
| 1194 | if (d->state == 0 && d->number == 0) { |
| 1195 | d->number = i; |
| 1196 | d->raid_disk = i; |
| 1197 | d->state = (1<<MD_DISK_REMOVED); |
| 1198 | d->state |= (1<<MD_DISK_FAULTY); |
| 1199 | failed++; |
| 1200 | } |
| 1201 | } |
| 1202 | sb->nr_disks = nr_disks; |
| 1203 | sb->active_disks = active; |
| 1204 | sb->working_disks = working; |
| 1205 | sb->failed_disks = failed; |
| 1206 | sb->spare_disks = spare; |
| 1207 | |
| 1208 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1209 | sb->sb_csum = calc_sb_csum(sb); |
| 1210 | } |
| 1211 | |
| 1212 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1213 | * rdev_size_change for 0.90.0 |
| 1214 | */ |
| 1215 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1216 | 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] | 1217 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1218 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1219 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1220 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1221 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1222 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1223 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1224 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1225 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1226 | rdev->sb_page); |
| 1227 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1228 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | * version 1 superblock |
| 1234 | */ |
| 1235 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1236 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1238 | __le32 disk_csum; |
| 1239 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | unsigned long long newcsum; |
| 1241 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1242 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | int i; |
| 1244 | |
| 1245 | disk_csum = sb->sb_csum; |
| 1246 | sb->sb_csum = 0; |
| 1247 | newcsum = 0; |
| 1248 | for (i=0; size>=4; size -= 4 ) |
| 1249 | newcsum += le32_to_cpu(*isuper++); |
| 1250 | |
| 1251 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1252 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
| 1254 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1255 | sb->sb_csum = disk_csum; |
| 1256 | return cpu_to_le32(csum); |
| 1257 | } |
| 1258 | |
| 1259 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1260 | { |
| 1261 | struct mdp_superblock_1 *sb; |
| 1262 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1263 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1265 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
| 1267 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1268 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | * It is always aligned to a 4K boundary and |
| 1270 | * depeding on minor_version, it can be: |
| 1271 | * 0: At least 8K, but less than 12K, from end of device |
| 1272 | * 1: At start of device |
| 1273 | * 2: 4K from start of device. |
| 1274 | */ |
| 1275 | switch(minor_version) { |
| 1276 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1277 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1278 | sb_start -= 8*2; |
| 1279 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1282 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | break; |
| 1284 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1285 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | break; |
| 1287 | default: |
| 1288 | return -EINVAL; |
| 1289 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1290 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1292 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1293 | * and it is safe to read 4k, so we do that |
| 1294 | */ |
| 1295 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | if (ret) return ret; |
| 1297 | |
| 1298 | |
| 1299 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1300 | |
| 1301 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1302 | sb->major_version != cpu_to_le32(1) || |
| 1303 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1304 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1305 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | return -EINVAL; |
| 1307 | |
| 1308 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1309 | printk("md: invalid superblock checksum on %s\n", |
| 1310 | bdevname(rdev->bdev,b)); |
| 1311 | return -EINVAL; |
| 1312 | } |
| 1313 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1314 | printk("md: data_size too small on %s\n", |
| 1315 | bdevname(rdev->bdev,b)); |
| 1316 | return -EINVAL; |
| 1317 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | rdev->preferred_minor = 0xffff; |
| 1320 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1321 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1323 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1324 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1325 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1326 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1327 | |
| 1328 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1329 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1330 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1331 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1332 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1333 | rdev->desc_nr = -1; |
| 1334 | else |
| 1335 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1336 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1337 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1338 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1339 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | __u64 ev1, ev2; |
| 1341 | struct mdp_superblock_1 *refsb = |
| 1342 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1343 | |
| 1344 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1345 | sb->level != refsb->level || |
| 1346 | sb->layout != refsb->layout || |
| 1347 | sb->chunksize != refsb->chunksize) { |
| 1348 | printk(KERN_WARNING "md: %s has strangely different" |
| 1349 | " superblock to %s\n", |
| 1350 | bdevname(rdev->bdev,b), |
| 1351 | bdevname(refdev->bdev,b2)); |
| 1352 | return -EINVAL; |
| 1353 | } |
| 1354 | ev1 = le64_to_cpu(sb->events); |
| 1355 | ev2 = le64_to_cpu(refsb->events); |
| 1356 | |
| 1357 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1358 | ret = 1; |
| 1359 | else |
| 1360 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1362 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1363 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1364 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1366 | rdev->sectors = rdev->sb_start; |
| 1367 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1369 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1370 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1371 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1372 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1376 | { |
| 1377 | 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] | 1378 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1380 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1381 | clear_bit(Faulty, &rdev->flags); |
| 1382 | clear_bit(In_sync, &rdev->flags); |
| 1383 | clear_bit(WriteMostly, &rdev->flags); |
| 1384 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | if (mddev->raid_disks == 0) { |
| 1387 | mddev->major_version = 1; |
| 1388 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1389 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1390 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1392 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1393 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1394 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | mddev->layout = le32_to_cpu(sb->layout); |
| 1396 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1397 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1398 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1399 | mddev->bitmap_info.offset = 0; |
| 1400 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1403 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1404 | |
| 1405 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1406 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1407 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1408 | mddev->bitmap_info.file == NULL ) |
| 1409 | mddev->bitmap_info.offset = |
| 1410 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1411 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1412 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1413 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1414 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1415 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1416 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1417 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1418 | } else { |
| 1419 | mddev->reshape_position = MaxSector; |
| 1420 | mddev->delta_disks = 0; |
| 1421 | mddev->new_level = mddev->level; |
| 1422 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1423 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1426 | } else if (mddev->pers == NULL) { |
| 1427 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | ++ev1; |
| 1429 | if (ev1 < mddev->events) |
| 1430 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1431 | } else if (mddev->bitmap) { |
| 1432 | /* If adding to array with a bitmap, then we can accept an |
| 1433 | * older device, but not too old. |
| 1434 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1435 | if (ev1 < mddev->bitmap->events_cleared) |
| 1436 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1437 | } else { |
| 1438 | if (ev1 < mddev->events) |
| 1439 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1440 | return 0; |
| 1441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1443 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1444 | if (rdev->desc_nr < 0 || |
| 1445 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1446 | role = 0xffff; |
| 1447 | rdev->desc_nr = -1; |
| 1448 | } else |
| 1449 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | switch(role) { |
| 1451 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | break; |
| 1453 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1454 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | break; |
| 1456 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1457 | if ((le32_to_cpu(sb->feature_map) & |
| 1458 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1459 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1460 | else |
| 1461 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | rdev->raid_disk = role; |
| 1463 | break; |
| 1464 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1465 | if (sb->devflags & WriteMostly1) |
| 1466 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1467 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1468 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1474 | { |
| 1475 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | mdk_rdev_t *rdev2; |
| 1477 | int max_dev, i; |
| 1478 | /* make rdev->sb match mddev and rdev data. */ |
| 1479 | |
| 1480 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1481 | |
| 1482 | sb->feature_map = 0; |
| 1483 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1484 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1486 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1487 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1488 | |
| 1489 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1490 | sb->events = cpu_to_le64(mddev->events); |
| 1491 | if (mddev->in_sync) |
| 1492 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1493 | else |
| 1494 | sb->resync_offset = cpu_to_le64(0); |
| 1495 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1496 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1497 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1498 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1499 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1500 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1501 | sb->level = cpu_to_le32(mddev->level); |
| 1502 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1503 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1504 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1505 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1506 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1507 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1508 | |
| 1509 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1510 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1511 | sb->feature_map |= |
| 1512 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1513 | sb->recovery_offset = |
| 1514 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1517 | if (mddev->reshape_position != MaxSector) { |
| 1518 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1519 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1520 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1521 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1522 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1523 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1524 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1527 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | if (rdev2->desc_nr+1 > max_dev) |
| 1529 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1530 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1531 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1532 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1533 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1534 | rdev->sb_size = max_dev * 2 + 256; |
| 1535 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1536 | if (rdev->sb_size & bmask) |
| 1537 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | for (i=0; i<max_dev;i++) |
| 1540 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1541 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1542 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1544 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1546 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1548 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1549 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | else |
| 1551 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1552 | } |
| 1553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1555 | } |
| 1556 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1557 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1558 | 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] | 1559 | { |
| 1560 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1561 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1562 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1563 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1564 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1565 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1566 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1567 | max_sectors -= rdev->data_offset; |
| 1568 | if (!num_sectors || num_sectors > max_sectors) |
| 1569 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1570 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1571 | /* minor version 0 with bitmap we can't move */ |
| 1572 | return 0; |
| 1573 | } else { |
| 1574 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1575 | sector_t sb_start; |
| 1576 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1577 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1578 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1579 | if (!num_sectors || num_sectors > max_sectors) |
| 1580 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1581 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1582 | } |
| 1583 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1584 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1585 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1586 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1587 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1588 | rdev->sb_page); |
| 1589 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1590 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1593 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | [0] = { |
| 1595 | .name = "0.90.0", |
| 1596 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1597 | .load_super = super_90_load, |
| 1598 | .validate_super = super_90_validate, |
| 1599 | .sync_super = super_90_sync, |
| 1600 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | }, |
| 1602 | [1] = { |
| 1603 | .name = "md-1", |
| 1604 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1605 | .load_super = super_1_load, |
| 1606 | .validate_super = super_1_validate, |
| 1607 | .sync_super = super_1_sync, |
| 1608 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | }, |
| 1610 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1613 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1614 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1616 | rcu_read_lock(); |
| 1617 | rdev_for_each_rcu(rdev, mddev1) |
| 1618 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1619 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1620 | rdev2->bdev->bd_contains) { |
| 1621 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1622 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1623 | } |
| 1624 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static LIST_HEAD(pending_raid_disks); |
| 1629 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1630 | /* |
| 1631 | * Try to register data integrity profile for an mddev |
| 1632 | * |
| 1633 | * This is called when an array is started and after a disk has been kicked |
| 1634 | * from the array. It only succeeds if all working and active component devices |
| 1635 | * are integrity capable with matching profiles. |
| 1636 | */ |
| 1637 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1638 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1639 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1640 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1641 | if (list_empty(&mddev->disks)) |
| 1642 | return 0; /* nothing to do */ |
| 1643 | if (blk_get_integrity(mddev->gendisk)) |
| 1644 | return 0; /* already registered */ |
| 1645 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1646 | /* skip spares and non-functional disks */ |
| 1647 | if (test_bit(Faulty, &rdev->flags)) |
| 1648 | continue; |
| 1649 | if (rdev->raid_disk < 0) |
| 1650 | continue; |
| 1651 | /* |
| 1652 | * If at least one rdev is not integrity capable, we can not |
| 1653 | * enable data integrity for the md device. |
| 1654 | */ |
| 1655 | if (!bdev_get_integrity(rdev->bdev)) |
| 1656 | return -EINVAL; |
| 1657 | if (!reference) { |
| 1658 | /* Use the first rdev as the reference */ |
| 1659 | reference = rdev; |
| 1660 | continue; |
| 1661 | } |
| 1662 | /* does this rdev's profile match the reference profile? */ |
| 1663 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1664 | rdev->bdev->bd_disk) < 0) |
| 1665 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1666 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1667 | /* |
| 1668 | * All component devices are integrity capable and have matching |
| 1669 | * profiles, register the common profile for the md device. |
| 1670 | */ |
| 1671 | if (blk_integrity_register(mddev->gendisk, |
| 1672 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1673 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1674 | mdname(mddev)); |
| 1675 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1676 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1677 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1678 | mdname(mddev)); |
| 1679 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1680 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1681 | EXPORT_SYMBOL(md_integrity_register); |
| 1682 | |
| 1683 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1684 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1685 | { |
| 1686 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1687 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1688 | |
| 1689 | if (!bi_mddev) /* nothing to do */ |
| 1690 | return; |
| 1691 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1692 | return; |
| 1693 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1694 | rdev->bdev->bd_disk) >= 0) |
| 1695 | return; |
| 1696 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1697 | blk_integrity_unregister(mddev->gendisk); |
| 1698 | } |
| 1699 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1702 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1703 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1704 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1705 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1706 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | if (rdev->mddev) { |
| 1709 | MD_BUG(); |
| 1710 | return -EINVAL; |
| 1711 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1712 | |
| 1713 | /* prevent duplicates */ |
| 1714 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1715 | return -EEXIST; |
| 1716 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1717 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1718 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1719 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1720 | if (mddev->pers) { |
| 1721 | /* Cannot change size, so fail |
| 1722 | * If mddev->level <= 0, then we don't care |
| 1723 | * about aligning sizes (e.g. linear) |
| 1724 | */ |
| 1725 | if (mddev->level > 0) |
| 1726 | return -ENOSPC; |
| 1727 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1728 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* Verify rdev->desc_nr is unique. |
| 1732 | * If it is -1, assign a free number, else |
| 1733 | * check number is not in use |
| 1734 | */ |
| 1735 | if (rdev->desc_nr < 0) { |
| 1736 | int choice = 0; |
| 1737 | if (mddev->pers) choice = mddev->raid_disks; |
| 1738 | while (find_rdev_nr(mddev, choice)) |
| 1739 | choice++; |
| 1740 | rdev->desc_nr = choice; |
| 1741 | } else { |
| 1742 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1743 | return -EBUSY; |
| 1744 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1745 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1746 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1747 | mdname(mddev), mddev->max_disks); |
| 1748 | return -EBUSY; |
| 1749 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1750 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1751 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1752 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1755 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1756 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1757 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1758 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1759 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1760 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1761 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1762 | kobject_del(&rdev->kobj); |
| 1763 | goto fail; |
| 1764 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1765 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1766 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1767 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1768 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1769 | |
| 1770 | /* May as well allow recovery to be retried once */ |
| 1771 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1774 | |
| 1775 | fail: |
| 1776 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1777 | b, mdname(mddev)); |
| 1778 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1781 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1782 | { |
| 1783 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1784 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1785 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1789 | { |
| 1790 | char b[BDEVNAME_SIZE]; |
| 1791 | if (!rdev->mddev) { |
| 1792 | MD_BUG(); |
| 1793 | return; |
| 1794 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1795 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1796 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1798 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1799 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1800 | sysfs_put(rdev->sysfs_state); |
| 1801 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1802 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1803 | * writing to 'remove' to "dev/state". We also need |
| 1804 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1805 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1806 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1807 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1808 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1809 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * prevent the device from being mounted, repartitioned or |
| 1814 | * otherwise reused by a RAID array (or any other kernel |
| 1815 | * subsystem), by bd_claiming the device. |
| 1816 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1817 | 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] | 1818 | { |
| 1819 | int err = 0; |
| 1820 | struct block_device *bdev; |
| 1821 | char b[BDEVNAME_SIZE]; |
| 1822 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1823 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | if (IS_ERR(bdev)) { |
| 1825 | printk(KERN_ERR "md: could not open %s.\n", |
| 1826 | __bdevname(dev, b)); |
| 1827 | return PTR_ERR(bdev); |
| 1828 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1829 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | if (err) { |
| 1831 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1832 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1833 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | return err; |
| 1835 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1836 | if (!shared) |
| 1837 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | rdev->bdev = bdev; |
| 1839 | return err; |
| 1840 | } |
| 1841 | |
| 1842 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1843 | { |
| 1844 | struct block_device *bdev = rdev->bdev; |
| 1845 | rdev->bdev = NULL; |
| 1846 | if (!bdev) |
| 1847 | MD_BUG(); |
| 1848 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1849 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | void md_autodetect_dev(dev_t dev); |
| 1853 | |
| 1854 | static void export_rdev(mdk_rdev_t * rdev) |
| 1855 | { |
| 1856 | char b[BDEVNAME_SIZE]; |
| 1857 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1858 | bdevname(rdev->bdev,b)); |
| 1859 | if (rdev->mddev) |
| 1860 | MD_BUG(); |
| 1861 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1863 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1864 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | #endif |
| 1866 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1867 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1871 | { |
| 1872 | unbind_rdev_from_array(rdev); |
| 1873 | export_rdev(rdev); |
| 1874 | } |
| 1875 | |
| 1876 | static void export_array(mddev_t *mddev) |
| 1877 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1878 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1880 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | if (!rdev->mddev) { |
| 1882 | MD_BUG(); |
| 1883 | continue; |
| 1884 | } |
| 1885 | kick_rdev_from_array(rdev); |
| 1886 | } |
| 1887 | if (!list_empty(&mddev->disks)) |
| 1888 | MD_BUG(); |
| 1889 | mddev->raid_disks = 0; |
| 1890 | mddev->major_version = 0; |
| 1891 | } |
| 1892 | |
| 1893 | static void print_desc(mdp_disk_t *desc) |
| 1894 | { |
| 1895 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1896 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1897 | } |
| 1898 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1899 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | { |
| 1901 | int i; |
| 1902 | |
| 1903 | printk(KERN_INFO |
| 1904 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1905 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1906 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1907 | sb->ctime); |
| 1908 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1909 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1910 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1911 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1912 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1913 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1914 | sb->failed_disks, sb->spare_disks, |
| 1915 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1916 | |
| 1917 | printk(KERN_INFO); |
| 1918 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1919 | mdp_disk_t *desc; |
| 1920 | |
| 1921 | desc = sb->disks + i; |
| 1922 | if (desc->number || desc->major || desc->minor || |
| 1923 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1924 | printk(" D %2d: ", i); |
| 1925 | print_desc(desc); |
| 1926 | } |
| 1927 | } |
| 1928 | printk(KERN_INFO "md: THIS: "); |
| 1929 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1932 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1933 | { |
| 1934 | __u8 *uuid; |
| 1935 | |
| 1936 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1937 | printk(KERN_INFO |
| 1938 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1939 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1940 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1941 | le32_to_cpu(sb->major_version), |
| 1942 | le32_to_cpu(sb->feature_map), |
| 1943 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1944 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1945 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1946 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1947 | sb->set_name, |
| 1948 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1949 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1950 | |
| 1951 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1952 | printk(KERN_INFO |
| 1953 | "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] | 1954 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1955 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1956 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1957 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1958 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1959 | le32_to_cpu(sb->level), |
| 1960 | (unsigned long long)le64_to_cpu(sb->size), |
| 1961 | le32_to_cpu(sb->raid_disks), |
| 1962 | le32_to_cpu(sb->layout), |
| 1963 | le32_to_cpu(sb->chunksize), |
| 1964 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1965 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1966 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1967 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1968 | le32_to_cpu(sb->dev_number), |
| 1969 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1970 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1971 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1972 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1973 | sb->devflags, |
| 1974 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1975 | (unsigned long long)le64_to_cpu(sb->events), |
| 1976 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1977 | le32_to_cpu(sb->sb_csum), |
| 1978 | le32_to_cpu(sb->max_dev) |
| 1979 | ); |
| 1980 | } |
| 1981 | |
| 1982 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | { |
| 1984 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1985 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1986 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1987 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1988 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1990 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1991 | switch (major_version) { |
| 1992 | case 0: |
| 1993 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1994 | break; |
| 1995 | case 1: |
| 1996 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1997 | break; |
| 1998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | } else |
| 2000 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2001 | } |
| 2002 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2003 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2005 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | mdk_rdev_t *rdev; |
| 2007 | mddev_t *mddev; |
| 2008 | char b[BDEVNAME_SIZE]; |
| 2009 | |
| 2010 | printk("\n"); |
| 2011 | printk("md: **********************************\n"); |
| 2012 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2013 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2014 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2016 | if (mddev->bitmap) |
| 2017 | bitmap_print_sb(mddev->bitmap); |
| 2018 | else |
| 2019 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2020 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2022 | printk("\n"); |
| 2023 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2024 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2025 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
| 2027 | printk("md: **********************************\n"); |
| 2028 | printk("\n"); |
| 2029 | } |
| 2030 | |
| 2031 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2032 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2034 | /* Update each superblock (in-memory image), but |
| 2035 | * if we are allowed to, skip spares which already |
| 2036 | * have the right event counter, or have one earlier |
| 2037 | * (which would mean they aren't being marked as dirty |
| 2038 | * with the rest of the array) |
| 2039 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2042 | /* First make sure individual recovery_offsets are correct */ |
| 2043 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2044 | if (rdev->raid_disk >= 0 && |
| 2045 | !test_bit(In_sync, &rdev->flags) && |
| 2046 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2047 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2048 | |
| 2049 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2050 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2051 | if (rdev->sb_events == mddev->events || |
| 2052 | (nospares && |
| 2053 | rdev->raid_disk < 0 && |
| 2054 | (rdev->sb_events&1)==0 && |
| 2055 | rdev->sb_events+1 == mddev->events)) { |
| 2056 | /* Don't update this superblock */ |
| 2057 | rdev->sb_loaded = 2; |
| 2058 | } else { |
| 2059 | super_types[mddev->major_version]. |
| 2060 | sync_super(mddev, rdev); |
| 2061 | rdev->sb_loaded = 1; |
| 2062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2066 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2069 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2070 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2072 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2073 | if (mddev->external) |
| 2074 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2076 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2077 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2078 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2079 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2080 | force_change = 1; |
| 2081 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2082 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2083 | * though if events isn't the right even/odd, we will have to do |
| 2084 | * spares after all |
| 2085 | */ |
| 2086 | nospares = 1; |
| 2087 | if (force_change) |
| 2088 | nospares = 0; |
| 2089 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2090 | /* If the array is degraded, then skipping spares is both |
| 2091 | * dangerous and fairly pointless. |
| 2092 | * Dangerous because a device that was removed from the array |
| 2093 | * might have a event_count that still looks up-to-date, |
| 2094 | * so it can be re-added without a resync. |
| 2095 | * Pointless because if there are any spares to skip, |
| 2096 | * then a recovery will happen and soon that array won't |
| 2097 | * be degraded any more and the spare can go back to sleep then. |
| 2098 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2099 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2100 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2101 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2102 | |
| 2103 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2104 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2105 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2106 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2107 | && (mddev->events & 1) |
| 2108 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2109 | mddev->events--; |
| 2110 | else { |
| 2111 | /* otherwise we have to go forward and ... */ |
| 2112 | mddev->events ++; |
| 2113 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2114 | /* .. if the array isn't clean, an 'even' event must also go |
| 2115 | * to spares. */ |
| 2116 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2117 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2118 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2119 | /* otherwise an 'odd' event must go to spares */ |
| 2120 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2121 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2122 | } |
| 2123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | if (!mddev->events) { |
| 2126 | /* |
| 2127 | * oops, this 64-bit counter should never wrap. |
| 2128 | * Either we are in around ~1 trillion A.C., assuming |
| 2129 | * 1 reboot per second, or we have a bug: |
| 2130 | */ |
| 2131 | MD_BUG(); |
| 2132 | mddev->events --; |
| 2133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | |
| 2135 | /* |
| 2136 | * do not write anything to disk if using |
| 2137 | * nonpersistent superblocks |
| 2138 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2139 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2140 | if (!mddev->external) |
| 2141 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2142 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2143 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2144 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2146 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2147 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2148 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | |
| 2150 | dprintk(KERN_INFO |
| 2151 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2152 | mdname(mddev),mddev->in_sync); |
| 2153 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2154 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2155 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | char b[BDEVNAME_SIZE]; |
| 2157 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2158 | if (rdev->sb_loaded != 1) |
| 2159 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2160 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | dprintk("(skipping faulty "); |
| 2162 | |
| 2163 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2164 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2165 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2166 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2167 | rdev->sb_page); |
| 2168 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2169 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2170 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2171 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } else |
| 2174 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2175 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | /* only need to write one superblock... */ |
| 2177 | break; |
| 2178 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2179 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2180 | /* 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] | 2181 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2182 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2183 | if (mddev->in_sync != sync_req || |
| 2184 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2185 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2186 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2187 | goto repeat; |
| 2188 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2189 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2190 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2191 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2192 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2193 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2197 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2198 | * We want to accept with case. For this we use cmd_match. |
| 2199 | */ |
| 2200 | static int cmd_match(const char *cmd, const char *str) |
| 2201 | { |
| 2202 | /* See if cmd, written into a sysfs file, matches |
| 2203 | * str. They must either be the same, or cmd can |
| 2204 | * have a trailing newline |
| 2205 | */ |
| 2206 | while (*cmd && *str && *cmd == *str) { |
| 2207 | cmd++; |
| 2208 | str++; |
| 2209 | } |
| 2210 | if (*cmd == '\n') |
| 2211 | cmd++; |
| 2212 | if (*str || *cmd) |
| 2213 | return 0; |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2217 | struct rdev_sysfs_entry { |
| 2218 | struct attribute attr; |
| 2219 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2220 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2221 | }; |
| 2222 | |
| 2223 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2224 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2225 | { |
| 2226 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2227 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2228 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2229 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2230 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2231 | sep = ","; |
| 2232 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2233 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2234 | len += sprintf(page+len, "%sin_sync",sep); |
| 2235 | sep = ","; |
| 2236 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2237 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2238 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2239 | sep = ","; |
| 2240 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2241 | if (test_bit(Blocked, &rdev->flags)) { |
| 2242 | len += sprintf(page+len, "%sblocked", sep); |
| 2243 | sep = ","; |
| 2244 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2245 | if (!test_bit(Faulty, &rdev->flags) && |
| 2246 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2247 | len += sprintf(page+len, "%sspare", sep); |
| 2248 | sep = ","; |
| 2249 | } |
| 2250 | return len+sprintf(page+len, "\n"); |
| 2251 | } |
| 2252 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2253 | static ssize_t |
| 2254 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2255 | { |
| 2256 | /* can write |
| 2257 | * faulty - simulates and error |
| 2258 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2259 | * writemostly - sets write_mostly |
| 2260 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2261 | * blocked - sets the Blocked flag |
| 2262 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2263 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2264 | */ |
| 2265 | int err = -EINVAL; |
| 2266 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2267 | md_error(rdev->mddev, rdev); |
| 2268 | err = 0; |
| 2269 | } else if (cmd_match(buf, "remove")) { |
| 2270 | if (rdev->raid_disk >= 0) |
| 2271 | err = -EBUSY; |
| 2272 | else { |
| 2273 | mddev_t *mddev = rdev->mddev; |
| 2274 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2275 | if (mddev->pers) |
| 2276 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2277 | md_new_event(mddev); |
| 2278 | err = 0; |
| 2279 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2280 | } else if (cmd_match(buf, "writemostly")) { |
| 2281 | set_bit(WriteMostly, &rdev->flags); |
| 2282 | err = 0; |
| 2283 | } else if (cmd_match(buf, "-writemostly")) { |
| 2284 | clear_bit(WriteMostly, &rdev->flags); |
| 2285 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2286 | } else if (cmd_match(buf, "blocked")) { |
| 2287 | set_bit(Blocked, &rdev->flags); |
| 2288 | err = 0; |
| 2289 | } else if (cmd_match(buf, "-blocked")) { |
| 2290 | clear_bit(Blocked, &rdev->flags); |
| 2291 | wake_up(&rdev->blocked_wait); |
| 2292 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2293 | md_wakeup_thread(rdev->mddev->thread); |
| 2294 | |
| 2295 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2296 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2297 | set_bit(In_sync, &rdev->flags); |
| 2298 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2299 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2300 | if (!err && rdev->sysfs_state) |
| 2301 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2302 | return err ? err : len; |
| 2303 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2304 | static struct rdev_sysfs_entry rdev_state = |
| 2305 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2306 | |
| 2307 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2308 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2309 | { |
| 2310 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2311 | } |
| 2312 | |
| 2313 | static ssize_t |
| 2314 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2315 | { |
| 2316 | char *e; |
| 2317 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2318 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2319 | atomic_set(&rdev->corrected_errors, n); |
| 2320 | return len; |
| 2321 | } |
| 2322 | return -EINVAL; |
| 2323 | } |
| 2324 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2325 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2326 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2327 | static ssize_t |
| 2328 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2329 | { |
| 2330 | if (rdev->raid_disk < 0) |
| 2331 | return sprintf(page, "none\n"); |
| 2332 | else |
| 2333 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2334 | } |
| 2335 | |
| 2336 | static ssize_t |
| 2337 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2338 | { |
| 2339 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2340 | int err; |
| 2341 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2342 | int slot = simple_strtoul(buf, &e, 10); |
| 2343 | if (strncmp(buf, "none", 4)==0) |
| 2344 | slot = -1; |
| 2345 | else if (e==buf || (*e && *e!= '\n')) |
| 2346 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2347 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2348 | /* Setting 'slot' on an active array requires also |
| 2349 | * updating the 'rd%d' link, and communicating |
| 2350 | * with the personality with ->hot_*_disk. |
| 2351 | * For now we only support removing |
| 2352 | * failed/spare devices. This normally happens automatically, |
| 2353 | * but not when the metadata is externally managed. |
| 2354 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2355 | if (rdev->raid_disk == -1) |
| 2356 | return -EEXIST; |
| 2357 | /* personality does all needed checks */ |
| 2358 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2359 | return -EINVAL; |
| 2360 | err = rdev->mddev->pers-> |
| 2361 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2362 | if (err) |
| 2363 | return err; |
| 2364 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2365 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2366 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2367 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2368 | } else if (rdev->mddev->pers) { |
| 2369 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2370 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2371 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2372 | */ |
| 2373 | |
| 2374 | if (rdev->raid_disk != -1) |
| 2375 | return -EBUSY; |
| 2376 | |
| 2377 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2378 | return -EINVAL; |
| 2379 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2380 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2381 | if (rdev2->raid_disk == slot) |
| 2382 | return -EEXIST; |
| 2383 | |
| 2384 | rdev->raid_disk = slot; |
| 2385 | if (test_bit(In_sync, &rdev->flags)) |
| 2386 | rdev->saved_raid_disk = slot; |
| 2387 | else |
| 2388 | rdev->saved_raid_disk = -1; |
| 2389 | err = rdev->mddev->pers-> |
| 2390 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2391 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2392 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2393 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2394 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2395 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2396 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2397 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2398 | printk(KERN_WARNING |
| 2399 | "md: cannot register " |
| 2400 | "%s for %s\n", |
| 2401 | nm, mdname(rdev->mddev)); |
| 2402 | |
| 2403 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2404 | } else { |
| 2405 | if (slot >= rdev->mddev->raid_disks) |
| 2406 | return -ENOSPC; |
| 2407 | rdev->raid_disk = slot; |
| 2408 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2409 | clear_bit(Faulty, &rdev->flags); |
| 2410 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2411 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2412 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2413 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2414 | return len; |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2419 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2420 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2421 | static ssize_t |
| 2422 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2423 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2424 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | static ssize_t |
| 2428 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2429 | { |
| 2430 | char *e; |
| 2431 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2432 | if (e==buf || (*e && *e != '\n')) |
| 2433 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2434 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2435 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2436 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2437 | /* Must set offset before size, so overlap checks |
| 2438 | * can be sane */ |
| 2439 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2440 | rdev->data_offset = offset; |
| 2441 | return len; |
| 2442 | } |
| 2443 | |
| 2444 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2445 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2446 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2447 | static ssize_t |
| 2448 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2449 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2450 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2451 | } |
| 2452 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2453 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2454 | { |
| 2455 | /* check if two start/length pairs overlap */ |
| 2456 | if (s1+l1 <= s2) |
| 2457 | return 0; |
| 2458 | if (s2+l2 <= s1) |
| 2459 | return 0; |
| 2460 | return 1; |
| 2461 | } |
| 2462 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2463 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2464 | { |
| 2465 | unsigned long long blocks; |
| 2466 | sector_t new; |
| 2467 | |
| 2468 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2469 | return -EINVAL; |
| 2470 | |
| 2471 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2472 | return -EINVAL; /* sector conversion overflow */ |
| 2473 | |
| 2474 | new = blocks * 2; |
| 2475 | if (new != blocks * 2) |
| 2476 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2477 | |
| 2478 | *sectors = new; |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2482 | static ssize_t |
| 2483 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2484 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2485 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2486 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2487 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2488 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2489 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2490 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2491 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2492 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2493 | sectors = super_types[my_mddev->major_version]. |
| 2494 | rdev_size_change(rdev, sectors); |
| 2495 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2496 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2497 | } else if (!sectors) |
| 2498 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2499 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2500 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2501 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2502 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2503 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2504 | rdev->sectors = sectors; |
| 2505 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2506 | /* need to check that all other rdevs with the same ->bdev |
| 2507 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2508 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2509 | * we have to change it back, we will have the lock again. |
| 2510 | */ |
| 2511 | mddev_t *mddev; |
| 2512 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2513 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2514 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2515 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2516 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2517 | mdk_rdev_t *rdev2; |
| 2518 | |
| 2519 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2520 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2521 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2522 | (rdev->bdev == rdev2->bdev && |
| 2523 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2524 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2525 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2526 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2527 | overlap = 1; |
| 2528 | break; |
| 2529 | } |
| 2530 | mddev_unlock(mddev); |
| 2531 | if (overlap) { |
| 2532 | mddev_put(mddev); |
| 2533 | break; |
| 2534 | } |
| 2535 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2536 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2537 | if (overlap) { |
| 2538 | /* Someone else could have slipped in a size |
| 2539 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2540 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2541 | * safe, and trust userspace not to race with |
| 2542 | * itself |
| 2543 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2544 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2545 | return -EBUSY; |
| 2546 | } |
| 2547 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2548 | return len; |
| 2549 | } |
| 2550 | |
| 2551 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2552 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2553 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame^] | 2554 | |
| 2555 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2556 | { |
| 2557 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2558 | |
| 2559 | if (test_bit(In_sync, &rdev->flags) || |
| 2560 | recovery_start == MaxSector) |
| 2561 | return sprintf(page, "none\n"); |
| 2562 | |
| 2563 | return sprintf(page, "%llu\n", recovery_start); |
| 2564 | } |
| 2565 | |
| 2566 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2567 | { |
| 2568 | unsigned long long recovery_start; |
| 2569 | |
| 2570 | if (cmd_match(buf, "none")) |
| 2571 | recovery_start = MaxSector; |
| 2572 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2573 | return -EINVAL; |
| 2574 | |
| 2575 | if (rdev->mddev->pers && |
| 2576 | rdev->raid_disk >= 0) |
| 2577 | return -EBUSY; |
| 2578 | |
| 2579 | rdev->recovery_offset = recovery_start; |
| 2580 | if (recovery_start == MaxSector) |
| 2581 | set_bit(In_sync, &rdev->flags); |
| 2582 | else |
| 2583 | clear_bit(In_sync, &rdev->flags); |
| 2584 | return len; |
| 2585 | } |
| 2586 | |
| 2587 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2588 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2589 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2590 | static struct attribute *rdev_default_attrs[] = { |
| 2591 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2592 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2593 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2594 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2595 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame^] | 2596 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2597 | NULL, |
| 2598 | }; |
| 2599 | static ssize_t |
| 2600 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2601 | { |
| 2602 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2603 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2604 | mddev_t *mddev = rdev->mddev; |
| 2605 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2606 | |
| 2607 | if (!entry->show) |
| 2608 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2609 | |
| 2610 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2611 | if (!rv) { |
| 2612 | if (rdev->mddev == NULL) |
| 2613 | rv = -EBUSY; |
| 2614 | else |
| 2615 | rv = entry->show(rdev, page); |
| 2616 | mddev_unlock(mddev); |
| 2617 | } |
| 2618 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | static ssize_t |
| 2622 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2623 | const char *page, size_t length) |
| 2624 | { |
| 2625 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2626 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2627 | ssize_t rv; |
| 2628 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2629 | |
| 2630 | if (!entry->store) |
| 2631 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2632 | if (!capable(CAP_SYS_ADMIN)) |
| 2633 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2634 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2635 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2636 | if (rdev->mddev == NULL) |
| 2637 | rv = -EBUSY; |
| 2638 | else |
| 2639 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2640 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2641 | } |
| 2642 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | static void rdev_free(struct kobject *ko) |
| 2646 | { |
| 2647 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2648 | kfree(rdev); |
| 2649 | } |
| 2650 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2651 | .show = rdev_attr_show, |
| 2652 | .store = rdev_attr_store, |
| 2653 | }; |
| 2654 | static struct kobj_type rdev_ktype = { |
| 2655 | .release = rdev_free, |
| 2656 | .sysfs_ops = &rdev_sysfs_ops, |
| 2657 | .default_attrs = rdev_default_attrs, |
| 2658 | }; |
| 2659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | /* |
| 2661 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2662 | * |
| 2663 | * mark the device faulty if: |
| 2664 | * |
| 2665 | * - the device is nonexistent (zero size) |
| 2666 | * - the device has no valid superblock |
| 2667 | * |
| 2668 | * a faulty rdev _never_ has rdev->sb set. |
| 2669 | */ |
| 2670 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2671 | { |
| 2672 | char b[BDEVNAME_SIZE]; |
| 2673 | int err; |
| 2674 | mdk_rdev_t *rdev; |
| 2675 | sector_t size; |
| 2676 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2677 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | if (!rdev) { |
| 2679 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2680 | return ERR_PTR(-ENOMEM); |
| 2681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
| 2683 | if ((err = alloc_disk_sb(rdev))) |
| 2684 | goto abort_free; |
| 2685 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2686 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | if (err) |
| 2688 | goto abort_free; |
| 2689 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2690 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2693 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2694 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2695 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2697 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2698 | rdev->last_read_error.tv_sec = 0; |
| 2699 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2701 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2702 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
| 2704 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2705 | if (!size) { |
| 2706 | printk(KERN_WARNING |
| 2707 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2708 | bdevname(rdev->bdev,b)); |
| 2709 | err = -EINVAL; |
| 2710 | goto abort_free; |
| 2711 | } |
| 2712 | |
| 2713 | if (super_format >= 0) { |
| 2714 | err = super_types[super_format]. |
| 2715 | load_super(rdev, NULL, super_minor); |
| 2716 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2717 | printk(KERN_WARNING |
| 2718 | "md: %s does not have a valid v%d.%d " |
| 2719 | "superblock, not importing!\n", |
| 2720 | bdevname(rdev->bdev,b), |
| 2721 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | goto abort_free; |
| 2723 | } |
| 2724 | if (err < 0) { |
| 2725 | printk(KERN_WARNING |
| 2726 | "md: could not read %s's sb, not importing!\n", |
| 2727 | bdevname(rdev->bdev,b)); |
| 2728 | goto abort_free; |
| 2729 | } |
| 2730 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2733 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
| 2735 | return rdev; |
| 2736 | |
| 2737 | abort_free: |
| 2738 | if (rdev->sb_page) { |
| 2739 | if (rdev->bdev) |
| 2740 | unlock_rdev(rdev); |
| 2741 | free_disk_sb(rdev); |
| 2742 | } |
| 2743 | kfree(rdev); |
| 2744 | return ERR_PTR(err); |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Check a full RAID array for plausibility |
| 2749 | */ |
| 2750 | |
| 2751 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2752 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | { |
| 2754 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2755 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | char b[BDEVNAME_SIZE]; |
| 2757 | |
| 2758 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2759 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | switch (super_types[mddev->major_version]. |
| 2761 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2762 | case 1: |
| 2763 | freshest = rdev; |
| 2764 | break; |
| 2765 | case 0: |
| 2766 | break; |
| 2767 | default: |
| 2768 | printk( KERN_ERR \ |
| 2769 | "md: fatal superblock inconsistency in %s" |
| 2770 | " -- removing from array\n", |
| 2771 | bdevname(rdev->bdev,b)); |
| 2772 | kick_rdev_from_array(rdev); |
| 2773 | } |
| 2774 | |
| 2775 | |
| 2776 | super_types[mddev->major_version]. |
| 2777 | validate_super(mddev, freshest); |
| 2778 | |
| 2779 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2780 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2781 | if (rdev->desc_nr >= mddev->max_disks || |
| 2782 | i > mddev->max_disks) { |
| 2783 | printk(KERN_WARNING |
| 2784 | "md: %s: %s: only %d devices permitted\n", |
| 2785 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2786 | mddev->max_disks); |
| 2787 | kick_rdev_from_array(rdev); |
| 2788 | continue; |
| 2789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | if (rdev != freshest) |
| 2791 | if (super_types[mddev->major_version]. |
| 2792 | validate_super(mddev, rdev)) { |
| 2793 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2794 | " from array!\n", |
| 2795 | bdevname(rdev->bdev,b)); |
| 2796 | kick_rdev_from_array(rdev); |
| 2797 | continue; |
| 2798 | } |
| 2799 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2800 | rdev->desc_nr = i++; |
| 2801 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2802 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2803 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2804 | rdev->raid_disk = -1; |
| 2805 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | } |
| 2807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2810 | /* Read a fixed-point number. |
| 2811 | * Numbers in sysfs attributes should be in "standard" units where |
| 2812 | * possible, so time should be in seconds. |
| 2813 | * However we internally use a a much smaller unit such as |
| 2814 | * milliseconds or jiffies. |
| 2815 | * This function takes a decimal number with a possible fractional |
| 2816 | * component, and produces an integer which is the result of |
| 2817 | * multiplying that number by 10^'scale'. |
| 2818 | * all without any floating-point arithmetic. |
| 2819 | */ |
| 2820 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2821 | { |
| 2822 | unsigned long result = 0; |
| 2823 | long decimals = -1; |
| 2824 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2825 | if (*cp == '.') |
| 2826 | decimals = 0; |
| 2827 | else if (decimals < scale) { |
| 2828 | unsigned int value; |
| 2829 | value = *cp - '0'; |
| 2830 | result = result * 10 + value; |
| 2831 | if (decimals >= 0) |
| 2832 | decimals++; |
| 2833 | } |
| 2834 | cp++; |
| 2835 | } |
| 2836 | if (*cp == '\n') |
| 2837 | cp++; |
| 2838 | if (*cp) |
| 2839 | return -EINVAL; |
| 2840 | if (decimals < 0) |
| 2841 | decimals = 0; |
| 2842 | while (decimals < scale) { |
| 2843 | result *= 10; |
| 2844 | decimals ++; |
| 2845 | } |
| 2846 | *res = result; |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2851 | static void md_safemode_timeout(unsigned long data); |
| 2852 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2853 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2854 | safe_delay_show(mddev_t *mddev, char *page) |
| 2855 | { |
| 2856 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2857 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2858 | } |
| 2859 | static ssize_t |
| 2860 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2861 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2862 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2863 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2864 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2865 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2866 | if (msec == 0) |
| 2867 | mddev->safemode_delay = 0; |
| 2868 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2869 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2870 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2871 | if (mddev->safemode_delay == 0) |
| 2872 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2873 | if (mddev->safemode_delay < old_delay) |
| 2874 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2875 | } |
| 2876 | return len; |
| 2877 | } |
| 2878 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2879 | __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] | 2880 | |
| 2881 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2882 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2883 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2884 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2885 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2886 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2887 | else if (mddev->clevel[0]) |
| 2888 | return sprintf(page, "%s\n", mddev->clevel); |
| 2889 | else if (mddev->level != LEVEL_NONE) |
| 2890 | return sprintf(page, "%d\n", mddev->level); |
| 2891 | else |
| 2892 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2893 | } |
| 2894 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2895 | static ssize_t |
| 2896 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2897 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2898 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2899 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2900 | struct mdk_personality *pers; |
| 2901 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2902 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2903 | |
| 2904 | if (mddev->pers == NULL) { |
| 2905 | if (len == 0) |
| 2906 | return 0; |
| 2907 | if (len >= sizeof(mddev->clevel)) |
| 2908 | return -ENOSPC; |
| 2909 | strncpy(mddev->clevel, buf, len); |
| 2910 | if (mddev->clevel[len-1] == '\n') |
| 2911 | len--; |
| 2912 | mddev->clevel[len] = 0; |
| 2913 | mddev->level = LEVEL_NONE; |
| 2914 | return rv; |
| 2915 | } |
| 2916 | |
| 2917 | /* request to change the personality. Need to ensure: |
| 2918 | * - array is not engaged in resync/recovery/reshape |
| 2919 | * - old personality can be suspended |
| 2920 | * - new personality will access other array. |
| 2921 | */ |
| 2922 | |
| 2923 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2924 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2925 | |
| 2926 | if (!mddev->pers->quiesce) { |
| 2927 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2928 | mdname(mddev), mddev->pers->name); |
| 2929 | return -EINVAL; |
| 2930 | } |
| 2931 | |
| 2932 | /* Now find the new personality */ |
| 2933 | if (len == 0 || len >= sizeof(level)) |
| 2934 | return -EINVAL; |
| 2935 | strncpy(level, buf, len); |
| 2936 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2937 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2938 | level[len] = 0; |
| 2939 | |
| 2940 | request_module("md-%s", level); |
| 2941 | spin_lock(&pers_lock); |
| 2942 | pers = find_pers(LEVEL_NONE, level); |
| 2943 | if (!pers || !try_module_get(pers->owner)) { |
| 2944 | spin_unlock(&pers_lock); |
| 2945 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2946 | return -EINVAL; |
| 2947 | } |
| 2948 | spin_unlock(&pers_lock); |
| 2949 | |
| 2950 | if (pers == mddev->pers) { |
| 2951 | /* Nothing to do! */ |
| 2952 | module_put(pers->owner); |
| 2953 | return rv; |
| 2954 | } |
| 2955 | if (!pers->takeover) { |
| 2956 | module_put(pers->owner); |
| 2957 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2958 | mdname(mddev), level); |
| 2959 | return -EINVAL; |
| 2960 | } |
| 2961 | |
| 2962 | /* ->takeover must set new_* and/or delta_disks |
| 2963 | * if it succeeds, and may set them when it fails. |
| 2964 | */ |
| 2965 | priv = pers->takeover(mddev); |
| 2966 | if (IS_ERR(priv)) { |
| 2967 | mddev->new_level = mddev->level; |
| 2968 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2969 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2970 | mddev->raid_disks -= mddev->delta_disks; |
| 2971 | mddev->delta_disks = 0; |
| 2972 | module_put(pers->owner); |
| 2973 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2974 | mdname(mddev), level); |
| 2975 | return PTR_ERR(priv); |
| 2976 | } |
| 2977 | |
| 2978 | /* Looks like we have a winner */ |
| 2979 | mddev_suspend(mddev); |
| 2980 | mddev->pers->stop(mddev); |
| 2981 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2982 | /* Invalidate devices that are now superfluous */ |
| 2983 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2984 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2985 | rdev->raid_disk = -1; |
| 2986 | clear_bit(In_sync, &rdev->flags); |
| 2987 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2988 | mddev->pers = pers; |
| 2989 | mddev->private = priv; |
| 2990 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2991 | mddev->level = mddev->new_level; |
| 2992 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2993 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2994 | mddev->delta_disks = 0; |
| 2995 | pers->run(mddev); |
| 2996 | mddev_resume(mddev); |
| 2997 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2998 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2999 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3000 | return rv; |
| 3001 | } |
| 3002 | |
| 3003 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3004 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3005 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3006 | |
| 3007 | static ssize_t |
| 3008 | layout_show(mddev_t *mddev, char *page) |
| 3009 | { |
| 3010 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3011 | if (mddev->reshape_position != MaxSector && |
| 3012 | mddev->layout != mddev->new_layout) |
| 3013 | return sprintf(page, "%d (%d)\n", |
| 3014 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3015 | return sprintf(page, "%d\n", mddev->layout); |
| 3016 | } |
| 3017 | |
| 3018 | static ssize_t |
| 3019 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3020 | { |
| 3021 | char *e; |
| 3022 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3023 | |
| 3024 | if (!*buf || (*e && *e != '\n')) |
| 3025 | return -EINVAL; |
| 3026 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3027 | if (mddev->pers) { |
| 3028 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3029 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3030 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3031 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3032 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3033 | if (err) { |
| 3034 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3035 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3036 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3037 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3038 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3039 | if (mddev->reshape_position == MaxSector) |
| 3040 | mddev->layout = n; |
| 3041 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3042 | return len; |
| 3043 | } |
| 3044 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3045 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3046 | |
| 3047 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3048 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3049 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3050 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3051 | if (mddev->raid_disks == 0) |
| 3052 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3053 | if (mddev->reshape_position != MaxSector && |
| 3054 | mddev->delta_disks != 0) |
| 3055 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3056 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3057 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3058 | } |
| 3059 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3060 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3061 | |
| 3062 | static ssize_t |
| 3063 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3064 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3065 | char *e; |
| 3066 | int rv = 0; |
| 3067 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3068 | |
| 3069 | if (!*buf || (*e && *e != '\n')) |
| 3070 | return -EINVAL; |
| 3071 | |
| 3072 | if (mddev->pers) |
| 3073 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3074 | else if (mddev->reshape_position != MaxSector) { |
| 3075 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3076 | mddev->delta_disks = n - olddisks; |
| 3077 | mddev->raid_disks = n; |
| 3078 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3079 | mddev->raid_disks = n; |
| 3080 | return rv ? rv : len; |
| 3081 | } |
| 3082 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3083 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3084 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3085 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3086 | chunk_size_show(mddev_t *mddev, char *page) |
| 3087 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3088 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3089 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3090 | return sprintf(page, "%d (%d)\n", |
| 3091 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3092 | mddev->chunk_sectors << 9); |
| 3093 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | static ssize_t |
| 3097 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3098 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3099 | char *e; |
| 3100 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3101 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3102 | if (!*buf || (*e && *e != '\n')) |
| 3103 | return -EINVAL; |
| 3104 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3105 | if (mddev->pers) { |
| 3106 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3107 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3108 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3109 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3110 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3111 | if (err) { |
| 3112 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3113 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3114 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3115 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3116 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3117 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3118 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3119 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3120 | return len; |
| 3121 | } |
| 3122 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3123 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3124 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3125 | static ssize_t |
| 3126 | resync_start_show(mddev_t *mddev, char *page) |
| 3127 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3128 | if (mddev->recovery_cp == MaxSector) |
| 3129 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3130 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3131 | } |
| 3132 | |
| 3133 | static ssize_t |
| 3134 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3135 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3136 | char *e; |
| 3137 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3138 | |
| 3139 | if (mddev->pers) |
| 3140 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame^] | 3141 | if (cmd_match(buf, "none")) |
| 3142 | n = MaxSector; |
| 3143 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3144 | return -EINVAL; |
| 3145 | |
| 3146 | mddev->recovery_cp = n; |
| 3147 | return len; |
| 3148 | } |
| 3149 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3150 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3151 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3152 | /* |
| 3153 | * The array state can be: |
| 3154 | * |
| 3155 | * clear |
| 3156 | * No devices, no size, no level |
| 3157 | * Equivalent to STOP_ARRAY ioctl |
| 3158 | * inactive |
| 3159 | * May have some settings, but array is not active |
| 3160 | * all IO results in error |
| 3161 | * When written, doesn't tear down array, but just stops it |
| 3162 | * suspended (not supported yet) |
| 3163 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3164 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3165 | * readonly |
| 3166 | * no resync can happen. no superblocks get written. |
| 3167 | * write requests fail |
| 3168 | * read-auto |
| 3169 | * like readonly, but behaves like 'clean' on a write request. |
| 3170 | * |
| 3171 | * clean - no pending writes, but otherwise active. |
| 3172 | * When written to inactive array, starts without resync |
| 3173 | * If a write request arrives then |
| 3174 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3175 | * if not known, block and switch to write-pending |
| 3176 | * If written to an active array that has pending writes, then fails. |
| 3177 | * active |
| 3178 | * fully active: IO and resync can be happening. |
| 3179 | * When written to inactive array, starts with resync |
| 3180 | * |
| 3181 | * write-pending |
| 3182 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3183 | * |
| 3184 | * active-idle |
| 3185 | * like active, but no writes have been seen for a while (100msec). |
| 3186 | * |
| 3187 | */ |
| 3188 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3189 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3190 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3191 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3192 | "write-pending", "active-idle", NULL }; |
| 3193 | |
| 3194 | static int match_word(const char *word, char **list) |
| 3195 | { |
| 3196 | int n; |
| 3197 | for (n=0; list[n]; n++) |
| 3198 | if (cmd_match(word, list[n])) |
| 3199 | break; |
| 3200 | return n; |
| 3201 | } |
| 3202 | |
| 3203 | static ssize_t |
| 3204 | array_state_show(mddev_t *mddev, char *page) |
| 3205 | { |
| 3206 | enum array_state st = inactive; |
| 3207 | |
| 3208 | if (mddev->pers) |
| 3209 | switch(mddev->ro) { |
| 3210 | case 1: |
| 3211 | st = readonly; |
| 3212 | break; |
| 3213 | case 2: |
| 3214 | st = read_auto; |
| 3215 | break; |
| 3216 | case 0: |
| 3217 | if (mddev->in_sync) |
| 3218 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3219 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3220 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3221 | else if (mddev->safemode) |
| 3222 | st = active_idle; |
| 3223 | else |
| 3224 | st = active; |
| 3225 | } |
| 3226 | else { |
| 3227 | if (list_empty(&mddev->disks) && |
| 3228 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3229 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3230 | st = clear; |
| 3231 | else |
| 3232 | st = inactive; |
| 3233 | } |
| 3234 | return sprintf(page, "%s\n", array_states[st]); |
| 3235 | } |
| 3236 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3237 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3238 | static int do_md_run(mddev_t * mddev); |
| 3239 | static int restart_array(mddev_t *mddev); |
| 3240 | |
| 3241 | static ssize_t |
| 3242 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3243 | { |
| 3244 | int err = -EINVAL; |
| 3245 | enum array_state st = match_word(buf, array_states); |
| 3246 | switch(st) { |
| 3247 | case bad_word: |
| 3248 | break; |
| 3249 | case clear: |
| 3250 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3251 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3252 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3253 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3254 | break; |
| 3255 | case inactive: |
| 3256 | /* stopping an active array */ |
| 3257 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3258 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3259 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3260 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3261 | } else |
| 3262 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3263 | break; |
| 3264 | case suspended: |
| 3265 | break; /* not supported yet */ |
| 3266 | case readonly: |
| 3267 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3268 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3269 | else { |
| 3270 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3271 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3272 | err = do_md_run(mddev); |
| 3273 | } |
| 3274 | break; |
| 3275 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3276 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3277 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3278 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3279 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3280 | err = restart_array(mddev); |
| 3281 | if (err == 0) { |
| 3282 | mddev->ro = 2; |
| 3283 | set_disk_ro(mddev->gendisk, 0); |
| 3284 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3285 | } else { |
| 3286 | mddev->ro = 2; |
| 3287 | err = do_md_run(mddev); |
| 3288 | } |
| 3289 | break; |
| 3290 | case clean: |
| 3291 | if (mddev->pers) { |
| 3292 | restart_array(mddev); |
| 3293 | spin_lock_irq(&mddev->write_lock); |
| 3294 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3295 | if (mddev->in_sync == 0) { |
| 3296 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3297 | if (mddev->safemode == 1) |
| 3298 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3299 | if (mddev->persistent) |
| 3300 | set_bit(MD_CHANGE_CLEAN, |
| 3301 | &mddev->flags); |
| 3302 | } |
| 3303 | err = 0; |
| 3304 | } else |
| 3305 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3306 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3307 | } else |
| 3308 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3309 | break; |
| 3310 | case active: |
| 3311 | if (mddev->pers) { |
| 3312 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3313 | if (mddev->external) |
| 3314 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3315 | wake_up(&mddev->sb_wait); |
| 3316 | err = 0; |
| 3317 | } else { |
| 3318 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3319 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3320 | err = do_md_run(mddev); |
| 3321 | } |
| 3322 | break; |
| 3323 | case write_pending: |
| 3324 | case active_idle: |
| 3325 | /* these cannot be set */ |
| 3326 | break; |
| 3327 | } |
| 3328 | if (err) |
| 3329 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3330 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3331 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3332 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3333 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3334 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3335 | static struct md_sysfs_entry md_array_state = |
| 3336 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3337 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3338 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3339 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3340 | return sprintf(page, "%d\n", |
| 3341 | atomic_read(&mddev->max_corr_read_errors)); |
| 3342 | } |
| 3343 | |
| 3344 | static ssize_t |
| 3345 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3346 | { |
| 3347 | char *e; |
| 3348 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3349 | |
| 3350 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3351 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3352 | return len; |
| 3353 | } |
| 3354 | return -EINVAL; |
| 3355 | } |
| 3356 | |
| 3357 | static struct md_sysfs_entry max_corr_read_errors = |
| 3358 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3359 | max_corrected_read_errors_store); |
| 3360 | |
| 3361 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3362 | null_show(mddev_t *mddev, char *page) |
| 3363 | { |
| 3364 | return -EINVAL; |
| 3365 | } |
| 3366 | |
| 3367 | static ssize_t |
| 3368 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3369 | { |
| 3370 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3371 | /* The new device is added to the array. |
| 3372 | * If the array has a persistent superblock, we read the |
| 3373 | * superblock to initialise info and check validity. |
| 3374 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3375 | * which mainly checks size. |
| 3376 | */ |
| 3377 | char *e; |
| 3378 | int major = simple_strtoul(buf, &e, 10); |
| 3379 | int minor; |
| 3380 | dev_t dev; |
| 3381 | mdk_rdev_t *rdev; |
| 3382 | int err; |
| 3383 | |
| 3384 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3385 | return -EINVAL; |
| 3386 | minor = simple_strtoul(e+1, &e, 10); |
| 3387 | if (*e && *e != '\n') |
| 3388 | return -EINVAL; |
| 3389 | dev = MKDEV(major, minor); |
| 3390 | if (major != MAJOR(dev) || |
| 3391 | minor != MINOR(dev)) |
| 3392 | return -EOVERFLOW; |
| 3393 | |
| 3394 | |
| 3395 | if (mddev->persistent) { |
| 3396 | rdev = md_import_device(dev, mddev->major_version, |
| 3397 | mddev->minor_version); |
| 3398 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3399 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3400 | mdk_rdev_t, same_set); |
| 3401 | err = super_types[mddev->major_version] |
| 3402 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3403 | if (err < 0) |
| 3404 | goto out; |
| 3405 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3406 | } else if (mddev->external) |
| 3407 | rdev = md_import_device(dev, -2, -1); |
| 3408 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3409 | rdev = md_import_device(dev, -1, -1); |
| 3410 | |
| 3411 | if (IS_ERR(rdev)) |
| 3412 | return PTR_ERR(rdev); |
| 3413 | err = bind_rdev_to_array(rdev, mddev); |
| 3414 | out: |
| 3415 | if (err) |
| 3416 | export_rdev(rdev); |
| 3417 | return err ? err : len; |
| 3418 | } |
| 3419 | |
| 3420 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3421 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3422 | |
| 3423 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3424 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3425 | { |
| 3426 | char *end; |
| 3427 | unsigned long chunk, end_chunk; |
| 3428 | |
| 3429 | if (!mddev->bitmap) |
| 3430 | goto out; |
| 3431 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3432 | while (*buf) { |
| 3433 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3434 | if (buf == end) break; |
| 3435 | if (*end == '-') { /* range */ |
| 3436 | buf = end + 1; |
| 3437 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3438 | if (buf == end) break; |
| 3439 | } |
| 3440 | if (*end && !isspace(*end)) break; |
| 3441 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3442 | buf = end; |
| 3443 | while (isspace(*buf)) buf++; |
| 3444 | } |
| 3445 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3446 | out: |
| 3447 | return len; |
| 3448 | } |
| 3449 | |
| 3450 | static struct md_sysfs_entry md_bitmap = |
| 3451 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3452 | |
| 3453 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3454 | size_show(mddev_t *mddev, char *page) |
| 3455 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3456 | return sprintf(page, "%llu\n", |
| 3457 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3458 | } |
| 3459 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3460 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3461 | |
| 3462 | static ssize_t |
| 3463 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3464 | { |
| 3465 | /* If array is inactive, we can reduce the component size, but |
| 3466 | * not increase it (except from 0). |
| 3467 | * If array is active, we can try an on-line resize |
| 3468 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3469 | sector_t sectors; |
| 3470 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3471 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3472 | if (err < 0) |
| 3473 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3474 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3475 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3476 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3477 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3478 | if (mddev->dev_sectors == 0 || |
| 3479 | mddev->dev_sectors > sectors) |
| 3480 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3481 | else |
| 3482 | err = -ENOSPC; |
| 3483 | } |
| 3484 | return err ? err : len; |
| 3485 | } |
| 3486 | |
| 3487 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3488 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3489 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3490 | |
| 3491 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3492 | * This is one of |
| 3493 | * 'none' for arrays with no metadata (good luck...) |
| 3494 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3495 | * or N.M for internally known formats |
| 3496 | */ |
| 3497 | static ssize_t |
| 3498 | metadata_show(mddev_t *mddev, char *page) |
| 3499 | { |
| 3500 | if (mddev->persistent) |
| 3501 | return sprintf(page, "%d.%d\n", |
| 3502 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3503 | else if (mddev->external) |
| 3504 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3505 | else |
| 3506 | return sprintf(page, "none\n"); |
| 3507 | } |
| 3508 | |
| 3509 | static ssize_t |
| 3510 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3511 | { |
| 3512 | int major, minor; |
| 3513 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3514 | /* Changing the details of 'external' metadata is |
| 3515 | * always permitted. Otherwise there must be |
| 3516 | * no devices attached to the array. |
| 3517 | */ |
| 3518 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3519 | ; |
| 3520 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3521 | return -EBUSY; |
| 3522 | |
| 3523 | if (cmd_match(buf, "none")) { |
| 3524 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3525 | mddev->external = 0; |
| 3526 | mddev->major_version = 0; |
| 3527 | mddev->minor_version = 90; |
| 3528 | return len; |
| 3529 | } |
| 3530 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3531 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3532 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3533 | namelen = sizeof(mddev->metadata_type)-1; |
| 3534 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3535 | mddev->metadata_type[namelen] = 0; |
| 3536 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3537 | mddev->metadata_type[--namelen] = 0; |
| 3538 | mddev->persistent = 0; |
| 3539 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3540 | mddev->major_version = 0; |
| 3541 | mddev->minor_version = 90; |
| 3542 | return len; |
| 3543 | } |
| 3544 | major = simple_strtoul(buf, &e, 10); |
| 3545 | if (e==buf || *e != '.') |
| 3546 | return -EINVAL; |
| 3547 | buf = e+1; |
| 3548 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3549 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3550 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3551 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3552 | return -ENOENT; |
| 3553 | mddev->major_version = major; |
| 3554 | mddev->minor_version = minor; |
| 3555 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3556 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3557 | return len; |
| 3558 | } |
| 3559 | |
| 3560 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3561 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3562 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3563 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3564 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3565 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3566 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3567 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3568 | type = "frozen"; |
| 3569 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3570 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3571 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3572 | type = "reshape"; |
| 3573 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3574 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3575 | type = "resync"; |
| 3576 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3577 | type = "check"; |
| 3578 | else |
| 3579 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3580 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3581 | type = "recover"; |
| 3582 | } |
| 3583 | return sprintf(page, "%s\n", type); |
| 3584 | } |
| 3585 | |
| 3586 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3587 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3588 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3589 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3590 | return -EINVAL; |
| 3591 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3592 | if (cmd_match(page, "frozen")) |
| 3593 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3594 | else |
| 3595 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3596 | |
| 3597 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3598 | if (mddev->sync_thread) { |
| 3599 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3600 | md_unregister_thread(mddev->sync_thread); |
| 3601 | mddev->sync_thread = NULL; |
| 3602 | mddev->recovery = 0; |
| 3603 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3604 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3605 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3606 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3607 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3608 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3609 | else if (cmd_match(page, "recover")) { |
| 3610 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3611 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3612 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3613 | int err; |
| 3614 | if (mddev->pers->start_reshape == NULL) |
| 3615 | return -EINVAL; |
| 3616 | err = mddev->pers->start_reshape(mddev); |
| 3617 | if (err) |
| 3618 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3619 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3620 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3621 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3622 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3623 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3624 | return -EINVAL; |
| 3625 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3626 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3627 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3628 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3629 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3630 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3631 | return len; |
| 3632 | } |
| 3633 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3634 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3635 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3636 | { |
| 3637 | return sprintf(page, "%llu\n", |
| 3638 | (unsigned long long) mddev->resync_mismatches); |
| 3639 | } |
| 3640 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3641 | static struct md_sysfs_entry md_scan_mode = |
| 3642 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3643 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3644 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3645 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3646 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3647 | static ssize_t |
| 3648 | sync_min_show(mddev_t *mddev, char *page) |
| 3649 | { |
| 3650 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3651 | mddev->sync_speed_min ? "local": "system"); |
| 3652 | } |
| 3653 | |
| 3654 | static ssize_t |
| 3655 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3656 | { |
| 3657 | int min; |
| 3658 | char *e; |
| 3659 | if (strncmp(buf, "system", 6)==0) { |
| 3660 | mddev->sync_speed_min = 0; |
| 3661 | return len; |
| 3662 | } |
| 3663 | min = simple_strtoul(buf, &e, 10); |
| 3664 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3665 | return -EINVAL; |
| 3666 | mddev->sync_speed_min = min; |
| 3667 | return len; |
| 3668 | } |
| 3669 | |
| 3670 | static struct md_sysfs_entry md_sync_min = |
| 3671 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3672 | |
| 3673 | static ssize_t |
| 3674 | sync_max_show(mddev_t *mddev, char *page) |
| 3675 | { |
| 3676 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3677 | mddev->sync_speed_max ? "local": "system"); |
| 3678 | } |
| 3679 | |
| 3680 | static ssize_t |
| 3681 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3682 | { |
| 3683 | int max; |
| 3684 | char *e; |
| 3685 | if (strncmp(buf, "system", 6)==0) { |
| 3686 | mddev->sync_speed_max = 0; |
| 3687 | return len; |
| 3688 | } |
| 3689 | max = simple_strtoul(buf, &e, 10); |
| 3690 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3691 | return -EINVAL; |
| 3692 | mddev->sync_speed_max = max; |
| 3693 | return len; |
| 3694 | } |
| 3695 | |
| 3696 | static struct md_sysfs_entry md_sync_max = |
| 3697 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3698 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3699 | static ssize_t |
| 3700 | degraded_show(mddev_t *mddev, char *page) |
| 3701 | { |
| 3702 | return sprintf(page, "%d\n", mddev->degraded); |
| 3703 | } |
| 3704 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3705 | |
| 3706 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3707 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3708 | { |
| 3709 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3710 | } |
| 3711 | |
| 3712 | static ssize_t |
| 3713 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3714 | { |
| 3715 | long n; |
| 3716 | |
| 3717 | if (strict_strtol(buf, 10, &n)) |
| 3718 | return -EINVAL; |
| 3719 | |
| 3720 | if (n != 0 && n != 1) |
| 3721 | return -EINVAL; |
| 3722 | |
| 3723 | mddev->parallel_resync = n; |
| 3724 | |
| 3725 | if (mddev->sync_thread) |
| 3726 | wake_up(&resync_wait); |
| 3727 | |
| 3728 | return len; |
| 3729 | } |
| 3730 | |
| 3731 | /* force parallel resync, even with shared block devices */ |
| 3732 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3733 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3734 | sync_force_parallel_show, sync_force_parallel_store); |
| 3735 | |
| 3736 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3737 | sync_speed_show(mddev_t *mddev, char *page) |
| 3738 | { |
| 3739 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3740 | if (mddev->curr_resync == 0) |
| 3741 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3742 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3743 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3744 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3745 | db = resync - mddev->resync_mark_cnt; |
| 3746 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3747 | } |
| 3748 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3749 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3750 | |
| 3751 | static ssize_t |
| 3752 | sync_completed_show(mddev_t *mddev, char *page) |
| 3753 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3754 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3755 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3756 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3757 | return sprintf(page, "none\n"); |
| 3758 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3759 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3760 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3761 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3762 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3763 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3764 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3765 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3766 | } |
| 3767 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3768 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3769 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3770 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3771 | min_sync_show(mddev_t *mddev, char *page) |
| 3772 | { |
| 3773 | return sprintf(page, "%llu\n", |
| 3774 | (unsigned long long)mddev->resync_min); |
| 3775 | } |
| 3776 | static ssize_t |
| 3777 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3778 | { |
| 3779 | unsigned long long min; |
| 3780 | if (strict_strtoull(buf, 10, &min)) |
| 3781 | return -EINVAL; |
| 3782 | if (min > mddev->resync_max) |
| 3783 | return -EINVAL; |
| 3784 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3785 | return -EBUSY; |
| 3786 | |
| 3787 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3788 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3789 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3790 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3791 | return -EINVAL; |
| 3792 | } |
| 3793 | mddev->resync_min = min; |
| 3794 | |
| 3795 | return len; |
| 3796 | } |
| 3797 | |
| 3798 | static struct md_sysfs_entry md_min_sync = |
| 3799 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3800 | |
| 3801 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3802 | max_sync_show(mddev_t *mddev, char *page) |
| 3803 | { |
| 3804 | if (mddev->resync_max == MaxSector) |
| 3805 | return sprintf(page, "max\n"); |
| 3806 | else |
| 3807 | return sprintf(page, "%llu\n", |
| 3808 | (unsigned long long)mddev->resync_max); |
| 3809 | } |
| 3810 | static ssize_t |
| 3811 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3812 | { |
| 3813 | if (strncmp(buf, "max", 3) == 0) |
| 3814 | mddev->resync_max = MaxSector; |
| 3815 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3816 | unsigned long long max; |
| 3817 | if (strict_strtoull(buf, 10, &max)) |
| 3818 | return -EINVAL; |
| 3819 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3820 | return -EINVAL; |
| 3821 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3822 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3823 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3824 | return -EBUSY; |
| 3825 | |
| 3826 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3827 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3828 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3829 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3830 | return -EINVAL; |
| 3831 | } |
| 3832 | mddev->resync_max = max; |
| 3833 | } |
| 3834 | wake_up(&mddev->recovery_wait); |
| 3835 | return len; |
| 3836 | } |
| 3837 | |
| 3838 | static struct md_sysfs_entry md_max_sync = |
| 3839 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3840 | |
| 3841 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3842 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3843 | { |
| 3844 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3845 | } |
| 3846 | |
| 3847 | static ssize_t |
| 3848 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3849 | { |
| 3850 | char *e; |
| 3851 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3852 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3853 | if (mddev->pers == NULL || |
| 3854 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3855 | return -EINVAL; |
| 3856 | if (buf == e || (*e && *e != '\n')) |
| 3857 | return -EINVAL; |
| 3858 | if (new >= mddev->suspend_hi || |
| 3859 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3860 | mddev->suspend_lo = new; |
| 3861 | mddev->pers->quiesce(mddev, 2); |
| 3862 | return len; |
| 3863 | } else |
| 3864 | return -EINVAL; |
| 3865 | } |
| 3866 | static struct md_sysfs_entry md_suspend_lo = |
| 3867 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3868 | |
| 3869 | |
| 3870 | static ssize_t |
| 3871 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3872 | { |
| 3873 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3874 | } |
| 3875 | |
| 3876 | static ssize_t |
| 3877 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3878 | { |
| 3879 | char *e; |
| 3880 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3881 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3882 | if (mddev->pers == NULL || |
| 3883 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3884 | return -EINVAL; |
| 3885 | if (buf == e || (*e && *e != '\n')) |
| 3886 | return -EINVAL; |
| 3887 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3888 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3889 | mddev->suspend_hi = new; |
| 3890 | mddev->pers->quiesce(mddev, 1); |
| 3891 | mddev->pers->quiesce(mddev, 0); |
| 3892 | return len; |
| 3893 | } else |
| 3894 | return -EINVAL; |
| 3895 | } |
| 3896 | static struct md_sysfs_entry md_suspend_hi = |
| 3897 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3898 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3899 | static ssize_t |
| 3900 | reshape_position_show(mddev_t *mddev, char *page) |
| 3901 | { |
| 3902 | if (mddev->reshape_position != MaxSector) |
| 3903 | return sprintf(page, "%llu\n", |
| 3904 | (unsigned long long)mddev->reshape_position); |
| 3905 | strcpy(page, "none\n"); |
| 3906 | return 5; |
| 3907 | } |
| 3908 | |
| 3909 | static ssize_t |
| 3910 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3911 | { |
| 3912 | char *e; |
| 3913 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3914 | if (mddev->pers) |
| 3915 | return -EBUSY; |
| 3916 | if (buf == e || (*e && *e != '\n')) |
| 3917 | return -EINVAL; |
| 3918 | mddev->reshape_position = new; |
| 3919 | mddev->delta_disks = 0; |
| 3920 | mddev->new_level = mddev->level; |
| 3921 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3922 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3923 | return len; |
| 3924 | } |
| 3925 | |
| 3926 | static struct md_sysfs_entry md_reshape_position = |
| 3927 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3928 | reshape_position_store); |
| 3929 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3930 | static ssize_t |
| 3931 | array_size_show(mddev_t *mddev, char *page) |
| 3932 | { |
| 3933 | if (mddev->external_size) |
| 3934 | return sprintf(page, "%llu\n", |
| 3935 | (unsigned long long)mddev->array_sectors/2); |
| 3936 | else |
| 3937 | return sprintf(page, "default\n"); |
| 3938 | } |
| 3939 | |
| 3940 | static ssize_t |
| 3941 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3942 | { |
| 3943 | sector_t sectors; |
| 3944 | |
| 3945 | if (strncmp(buf, "default", 7) == 0) { |
| 3946 | if (mddev->pers) |
| 3947 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3948 | else |
| 3949 | sectors = mddev->array_sectors; |
| 3950 | |
| 3951 | mddev->external_size = 0; |
| 3952 | } else { |
| 3953 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3954 | return -EINVAL; |
| 3955 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3956 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3957 | |
| 3958 | mddev->external_size = 1; |
| 3959 | } |
| 3960 | |
| 3961 | mddev->array_sectors = sectors; |
| 3962 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3963 | if (mddev->pers) |
| 3964 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3965 | |
| 3966 | return len; |
| 3967 | } |
| 3968 | |
| 3969 | static struct md_sysfs_entry md_array_size = |
| 3970 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3971 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3972 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3973 | static struct attribute *md_default_attrs[] = { |
| 3974 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3975 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3976 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3977 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3978 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3979 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3980 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3981 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3982 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3983 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3984 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3985 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3986 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3987 | NULL, |
| 3988 | }; |
| 3989 | |
| 3990 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3991 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3992 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3993 | &md_sync_min.attr, |
| 3994 | &md_sync_max.attr, |
| 3995 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3996 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3997 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3998 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3999 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4000 | &md_suspend_lo.attr, |
| 4001 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4002 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4003 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4004 | NULL, |
| 4005 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4006 | static struct attribute_group md_redundancy_group = { |
| 4007 | .name = NULL, |
| 4008 | .attrs = md_redundancy_attrs, |
| 4009 | }; |
| 4010 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4011 | |
| 4012 | static ssize_t |
| 4013 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4014 | { |
| 4015 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4016 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4017 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4018 | |
| 4019 | if (!entry->show) |
| 4020 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4021 | rv = mddev_lock(mddev); |
| 4022 | if (!rv) { |
| 4023 | rv = entry->show(mddev, page); |
| 4024 | mddev_unlock(mddev); |
| 4025 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4026 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | static ssize_t |
| 4030 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4031 | const char *page, size_t length) |
| 4032 | { |
| 4033 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4034 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4035 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4036 | |
| 4037 | if (!entry->store) |
| 4038 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4039 | if (!capable(CAP_SYS_ADMIN)) |
| 4040 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4041 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4042 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4043 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4044 | if (!rv) { |
| 4045 | rv = entry->store(mddev, page, length); |
| 4046 | mddev_unlock(mddev); |
| 4047 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4048 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | static void md_free(struct kobject *ko) |
| 4052 | { |
| 4053 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4054 | |
| 4055 | if (mddev->sysfs_state) |
| 4056 | sysfs_put(mddev->sysfs_state); |
| 4057 | |
| 4058 | if (mddev->gendisk) { |
| 4059 | del_gendisk(mddev->gendisk); |
| 4060 | put_disk(mddev->gendisk); |
| 4061 | } |
| 4062 | if (mddev->queue) |
| 4063 | blk_cleanup_queue(mddev->queue); |
| 4064 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4065 | kfree(mddev); |
| 4066 | } |
| 4067 | |
| 4068 | static struct sysfs_ops md_sysfs_ops = { |
| 4069 | .show = md_attr_show, |
| 4070 | .store = md_attr_store, |
| 4071 | }; |
| 4072 | static struct kobj_type md_ktype = { |
| 4073 | .release = md_free, |
| 4074 | .sysfs_ops = &md_sysfs_ops, |
| 4075 | .default_attrs = md_default_attrs, |
| 4076 | }; |
| 4077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | int mdp_major = 0; |
| 4079 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4080 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4081 | { |
| 4082 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4083 | |
| 4084 | if (mddev->private == &md_redundancy_group) { |
| 4085 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 4086 | if (mddev->sysfs_action) |
| 4087 | sysfs_put(mddev->sysfs_action); |
| 4088 | mddev->sysfs_action = NULL; |
| 4089 | mddev->private = NULL; |
| 4090 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4091 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4092 | kobject_del(&mddev->kobj); |
| 4093 | kobject_put(&mddev->kobj); |
| 4094 | } |
| 4095 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4096 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4098 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | mddev_t *mddev = mddev_find(dev); |
| 4100 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4101 | int partitioned; |
| 4102 | int shift; |
| 4103 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4104 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | |
| 4106 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4107 | return -ENODEV; |
| 4108 | |
| 4109 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4110 | shift = partitioned ? MdpMinorShift : 0; |
| 4111 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4113 | /* wait for any previous instance if this device |
| 4114 | * to be completed removed (mddev_delayed_delete). |
| 4115 | */ |
| 4116 | flush_scheduled_work(); |
| 4117 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4118 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4119 | error = -EEXIST; |
| 4120 | if (mddev->gendisk) |
| 4121 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4122 | |
| 4123 | if (name) { |
| 4124 | /* Need to ensure that 'name' is not a duplicate. |
| 4125 | */ |
| 4126 | mddev_t *mddev2; |
| 4127 | spin_lock(&all_mddevs_lock); |
| 4128 | |
| 4129 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4130 | if (mddev2->gendisk && |
| 4131 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4132 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4133 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4134 | } |
| 4135 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4137 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4138 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4139 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4140 | if (!mddev->queue) |
| 4141 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4142 | mddev->queue->queuedata = mddev; |
| 4143 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4144 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4145 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4146 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4147 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | disk = alloc_disk(1 << shift); |
| 4150 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4151 | blk_cleanup_queue(mddev->queue); |
| 4152 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4153 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4155 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4157 | if (name) |
| 4158 | strcpy(disk->disk_name, name); |
| 4159 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4161 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | disk->fops = &md_fops; |
| 4164 | disk->private_data = mddev; |
| 4165 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4166 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4167 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4168 | * remove it now. |
| 4169 | */ |
| 4170 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | add_disk(disk); |
| 4172 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4173 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4174 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4175 | if (error) { |
| 4176 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4177 | * __must_check, we must do something with the result |
| 4178 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4179 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4180 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4181 | error = 0; |
| 4182 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4183 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4184 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4185 | abort: |
| 4186 | mutex_unlock(&disks_mutex); |
| 4187 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4188 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4189 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4190 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4191 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4192 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4196 | { |
| 4197 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | return NULL; |
| 4199 | } |
| 4200 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4201 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4202 | { |
| 4203 | /* val must be "md_*" where * is not all digits. |
| 4204 | * We allocate an array with a large free minor number, and |
| 4205 | * set the name to val. val must not already be an active name. |
| 4206 | */ |
| 4207 | int len = strlen(val); |
| 4208 | char buf[DISK_NAME_LEN]; |
| 4209 | |
| 4210 | while (len && val[len-1] == '\n') |
| 4211 | len--; |
| 4212 | if (len >= DISK_NAME_LEN) |
| 4213 | return -E2BIG; |
| 4214 | strlcpy(buf, val, len+1); |
| 4215 | if (strncmp(buf, "md_", 3) != 0) |
| 4216 | return -EINVAL; |
| 4217 | return md_alloc(0, buf); |
| 4218 | } |
| 4219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | static void md_safemode_timeout(unsigned long data) |
| 4221 | { |
| 4222 | mddev_t *mddev = (mddev_t *) data; |
| 4223 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4224 | if (!atomic_read(&mddev->writes_pending)) { |
| 4225 | mddev->safemode = 1; |
| 4226 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4227 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | md_wakeup_thread(mddev->thread); |
| 4230 | } |
| 4231 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4232 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | |
| 4234 | static int do_md_run(mddev_t * mddev) |
| 4235 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4236 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | mdk_rdev_t *rdev; |
| 4238 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4239 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4241 | if (list_empty(&mddev->disks)) |
| 4242 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | |
| 4245 | if (mddev->pers) |
| 4246 | return -EBUSY; |
| 4247 | |
| 4248 | /* |
| 4249 | * Analyze all RAID superblock(s) |
| 4250 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4251 | if (!mddev->raid_disks) { |
| 4252 | if (!mddev->persistent) |
| 4253 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4254 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4257 | if (mddev->level != LEVEL_NONE) |
| 4258 | request_module("md-level-%d", mddev->level); |
| 4259 | else if (mddev->clevel[0]) |
| 4260 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | |
| 4262 | /* |
| 4263 | * Drop all container device buffers, from now on |
| 4264 | * the only valid external interface is through the md |
| 4265 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4267 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4268 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | continue; |
| 4270 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4271 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4272 | |
| 4273 | /* perform some consistency tests on the device. |
| 4274 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4275 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4276 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4277 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4278 | if (mddev->dev_sectors && |
| 4279 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4280 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4281 | printk("md: %s: data overlaps metadata\n", |
| 4282 | mdname(mddev)); |
| 4283 | return -EINVAL; |
| 4284 | } |
| 4285 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4286 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4287 | > rdev->data_offset) { |
| 4288 | printk("md: %s: metadata overlaps data\n", |
| 4289 | mdname(mddev)); |
| 4290 | return -EINVAL; |
| 4291 | } |
| 4292 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4293 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | md_probe(mddev->unit, NULL, NULL); |
| 4297 | disk = mddev->gendisk; |
| 4298 | if (!disk) |
| 4299 | return -ENOMEM; |
| 4300 | |
| 4301 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4302 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4303 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4305 | if (mddev->level != LEVEL_NONE) |
| 4306 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4307 | mddev->level); |
| 4308 | else |
| 4309 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4310 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | return -EINVAL; |
| 4312 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4313 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4315 | if (mddev->level != pers->level) { |
| 4316 | mddev->level = pers->level; |
| 4317 | mddev->new_level = pers->level; |
| 4318 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4319 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4321 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4322 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4323 | /* This personality cannot handle reshaping... */ |
| 4324 | mddev->pers = NULL; |
| 4325 | module_put(pers->owner); |
| 4326 | return -EINVAL; |
| 4327 | } |
| 4328 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4329 | if (pers->sync_request) { |
| 4330 | /* Warn if this is a potentially silly |
| 4331 | * configuration. |
| 4332 | */ |
| 4333 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4334 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4335 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4336 | |
| 4337 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4338 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4339 | if (rdev < rdev2 && |
| 4340 | rdev->bdev->bd_contains == |
| 4341 | rdev2->bdev->bd_contains) { |
| 4342 | printk(KERN_WARNING |
| 4343 | "%s: WARNING: %s appears to be" |
| 4344 | " on the same physical disk as" |
| 4345 | " %s.\n", |
| 4346 | mdname(mddev), |
| 4347 | bdevname(rdev->bdev,b), |
| 4348 | bdevname(rdev2->bdev,b2)); |
| 4349 | warned = 1; |
| 4350 | } |
| 4351 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4352 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4353 | if (warned) |
| 4354 | printk(KERN_WARNING |
| 4355 | "True protection against single-disk" |
| 4356 | " failure might be compromised.\n"); |
| 4357 | } |
| 4358 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4359 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4360 | /* may be over-ridden by personality */ |
| 4361 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4362 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4363 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4364 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4366 | if (start_readonly) |
| 4367 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4368 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4369 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4370 | if (err) |
| 4371 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4372 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4373 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4374 | " but 'external_size' not in effect?\n", __func__); |
| 4375 | printk(KERN_ERR |
| 4376 | "md: invalid array_size %llu > default size %llu\n", |
| 4377 | (unsigned long long)mddev->array_sectors / 2, |
| 4378 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4379 | err = -EINVAL; |
| 4380 | mddev->pers->stop(mddev); |
| 4381 | } |
| 4382 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4383 | err = bitmap_create(mddev); |
| 4384 | if (err) { |
| 4385 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4386 | mdname(mddev), err); |
| 4387 | mddev->pers->stop(mddev); |
| 4388 | } |
| 4389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | module_put(mddev->pers->owner); |
| 4392 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4393 | bitmap_destroy(mddev); |
| 4394 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4396 | if (mddev->pers->sync_request) { |
| 4397 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4398 | printk(KERN_WARNING |
| 4399 | "md: cannot register extra attributes for %s\n", |
| 4400 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4401 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4402 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4403 | mddev->ro = 0; |
| 4404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4406 | atomic_set(&mddev->max_corr_read_errors, |
| 4407 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | mddev->safemode = 0; |
| 4409 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4410 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4411 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4413 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4414 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4415 | if (rdev->raid_disk >= 0) { |
| 4416 | char nm[20]; |
| 4417 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4418 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4419 | printk("md: cannot register %s for %s\n", |
| 4420 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | |
| 4423 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4424 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4425 | if (mddev->flags) |
| 4426 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4428 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4430 | /* If there is a partially-recovered drive we need to |
| 4431 | * start recovery here. If we leave it to md_check_recovery, |
| 4432 | * it will remove the drives and not do the right thing |
| 4433 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4434 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4435 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4436 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4437 | if (rdev->raid_disk >= 0 && |
| 4438 | !test_bit(In_sync, &rdev->flags) && |
| 4439 | !test_bit(Faulty, &rdev->flags)) |
| 4440 | /* complete an interrupted recovery */ |
| 4441 | spares++; |
| 4442 | if (spares && mddev->pers->sync_request) { |
| 4443 | mddev->recovery = 0; |
| 4444 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4445 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4446 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4447 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4448 | if (!mddev->sync_thread) { |
| 4449 | printk(KERN_ERR "%s: could not start resync" |
| 4450 | " thread...\n", |
| 4451 | mdname(mddev)); |
| 4452 | /* leave the spares where they are, it shouldn't hurt */ |
| 4453 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4454 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4455 | } |
| 4456 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4457 | md_wakeup_thread(mddev->thread); |
| 4458 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4459 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4460 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4461 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4462 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4463 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4464 | if (mddev->sysfs_action) |
| 4465 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4466 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4467 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | return 0; |
| 4469 | } |
| 4470 | |
| 4471 | static int restart_array(mddev_t *mddev) |
| 4472 | { |
| 4473 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4475 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4477 | return -ENXIO; |
| 4478 | if (!mddev->pers) |
| 4479 | return -EINVAL; |
| 4480 | if (!mddev->ro) |
| 4481 | return -EBUSY; |
| 4482 | mddev->safemode = 0; |
| 4483 | mddev->ro = 0; |
| 4484 | set_disk_ro(disk, 0); |
| 4485 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4486 | mdname(mddev)); |
| 4487 | /* Kick recovery or resync if necessary */ |
| 4488 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4489 | md_wakeup_thread(mddev->thread); |
| 4490 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4491 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4492 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | } |
| 4494 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4495 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4496 | * to the file ourselves */ |
| 4497 | static int deny_bitmap_write_access(struct file * file) |
| 4498 | { |
| 4499 | struct inode *inode = file->f_mapping->host; |
| 4500 | |
| 4501 | spin_lock(&inode->i_lock); |
| 4502 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4503 | spin_unlock(&inode->i_lock); |
| 4504 | return -ETXTBSY; |
| 4505 | } |
| 4506 | atomic_set(&inode->i_writecount, -1); |
| 4507 | spin_unlock(&inode->i_lock); |
| 4508 | |
| 4509 | return 0; |
| 4510 | } |
| 4511 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4512 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4513 | { |
| 4514 | struct inode *inode = file->f_mapping->host; |
| 4515 | |
| 4516 | spin_lock(&inode->i_lock); |
| 4517 | atomic_set(&inode->i_writecount, 1); |
| 4518 | spin_unlock(&inode->i_lock); |
| 4519 | } |
| 4520 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4521 | /* mode: |
| 4522 | * 0 - completely stop and dis-assemble array |
| 4523 | * 1 - switch to readonly |
| 4524 | * 2 - stop but do not disassemble array |
| 4525 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4526 | 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] | 4527 | { |
| 4528 | int err = 0; |
| 4529 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4530 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4532 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4533 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4534 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4535 | err = -EBUSY; |
| 4536 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | |
| 4538 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4539 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4541 | md_unregister_thread(mddev->sync_thread); |
| 4542 | mddev->sync_thread = NULL; |
| 4543 | } |
| 4544 | |
| 4545 | del_timer_sync(&mddev->safemode_timer); |
| 4546 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4547 | switch(mode) { |
| 4548 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4550 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | goto out; |
| 4552 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4553 | break; |
| 4554 | case 0: /* disassemble */ |
| 4555 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4556 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4557 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | if (mddev->ro) |
| 4559 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4562 | mddev->queue->merge_bvec_fn = NULL; |
| 4563 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4564 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4566 | if (mddev->pers->sync_request) |
| 4567 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4569 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4570 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4571 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4572 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4573 | if (rdev->raid_disk >= 0) { |
| 4574 | char nm[20]; |
| 4575 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4576 | sysfs_remove_link(&mddev->kobj, nm); |
| 4577 | } |
| 4578 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4579 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4580 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | if (mddev->ro) |
| 4583 | mddev->ro = 0; |
| 4584 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4585 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | /* mark array as shutdown cleanly */ |
| 4587 | mddev->in_sync = 1; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4588 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4590 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4592 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4593 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4595 | out: |
| 4596 | mutex_unlock(&mddev->open_mutex); |
| 4597 | if (err) |
| 4598 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | /* |
| 4600 | * Free resources if final stop |
| 4601 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4602 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4605 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4606 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4607 | if (mddev->bitmap_info.file) { |
| 4608 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4609 | fput(mddev->bitmap_info.file); |
| 4610 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4611 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4612 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4613 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4614 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4615 | flush_scheduled_work(); |
| 4616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | export_array(mddev); |
| 4618 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4619 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4620 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4621 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4622 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4623 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4624 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4625 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4626 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4627 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4628 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4629 | mddev->level = LEVEL_NONE; |
| 4630 | mddev->clevel[0] = 0; |
| 4631 | mddev->flags = 0; |
| 4632 | mddev->ro = 0; |
| 4633 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4634 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4635 | mddev->ctime = mddev->utime = 0; |
| 4636 | mddev->layout = 0; |
| 4637 | mddev->max_disks = 0; |
| 4638 | mddev->events = 0; |
| 4639 | mddev->delta_disks = 0; |
| 4640 | mddev->new_level = LEVEL_NONE; |
| 4641 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4642 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4643 | mddev->curr_resync = 0; |
| 4644 | mddev->resync_mismatches = 0; |
| 4645 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4646 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4647 | mddev->recovery = 0; |
| 4648 | mddev->in_sync = 0; |
| 4649 | mddev->changed = 0; |
| 4650 | mddev->degraded = 0; |
| 4651 | mddev->barriers_work = 0; |
| 4652 | mddev->safemode = 0; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4653 | mddev->bitmap_info.offset = 0; |
| 4654 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 4655 | mddev->bitmap_info.chunksize = 0; |
| 4656 | mddev->bitmap_info.daemon_sleep = 0; |
| 4657 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4658 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4659 | if (mddev->hold_active == UNTIL_STOP) |
| 4660 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4661 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4662 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4664 | mdname(mddev)); |
| 4665 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4666 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4667 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4668 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | return err; |
| 4670 | } |
| 4671 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4672 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | static void autorun_array(mddev_t *mddev) |
| 4674 | { |
| 4675 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | int err; |
| 4677 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4678 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | |
| 4681 | printk(KERN_INFO "md: running: "); |
| 4682 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4683 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | char b[BDEVNAME_SIZE]; |
| 4685 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4686 | } |
| 4687 | printk("\n"); |
| 4688 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4689 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | if (err) { |
| 4691 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4692 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | /* |
| 4697 | * lets try to run arrays based on all disks that have arrived |
| 4698 | * until now. (those are in pending_raid_disks) |
| 4699 | * |
| 4700 | * the method: pick the first pending disk, collect all disks with |
| 4701 | * the same UUID, remove all from the pending list and put them into |
| 4702 | * the 'same_array' list. Then order this list based on superblock |
| 4703 | * update time (freshest comes first), kick out 'old' disks and |
| 4704 | * compare superblocks. If everything's fine then run it. |
| 4705 | * |
| 4706 | * If "unit" is allocated, then bump its reference count |
| 4707 | */ |
| 4708 | static void autorun_devices(int part) |
| 4709 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4710 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | mddev_t *mddev; |
| 4712 | char b[BDEVNAME_SIZE]; |
| 4713 | |
| 4714 | printk(KERN_INFO "md: autorun ...\n"); |
| 4715 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4716 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4718 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | rdev0 = list_entry(pending_raid_disks.next, |
| 4720 | mdk_rdev_t, same_set); |
| 4721 | |
| 4722 | printk(KERN_INFO "md: considering %s ...\n", |
| 4723 | bdevname(rdev0->bdev,b)); |
| 4724 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4725 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4727 | printk(KERN_INFO "md: adding %s ...\n", |
| 4728 | bdevname(rdev->bdev,b)); |
| 4729 | list_move(&rdev->same_set, &candidates); |
| 4730 | } |
| 4731 | /* |
| 4732 | * now we have a set of devices, with all of them having |
| 4733 | * mostly sane superblocks. It's time to allocate the |
| 4734 | * mddev. |
| 4735 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4736 | if (part) { |
| 4737 | dev = MKDEV(mdp_major, |
| 4738 | rdev0->preferred_minor << MdpMinorShift); |
| 4739 | unit = MINOR(dev) >> MdpMinorShift; |
| 4740 | } else { |
| 4741 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4742 | unit = MINOR(dev); |
| 4743 | } |
| 4744 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4746 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4747 | break; |
| 4748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | |
| 4750 | md_probe(dev, NULL, NULL); |
| 4751 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4752 | if (!mddev || !mddev->gendisk) { |
| 4753 | if (mddev) |
| 4754 | mddev_put(mddev); |
| 4755 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | "md: cannot allocate memory for md drive.\n"); |
| 4757 | break; |
| 4758 | } |
| 4759 | if (mddev_lock(mddev)) |
| 4760 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4761 | mdname(mddev)); |
| 4762 | else if (mddev->raid_disks || mddev->major_version |
| 4763 | || !list_empty(&mddev->disks)) { |
| 4764 | printk(KERN_WARNING |
| 4765 | "md: %s already running, cannot run %s\n", |
| 4766 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4767 | mddev_unlock(mddev); |
| 4768 | } else { |
| 4769 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4770 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4771 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | list_del_init(&rdev->same_set); |
| 4773 | if (bind_rdev_to_array(rdev, mddev)) |
| 4774 | export_rdev(rdev); |
| 4775 | } |
| 4776 | autorun_array(mddev); |
| 4777 | mddev_unlock(mddev); |
| 4778 | } |
| 4779 | /* on success, candidates will be empty, on error |
| 4780 | * it won't... |
| 4781 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4782 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4783 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4785 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | mddev_put(mddev); |
| 4787 | } |
| 4788 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4789 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4790 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | static int get_version(void __user * arg) |
| 4793 | { |
| 4794 | mdu_version_t ver; |
| 4795 | |
| 4796 | ver.major = MD_MAJOR_VERSION; |
| 4797 | ver.minor = MD_MINOR_VERSION; |
| 4798 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4799 | |
| 4800 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4801 | return -EFAULT; |
| 4802 | |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4807 | { |
| 4808 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4809 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4812 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4813 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4815 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | failed++; |
| 4817 | else { |
| 4818 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4819 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4820 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | else |
| 4822 | spare++; |
| 4823 | } |
| 4824 | } |
| 4825 | |
| 4826 | info.major_version = mddev->major_version; |
| 4827 | info.minor_version = mddev->minor_version; |
| 4828 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4829 | info.ctime = mddev->ctime; |
| 4830 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4831 | info.size = mddev->dev_sectors / 2; |
| 4832 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4833 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | info.nr_disks = nr; |
| 4835 | info.raid_disks = mddev->raid_disks; |
| 4836 | info.md_minor = mddev->md_minor; |
| 4837 | info.not_persistent= !mddev->persistent; |
| 4838 | |
| 4839 | info.utime = mddev->utime; |
| 4840 | info.state = 0; |
| 4841 | if (mddev->in_sync) |
| 4842 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4843 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4844 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4845 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | info.working_disks = working; |
| 4847 | info.failed_disks = failed; |
| 4848 | info.spare_disks = spare; |
| 4849 | |
| 4850 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4851 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | |
| 4853 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4854 | return -EFAULT; |
| 4855 | |
| 4856 | return 0; |
| 4857 | } |
| 4858 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4859 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4860 | { |
| 4861 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4862 | char *ptr, *buf = NULL; |
| 4863 | int err = -ENOMEM; |
| 4864 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4865 | if (md_allow_write(mddev)) |
| 4866 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4867 | else |
| 4868 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4869 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4870 | if (!file) |
| 4871 | goto out; |
| 4872 | |
| 4873 | /* bitmap disabled, zero the first byte and copy out */ |
| 4874 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4875 | file->pathname[0] = '\0'; |
| 4876 | goto copy_out; |
| 4877 | } |
| 4878 | |
| 4879 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4880 | if (!buf) |
| 4881 | goto out; |
| 4882 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4883 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4884 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4885 | goto out; |
| 4886 | |
| 4887 | strcpy(file->pathname, ptr); |
| 4888 | |
| 4889 | copy_out: |
| 4890 | err = 0; |
| 4891 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4892 | err = -EFAULT; |
| 4893 | out: |
| 4894 | kfree(buf); |
| 4895 | kfree(file); |
| 4896 | return err; |
| 4897 | } |
| 4898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4900 | { |
| 4901 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | mdk_rdev_t *rdev; |
| 4903 | |
| 4904 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4905 | return -EFAULT; |
| 4906 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4907 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | if (rdev) { |
| 4909 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4910 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4911 | info.raid_disk = rdev->raid_disk; |
| 4912 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4913 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4915 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4917 | info.state |= (1<<MD_DISK_SYNC); |
| 4918 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4919 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4920 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | } else { |
| 4922 | info.major = info.minor = 0; |
| 4923 | info.raid_disk = -1; |
| 4924 | info.state = (1<<MD_DISK_REMOVED); |
| 4925 | } |
| 4926 | |
| 4927 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4928 | return -EFAULT; |
| 4929 | |
| 4930 | return 0; |
| 4931 | } |
| 4932 | |
| 4933 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4934 | { |
| 4935 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4936 | mdk_rdev_t *rdev; |
| 4937 | dev_t dev = MKDEV(info->major,info->minor); |
| 4938 | |
| 4939 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4940 | return -EOVERFLOW; |
| 4941 | |
| 4942 | if (!mddev->raid_disks) { |
| 4943 | int err; |
| 4944 | /* expecting a device which has a superblock */ |
| 4945 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4946 | if (IS_ERR(rdev)) { |
| 4947 | printk(KERN_WARNING |
| 4948 | "md: md_import_device returned %ld\n", |
| 4949 | PTR_ERR(rdev)); |
| 4950 | return PTR_ERR(rdev); |
| 4951 | } |
| 4952 | if (!list_empty(&mddev->disks)) { |
| 4953 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4954 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4955 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4957 | if (err < 0) { |
| 4958 | printk(KERN_WARNING |
| 4959 | "md: %s has different UUID to %s\n", |
| 4960 | bdevname(rdev->bdev,b), |
| 4961 | bdevname(rdev0->bdev,b2)); |
| 4962 | export_rdev(rdev); |
| 4963 | return -EINVAL; |
| 4964 | } |
| 4965 | } |
| 4966 | err = bind_rdev_to_array(rdev, mddev); |
| 4967 | if (err) |
| 4968 | export_rdev(rdev); |
| 4969 | return err; |
| 4970 | } |
| 4971 | |
| 4972 | /* |
| 4973 | * add_new_disk can be used once the array is assembled |
| 4974 | * to add "hot spares". They must already have a superblock |
| 4975 | * written |
| 4976 | */ |
| 4977 | if (mddev->pers) { |
| 4978 | int err; |
| 4979 | if (!mddev->pers->hot_add_disk) { |
| 4980 | printk(KERN_WARNING |
| 4981 | "%s: personality does not support diskops!\n", |
| 4982 | mdname(mddev)); |
| 4983 | return -EINVAL; |
| 4984 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4985 | if (mddev->persistent) |
| 4986 | rdev = md_import_device(dev, mddev->major_version, |
| 4987 | mddev->minor_version); |
| 4988 | else |
| 4989 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | if (IS_ERR(rdev)) { |
| 4991 | printk(KERN_WARNING |
| 4992 | "md: md_import_device returned %ld\n", |
| 4993 | PTR_ERR(rdev)); |
| 4994 | return PTR_ERR(rdev); |
| 4995 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4996 | /* set save_raid_disk if appropriate */ |
| 4997 | if (!mddev->persistent) { |
| 4998 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4999 | info->raid_disk < mddev->raid_disks) |
| 5000 | rdev->raid_disk = info->raid_disk; |
| 5001 | else |
| 5002 | rdev->raid_disk = -1; |
| 5003 | } else |
| 5004 | super_types[mddev->major_version]. |
| 5005 | validate_super(mddev, rdev); |
| 5006 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5007 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5008 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5009 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5010 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5011 | else |
| 5012 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | rdev->raid_disk = -1; |
| 5015 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5016 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5017 | /* If there is hot_add_disk but no hot_remove_disk |
| 5018 | * then added disks for geometry changes, |
| 5019 | * and should be added immediately. |
| 5020 | */ |
| 5021 | super_types[mddev->major_version]. |
| 5022 | validate_super(mddev, rdev); |
| 5023 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5024 | if (err) |
| 5025 | unbind_rdev_from_array(rdev); |
| 5026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | if (err) |
| 5028 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5029 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5030 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5031 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5032 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5033 | if (mddev->degraded) |
| 5034 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5035 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5036 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | return err; |
| 5038 | } |
| 5039 | |
| 5040 | /* otherwise, add_new_disk is only allowed |
| 5041 | * for major_version==0 superblocks |
| 5042 | */ |
| 5043 | if (mddev->major_version != 0) { |
| 5044 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5045 | mdname(mddev)); |
| 5046 | return -EINVAL; |
| 5047 | } |
| 5048 | |
| 5049 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5050 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5051 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | if (IS_ERR(rdev)) { |
| 5053 | printk(KERN_WARNING |
| 5054 | "md: error, md_import_device() returned %ld\n", |
| 5055 | PTR_ERR(rdev)); |
| 5056 | return PTR_ERR(rdev); |
| 5057 | } |
| 5058 | rdev->desc_nr = info->number; |
| 5059 | if (info->raid_disk < mddev->raid_disks) |
| 5060 | rdev->raid_disk = info->raid_disk; |
| 5061 | else |
| 5062 | rdev->raid_disk = -1; |
| 5063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5065 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5066 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5068 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5069 | set_bit(WriteMostly, &rdev->flags); |
| 5070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | if (!mddev->persistent) { |
| 5072 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5073 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5075 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5076 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5078 | err = bind_rdev_to_array(rdev, mddev); |
| 5079 | if (err) { |
| 5080 | export_rdev(rdev); |
| 5081 | return err; |
| 5082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
| 5088 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5089 | { |
| 5090 | char b[BDEVNAME_SIZE]; |
| 5091 | mdk_rdev_t *rdev; |
| 5092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | rdev = find_rdev(mddev, dev); |
| 5094 | if (!rdev) |
| 5095 | return -ENXIO; |
| 5096 | |
| 5097 | if (rdev->raid_disk >= 0) |
| 5098 | goto busy; |
| 5099 | |
| 5100 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5101 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5102 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | |
| 5104 | return 0; |
| 5105 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5106 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5108 | return -EBUSY; |
| 5109 | } |
| 5110 | |
| 5111 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5112 | { |
| 5113 | char b[BDEVNAME_SIZE]; |
| 5114 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | mdk_rdev_t *rdev; |
| 5116 | |
| 5117 | if (!mddev->pers) |
| 5118 | return -ENODEV; |
| 5119 | |
| 5120 | if (mddev->major_version != 0) { |
| 5121 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5122 | " version-0 superblocks.\n", |
| 5123 | mdname(mddev)); |
| 5124 | return -EINVAL; |
| 5125 | } |
| 5126 | if (!mddev->pers->hot_add_disk) { |
| 5127 | printk(KERN_WARNING |
| 5128 | "%s: personality does not support diskops!\n", |
| 5129 | mdname(mddev)); |
| 5130 | return -EINVAL; |
| 5131 | } |
| 5132 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5133 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | if (IS_ERR(rdev)) { |
| 5135 | printk(KERN_WARNING |
| 5136 | "md: error, md_import_device() returned %ld\n", |
| 5137 | PTR_ERR(rdev)); |
| 5138 | return -EINVAL; |
| 5139 | } |
| 5140 | |
| 5141 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5142 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5144 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5146 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5148 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | printk(KERN_WARNING |
| 5150 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5151 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5152 | err = -EINVAL; |
| 5153 | goto abort_export; |
| 5154 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5155 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5157 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5158 | err = bind_rdev_to_array(rdev, mddev); |
| 5159 | if (err) |
| 5160 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
| 5162 | /* |
| 5163 | * The rest should better be atomic, we can have disk failures |
| 5164 | * noticed in interrupt contexts ... |
| 5165 | */ |
| 5166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | rdev->raid_disk = -1; |
| 5168 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5169 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | |
| 5171 | /* |
| 5172 | * Kick recovery, maybe this spare has to be added to the |
| 5173 | * array immediately. |
| 5174 | */ |
| 5175 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5176 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5177 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | return 0; |
| 5179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | abort_export: |
| 5181 | export_rdev(rdev); |
| 5182 | return err; |
| 5183 | } |
| 5184 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5185 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5186 | { |
| 5187 | int err; |
| 5188 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5189 | if (mddev->pers) { |
| 5190 | if (!mddev->pers->quiesce) |
| 5191 | return -EBUSY; |
| 5192 | if (mddev->recovery || mddev->sync_thread) |
| 5193 | return -EBUSY; |
| 5194 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5197 | |
| 5198 | if (fd >= 0) { |
| 5199 | if (mddev->bitmap) |
| 5200 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5201 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5202 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5203 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5204 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5205 | mdname(mddev)); |
| 5206 | return -EBADF; |
| 5207 | } |
| 5208 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5209 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5210 | if (err) { |
| 5211 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5212 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5213 | fput(mddev->bitmap_info.file); |
| 5214 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5215 | return err; |
| 5216 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5217 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5218 | } else if (mddev->bitmap == NULL) |
| 5219 | return -ENOENT; /* cannot remove what isn't there */ |
| 5220 | err = 0; |
| 5221 | if (mddev->pers) { |
| 5222 | mddev->pers->quiesce(mddev, 1); |
| 5223 | if (fd >= 0) |
| 5224 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5225 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5226 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5227 | fd = -1; /* make sure to put the file */ |
| 5228 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5229 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5230 | } |
| 5231 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5232 | if (mddev->bitmap_info.file) { |
| 5233 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5234 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5235 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5236 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5237 | } |
| 5238 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5239 | return err; |
| 5240 | } |
| 5241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | /* |
| 5243 | * set_array_info is used two different ways |
| 5244 | * The original usage is when creating a new array. |
| 5245 | * In this usage, raid_disks is > 0 and it together with |
| 5246 | * level, size, not_persistent,layout,chunksize determine the |
| 5247 | * shape of the array. |
| 5248 | * This will always create an array with a type-0.90.0 superblock. |
| 5249 | * The newer usage is when assembling an array. |
| 5250 | * In this case raid_disks will be 0, and the major_version field is |
| 5251 | * use to determine which style super-blocks are to be found on the devices. |
| 5252 | * The minor and patch _version numbers are also kept incase the |
| 5253 | * super_block handler wishes to interpret them. |
| 5254 | */ |
| 5255 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5256 | { |
| 5257 | |
| 5258 | if (info->raid_disks == 0) { |
| 5259 | /* just setting version number for superblock loading */ |
| 5260 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5261 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | super_types[info->major_version].name == NULL) { |
| 5263 | /* maybe try to auto-load a module? */ |
| 5264 | printk(KERN_INFO |
| 5265 | "md: superblock version %d not known\n", |
| 5266 | info->major_version); |
| 5267 | return -EINVAL; |
| 5268 | } |
| 5269 | mddev->major_version = info->major_version; |
| 5270 | mddev->minor_version = info->minor_version; |
| 5271 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5272 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | return 0; |
| 5274 | } |
| 5275 | mddev->major_version = MD_MAJOR_VERSION; |
| 5276 | mddev->minor_version = MD_MINOR_VERSION; |
| 5277 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5278 | mddev->ctime = get_seconds(); |
| 5279 | |
| 5280 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5281 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5282 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | mddev->raid_disks = info->raid_disks; |
| 5284 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5285 | * openned |
| 5286 | */ |
| 5287 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5288 | mddev->recovery_cp = MaxSector; |
| 5289 | else |
| 5290 | mddev->recovery_cp = 0; |
| 5291 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5292 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | |
| 5294 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5295 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | |
| 5297 | mddev->max_disks = MD_SB_DISKS; |
| 5298 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5299 | if (mddev->persistent) |
| 5300 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5301 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5303 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5304 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5305 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5306 | mddev->reshape_position = MaxSector; |
| 5307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | /* |
| 5309 | * Generate a 128 bit UUID |
| 5310 | */ |
| 5311 | get_random_bytes(mddev->uuid, 16); |
| 5312 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5313 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5314 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5315 | mddev->new_layout = mddev->layout; |
| 5316 | mddev->delta_disks = 0; |
| 5317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | return 0; |
| 5319 | } |
| 5320 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5321 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5322 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5323 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5324 | |
| 5325 | if (mddev->external_size) |
| 5326 | return; |
| 5327 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5328 | mddev->array_sectors = array_sectors; |
| 5329 | } |
| 5330 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5331 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5332 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5333 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5334 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5335 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5336 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5337 | |
| 5338 | if (mddev->pers->resize == NULL) |
| 5339 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5340 | /* The "num_sectors" is the number of sectors of each device that |
| 5341 | * is used. This can only make sense for arrays with redundancy. |
| 5342 | * linear and raid0 always use whatever space is available. We can only |
| 5343 | * consider changing this number if no resync or reconstruction is |
| 5344 | * 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] | 5345 | * 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] | 5346 | * of each device. If num_sectors is zero, we find the largest size |
| 5347 | * that fits. |
| 5348 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5349 | */ |
| 5350 | if (mddev->sync_thread) |
| 5351 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5352 | if (mddev->bitmap) |
| 5353 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5354 | * grow, and re-add. |
| 5355 | */ |
| 5356 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5357 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5358 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5359 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5360 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5361 | num_sectors = avail; |
| 5362 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5363 | return -ENOSPC; |
| 5364 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5365 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5366 | if (!rv) |
| 5367 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5368 | return rv; |
| 5369 | } |
| 5370 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5371 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5372 | { |
| 5373 | int rv; |
| 5374 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5375 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5376 | return -EINVAL; |
| 5377 | if (raid_disks <= 0 || |
| 5378 | raid_disks >= mddev->max_disks) |
| 5379 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5380 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5381 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5382 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5383 | |
| 5384 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5385 | return rv; |
| 5386 | } |
| 5387 | |
| 5388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | /* |
| 5390 | * update_array_info is used to change the configuration of an |
| 5391 | * on-line array. |
| 5392 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5393 | * fields in the info are checked against the array. |
| 5394 | * Any differences that cannot be handled will cause an error. |
| 5395 | * Normally, only one change can be managed at a time. |
| 5396 | */ |
| 5397 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5398 | { |
| 5399 | int rv = 0; |
| 5400 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5401 | int state = 0; |
| 5402 | |
| 5403 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5404 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5405 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
| 5407 | if (mddev->major_version != info->major_version || |
| 5408 | mddev->minor_version != info->minor_version || |
| 5409 | /* mddev->patch_version != info->patch_version || */ |
| 5410 | mddev->ctime != info->ctime || |
| 5411 | mddev->level != info->level || |
| 5412 | /* mddev->layout != info->layout || */ |
| 5413 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5414 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5415 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5416 | ((state^info->state) & 0xfffffe00) |
| 5417 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | return -EINVAL; |
| 5419 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5420 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5421 | cnt++; |
| 5422 | if (mddev->raid_disks != info->raid_disks) |
| 5423 | cnt++; |
| 5424 | if (mddev->layout != info->layout) |
| 5425 | cnt++; |
| 5426 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5427 | cnt++; |
| 5428 | if (cnt == 0) |
| 5429 | return 0; |
| 5430 | if (cnt > 1) |
| 5431 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | |
| 5433 | if (mddev->layout != info->layout) { |
| 5434 | /* Change layout |
| 5435 | * we don't need to do anything at the md level, the |
| 5436 | * personality will take care of it all. |
| 5437 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5438 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5440 | else { |
| 5441 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5442 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5443 | if (rv) |
| 5444 | mddev->new_layout = mddev->layout; |
| 5445 | return rv; |
| 5446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5448 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5449 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5451 | if (mddev->raid_disks != info->raid_disks) |
| 5452 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5454 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5455 | if (mddev->pers->quiesce == NULL) |
| 5456 | return -EINVAL; |
| 5457 | if (mddev->recovery || mddev->sync_thread) |
| 5458 | return -EBUSY; |
| 5459 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5460 | /* add the bitmap */ |
| 5461 | if (mddev->bitmap) |
| 5462 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5463 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5464 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5465 | mddev->bitmap_info.offset = |
| 5466 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5467 | mddev->pers->quiesce(mddev, 1); |
| 5468 | rv = bitmap_create(mddev); |
| 5469 | if (rv) |
| 5470 | bitmap_destroy(mddev); |
| 5471 | mddev->pers->quiesce(mddev, 0); |
| 5472 | } else { |
| 5473 | /* remove the bitmap */ |
| 5474 | if (!mddev->bitmap) |
| 5475 | return -ENOENT; |
| 5476 | if (mddev->bitmap->file) |
| 5477 | return -EINVAL; |
| 5478 | mddev->pers->quiesce(mddev, 1); |
| 5479 | bitmap_destroy(mddev); |
| 5480 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5481 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5482 | } |
| 5483 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5484 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | return rv; |
| 5486 | } |
| 5487 | |
| 5488 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5489 | { |
| 5490 | mdk_rdev_t *rdev; |
| 5491 | |
| 5492 | if (mddev->pers == NULL) |
| 5493 | return -ENODEV; |
| 5494 | |
| 5495 | rdev = find_rdev(mddev, dev); |
| 5496 | if (!rdev) |
| 5497 | return -ENODEV; |
| 5498 | |
| 5499 | md_error(mddev, rdev); |
| 5500 | return 0; |
| 5501 | } |
| 5502 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5503 | /* |
| 5504 | * We have a problem here : there is no easy way to give a CHS |
| 5505 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5506 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5507 | * dosfs just mad... ;-) |
| 5508 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5509 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5510 | { |
| 5511 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5512 | |
| 5513 | geo->heads = 2; |
| 5514 | geo->sectors = 4; |
| 5515 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5516 | return 0; |
| 5517 | } |
| 5518 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5519 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | unsigned int cmd, unsigned long arg) |
| 5521 | { |
| 5522 | int err = 0; |
| 5523 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | mddev_t *mddev = NULL; |
| 5525 | |
| 5526 | if (!capable(CAP_SYS_ADMIN)) |
| 5527 | return -EACCES; |
| 5528 | |
| 5529 | /* |
| 5530 | * Commands dealing with the RAID driver but not any |
| 5531 | * particular array: |
| 5532 | */ |
| 5533 | switch (cmd) |
| 5534 | { |
| 5535 | case RAID_VERSION: |
| 5536 | err = get_version(argp); |
| 5537 | goto done; |
| 5538 | |
| 5539 | case PRINT_RAID_DEBUG: |
| 5540 | err = 0; |
| 5541 | md_print_devices(); |
| 5542 | goto done; |
| 5543 | |
| 5544 | #ifndef MODULE |
| 5545 | case RAID_AUTORUN: |
| 5546 | err = 0; |
| 5547 | autostart_arrays(arg); |
| 5548 | goto done; |
| 5549 | #endif |
| 5550 | default:; |
| 5551 | } |
| 5552 | |
| 5553 | /* |
| 5554 | * Commands creating/starting a new array: |
| 5555 | */ |
| 5556 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5557 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | |
| 5559 | if (!mddev) { |
| 5560 | BUG(); |
| 5561 | goto abort; |
| 5562 | } |
| 5563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | err = mddev_lock(mddev); |
| 5565 | if (err) { |
| 5566 | printk(KERN_INFO |
| 5567 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5568 | err, cmd); |
| 5569 | goto abort; |
| 5570 | } |
| 5571 | |
| 5572 | switch (cmd) |
| 5573 | { |
| 5574 | case SET_ARRAY_INFO: |
| 5575 | { |
| 5576 | mdu_array_info_t info; |
| 5577 | if (!arg) |
| 5578 | memset(&info, 0, sizeof(info)); |
| 5579 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5580 | err = -EFAULT; |
| 5581 | goto abort_unlock; |
| 5582 | } |
| 5583 | if (mddev->pers) { |
| 5584 | err = update_array_info(mddev, &info); |
| 5585 | if (err) { |
| 5586 | printk(KERN_WARNING "md: couldn't update" |
| 5587 | " array info. %d\n", err); |
| 5588 | goto abort_unlock; |
| 5589 | } |
| 5590 | goto done_unlock; |
| 5591 | } |
| 5592 | if (!list_empty(&mddev->disks)) { |
| 5593 | printk(KERN_WARNING |
| 5594 | "md: array %s already has disks!\n", |
| 5595 | mdname(mddev)); |
| 5596 | err = -EBUSY; |
| 5597 | goto abort_unlock; |
| 5598 | } |
| 5599 | if (mddev->raid_disks) { |
| 5600 | printk(KERN_WARNING |
| 5601 | "md: array %s already initialised!\n", |
| 5602 | mdname(mddev)); |
| 5603 | err = -EBUSY; |
| 5604 | goto abort_unlock; |
| 5605 | } |
| 5606 | err = set_array_info(mddev, &info); |
| 5607 | if (err) { |
| 5608 | printk(KERN_WARNING "md: couldn't set" |
| 5609 | " array info. %d\n", err); |
| 5610 | goto abort_unlock; |
| 5611 | } |
| 5612 | } |
| 5613 | goto done_unlock; |
| 5614 | |
| 5615 | default:; |
| 5616 | } |
| 5617 | |
| 5618 | /* |
| 5619 | * Commands querying/configuring an existing array: |
| 5620 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5621 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5622 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5623 | if ((!mddev->raid_disks && !mddev->external) |
| 5624 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5625 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5626 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | err = -ENODEV; |
| 5628 | goto abort_unlock; |
| 5629 | } |
| 5630 | |
| 5631 | /* |
| 5632 | * Commands even a read-only array can execute: |
| 5633 | */ |
| 5634 | switch (cmd) |
| 5635 | { |
| 5636 | case GET_ARRAY_INFO: |
| 5637 | err = get_array_info(mddev, argp); |
| 5638 | goto done_unlock; |
| 5639 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5640 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5641 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5642 | goto done_unlock; |
| 5643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | case GET_DISK_INFO: |
| 5645 | err = get_disk_info(mddev, argp); |
| 5646 | goto done_unlock; |
| 5647 | |
| 5648 | case RESTART_ARRAY_RW: |
| 5649 | err = restart_array(mddev); |
| 5650 | goto done_unlock; |
| 5651 | |
| 5652 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5653 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | goto done_unlock; |
| 5655 | |
| 5656 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5657 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | goto done_unlock; |
| 5659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | /* |
| 5663 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5664 | * superblock, so we do not allow them on read-only arrays. |
| 5665 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5666 | * here and hit the 'default' below, so only disallow |
| 5667 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5669 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5670 | if (mddev->ro == 2) { |
| 5671 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5672 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5673 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5674 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5675 | } else { |
| 5676 | err = -EROFS; |
| 5677 | goto abort_unlock; |
| 5678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | switch (cmd) |
| 5682 | { |
| 5683 | case ADD_NEW_DISK: |
| 5684 | { |
| 5685 | mdu_disk_info_t info; |
| 5686 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5687 | err = -EFAULT; |
| 5688 | else |
| 5689 | err = add_new_disk(mddev, &info); |
| 5690 | goto done_unlock; |
| 5691 | } |
| 5692 | |
| 5693 | case HOT_REMOVE_DISK: |
| 5694 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5695 | goto done_unlock; |
| 5696 | |
| 5697 | case HOT_ADD_DISK: |
| 5698 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5699 | goto done_unlock; |
| 5700 | |
| 5701 | case SET_DISK_FAULTY: |
| 5702 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5703 | goto done_unlock; |
| 5704 | |
| 5705 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5706 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | goto done_unlock; |
| 5708 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5709 | case SET_BITMAP_FILE: |
| 5710 | err = set_bitmap_file(mddev, (int)arg); |
| 5711 | goto done_unlock; |
| 5712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | err = -EINVAL; |
| 5715 | goto abort_unlock; |
| 5716 | } |
| 5717 | |
| 5718 | done_unlock: |
| 5719 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5720 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5721 | err != -EINVAL) |
| 5722 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | mddev_unlock(mddev); |
| 5724 | |
| 5725 | return err; |
| 5726 | done: |
| 5727 | if (err) |
| 5728 | MD_BUG(); |
| 5729 | abort: |
| 5730 | return err; |
| 5731 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5732 | #ifdef CONFIG_COMPAT |
| 5733 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5734 | unsigned int cmd, unsigned long arg) |
| 5735 | { |
| 5736 | switch (cmd) { |
| 5737 | case HOT_REMOVE_DISK: |
| 5738 | case HOT_ADD_DISK: |
| 5739 | case SET_DISK_FAULTY: |
| 5740 | case SET_BITMAP_FILE: |
| 5741 | /* These take in integer arg, do not convert */ |
| 5742 | break; |
| 5743 | default: |
| 5744 | arg = (unsigned long)compat_ptr(arg); |
| 5745 | break; |
| 5746 | } |
| 5747 | |
| 5748 | return md_ioctl(bdev, mode, cmd, arg); |
| 5749 | } |
| 5750 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5752 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | { |
| 5754 | /* |
| 5755 | * Succeed if we can lock the mddev, which confirms that |
| 5756 | * it isn't being stopped right now. |
| 5757 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5758 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | int err; |
| 5760 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5761 | if (mddev->gendisk != bdev->bd_disk) { |
| 5762 | /* we are racing with mddev_put which is discarding this |
| 5763 | * bd_disk. |
| 5764 | */ |
| 5765 | mddev_put(mddev); |
| 5766 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5767 | flush_scheduled_work(); |
| 5768 | /* Then retry the open from the top */ |
| 5769 | return -ERESTARTSYS; |
| 5770 | } |
| 5771 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5772 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5773 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | goto out; |
| 5775 | |
| 5776 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5777 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5778 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5780 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | out: |
| 5782 | return err; |
| 5783 | } |
| 5784 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5785 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5787 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5789 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5790 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | mddev_put(mddev); |
| 5792 | |
| 5793 | return 0; |
| 5794 | } |
| 5795 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5796 | static int md_media_changed(struct gendisk *disk) |
| 5797 | { |
| 5798 | mddev_t *mddev = disk->private_data; |
| 5799 | |
| 5800 | return mddev->changed; |
| 5801 | } |
| 5802 | |
| 5803 | static int md_revalidate(struct gendisk *disk) |
| 5804 | { |
| 5805 | mddev_t *mddev = disk->private_data; |
| 5806 | |
| 5807 | mddev->changed = 0; |
| 5808 | return 0; |
| 5809 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5810 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | { |
| 5812 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5813 | .open = md_open, |
| 5814 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5815 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5816 | #ifdef CONFIG_COMPAT |
| 5817 | .compat_ioctl = md_compat_ioctl, |
| 5818 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5819 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5820 | .media_changed = md_media_changed, |
| 5821 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | }; |
| 5823 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5824 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | { |
| 5826 | mdk_thread_t *thread = arg; |
| 5827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | /* |
| 5829 | * md_thread is a 'system-thread', it's priority should be very |
| 5830 | * high. We avoid resource deadlocks individually in each |
| 5831 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5832 | * the very same RT priority as kswapd, thus we will never get |
| 5833 | * into a priority inversion deadlock. |
| 5834 | * |
| 5835 | * we definitely have to have equal or higher priority than |
| 5836 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5837 | * many dirty RAID5 blocks. |
| 5838 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5840 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5841 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5843 | /* We need to wait INTERRUPTIBLE so that |
| 5844 | * we don't add to the load-average. |
| 5845 | * That means we need to be sure no signals are |
| 5846 | * pending |
| 5847 | */ |
| 5848 | if (signal_pending(current)) |
| 5849 | flush_signals(current); |
| 5850 | |
| 5851 | wait_event_interruptible_timeout |
| 5852 | (thread->wqueue, |
| 5853 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5854 | || kthread_should_stop(), |
| 5855 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
| 5857 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5858 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5859 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | return 0; |
| 5863 | } |
| 5864 | |
| 5865 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5866 | { |
| 5867 | if (thread) { |
| 5868 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5869 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5870 | wake_up(&thread->wqueue); |
| 5871 | } |
| 5872 | } |
| 5873 | |
| 5874 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5875 | const char *name) |
| 5876 | { |
| 5877 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5879 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | if (!thread) |
| 5881 | return NULL; |
| 5882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | init_waitqueue_head(&thread->wqueue); |
| 5884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | thread->run = run; |
| 5886 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5887 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5888 | thread->tsk = kthread_run(md_thread, thread, |
| 5889 | "%s_%s", |
| 5890 | mdname(thread->mddev), |
| 5891 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5892 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | kfree(thread); |
| 5894 | return NULL; |
| 5895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | return thread; |
| 5897 | } |
| 5898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | void md_unregister_thread(mdk_thread_t *thread) |
| 5900 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5901 | if (!thread) |
| 5902 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5903 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5904 | |
| 5905 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | kfree(thread); |
| 5907 | } |
| 5908 | |
| 5909 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5910 | { |
| 5911 | if (!mddev) { |
| 5912 | MD_BUG(); |
| 5913 | return; |
| 5914 | } |
| 5915 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5916 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5918 | |
| 5919 | if (mddev->external) |
| 5920 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5921 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5923 | mdname(mddev), |
| 5924 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5925 | __builtin_return_address(0),__builtin_return_address(1), |
| 5926 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5927 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5928 | if (!mddev->pers) |
| 5929 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | if (!mddev->pers->error_handler) |
| 5931 | return; |
| 5932 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5933 | if (mddev->degraded) |
| 5934 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5935 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5937 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5938 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5939 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | } |
| 5941 | |
| 5942 | /* seq_file implementation /proc/mdstat */ |
| 5943 | |
| 5944 | static void status_unused(struct seq_file *seq) |
| 5945 | { |
| 5946 | int i = 0; |
| 5947 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | |
| 5949 | seq_printf(seq, "unused devices: "); |
| 5950 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5951 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | char b[BDEVNAME_SIZE]; |
| 5953 | i++; |
| 5954 | seq_printf(seq, "%s ", |
| 5955 | bdevname(rdev->bdev,b)); |
| 5956 | } |
| 5957 | if (!i) |
| 5958 | seq_printf(seq, "<none>"); |
| 5959 | |
| 5960 | seq_printf(seq, "\n"); |
| 5961 | } |
| 5962 | |
| 5963 | |
| 5964 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5965 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5966 | sector_t max_sectors, resync, res; |
| 5967 | unsigned long dt, db; |
| 5968 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5969 | int scale; |
| 5970 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5972 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | |
| 5974 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5975 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5977 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | |
| 5979 | /* |
| 5980 | * Should not happen. |
| 5981 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5982 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | MD_BUG(); |
| 5984 | return; |
| 5985 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5986 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5987 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5988 | * u32, as those are the requirements for sector_div. |
| 5989 | * Thus 'scale' must be at least 10 |
| 5990 | */ |
| 5991 | scale = 10; |
| 5992 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5993 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5994 | scale++; |
| 5995 | } |
| 5996 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5997 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5998 | |
| 5999 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6001 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | seq_printf(seq, "["); |
| 6003 | for (i = 0; i < x; i++) |
| 6004 | seq_printf(seq, "="); |
| 6005 | seq_printf(seq, ">"); |
| 6006 | for (i = 0; i < y; i++) |
| 6007 | seq_printf(seq, "."); |
| 6008 | seq_printf(seq, "] "); |
| 6009 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6010 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6011 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6012 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6013 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6014 | "check" : |
| 6015 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6016 | "resync" : "recovery"))), |
| 6017 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6018 | (unsigned long long) resync/2, |
| 6019 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
| 6021 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | * dt: time from mark until now |
| 6023 | * db: blocks written from mark until now |
| 6024 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6025 | * |
| 6026 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6027 | * So we divide before multiply in case it is 32bit and close |
| 6028 | * to the limit. |
| 6029 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6030 | * near the end of resync when the number of remaining sectors |
| 6031 | * is close to 'db'. |
| 6032 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6033 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | */ |
| 6035 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6036 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6037 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6038 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6040 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6041 | sector_div(rt, db/32+1); |
| 6042 | rt *= dt; |
| 6043 | rt >>= 5; |
| 6044 | |
| 6045 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6046 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6048 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6052 | { |
| 6053 | struct list_head *tmp; |
| 6054 | loff_t l = *pos; |
| 6055 | mddev_t *mddev; |
| 6056 | |
| 6057 | if (l >= 0x10000) |
| 6058 | return NULL; |
| 6059 | if (!l--) |
| 6060 | /* header */ |
| 6061 | return (void*)1; |
| 6062 | |
| 6063 | spin_lock(&all_mddevs_lock); |
| 6064 | list_for_each(tmp,&all_mddevs) |
| 6065 | if (!l--) { |
| 6066 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6067 | mddev_get(mddev); |
| 6068 | spin_unlock(&all_mddevs_lock); |
| 6069 | return mddev; |
| 6070 | } |
| 6071 | spin_unlock(&all_mddevs_lock); |
| 6072 | if (!l--) |
| 6073 | return (void*)2;/* tail */ |
| 6074 | return NULL; |
| 6075 | } |
| 6076 | |
| 6077 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6078 | { |
| 6079 | struct list_head *tmp; |
| 6080 | mddev_t *next_mddev, *mddev = v; |
| 6081 | |
| 6082 | ++*pos; |
| 6083 | if (v == (void*)2) |
| 6084 | return NULL; |
| 6085 | |
| 6086 | spin_lock(&all_mddevs_lock); |
| 6087 | if (v == (void*)1) |
| 6088 | tmp = all_mddevs.next; |
| 6089 | else |
| 6090 | tmp = mddev->all_mddevs.next; |
| 6091 | if (tmp != &all_mddevs) |
| 6092 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6093 | else { |
| 6094 | next_mddev = (void*)2; |
| 6095 | *pos = 0x10000; |
| 6096 | } |
| 6097 | spin_unlock(&all_mddevs_lock); |
| 6098 | |
| 6099 | if (v != (void*)1) |
| 6100 | mddev_put(mddev); |
| 6101 | return next_mddev; |
| 6102 | |
| 6103 | } |
| 6104 | |
| 6105 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6106 | { |
| 6107 | mddev_t *mddev = v; |
| 6108 | |
| 6109 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6110 | mddev_put(mddev); |
| 6111 | } |
| 6112 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6113 | struct mdstat_info { |
| 6114 | int event; |
| 6115 | }; |
| 6116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6118 | { |
| 6119 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6120 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6122 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6123 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | |
| 6125 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6126 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | seq_printf(seq, "Personalities : "); |
| 6128 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6129 | list_for_each_entry(pers, &pers_list, list) |
| 6130 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | |
| 6132 | spin_unlock(&pers_lock); |
| 6133 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6134 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | return 0; |
| 6136 | } |
| 6137 | if (v == (void*)2) { |
| 6138 | status_unused(seq); |
| 6139 | return 0; |
| 6140 | } |
| 6141 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6142 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6146 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6147 | mddev->pers ? "" : "in"); |
| 6148 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6149 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6151 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6152 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | seq_printf(seq, " %s", mddev->pers->name); |
| 6154 | } |
| 6155 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6156 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6157 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | char b[BDEVNAME_SIZE]; |
| 6159 | seq_printf(seq, " %s[%d]", |
| 6160 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6161 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6162 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6163 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | seq_printf(seq, "(F)"); |
| 6165 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6166 | } else if (rdev->raid_disk < 0) |
| 6167 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6168 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | } |
| 6170 | |
| 6171 | if (!list_empty(&mddev->disks)) { |
| 6172 | if (mddev->pers) |
| 6173 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6174 | (unsigned long long) |
| 6175 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | else |
| 6177 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6178 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6180 | if (mddev->persistent) { |
| 6181 | if (mddev->major_version != 0 || |
| 6182 | mddev->minor_version != 90) { |
| 6183 | seq_printf(seq," super %d.%d", |
| 6184 | mddev->major_version, |
| 6185 | mddev->minor_version); |
| 6186 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6187 | } else if (mddev->external) |
| 6188 | seq_printf(seq, " super external:%s", |
| 6189 | mddev->metadata_type); |
| 6190 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6191 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | |
| 6193 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6194 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6196 | if (mddev->pers->sync_request) { |
| 6197 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6198 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6199 | seq_printf(seq, "\n "); |
| 6200 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6201 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6202 | else if (mddev->recovery_cp < MaxSector) |
| 6203 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6204 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6205 | } else |
| 6206 | seq_printf(seq, "\n "); |
| 6207 | |
| 6208 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6209 | unsigned long chunk_kb; |
| 6210 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6211 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6212 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6213 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6214 | "%lu%s chunk", |
| 6215 | bitmap->pages - bitmap->missing_pages, |
| 6216 | bitmap->pages, |
| 6217 | (bitmap->pages - bitmap->missing_pages) |
| 6218 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6219 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6220 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6221 | if (bitmap->file) { |
| 6222 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6223 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6224 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6225 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6226 | seq_printf(seq, "\n"); |
| 6227 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | seq_printf(seq, "\n"); |
| 6231 | } |
| 6232 | mddev_unlock(mddev); |
| 6233 | |
| 6234 | return 0; |
| 6235 | } |
| 6236 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6237 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | .start = md_seq_start, |
| 6239 | .next = md_seq_next, |
| 6240 | .stop = md_seq_stop, |
| 6241 | .show = md_seq_show, |
| 6242 | }; |
| 6243 | |
| 6244 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6245 | { |
| 6246 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6247 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6248 | if (mi == NULL) |
| 6249 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | |
| 6251 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6252 | if (error) |
| 6253 | kfree(mi); |
| 6254 | else { |
| 6255 | struct seq_file *p = file->private_data; |
| 6256 | p->private = mi; |
| 6257 | mi->event = atomic_read(&md_event_count); |
| 6258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | return error; |
| 6260 | } |
| 6261 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6262 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6263 | { |
| 6264 | struct seq_file *m = filp->private_data; |
| 6265 | struct mdstat_info *mi = m->private; |
| 6266 | int mask; |
| 6267 | |
| 6268 | poll_wait(filp, &md_event_waiters, wait); |
| 6269 | |
| 6270 | /* always allow read */ |
| 6271 | mask = POLLIN | POLLRDNORM; |
| 6272 | |
| 6273 | if (mi->event != atomic_read(&md_event_count)) |
| 6274 | mask |= POLLERR | POLLPRI; |
| 6275 | return mask; |
| 6276 | } |
| 6277 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6278 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6279 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | .open = md_seq_open, |
| 6281 | .read = seq_read, |
| 6282 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6283 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6284 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6285 | }; |
| 6286 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6287 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6290 | list_add_tail(&p->list, &pers_list); |
| 6291 | 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] | 6292 | spin_unlock(&pers_lock); |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6296 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6298 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6300 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | spin_unlock(&pers_lock); |
| 6302 | return 0; |
| 6303 | } |
| 6304 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6305 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | { |
| 6307 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6309 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | |
| 6311 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6312 | rcu_read_lock(); |
| 6313 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6315 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6316 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6317 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6318 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6319 | * as sync_io is counted when a request starts, and |
| 6320 | * disk_stats is counted when it completes. |
| 6321 | * So resync activity will cause curr_events to be smaller than |
| 6322 | * when there was no such activity. |
| 6323 | * non-sync IO will cause disk_stat to increase without |
| 6324 | * increasing sync_io so curr_events will (eventually) |
| 6325 | * be larger than it was before. Once it becomes |
| 6326 | * substantially larger, the test below will cause |
| 6327 | * the array to appear non-idle, and resync will slow |
| 6328 | * down. |
| 6329 | * If there is a lot of outstanding resync activity when |
| 6330 | * we set last_event to curr_events, then all that activity |
| 6331 | * completing might cause the array to appear non-idle |
| 6332 | * and resync will be slowed down even though there might |
| 6333 | * not have been non-resync activity. This will only |
| 6334 | * happen once though. 'last_events' will soon reflect |
| 6335 | * the state where there is little or no outstanding |
| 6336 | * resync requests, and further resync activity will |
| 6337 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6338 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6340 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | rdev->last_events = curr_events; |
| 6342 | idle = 0; |
| 6343 | } |
| 6344 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6345 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | return idle; |
| 6347 | } |
| 6348 | |
| 6349 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6350 | { |
| 6351 | /* another "blocks" (512byte) blocks have been synced */ |
| 6352 | atomic_sub(blocks, &mddev->recovery_active); |
| 6353 | wake_up(&mddev->recovery_wait); |
| 6354 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6355 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | md_wakeup_thread(mddev->thread); |
| 6357 | // stop recovery, signal do_sync .... |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6362 | /* md_write_start(mddev, bi) |
| 6363 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6364 | * in superblock) before writing, schedule a superblock update |
| 6365 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6366 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6367 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6369 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6370 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6371 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6372 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6373 | BUG_ON(mddev->ro == 1); |
| 6374 | if (mddev->ro == 2) { |
| 6375 | /* need to switch to read/write */ |
| 6376 | mddev->ro = 0; |
| 6377 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6378 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6379 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6380 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6381 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6382 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6383 | if (mddev->safemode == 1) |
| 6384 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6385 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6386 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6387 | if (mddev->in_sync) { |
| 6388 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6389 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6390 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6391 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6392 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6393 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6394 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6395 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6396 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6397 | wait_event(mddev->sb_wait, |
| 6398 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6399 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | void md_write_end(mddev_t *mddev) |
| 6403 | { |
| 6404 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6405 | if (mddev->safemode == 2) |
| 6406 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6407 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6409 | } |
| 6410 | } |
| 6411 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6412 | /* md_allow_write(mddev) |
| 6413 | * Calling this ensures that the array is marked 'active' so that writes |
| 6414 | * may proceed without blocking. It is important to call this before |
| 6415 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6416 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6417 | * |
| 6418 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6419 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6420 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6421 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6422 | { |
| 6423 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6424 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6425 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6426 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6427 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6428 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6429 | |
| 6430 | spin_lock_irq(&mddev->write_lock); |
| 6431 | if (mddev->in_sync) { |
| 6432 | mddev->in_sync = 0; |
| 6433 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6434 | if (mddev->safemode_delay && |
| 6435 | mddev->safemode == 0) |
| 6436 | mddev->safemode = 1; |
| 6437 | spin_unlock_irq(&mddev->write_lock); |
| 6438 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6439 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6440 | } else |
| 6441 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6442 | |
| 6443 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6444 | return -EAGAIN; |
| 6445 | else |
| 6446 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6447 | } |
| 6448 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | #define SYNC_MARKS 10 |
| 6451 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6452 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | { |
| 6454 | mddev_t *mddev2; |
| 6455 | unsigned int currspeed = 0, |
| 6456 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6457 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | unsigned long mark[SYNC_MARKS]; |
| 6459 | sector_t mark_cnt[SYNC_MARKS]; |
| 6460 | int last_mark,m; |
| 6461 | struct list_head *tmp; |
| 6462 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6463 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6464 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6465 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | |
| 6467 | /* just incase thread restarts... */ |
| 6468 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6469 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6470 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6471 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6473 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6474 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6475 | desc = "data-check"; |
| 6476 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6477 | desc = "requested-resync"; |
| 6478 | else |
| 6479 | desc = "resync"; |
| 6480 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6481 | desc = "reshape"; |
| 6482 | else |
| 6483 | desc = "recovery"; |
| 6484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | /* we overload curr_resync somewhat here. |
| 6486 | * 0 == not engaged in resync at all |
| 6487 | * 2 == checking that there is no conflict with another sync |
| 6488 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6489 | * commense |
| 6490 | * other == active in resync - this many blocks |
| 6491 | * |
| 6492 | * Before starting a resync we must have set curr_resync to |
| 6493 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6494 | * less than ours. When we find one that is the same or higher |
| 6495 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6496 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6497 | * This will mean we have to start checking from the beginning again. |
| 6498 | * |
| 6499 | */ |
| 6500 | |
| 6501 | do { |
| 6502 | mddev->curr_resync = 2; |
| 6503 | |
| 6504 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6505 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6506 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | goto skip; |
| 6508 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6509 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | if (mddev2 == mddev) |
| 6511 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6512 | if (!mddev->parallel_resync |
| 6513 | && mddev2->curr_resync |
| 6514 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | DEFINE_WAIT(wq); |
| 6516 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6517 | /* arbitrarily yield */ |
| 6518 | mddev->curr_resync = 1; |
| 6519 | wake_up(&resync_wait); |
| 6520 | } |
| 6521 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6522 | /* no need to wait here, we can wait the next |
| 6523 | * time 'round when curr_resync == 2 |
| 6524 | */ |
| 6525 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6526 | /* We need to wait 'interruptible' so as not to |
| 6527 | * contribute to the load average, and not to |
| 6528 | * be caught by 'softlockup' |
| 6529 | */ |
| 6530 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6531 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6532 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6533 | printk(KERN_INFO "md: delaying %s of %s" |
| 6534 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6536 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6538 | if (signal_pending(current)) |
| 6539 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | schedule(); |
| 6541 | finish_wait(&resync_wait, &wq); |
| 6542 | goto try_again; |
| 6543 | } |
| 6544 | finish_wait(&resync_wait, &wq); |
| 6545 | } |
| 6546 | } |
| 6547 | } while (mddev->curr_resync < 2); |
| 6548 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6549 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6550 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6552 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | */ |
| 6554 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6555 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6556 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6557 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6558 | j = mddev->resync_min; |
| 6559 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6560 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6561 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6562 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6563 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6564 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6566 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6567 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6568 | rcu_read_lock(); |
| 6569 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6570 | if (rdev->raid_disk >= 0 && |
| 6571 | !test_bit(Faulty, &rdev->flags) && |
| 6572 | !test_bit(In_sync, &rdev->flags) && |
| 6573 | rdev->recovery_offset < j) |
| 6574 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6575 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6578 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6579 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6580 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6581 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6582 | "(but not more than %d KB/sec) for %s.\n", |
| 6583 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6585 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6586 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6587 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6589 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6590 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | } |
| 6592 | last_mark = 0; |
| 6593 | mddev->resync_mark = mark[last_mark]; |
| 6594 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6595 | |
| 6596 | /* |
| 6597 | * Tune reconstruction: |
| 6598 | */ |
| 6599 | window = 32*(PAGE_SIZE/512); |
| 6600 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6601 | window/2,(unsigned long long) max_sectors/2); |
| 6602 | |
| 6603 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | last_check = 0; |
| 6605 | |
| 6606 | if (j>2) { |
| 6607 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6608 | "md: resuming %s of %s from checkpoint.\n", |
| 6609 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | mddev->curr_resync = j; |
| 6611 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6612 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | |
| 6614 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6615 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6617 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6618 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6619 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6620 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6621 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6622 | > (max_sectors >> 4)) || |
| 6623 | (j - mddev->curr_resync_completed)*2 |
| 6624 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6625 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6626 | /* time to update curr_resync_completed */ |
| 6627 | blk_unplug(mddev->queue); |
| 6628 | wait_event(mddev->recovery_wait, |
| 6629 | atomic_read(&mddev->recovery_active) == 0); |
| 6630 | mddev->curr_resync_completed = |
| 6631 | mddev->curr_resync; |
| 6632 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6633 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6634 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6635 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6636 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6637 | /* As this condition is controlled by user-space, |
| 6638 | * we can block indefinitely, so use '_interruptible' |
| 6639 | * to avoid triggering warnings. |
| 6640 | */ |
| 6641 | flush_signals(current); /* just in case */ |
| 6642 | wait_event_interruptible(mddev->recovery_wait, |
| 6643 | mddev->resync_max > j |
| 6644 | || kthread_should_stop()); |
| 6645 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6646 | |
| 6647 | if (kthread_should_stop()) |
| 6648 | goto interrupted; |
| 6649 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6650 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6651 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6652 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6653 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | goto out; |
| 6655 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6656 | |
| 6657 | if (!skipped) { /* actual IO requested */ |
| 6658 | io_sectors += sectors; |
| 6659 | atomic_add(sectors, &mddev->recovery_active); |
| 6660 | } |
| 6661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | j += sectors; |
| 6663 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6664 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6665 | if (last_check == 0) |
| 6666 | /* this is the earliers that rebuilt will be |
| 6667 | * visible in /proc/mdstat |
| 6668 | */ |
| 6669 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6670 | |
| 6671 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6672 | continue; |
| 6673 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6674 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6676 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | break; |
| 6678 | |
| 6679 | repeat: |
| 6680 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6681 | /* step marks */ |
| 6682 | int next = (last_mark+1) % SYNC_MARKS; |
| 6683 | |
| 6684 | mddev->resync_mark = mark[next]; |
| 6685 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6686 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6687 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | last_mark = next; |
| 6689 | } |
| 6690 | |
| 6691 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6692 | if (kthread_should_stop()) |
| 6693 | goto interrupted; |
| 6694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | |
| 6696 | /* |
| 6697 | * this loop exits only if either when we are slower than |
| 6698 | * the 'hard' speed limit, or the system was IO-idle for |
| 6699 | * a jiffy. |
| 6700 | * the system might be non-idle CPU-wise, but we only care |
| 6701 | * about not overloading the IO subsystem. (things like an |
| 6702 | * e2fsck being done on the RAID array should execute fast) |
| 6703 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6704 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | cond_resched(); |
| 6706 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6707 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6708 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6710 | if (currspeed > speed_min(mddev)) { |
| 6711 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6712 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6713 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | goto repeat; |
| 6715 | } |
| 6716 | } |
| 6717 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6718 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | /* |
| 6720 | * this also signals 'finished resyncing' to md_stop |
| 6721 | */ |
| 6722 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6723 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | |
| 6725 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6726 | |
| 6727 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6728 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6730 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6731 | mddev->curr_resync > 2) { |
| 6732 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6733 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6734 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6735 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6736 | "md: checkpointing %s of %s.\n", |
| 6737 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6738 | mddev->recovery_cp = mddev->curr_resync; |
| 6739 | } |
| 6740 | } else |
| 6741 | mddev->recovery_cp = MaxSector; |
| 6742 | } else { |
| 6743 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6744 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6745 | rcu_read_lock(); |
| 6746 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6747 | if (rdev->raid_disk >= 0 && |
| 6748 | !test_bit(Faulty, &rdev->flags) && |
| 6749 | !test_bit(In_sync, &rdev->flags) && |
| 6750 | rdev->recovery_offset < mddev->curr_resync) |
| 6751 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6752 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6755 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6757 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6758 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6759 | /* We completed so min/max setting can be forgotten if used. */ |
| 6760 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6761 | mddev->resync_min = 0; |
| 6762 | mddev->resync_max = MaxSector; |
| 6763 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6764 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6766 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6767 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6768 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | wake_up(&resync_wait); |
| 6770 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6771 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6772 | return; |
| 6773 | |
| 6774 | interrupted: |
| 6775 | /* |
| 6776 | * got a signal, exit. |
| 6777 | */ |
| 6778 | printk(KERN_INFO |
| 6779 | "md: md_do_sync() got signal ... exiting\n"); |
| 6780 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6781 | goto out; |
| 6782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6784 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | |
| 6786 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6787 | static int remove_and_add_spares(mddev_t *mddev) |
| 6788 | { |
| 6789 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6790 | int spares = 0; |
| 6791 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6792 | mddev->curr_resync_completed = 0; |
| 6793 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6794 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6795 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6796 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6797 | (test_bit(Faulty, &rdev->flags) || |
| 6798 | ! test_bit(In_sync, &rdev->flags)) && |
| 6799 | atomic_read(&rdev->nr_pending)==0) { |
| 6800 | if (mddev->pers->hot_remove_disk( |
| 6801 | mddev, rdev->raid_disk)==0) { |
| 6802 | char nm[20]; |
| 6803 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6804 | sysfs_remove_link(&mddev->kobj, nm); |
| 6805 | rdev->raid_disk = -1; |
| 6806 | } |
| 6807 | } |
| 6808 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6809 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6810 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6811 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6812 | !test_bit(In_sync, &rdev->flags) && |
| 6813 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6814 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6815 | if (rdev->raid_disk < 0 |
| 6816 | && !test_bit(Faulty, &rdev->flags)) { |
| 6817 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6818 | if (mddev->pers-> |
| 6819 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6820 | char nm[20]; |
| 6821 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6822 | if (sysfs_create_link(&mddev->kobj, |
| 6823 | &rdev->kobj, nm)) |
| 6824 | printk(KERN_WARNING |
| 6825 | "md: cannot register " |
| 6826 | "%s for %s\n", |
| 6827 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6828 | spares++; |
| 6829 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6830 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6831 | } else |
| 6832 | break; |
| 6833 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6834 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6835 | } |
| 6836 | return spares; |
| 6837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | /* |
| 6839 | * This routine is regularly called by all per-raid-array threads to |
| 6840 | * deal with generic issues like resync and super-block update. |
| 6841 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6842 | * need this as they never do any recovery or update the superblock. |
| 6843 | * |
| 6844 | * It does not do any resync itself, but rather "forks" off other threads |
| 6845 | * to do that as needed. |
| 6846 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6847 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6848 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6849 | * and wakeups up this thread which will reap the thread and finish up. |
| 6850 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6851 | * |
| 6852 | * The overall approach is: |
| 6853 | * 1/ if the superblock needs updating, update it. |
| 6854 | * 2/ If a recovery thread is running, don't do anything else. |
| 6855 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6856 | * 4/ If there are any faulty devices, remove them. |
| 6857 | * 5/ If array is degraded, try to add spares devices |
| 6858 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6859 | */ |
| 6860 | void md_check_recovery(mddev_t *mddev) |
| 6861 | { |
| 6862 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | |
| 6864 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6865 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6866 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | |
| 6868 | if (mddev->ro) |
| 6869 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6870 | |
| 6871 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6872 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6873 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6874 | mdname(mddev)); |
| 6875 | mddev->safemode = 2; |
| 6876 | } |
| 6877 | flush_signals(current); |
| 6878 | } |
| 6879 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6880 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6881 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6883 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6885 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6886 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6887 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6888 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | )) |
| 6890 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6891 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6892 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6893 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6894 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6895 | if (mddev->ro) { |
| 6896 | /* Only thing we do on a ro array is remove |
| 6897 | * failed devices. |
| 6898 | */ |
| 6899 | remove_and_add_spares(mddev); |
| 6900 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6901 | goto unlock; |
| 6902 | } |
| 6903 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6904 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6905 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6906 | spin_lock_irq(&mddev->write_lock); |
| 6907 | if (mddev->safemode && |
| 6908 | !atomic_read(&mddev->writes_pending) && |
| 6909 | !mddev->in_sync && |
| 6910 | mddev->recovery_cp == MaxSector) { |
| 6911 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6912 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6913 | if (mddev->persistent) |
| 6914 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6915 | } |
| 6916 | if (mddev->safemode == 1) |
| 6917 | mddev->safemode = 0; |
| 6918 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6919 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6920 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6921 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6922 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6923 | if (mddev->flags) |
| 6924 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6925 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6926 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6927 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6928 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6929 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6932 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6933 | /* resync/recovery still happening */ |
| 6934 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6935 | goto unlock; |
| 6936 | } |
| 6937 | if (mddev->sync_thread) { |
| 6938 | /* resync has finished, collect result */ |
| 6939 | md_unregister_thread(mddev->sync_thread); |
| 6940 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6941 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6942 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | /* success...*/ |
| 6944 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6945 | if (mddev->pers->spare_active(mddev)) |
| 6946 | sysfs_notify(&mddev->kobj, NULL, |
| 6947 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6949 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6950 | mddev->pers->finish_reshape) |
| 6951 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6952 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6953 | |
| 6954 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6955 | * information must be scrapped |
| 6956 | */ |
| 6957 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6958 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6959 | rdev->saved_raid_disk = -1; |
| 6960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | mddev->recovery = 0; |
| 6962 | /* flag recovery needed just to double check */ |
| 6963 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6964 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6965 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | goto unlock; |
| 6967 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6968 | /* Set RUNNING before clearing NEEDED to avoid |
| 6969 | * any transients in the value of "sync_action". |
| 6970 | */ |
| 6971 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6972 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6973 | /* Clear some bits that don't mean anything, but |
| 6974 | * might be left set |
| 6975 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6976 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6977 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6979 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6980 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | /* no recovery is running. |
| 6982 | * remove any failed drives, then |
| 6983 | * add spares if possible. |
| 6984 | * Spare are also removed and re-added, to allow |
| 6985 | * the personality to fail the re-add. |
| 6986 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6988 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6989 | if (mddev->pers->check_reshape == NULL || |
| 6990 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6991 | /* Cannot proceed */ |
| 6992 | goto unlock; |
| 6993 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6994 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6995 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6996 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6997 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6998 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6999 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7000 | } else if (mddev->recovery_cp < MaxSector) { |
| 7001 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7002 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7003 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7004 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7008 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7009 | /* We are adding a device or devices to an array |
| 7010 | * which has the bitmap stored on all devices. |
| 7011 | * So make sure all bitmap pages get written |
| 7012 | */ |
| 7013 | bitmap_write_all(mddev->bitmap); |
| 7014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7016 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7017 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | if (!mddev->sync_thread) { |
| 7019 | printk(KERN_ERR "%s: could not start resync" |
| 7020 | " thread...\n", |
| 7021 | mdname(mddev)); |
| 7022 | /* leave the spares where they are, it shouldn't hurt */ |
| 7023 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7024 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7026 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7027 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | } |
| 7029 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7030 | if (!mddev->sync_thread) { |
| 7031 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7032 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7033 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7034 | if (mddev->sysfs_action) |
| 7035 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | mddev_unlock(mddev); |
| 7038 | } |
| 7039 | } |
| 7040 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7041 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7042 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7043 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7044 | wait_event_timeout(rdev->blocked_wait, |
| 7045 | !test_bit(Blocked, &rdev->flags), |
| 7046 | msecs_to_jiffies(5000)); |
| 7047 | rdev_dec_pending(rdev, mddev); |
| 7048 | } |
| 7049 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7050 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7051 | static int md_notify_reboot(struct notifier_block *this, |
| 7052 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | { |
| 7054 | struct list_head *tmp; |
| 7055 | mddev_t *mddev; |
| 7056 | |
| 7057 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7058 | |
| 7059 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7060 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7061 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7062 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7063 | /* Force a switch to readonly even array |
| 7064 | * appears to still be in use. Hence |
| 7065 | * the '100'. |
| 7066 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7067 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7068 | mddev_unlock(mddev); |
| 7069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | /* |
| 7071 | * certain more exotic SCSI devices are known to be |
| 7072 | * volatile wrt too early system reboots. While the |
| 7073 | * right place to handle this issue is the given |
| 7074 | * driver, we do want to have a safe RAID driver ... |
| 7075 | */ |
| 7076 | mdelay(1000*1); |
| 7077 | } |
| 7078 | return NOTIFY_DONE; |
| 7079 | } |
| 7080 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7081 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7082 | .notifier_call = md_notify_reboot, |
| 7083 | .next = NULL, |
| 7084 | .priority = INT_MAX, /* before any real devices */ |
| 7085 | }; |
| 7086 | |
| 7087 | static void md_geninit(void) |
| 7088 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7089 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7090 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7091 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7092 | } |
| 7093 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7094 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7096 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | return -1; |
| 7098 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7099 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | return -1; |
| 7101 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7102 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7103 | md_probe, NULL, NULL); |
| 7104 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7105 | md_probe, NULL, NULL); |
| 7106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7108 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | |
| 7110 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7111 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | } |
| 7113 | |
| 7114 | |
| 7115 | #ifndef MODULE |
| 7116 | |
| 7117 | /* |
| 7118 | * Searches all registered partitions for autorun RAID arrays |
| 7119 | * at boot time. |
| 7120 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7121 | |
| 7122 | static LIST_HEAD(all_detected_devices); |
| 7123 | struct detected_devices_node { |
| 7124 | struct list_head list; |
| 7125 | dev_t dev; |
| 7126 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | |
| 7128 | void md_autodetect_dev(dev_t dev) |
| 7129 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7130 | struct detected_devices_node *node_detected_dev; |
| 7131 | |
| 7132 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7133 | if (node_detected_dev) { |
| 7134 | node_detected_dev->dev = dev; |
| 7135 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7136 | } else { |
| 7137 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7138 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | } |
| 7141 | |
| 7142 | |
| 7143 | static void autostart_arrays(int part) |
| 7144 | { |
| 7145 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7146 | struct detected_devices_node *node_detected_dev; |
| 7147 | dev_t dev; |
| 7148 | int i_scanned, i_passed; |
| 7149 | |
| 7150 | i_scanned = 0; |
| 7151 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | |
| 7153 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7154 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7155 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7156 | i_scanned++; |
| 7157 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7158 | struct detected_devices_node, list); |
| 7159 | list_del(&node_detected_dev->list); |
| 7160 | dev = node_detected_dev->dev; |
| 7161 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7162 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | if (IS_ERR(rdev)) |
| 7164 | continue; |
| 7165 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7166 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | MD_BUG(); |
| 7168 | continue; |
| 7169 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7170 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7172 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7173 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7174 | |
| 7175 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7176 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | |
| 7178 | autorun_devices(part); |
| 7179 | } |
| 7180 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7181 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | |
| 7183 | static __exit void md_exit(void) |
| 7184 | { |
| 7185 | mddev_t *mddev; |
| 7186 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7187 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7188 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7189 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7191 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | unregister_blkdev(mdp_major, "mdp"); |
| 7193 | unregister_reboot_notifier(&md_notifier); |
| 7194 | unregister_sysctl_table(raid_table_header); |
| 7195 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7196 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7198 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | } |
| 7200 | } |
| 7201 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7202 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | module_exit(md_exit) |
| 7204 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7205 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7206 | { |
| 7207 | return sprintf(buffer, "%d", start_readonly); |
| 7208 | } |
| 7209 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7210 | { |
| 7211 | char *e; |
| 7212 | int num = simple_strtoul(val, &e, 10); |
| 7213 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7214 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7215 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7216 | } |
| 7217 | return -EINVAL; |
| 7218 | } |
| 7219 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7220 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7221 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7222 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7223 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | EXPORT_SYMBOL(register_md_personality); |
| 7226 | EXPORT_SYMBOL(unregister_md_personality); |
| 7227 | EXPORT_SYMBOL(md_error); |
| 7228 | EXPORT_SYMBOL(md_done_sync); |
| 7229 | EXPORT_SYMBOL(md_write_start); |
| 7230 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | EXPORT_SYMBOL(md_register_thread); |
| 7232 | EXPORT_SYMBOL(md_unregister_thread); |
| 7233 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | EXPORT_SYMBOL(md_check_recovery); |
| 7235 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7236 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7237 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7238 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |