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> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 61 | /* pers_list is a list of registered personalities protected |
| 62 | * by pers_lock. |
| 63 | * pers_lock does extra service to protect accesses to |
| 64 | * mddev->thread when the mutex cannot be held. |
| 65 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 66 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(pers_lock); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | static void md_print_devices(void); |
| 70 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 72 | static struct workqueue_struct *md_wq; |
| 73 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 78 | * Default number of read corrections we'll attempt on an rdev |
| 79 | * before ejecting it from the array. We divide the read error |
| 80 | * count by 2 for every hour elapsed between read errors. |
| 81 | */ |
| 82 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 85 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 86 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 87 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 89 | * speed limit - in case reconstruction slows down your system despite |
| 90 | * idle IO detection. |
| 91 | * |
| 92 | * 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] | 93 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | |
| 96 | static int sysctl_speed_limit_min = 1000; |
| 97 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 98 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | { |
| 100 | return mddev->sync_speed_min ? |
| 101 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 102 | } |
| 103 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_max ? |
| 107 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | static struct ctl_table_header *raid_table_header; |
| 111 | |
| 112 | static ctl_table raid_table[] = { |
| 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .procname = "speed_limit_min", |
| 115 | .data = &sysctl_speed_limit_min, |
| 116 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 117 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 118 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }, |
| 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 125 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 127 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .procname = "raid", |
| 133 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 134 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .child = raid_table, |
| 136 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 137 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static ctl_table raid_root_table[] = { |
| 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .procname = "dev", |
| 143 | .maxlen = 0, |
| 144 | .mode = 0555, |
| 145 | .child = raid_dir_table, |
| 146 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 147 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 150 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 152 | static int start_readonly; |
| 153 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 154 | /* bio_clone_mddev |
| 155 | * like bio_clone, but with a local bio set |
| 156 | */ |
| 157 | |
| 158 | static void mddev_bio_destructor(struct bio *bio) |
| 159 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 160 | struct mddev *mddev, **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | |
| 162 | mddevp = (void*)bio; |
| 163 | mddev = mddevp[-1]; |
| 164 | |
| 165 | bio_free(bio, mddev->bio_set); |
| 166 | } |
| 167 | |
| 168 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 169 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 170 | { |
| 171 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 172 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | |
| 174 | if (!mddev || !mddev->bio_set) |
| 175 | return bio_alloc(gfp_mask, nr_iovecs); |
| 176 | |
| 177 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 178 | mddev->bio_set); |
| 179 | if (!b) |
| 180 | return NULL; |
| 181 | mddevp = (void*)b; |
| 182 | mddevp[-1] = mddev; |
| 183 | b->bi_destructor = mddev_bio_destructor; |
| 184 | return b; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 187 | |
| 188 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 189 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 190 | { |
| 191 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 192 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 193 | |
| 194 | if (!mddev || !mddev->bio_set) |
| 195 | return bio_clone(bio, gfp_mask); |
| 196 | |
| 197 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 198 | mddev->bio_set); |
| 199 | if (!b) |
| 200 | return NULL; |
| 201 | mddevp = (void*)b; |
| 202 | mddevp[-1] = mddev; |
| 203 | b->bi_destructor = mddev_bio_destructor; |
| 204 | __bio_clone(b, bio); |
| 205 | if (bio_integrity(bio)) { |
| 206 | int ret; |
| 207 | |
| 208 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 209 | |
| 210 | if (ret < 0) { |
| 211 | bio_put(b); |
| 212 | return NULL; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return b; |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 219 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 220 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 221 | { |
| 222 | /* 'bio' is a cloned bio which we need to trim to match |
| 223 | * the given offset and size. |
| 224 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 225 | */ |
| 226 | int i; |
| 227 | struct bio_vec *bvec; |
| 228 | int sofar = 0; |
| 229 | |
| 230 | size <<= 9; |
| 231 | if (offset == 0 && size == bio->bi_size) |
| 232 | return; |
| 233 | |
| 234 | bio->bi_sector += offset; |
| 235 | bio->bi_size = size; |
| 236 | offset <<= 9; |
| 237 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 238 | |
| 239 | while (bio->bi_idx < bio->bi_vcnt && |
| 240 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 241 | /* remove this whole bio_vec */ |
| 242 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 243 | bio->bi_idx++; |
| 244 | } |
| 245 | if (bio->bi_idx < bio->bi_vcnt) { |
| 246 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 247 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 248 | } |
| 249 | /* avoid any complications with bi_idx being non-zero*/ |
| 250 | if (bio->bi_idx) { |
| 251 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 252 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 253 | bio->bi_vcnt -= bio->bi_idx; |
| 254 | bio->bi_idx = 0; |
| 255 | } |
| 256 | /* Make sure vcnt and last bv are not too big */ |
| 257 | bio_for_each_segment(bvec, bio, i) { |
| 258 | if (sofar + bvec->bv_len > size) |
| 259 | bvec->bv_len = size - sofar; |
| 260 | if (bvec->bv_len == 0) { |
| 261 | bio->bi_vcnt = i; |
| 262 | break; |
| 263 | } |
| 264 | sofar += bvec->bv_len; |
| 265 | } |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 270 | * We have a system wide 'event count' that is incremented |
| 271 | * on any 'interesting' event, and readers of /proc/mdstat |
| 272 | * can use 'poll' or 'select' to find out when the event |
| 273 | * count increases. |
| 274 | * |
| 275 | * Events are: |
| 276 | * start array, stop array, error, add device, remove device, |
| 277 | * start build, activate spare |
| 278 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 279 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 280 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 281 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 282 | { |
| 283 | atomic_inc(&md_event_count); |
| 284 | wake_up(&md_event_waiters); |
| 285 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 287 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 288 | /* Alternate version that can be called from interrupts |
| 289 | * when calling sysfs_notify isn't needed. |
| 290 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 291 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 292 | { |
| 293 | atomic_inc(&md_event_count); |
| 294 | wake_up(&md_event_waiters); |
| 295 | } |
| 296 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 297 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * Enables to iterate over all existing md arrays |
| 299 | * all_mddevs_lock protects this list. |
| 300 | */ |
| 301 | static LIST_HEAD(all_mddevs); |
| 302 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 303 | |
| 304 | |
| 305 | /* |
| 306 | * iterates through all used mddevs in the system. |
| 307 | * We take care to grab the all_mddevs_lock whenever navigating |
| 308 | * the list, and to always hold a refcount when unlocked. |
| 309 | * Any code which breaks out of this loop while own |
| 310 | * a reference to the current mddev and must mddev_put it. |
| 311 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 312 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | \ |
| 314 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 315 | _tmp = all_mddevs.next; \ |
| 316 | _mddev = NULL;}); \ |
| 317 | ({ if (_tmp != &all_mddevs) \ |
| 318 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 320 | if (_mddev) mddev_put(_mddev); \ |
| 321 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 322 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 324 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | ) |
| 326 | |
| 327 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 328 | /* Rather than calling directly into the personality make_request function, |
| 329 | * IO requests come here first so that we can check if the device is |
| 330 | * being suspended pending a reconfiguration. |
| 331 | * We hold a refcount over the call to ->make_request. By the time that |
| 332 | * call has finished, the bio has been linked into some internal structure |
| 333 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 334 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 335 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 337 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 338 | struct mddev *mddev = q->queuedata; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 339 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 340 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 341 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 342 | if (mddev == NULL || mddev->pers == NULL |
| 343 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 344 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 345 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 346 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 347 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 348 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 349 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 350 | DEFINE_WAIT(__wait); |
| 351 | for (;;) { |
| 352 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 353 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 354 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 355 | break; |
| 356 | rcu_read_unlock(); |
| 357 | schedule(); |
| 358 | rcu_read_lock(); |
| 359 | } |
| 360 | finish_wait(&mddev->sb_wait, &__wait); |
| 361 | } |
| 362 | atomic_inc(&mddev->active_io); |
| 363 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 364 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 365 | /* |
| 366 | * save the sectors now since our bio can |
| 367 | * go away inside make_request |
| 368 | */ |
| 369 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 370 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 371 | |
| 372 | cpu = part_stat_lock(); |
| 373 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 374 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 375 | part_stat_unlock(); |
| 376 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 377 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 378 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 381 | /* mddev_suspend makes sure no new requests are submitted |
| 382 | * to the device, and that any requests that have been submitted |
| 383 | * are completely handled. |
| 384 | * Once ->stop is called and completes, the module will be completely |
| 385 | * unused. |
| 386 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 387 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 388 | { |
| 389 | BUG_ON(mddev->suspended); |
| 390 | mddev->suspended = 1; |
| 391 | synchronize_rcu(); |
| 392 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 393 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 394 | |
| 395 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 396 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 398 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 399 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 400 | { |
| 401 | mddev->suspended = 0; |
| 402 | wake_up(&mddev->sb_wait); |
| 403 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 404 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 405 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 406 | md_wakeup_thread(mddev->thread); |
| 407 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 408 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 409 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 410 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 411 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 412 | { |
| 413 | return mddev->suspended; |
| 414 | } |
| 415 | EXPORT_SYMBOL(mddev_congested); |
| 416 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | */ |
| 420 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 423 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 424 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | |
| 426 | rdev_dec_pending(rdev, mddev); |
| 427 | |
| 428 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 429 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 430 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
| 432 | bio_put(bio); |
| 433 | } |
| 434 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 435 | static void md_submit_flush_data(struct work_struct *ws); |
| 436 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 437 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 439 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 440 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 442 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 443 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 445 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 446 | if (rdev->raid_disk >= 0 && |
| 447 | !test_bit(Faulty, &rdev->flags)) { |
| 448 | /* Take two references, one is dropped |
| 449 | * when request finishes, one after |
| 450 | * we reclaim rcu_read_lock |
| 451 | */ |
| 452 | struct bio *bi; |
| 453 | atomic_inc(&rdev->nr_pending); |
| 454 | atomic_inc(&rdev->nr_pending); |
| 455 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 456 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 457 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 458 | bi->bi_private = rdev; |
| 459 | bi->bi_bdev = rdev->bdev; |
| 460 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 461 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 462 | rcu_read_lock(); |
| 463 | rdev_dec_pending(rdev, mddev); |
| 464 | } |
| 465 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 466 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 467 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 468 | } |
| 469 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 470 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 471 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 472 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 473 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 475 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 476 | /* an empty barrier - all done */ |
| 477 | bio_endio(bio, 0); |
| 478 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 479 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 480 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 481 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 482 | |
| 483 | mddev->flush_bio = NULL; |
| 484 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 485 | } |
| 486 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 487 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 488 | { |
| 489 | spin_lock_irq(&mddev->write_lock); |
| 490 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 491 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 492 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 493 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 494 | spin_unlock_irq(&mddev->write_lock); |
| 495 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 496 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 497 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 498 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 499 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 500 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 501 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 502 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 503 | struct mddev *mddev = cb->data; |
| 504 | md_wakeup_thread(mddev->thread); |
| 505 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 506 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 507 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 508 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 509 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | atomic_inc(&mddev->active); |
| 512 | return mddev; |
| 513 | } |
| 514 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 515 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 516 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 517 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 519 | struct bio_set *bs = NULL; |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 522 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 523 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 524 | mddev->ctime == 0 && !mddev->hold_active) { |
| 525 | /* Array is not configured at all, and not held active, |
| 526 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 527 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 528 | bs = mddev->bio_set; |
| 529 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 530 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 531 | /* We did a probe so need to clean up. Call |
| 532 | * queue_work inside the spinlock so that |
| 533 | * flush_workqueue() after mddev_find will |
| 534 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 535 | */ |
| 536 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 537 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 538 | } else |
| 539 | kfree(mddev); |
| 540 | } |
| 541 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 542 | if (bs) |
| 543 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 546 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 547 | { |
| 548 | mutex_init(&mddev->open_mutex); |
| 549 | mutex_init(&mddev->reconfig_mutex); |
| 550 | mutex_init(&mddev->bitmap_info.mutex); |
| 551 | INIT_LIST_HEAD(&mddev->disks); |
| 552 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 553 | init_timer(&mddev->safemode_timer); |
| 554 | atomic_set(&mddev->active, 1); |
| 555 | atomic_set(&mddev->openers, 0); |
| 556 | atomic_set(&mddev->active_io, 0); |
| 557 | spin_lock_init(&mddev->write_lock); |
| 558 | atomic_set(&mddev->flush_pending, 0); |
| 559 | init_waitqueue_head(&mddev->sb_wait); |
| 560 | init_waitqueue_head(&mddev->recovery_wait); |
| 561 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 562 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 563 | mddev->resync_min = 0; |
| 564 | mddev->resync_max = MaxSector; |
| 565 | mddev->level = LEVEL_NONE; |
| 566 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 567 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 568 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 569 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 571 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 573 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 574 | unit &= ~((1<<MdpMinorShift)-1); |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | retry: |
| 577 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 579 | if (unit) { |
| 580 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 581 | if (mddev->unit == unit) { |
| 582 | mddev_get(mddev); |
| 583 | spin_unlock(&all_mddevs_lock); |
| 584 | kfree(new); |
| 585 | return mddev; |
| 586 | } |
| 587 | |
| 588 | if (new) { |
| 589 | list_add(&new->all_mddevs, &all_mddevs); |
| 590 | spin_unlock(&all_mddevs_lock); |
| 591 | new->hold_active = UNTIL_IOCTL; |
| 592 | return new; |
| 593 | } |
| 594 | } else if (new) { |
| 595 | /* find an unused unit number */ |
| 596 | static int next_minor = 512; |
| 597 | int start = next_minor; |
| 598 | int is_free = 0; |
| 599 | int dev = 0; |
| 600 | while (!is_free) { |
| 601 | dev = MKDEV(MD_MAJOR, next_minor); |
| 602 | next_minor++; |
| 603 | if (next_minor > MINORMASK) |
| 604 | next_minor = 0; |
| 605 | if (next_minor == start) { |
| 606 | /* Oh dear, all in use. */ |
| 607 | spin_unlock(&all_mddevs_lock); |
| 608 | kfree(new); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | is_free = 1; |
| 613 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 614 | if (mddev->unit == dev) { |
| 615 | is_free = 0; |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | new->unit = dev; |
| 620 | new->md_minor = MINOR(dev); |
| 621 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | list_add(&new->all_mddevs, &all_mddevs); |
| 623 | spin_unlock(&all_mddevs_lock); |
| 624 | return new; |
| 625 | } |
| 626 | spin_unlock(&all_mddevs_lock); |
| 627 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 628 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (!new) |
| 630 | return NULL; |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | new->unit = unit; |
| 633 | if (MAJOR(unit) == MD_MAJOR) |
| 634 | new->md_minor = MINOR(unit); |
| 635 | else |
| 636 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 637 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 638 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | goto retry; |
| 641 | } |
| 642 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 643 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 645 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 648 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 649 | { |
| 650 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 651 | } |
| 652 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 653 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 655 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 658 | static struct attribute_group md_redundancy_group; |
| 659 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 660 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 662 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 663 | /* These cannot be removed under reconfig_mutex as |
| 664 | * an access to the files will try to take reconfig_mutex |
| 665 | * while holding the file unremovable, which leads to |
| 666 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 667 | * So hold set sysfs_active while the remove in happeing, |
| 668 | * and anything else which might set ->to_remove or my |
| 669 | * otherwise change the sysfs namespace will fail with |
| 670 | * -EBUSY if sysfs_active is still set. |
| 671 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 672 | * test it under the same mutex to ensure its correct value |
| 673 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 674 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 675 | struct attribute_group *to_remove = mddev->to_remove; |
| 676 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 677 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 678 | mutex_unlock(&mddev->reconfig_mutex); |
| 679 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 680 | if (mddev->kobj.sd) { |
| 681 | if (to_remove != &md_redundancy_group) |
| 682 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 683 | if (mddev->pers == NULL || |
| 684 | mddev->pers->sync_request == NULL) { |
| 685 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 686 | if (mddev->sysfs_action) |
| 687 | sysfs_put(mddev->sysfs_action); |
| 688 | mddev->sysfs_action = NULL; |
| 689 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 690 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 691 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 692 | } else |
| 693 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 695 | /* As we've dropped the mutex we need a spinlock to |
| 696 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 697 | */ |
| 698 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 699 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 700 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 703 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 705 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 707 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (rdev->desc_nr == nr) |
| 709 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | return NULL; |
| 712 | } |
| 713 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 714 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 716 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 718 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (rdev->bdev->bd_dev == dev) |
| 720 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return NULL; |
| 723 | } |
| 724 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 725 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 726 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 727 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 728 | list_for_each_entry(pers, &pers_list, list) { |
| 729 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 730 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 731 | if (strcmp(pers->name, clevel)==0) |
| 732 | return pers; |
| 733 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
| 736 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 737 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 738 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 740 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 741 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 744 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
| 746 | if (rdev->sb_page) |
| 747 | MD_BUG(); |
| 748 | |
| 749 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 750 | if (!rdev->sb_page) { |
| 751 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 752 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 758 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
| 760 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 761 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | rdev->sb_loaded = 0; |
| 763 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 764 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 765 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 767 | if (rdev->bb_page) { |
| 768 | put_page(rdev->bb_page); |
| 769 | rdev->bb_page = NULL; |
| 770 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 771 | kfree(rdev->badblocks.page); |
| 772 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 774 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 776 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 777 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 778 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 779 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 780 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 781 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 782 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 783 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 784 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 785 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 786 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 787 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 788 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 789 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 790 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 791 | } |
| 792 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 793 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 794 | sector_t sector, int size, struct page *page) |
| 795 | { |
| 796 | /* write first size bytes of page to sector of rdev |
| 797 | * Increment mddev->pending_writes before returning |
| 798 | * and decrement it on completion, waking up sb_wait |
| 799 | * if zero is reached. |
| 800 | * If an error occurred, call md_error |
| 801 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 802 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 803 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 804 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 805 | bio->bi_sector = sector; |
| 806 | bio_add_page(bio, page, size, 0); |
| 807 | bio->bi_private = rdev; |
| 808 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 809 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 810 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 811 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 812 | } |
| 813 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 814 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 815 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 816 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 817 | DEFINE_WAIT(wq); |
| 818 | for(;;) { |
| 819 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 820 | if (atomic_read(&mddev->pending_writes)==0) |
| 821 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 822 | schedule(); |
| 823 | } |
| 824 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 825 | } |
| 826 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 827 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 832 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 833 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 835 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | struct completion event; |
| 837 | int ret; |
| 838 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 839 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 841 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 842 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 843 | if (metadata_op) |
| 844 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 845 | else if (rdev->mddev->reshape_position != MaxSector && |
| 846 | (rdev->mddev->reshape_backwards == |
| 847 | (sector >= rdev->mddev->reshape_position))) |
| 848 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 849 | else |
| 850 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | bio_add_page(bio, page, size, 0); |
| 852 | init_completion(&event); |
| 853 | bio->bi_private = &event; |
| 854 | bio->bi_end_io = bi_complete; |
| 855 | submit_bio(rw, bio); |
| 856 | wait_for_completion(&event); |
| 857 | |
| 858 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 859 | bio_put(bio); |
| 860 | return ret; |
| 861 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 862 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 864 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { |
| 866 | char b[BDEVNAME_SIZE]; |
| 867 | if (!rdev->sb_page) { |
| 868 | MD_BUG(); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | if (rdev->sb_loaded) |
| 872 | return 0; |
| 873 | |
| 874 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 875 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | goto fail; |
| 877 | rdev->sb_loaded = 1; |
| 878 | return 0; |
| 879 | |
| 880 | fail: |
| 881 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 882 | bdevname(rdev->bdev,b)); |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 887 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 888 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 889 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 890 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 891 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 895 | { |
| 896 | int ret; |
| 897 | mdp_super_t *tmp1, *tmp2; |
| 898 | |
| 899 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 900 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 901 | |
| 902 | if (!tmp1 || !tmp2) { |
| 903 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 904 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | goto abort; |
| 906 | } |
| 907 | |
| 908 | *tmp1 = *sb1; |
| 909 | *tmp2 = *sb2; |
| 910 | |
| 911 | /* |
| 912 | * nr_disks is not constant |
| 913 | */ |
| 914 | tmp1->nr_disks = 0; |
| 915 | tmp2->nr_disks = 0; |
| 916 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 917 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 919 | kfree(tmp1); |
| 920 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return ret; |
| 922 | } |
| 923 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 924 | |
| 925 | static u32 md_csum_fold(u32 csum) |
| 926 | { |
| 927 | csum = (csum & 0xffff) + (csum >> 16); |
| 928 | return (csum & 0xffff) + (csum >> 16); |
| 929 | } |
| 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 932 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 933 | u64 newcsum = 0; |
| 934 | u32 *sb32 = (u32*)sb; |
| 935 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | unsigned int disk_csum, csum; |
| 937 | |
| 938 | disk_csum = sb->sb_csum; |
| 939 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 940 | |
| 941 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 942 | newcsum += sb32[i]; |
| 943 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 944 | |
| 945 | |
| 946 | #ifdef CONFIG_ALPHA |
| 947 | /* This used to use csum_partial, which was wrong for several |
| 948 | * reasons including that different results are returned on |
| 949 | * different architectures. It isn't critical that we get exactly |
| 950 | * the same return value as before (we always csum_fold before |
| 951 | * testing, and that removes any differences). However as we |
| 952 | * know that csum_partial always returned a 16bit value on |
| 953 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 954 | */ |
| 955 | sb->sb_csum = md_csum_fold(disk_csum); |
| 956 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 958 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | return csum; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | /* |
| 964 | * Handle superblock details. |
| 965 | * We want to be able to handle multiple superblock formats |
| 966 | * so we have a common interface to them all, and an array of |
| 967 | * different handlers. |
| 968 | * We rely on user-space to write the initial superblock, and support |
| 969 | * reading and updating of superblocks. |
| 970 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 971 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | * loads and validates a superblock on dev. |
| 973 | * if refdev != NULL, compare superblocks on both devices |
| 974 | * Return: |
| 975 | * 0 - dev has a superblock that is compatible with refdev |
| 976 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 977 | * so dev should be used as the refdev in future |
| 978 | * -EINVAL superblock incompatible or invalid |
| 979 | * -othererror e.g. -EIO |
| 980 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 981 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | * Verify that dev is acceptable into mddev. |
| 983 | * The first time, mddev->raid_disks will be 0, and data from |
| 984 | * dev should be merged in. Subsequent calls check that dev |
| 985 | * is new enough. Return 0 or -EINVAL |
| 986 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 987 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | * Update the superblock for rdev with data in mddev |
| 989 | * This does not write to disc. |
| 990 | * |
| 991 | */ |
| 992 | |
| 993 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 994 | char *name; |
| 995 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 996 | int (*load_super)(struct md_rdev *rdev, |
| 997 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 998 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 999 | int (*validate_super)(struct mddev *mddev, |
| 1000 | struct md_rdev *rdev); |
| 1001 | void (*sync_super)(struct mddev *mddev, |
| 1002 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1003 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1004 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1005 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1006 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | }; |
| 1008 | |
| 1009 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1010 | * Check that the given mddev has no bitmap. |
| 1011 | * |
| 1012 | * This function is called from the run method of all personalities that do not |
| 1013 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1014 | * has a bitmap. Otherwise, it returns 0. |
| 1015 | * |
| 1016 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1017 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1018 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1019 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1020 | return 0; |
| 1021 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1022 | mdname(mddev), mddev->pers->name); |
| 1023 | return 1; |
| 1024 | } |
| 1025 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1026 | |
| 1027 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * load_super for 0.90.0 |
| 1029 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1030 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1033 | mdp_super_t *sb; |
| 1034 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1037 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | * it's at the end of the disk. |
| 1039 | * |
| 1040 | * It also happens to be a multiple of 4Kb. |
| 1041 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1042 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1044 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (ret) return ret; |
| 1046 | |
| 1047 | ret = -EINVAL; |
| 1048 | |
| 1049 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1050 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
| 1052 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1053 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1054 | b); |
| 1055 | goto abort; |
| 1056 | } |
| 1057 | |
| 1058 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1059 | sb->minor_version < 90 || |
| 1060 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1062 | sb->major_version, sb->minor_version, |
| 1063 | b); |
| 1064 | goto abort; |
| 1065 | } |
| 1066 | |
| 1067 | if (sb->raid_disks <= 0) |
| 1068 | goto abort; |
| 1069 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1070 | 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] | 1071 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1072 | b); |
| 1073 | goto abort; |
| 1074 | } |
| 1075 | |
| 1076 | rdev->preferred_minor = sb->md_minor; |
| 1077 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1078 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1079 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1080 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
| 1082 | if (sb->level == LEVEL_MULTIPATH) |
| 1083 | rdev->desc_nr = -1; |
| 1084 | else |
| 1085 | rdev->desc_nr = sb->this_disk.number; |
| 1086 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1087 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1089 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1091 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | if (!uuid_equal(refsb, sb)) { |
| 1093 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1094 | b, bdevname(refdev->bdev,b2)); |
| 1095 | goto abort; |
| 1096 | } |
| 1097 | if (!sb_equal(refsb, sb)) { |
| 1098 | printk(KERN_WARNING "md: %s has same UUID" |
| 1099 | " but different superblock to %s\n", |
| 1100 | b, bdevname(refdev->bdev, b2)); |
| 1101 | goto abort; |
| 1102 | } |
| 1103 | ev1 = md_event(sb); |
| 1104 | ev2 = md_event(refsb); |
| 1105 | if (ev1 > ev2) |
| 1106 | ret = 1; |
| 1107 | else |
| 1108 | ret = 0; |
| 1109 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1110 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1111 | /* Limit to 4TB as metadata cannot record more than that. |
| 1112 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1113 | * record this size) |
| 1114 | */ |
| 1115 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1116 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1118 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1119 | /* "this cannot possibly happen" ... */ |
| 1120 | ret = -EINVAL; |
| 1121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | abort: |
| 1123 | return ret; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * validate_super for 0.90.0 |
| 1128 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1129 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
| 1131 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1132 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1133 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1136 | clear_bit(Faulty, &rdev->flags); |
| 1137 | clear_bit(In_sync, &rdev->flags); |
| 1138 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (mddev->raid_disks == 0) { |
| 1141 | mddev->major_version = 0; |
| 1142 | mddev->minor_version = sb->minor_version; |
| 1143 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1144 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1145 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | mddev->ctime = sb->ctime; |
| 1147 | mddev->utime = sb->utime; |
| 1148 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1149 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | mddev->layout = sb->layout; |
| 1151 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1152 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1153 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1154 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1155 | mddev->bitmap_info.space = 0; |
| 1156 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1157 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1158 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1159 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1161 | if (mddev->minor_version >= 91) { |
| 1162 | mddev->reshape_position = sb->reshape_position; |
| 1163 | mddev->delta_disks = sb->delta_disks; |
| 1164 | mddev->new_level = sb->new_level; |
| 1165 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1166 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1167 | if (mddev->delta_disks < 0) |
| 1168 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1169 | } else { |
| 1170 | mddev->reshape_position = MaxSector; |
| 1171 | mddev->delta_disks = 0; |
| 1172 | mddev->new_level = mddev->level; |
| 1173 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1174 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1178 | mddev->recovery_cp = MaxSector; |
| 1179 | else { |
| 1180 | if (sb->events_hi == sb->cp_events_hi && |
| 1181 | sb->events_lo == sb->cp_events_lo) { |
| 1182 | mddev->recovery_cp = sb->recovery_cp; |
| 1183 | } else |
| 1184 | mddev->recovery_cp = 0; |
| 1185 | } |
| 1186 | |
| 1187 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1188 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1189 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1190 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1191 | |
| 1192 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1193 | |
| 1194 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1195 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1196 | mddev->bitmap_info.offset = |
| 1197 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1198 | mddev->bitmap_info.space = |
| 1199 | mddev->bitmap_info.space; |
| 1200 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1201 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1202 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1203 | /* Insist on good event counter while assembling, except |
| 1204 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1206 | if (sb->disks[rdev->desc_nr].state & ( |
| 1207 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1208 | if (ev1 < mddev->events) |
| 1209 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1210 | } else if (mddev->bitmap) { |
| 1211 | /* if adding to array with a bitmap, then we can accept an |
| 1212 | * older device ... but not too old. |
| 1213 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1214 | if (ev1 < mddev->bitmap->events_cleared) |
| 1215 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1216 | } else { |
| 1217 | if (ev1 < mddev->events) |
| 1218 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1219 | return 0; |
| 1220 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | desc = sb->disks + rdev->desc_nr; |
| 1224 | |
| 1225 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1226 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1227 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1228 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1229 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1231 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1232 | /* active but not in sync implies recovery up to |
| 1233 | * reshape position. We don't know exactly where |
| 1234 | * that is, so set to zero for now */ |
| 1235 | if (mddev->minor_version >= 91) { |
| 1236 | rdev->recovery_offset = 0; |
| 1237 | rdev->raid_disk = desc->raid_disk; |
| 1238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1240 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1241 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1242 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1243 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * sync_super for 0.90.0 |
| 1249 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1250 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | { |
| 1252 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1253 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
| 1257 | /* make rdev->sb match mddev data.. |
| 1258 | * |
| 1259 | * 1/ zero out disks |
| 1260 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1261 | * 3/ any empty disks < next_spare become removed |
| 1262 | * |
| 1263 | * disks[0] gets initialised to REMOVED because |
| 1264 | * we cannot be sure from other fields if it has |
| 1265 | * been initialised or not. |
| 1266 | */ |
| 1267 | int i; |
| 1268 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1269 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1270 | rdev->sb_size = MD_SB_BYTES; |
| 1271 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1272 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
| 1274 | memset(sb, 0, sizeof(*sb)); |
| 1275 | |
| 1276 | sb->md_magic = MD_SB_MAGIC; |
| 1277 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | sb->patch_version = mddev->patch_version; |
| 1279 | sb->gvalid_words = 0; /* ignored */ |
| 1280 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1281 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1282 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1283 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1284 | |
| 1285 | sb->ctime = mddev->ctime; |
| 1286 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1287 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | sb->raid_disks = mddev->raid_disks; |
| 1289 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1290 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | sb->utime = mddev->utime; |
| 1292 | sb->state = 0; |
| 1293 | sb->events_hi = (mddev->events>>32); |
| 1294 | sb->events_lo = (u32)mddev->events; |
| 1295 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1296 | if (mddev->reshape_position == MaxSector) |
| 1297 | sb->minor_version = 90; |
| 1298 | else { |
| 1299 | sb->minor_version = 91; |
| 1300 | sb->reshape_position = mddev->reshape_position; |
| 1301 | sb->new_level = mddev->new_level; |
| 1302 | sb->delta_disks = mddev->delta_disks; |
| 1303 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1304 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1305 | } |
| 1306 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | if (mddev->in_sync) |
| 1308 | { |
| 1309 | sb->recovery_cp = mddev->recovery_cp; |
| 1310 | sb->cp_events_hi = (mddev->events>>32); |
| 1311 | sb->cp_events_lo = (u32)mddev->events; |
| 1312 | if (mddev->recovery_cp == MaxSector) |
| 1313 | sb->state = (1<< MD_SB_CLEAN); |
| 1314 | } else |
| 1315 | sb->recovery_cp = 0; |
| 1316 | |
| 1317 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1318 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1320 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1321 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1324 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1326 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1327 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1328 | |
| 1329 | if (rdev2->raid_disk >= 0 && |
| 1330 | sb->minor_version >= 91) |
| 1331 | /* we have nowhere to store the recovery_offset, |
| 1332 | * but if it is not below the reshape_position, |
| 1333 | * we can piggy-back on that. |
| 1334 | */ |
| 1335 | is_active = 1; |
| 1336 | if (rdev2->raid_disk < 0 || |
| 1337 | test_bit(Faulty, &rdev2->flags)) |
| 1338 | is_active = 0; |
| 1339 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1340 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1342 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1343 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | d = &sb->disks[rdev2->desc_nr]; |
| 1345 | nr_disks++; |
| 1346 | d->number = rdev2->desc_nr; |
| 1347 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1348 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1349 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | d->raid_disk = rdev2->raid_disk; |
| 1351 | else |
| 1352 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1353 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1355 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1357 | if (test_bit(In_sync, &rdev2->flags)) |
| 1358 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | active++; |
| 1360 | working++; |
| 1361 | } else { |
| 1362 | d->state = 0; |
| 1363 | spare++; |
| 1364 | working++; |
| 1365 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1366 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1367 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1370 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1371 | mdp_disk_t *d = &sb->disks[i]; |
| 1372 | if (d->state == 0 && d->number == 0) { |
| 1373 | d->number = i; |
| 1374 | d->raid_disk = i; |
| 1375 | d->state = (1<<MD_DISK_REMOVED); |
| 1376 | d->state |= (1<<MD_DISK_FAULTY); |
| 1377 | failed++; |
| 1378 | } |
| 1379 | } |
| 1380 | sb->nr_disks = nr_disks; |
| 1381 | sb->active_disks = active; |
| 1382 | sb->working_disks = working; |
| 1383 | sb->failed_disks = failed; |
| 1384 | sb->spare_disks = spare; |
| 1385 | |
| 1386 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1387 | sb->sb_csum = calc_sb_csum(sb); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1391 | * rdev_size_change for 0.90.0 |
| 1392 | */ |
| 1393 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1394 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1395 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1396 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1397 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1398 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1399 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1400 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1401 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1402 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1403 | /* Limit to 4TB as metadata cannot record more than that. |
| 1404 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1405 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1406 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1407 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1408 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1409 | rdev->sb_page); |
| 1410 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1411 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1412 | } |
| 1413 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1414 | static int |
| 1415 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1416 | { |
| 1417 | /* non-zero offset changes not possible with v0.90 */ |
| 1418 | return new_offset == 0; |
| 1419 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1420 | |
| 1421 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | * version 1 superblock |
| 1423 | */ |
| 1424 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1425 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1427 | __le32 disk_csum; |
| 1428 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | unsigned long long newcsum; |
| 1430 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1431 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | int i; |
| 1433 | |
| 1434 | disk_csum = sb->sb_csum; |
| 1435 | sb->sb_csum = 0; |
| 1436 | newcsum = 0; |
| 1437 | for (i=0; size>=4; size -= 4 ) |
| 1438 | newcsum += le32_to_cpu(*isuper++); |
| 1439 | |
| 1440 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1441 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1444 | sb->sb_csum = disk_csum; |
| 1445 | return cpu_to_le32(csum); |
| 1446 | } |
| 1447 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1448 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1449 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1450 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
| 1452 | struct mdp_superblock_1 *sb; |
| 1453 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1454 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1455 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1457 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1460 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | * It is always aligned to a 4K boundary and |
| 1462 | * depeding on minor_version, it can be: |
| 1463 | * 0: At least 8K, but less than 12K, from end of device |
| 1464 | * 1: At start of device |
| 1465 | * 2: 4K from start of device. |
| 1466 | */ |
| 1467 | switch(minor_version) { |
| 1468 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1469 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1470 | sb_start -= 8*2; |
| 1471 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1474 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | break; |
| 1476 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1477 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | default: |
| 1480 | return -EINVAL; |
| 1481 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1482 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1484 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1485 | * and it is safe to read 4k, so we do that |
| 1486 | */ |
| 1487 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | if (ret) return ret; |
| 1489 | |
| 1490 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1491 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
| 1493 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1494 | sb->major_version != cpu_to_le32(1) || |
| 1495 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1496 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1497 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | return -EINVAL; |
| 1499 | |
| 1500 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1501 | printk("md: invalid superblock checksum on %s\n", |
| 1502 | bdevname(rdev->bdev,b)); |
| 1503 | return -EINVAL; |
| 1504 | } |
| 1505 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1506 | printk("md: data_size too small on %s\n", |
| 1507 | bdevname(rdev->bdev,b)); |
| 1508 | return -EINVAL; |
| 1509 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1510 | if (sb->pad0 || |
| 1511 | sb->pad3[0] || |
| 1512 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1513 | /* Some padding is non-zero, might be a new feature */ |
| 1514 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | rdev->preferred_minor = 0xffff; |
| 1517 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1518 | rdev->new_data_offset = rdev->data_offset; |
| 1519 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1520 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1521 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1522 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1524 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1525 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1526 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1527 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1528 | |
| 1529 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1530 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1531 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1532 | if (minor_version |
| 1533 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1534 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1535 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1536 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1537 | rdev->desc_nr = -1; |
| 1538 | else |
| 1539 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1540 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1541 | if (!rdev->bb_page) { |
| 1542 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1543 | if (!rdev->bb_page) |
| 1544 | return -ENOMEM; |
| 1545 | } |
| 1546 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1547 | rdev->badblocks.count == 0) { |
| 1548 | /* need to load the bad block list. |
| 1549 | * Currently we limit it to one page. |
| 1550 | */ |
| 1551 | s32 offset; |
| 1552 | sector_t bb_sector; |
| 1553 | u64 *bbp; |
| 1554 | int i; |
| 1555 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1556 | if (sectors > (PAGE_SIZE / 512)) |
| 1557 | return -EINVAL; |
| 1558 | offset = le32_to_cpu(sb->bblog_offset); |
| 1559 | if (offset == 0) |
| 1560 | return -EINVAL; |
| 1561 | bb_sector = (long long)offset; |
| 1562 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1563 | rdev->bb_page, READ, true)) |
| 1564 | return -EIO; |
| 1565 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1566 | rdev->badblocks.shift = sb->bblog_shift; |
| 1567 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1568 | u64 bb = le64_to_cpu(*bbp); |
| 1569 | int count = bb & (0x3ff); |
| 1570 | u64 sector = bb >> 10; |
| 1571 | sector <<= sb->bblog_shift; |
| 1572 | count <<= sb->bblog_shift; |
| 1573 | if (bb + 1 == 0) |
| 1574 | break; |
| 1575 | if (md_set_badblocks(&rdev->badblocks, |
| 1576 | sector, count, 1) == 0) |
| 1577 | return -EINVAL; |
| 1578 | } |
| 1579 | } else if (sb->bblog_offset == 0) |
| 1580 | rdev->badblocks.shift = -1; |
| 1581 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1582 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1583 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1584 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1586 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1589 | sb->level != refsb->level || |
| 1590 | sb->layout != refsb->layout || |
| 1591 | sb->chunksize != refsb->chunksize) { |
| 1592 | printk(KERN_WARNING "md: %s has strangely different" |
| 1593 | " superblock to %s\n", |
| 1594 | bdevname(rdev->bdev,b), |
| 1595 | bdevname(refdev->bdev,b2)); |
| 1596 | return -EINVAL; |
| 1597 | } |
| 1598 | ev1 = le64_to_cpu(sb->events); |
| 1599 | ev2 = le64_to_cpu(refsb->events); |
| 1600 | |
| 1601 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1602 | ret = 1; |
| 1603 | else |
| 1604 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1606 | if (minor_version) { |
| 1607 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1608 | sectors -= rdev->data_offset; |
| 1609 | } else |
| 1610 | sectors = rdev->sb_start; |
| 1611 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1613 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1614 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1617 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1619 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1620 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1622 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1623 | clear_bit(Faulty, &rdev->flags); |
| 1624 | clear_bit(In_sync, &rdev->flags); |
| 1625 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | if (mddev->raid_disks == 0) { |
| 1628 | mddev->major_version = 1; |
| 1629 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1630 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1631 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1633 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1634 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1635 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | mddev->layout = le32_to_cpu(sb->layout); |
| 1637 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1638 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1639 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1640 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1641 | mddev->bitmap_info.space = 0; |
| 1642 | /* Default location for bitmap is 1K after superblock |
| 1643 | * using 3K - total of 4K |
| 1644 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1645 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1646 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1647 | mddev->reshape_backwards = 0; |
| 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1650 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1651 | |
| 1652 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1653 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1654 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1655 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1656 | mddev->bitmap_info.offset = |
| 1657 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1658 | /* Metadata doesn't record how much space is available. |
| 1659 | * For 1.0, we assume we can use up to the superblock |
| 1660 | * if before, else to 4K beyond superblock. |
| 1661 | * For others, assume no change is possible. |
| 1662 | */ |
| 1663 | if (mddev->minor_version > 0) |
| 1664 | mddev->bitmap_info.space = 0; |
| 1665 | else if (mddev->bitmap_info.offset > 0) |
| 1666 | mddev->bitmap_info.space = |
| 1667 | 8 - mddev->bitmap_info.offset; |
| 1668 | else |
| 1669 | mddev->bitmap_info.space = |
| 1670 | -mddev->bitmap_info.offset; |
| 1671 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1672 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1673 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1674 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1675 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1676 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1677 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1678 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1679 | if (mddev->delta_disks < 0 || |
| 1680 | (mddev->delta_disks == 0 && |
| 1681 | (le32_to_cpu(sb->feature_map) |
| 1682 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1683 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1684 | } else { |
| 1685 | mddev->reshape_position = MaxSector; |
| 1686 | mddev->delta_disks = 0; |
| 1687 | mddev->new_level = mddev->level; |
| 1688 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1689 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1692 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1693 | /* Insist of good event counter while assembling, except for |
| 1694 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1696 | if (rdev->desc_nr >= 0 && |
| 1697 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1698 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1699 | if (ev1 < mddev->events) |
| 1700 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1701 | } else if (mddev->bitmap) { |
| 1702 | /* If adding to array with a bitmap, then we can accept an |
| 1703 | * older device, but not too old. |
| 1704 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1705 | if (ev1 < mddev->bitmap->events_cleared) |
| 1706 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1707 | } else { |
| 1708 | if (ev1 < mddev->events) |
| 1709 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1710 | return 0; |
| 1711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1713 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1714 | if (rdev->desc_nr < 0 || |
| 1715 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1716 | role = 0xffff; |
| 1717 | rdev->desc_nr = -1; |
| 1718 | } else |
| 1719 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | switch(role) { |
| 1721 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1724 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | break; |
| 1726 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1727 | if ((le32_to_cpu(sb->feature_map) & |
| 1728 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1729 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1730 | else |
| 1731 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | rdev->raid_disk = role; |
| 1733 | break; |
| 1734 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1735 | if (sb->devflags & WriteMostly1) |
| 1736 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1737 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1738 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1739 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1740 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1745 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | { |
| 1747 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1748 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | int max_dev, i; |
| 1750 | /* make rdev->sb match mddev and rdev data. */ |
| 1751 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1752 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
| 1754 | sb->feature_map = 0; |
| 1755 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1756 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1758 | |
| 1759 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1760 | sb->events = cpu_to_le64(mddev->events); |
| 1761 | if (mddev->in_sync) |
| 1762 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1763 | else |
| 1764 | sb->resync_offset = cpu_to_le64(0); |
| 1765 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1766 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1767 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1768 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1769 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1770 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1771 | sb->level = cpu_to_le32(mddev->level); |
| 1772 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1773 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1774 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1775 | sb->devflags |= WriteMostly1; |
| 1776 | else |
| 1777 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1778 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1779 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1780 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1781 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1782 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1783 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1784 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1785 | |
| 1786 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1787 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1788 | sb->feature_map |= |
| 1789 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1790 | sb->recovery_offset = |
| 1791 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1792 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1793 | if (test_bit(Replacement, &rdev->flags)) |
| 1794 | sb->feature_map |= |
| 1795 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1796 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1797 | if (mddev->reshape_position != MaxSector) { |
| 1798 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1799 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1800 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1801 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1802 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1803 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1804 | if (mddev->delta_disks == 0 && |
| 1805 | mddev->reshape_backwards) |
| 1806 | sb->feature_map |
| 1807 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1808 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1809 | sb->feature_map |
| 1810 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1811 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1812 | - rdev->data_offset)); |
| 1813 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1814 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1815 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1816 | if (rdev->badblocks.count == 0) |
| 1817 | /* Nothing to do for bad blocks*/ ; |
| 1818 | else if (sb->bblog_offset == 0) |
| 1819 | /* Cannot record bad blocks on this device */ |
| 1820 | md_error(mddev, rdev); |
| 1821 | else { |
| 1822 | struct badblocks *bb = &rdev->badblocks; |
| 1823 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1824 | u64 *p = bb->page; |
| 1825 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1826 | if (bb->changed) { |
| 1827 | unsigned seq; |
| 1828 | |
| 1829 | retry: |
| 1830 | seq = read_seqbegin(&bb->lock); |
| 1831 | |
| 1832 | memset(bbp, 0xff, PAGE_SIZE); |
| 1833 | |
| 1834 | for (i = 0 ; i < bb->count ; i++) { |
| 1835 | u64 internal_bb = *p++; |
| 1836 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1837 | | BB_LEN(internal_bb)); |
| 1838 | *bbp++ = cpu_to_le64(store_bb); |
| 1839 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1840 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1841 | if (read_seqretry(&bb->lock, seq)) |
| 1842 | goto retry; |
| 1843 | |
| 1844 | bb->sector = (rdev->sb_start + |
| 1845 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1846 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1851 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | if (rdev2->desc_nr+1 > max_dev) |
| 1853 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1854 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1855 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1856 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1857 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1858 | rdev->sb_size = max_dev * 2 + 256; |
| 1859 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1860 | if (rdev->sb_size & bmask) |
| 1861 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1862 | } else |
| 1863 | max_dev = le32_to_cpu(sb->max_dev); |
| 1864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | for (i=0; i<max_dev;i++) |
| 1866 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1867 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1868 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1870 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1872 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1874 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1875 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | else |
| 1877 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1878 | } |
| 1879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1881 | } |
| 1882 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1883 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1884 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1885 | { |
| 1886 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1887 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1888 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1889 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1890 | if (rdev->data_offset != rdev->new_data_offset) |
| 1891 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1892 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1893 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1894 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1895 | max_sectors -= rdev->data_offset; |
| 1896 | if (!num_sectors || num_sectors > max_sectors) |
| 1897 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1898 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1899 | /* minor version 0 with bitmap we can't move */ |
| 1900 | return 0; |
| 1901 | } else { |
| 1902 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1903 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1904 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1905 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1906 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1907 | if (!num_sectors || num_sectors > max_sectors) |
| 1908 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1909 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1910 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1911 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1912 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1913 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1914 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1915 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1916 | rdev->sb_page); |
| 1917 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1918 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1919 | |
| 1920 | } |
| 1921 | |
| 1922 | static int |
| 1923 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1924 | unsigned long long new_offset) |
| 1925 | { |
| 1926 | /* All necessary checks on new >= old have been done */ |
| 1927 | struct bitmap *bitmap; |
| 1928 | if (new_offset >= rdev->data_offset) |
| 1929 | return 1; |
| 1930 | |
| 1931 | /* with 1.0 metadata, there is no metadata to tread on |
| 1932 | * so we can always move back */ |
| 1933 | if (rdev->mddev->minor_version == 0) |
| 1934 | return 1; |
| 1935 | |
| 1936 | /* otherwise we must be sure not to step on |
| 1937 | * any metadata, so stay: |
| 1938 | * 36K beyond start of superblock |
| 1939 | * beyond end of badblocks |
| 1940 | * beyond write-intent bitmap |
| 1941 | */ |
| 1942 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1943 | return 0; |
| 1944 | bitmap = rdev->mddev->bitmap; |
| 1945 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1946 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1947 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1948 | return 0; |
| 1949 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1950 | return 0; |
| 1951 | |
| 1952 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1955 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | [0] = { |
| 1957 | .name = "0.90.0", |
| 1958 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1959 | .load_super = super_90_load, |
| 1960 | .validate_super = super_90_validate, |
| 1961 | .sync_super = super_90_sync, |
| 1962 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1963 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | }, |
| 1965 | [1] = { |
| 1966 | .name = "md-1", |
| 1967 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1968 | .load_super = super_1_load, |
| 1969 | .validate_super = super_1_validate, |
| 1970 | .sync_super = super_1_sync, |
| 1971 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1972 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | }, |
| 1974 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1976 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1977 | { |
| 1978 | if (mddev->sync_super) { |
| 1979 | mddev->sync_super(mddev, rdev); |
| 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1984 | |
| 1985 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1986 | } |
| 1987 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1988 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1990 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1992 | rcu_read_lock(); |
| 1993 | rdev_for_each_rcu(rdev, mddev1) |
| 1994 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1995 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1996 | rdev2->bdev->bd_contains) { |
| 1997 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1998 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1999 | } |
| 2000 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | static LIST_HEAD(pending_raid_disks); |
| 2005 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2006 | /* |
| 2007 | * Try to register data integrity profile for an mddev |
| 2008 | * |
| 2009 | * This is called when an array is started and after a disk has been kicked |
| 2010 | * from the array. It only succeeds if all working and active component devices |
| 2011 | * are integrity capable with matching profiles. |
| 2012 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2013 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2014 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2015 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2016 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2017 | if (list_empty(&mddev->disks)) |
| 2018 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2019 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2020 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2021 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2022 | /* skip spares and non-functional disks */ |
| 2023 | if (test_bit(Faulty, &rdev->flags)) |
| 2024 | continue; |
| 2025 | if (rdev->raid_disk < 0) |
| 2026 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2027 | if (!reference) { |
| 2028 | /* Use the first rdev as the reference */ |
| 2029 | reference = rdev; |
| 2030 | continue; |
| 2031 | } |
| 2032 | /* does this rdev's profile match the reference profile? */ |
| 2033 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2034 | rdev->bdev->bd_disk) < 0) |
| 2035 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2036 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2037 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2038 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2039 | /* |
| 2040 | * All component devices are integrity capable and have matching |
| 2041 | * profiles, register the common profile for the md device. |
| 2042 | */ |
| 2043 | if (blk_integrity_register(mddev->gendisk, |
| 2044 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2045 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2046 | mdname(mddev)); |
| 2047 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2048 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2049 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2050 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2051 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2052 | mdname(mddev)); |
| 2053 | return -EINVAL; |
| 2054 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2055 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2056 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2057 | EXPORT_SYMBOL(md_integrity_register); |
| 2058 | |
| 2059 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2060 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2061 | { |
| 2062 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2063 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 2064 | |
| 2065 | if (!bi_mddev) /* nothing to do */ |
| 2066 | return; |
| 2067 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2068 | return; |
| 2069 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2070 | rdev->bdev->bd_disk) >= 0) |
| 2071 | return; |
| 2072 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2073 | blk_integrity_unregister(mddev->gendisk); |
| 2074 | } |
| 2075 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2076 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2077 | static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2079 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2080 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2081 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2082 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
| 2084 | if (rdev->mddev) { |
| 2085 | MD_BUG(); |
| 2086 | return -EINVAL; |
| 2087 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2088 | |
| 2089 | /* prevent duplicates */ |
| 2090 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2091 | return -EEXIST; |
| 2092 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2093 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2094 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2095 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2096 | if (mddev->pers) { |
| 2097 | /* Cannot change size, so fail |
| 2098 | * If mddev->level <= 0, then we don't care |
| 2099 | * about aligning sizes (e.g. linear) |
| 2100 | */ |
| 2101 | if (mddev->level > 0) |
| 2102 | return -ENOSPC; |
| 2103 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2104 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
| 2107 | /* Verify rdev->desc_nr is unique. |
| 2108 | * If it is -1, assign a free number, else |
| 2109 | * check number is not in use |
| 2110 | */ |
| 2111 | if (rdev->desc_nr < 0) { |
| 2112 | int choice = 0; |
| 2113 | if (mddev->pers) choice = mddev->raid_disks; |
| 2114 | while (find_rdev_nr(mddev, choice)) |
| 2115 | choice++; |
| 2116 | rdev->desc_nr = choice; |
| 2117 | } else { |
| 2118 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2119 | return -EBUSY; |
| 2120 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2121 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2122 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2123 | mdname(mddev), mddev->max_disks); |
| 2124 | return -EBUSY; |
| 2125 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2126 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2127 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2128 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2131 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2132 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2133 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2134 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2135 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2136 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2137 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2138 | /* failure here is OK */; |
| 2139 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2140 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2141 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2142 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2143 | |
| 2144 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2145 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2148 | |
| 2149 | fail: |
| 2150 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2151 | b, mdname(mddev)); |
| 2152 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2155 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2156 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2157 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2158 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2159 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2162 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | { |
| 2164 | char b[BDEVNAME_SIZE]; |
| 2165 | if (!rdev->mddev) { |
| 2166 | MD_BUG(); |
| 2167 | return; |
| 2168 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2169 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2170 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2172 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2173 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2174 | sysfs_put(rdev->sysfs_state); |
| 2175 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2176 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2177 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2178 | * writing to 'remove' to "dev/state". We also need |
| 2179 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2180 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2181 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2182 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2183 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2184 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * prevent the device from being mounted, repartitioned or |
| 2189 | * otherwise reused by a RAID array (or any other kernel |
| 2190 | * subsystem), by bd_claiming the device. |
| 2191 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2192 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
| 2194 | int err = 0; |
| 2195 | struct block_device *bdev; |
| 2196 | char b[BDEVNAME_SIZE]; |
| 2197 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2198 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2199 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | if (IS_ERR(bdev)) { |
| 2201 | printk(KERN_ERR "md: could not open %s.\n", |
| 2202 | __bdevname(dev, b)); |
| 2203 | return PTR_ERR(bdev); |
| 2204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | rdev->bdev = bdev; |
| 2206 | return err; |
| 2207 | } |
| 2208 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2209 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | { |
| 2211 | struct block_device *bdev = rdev->bdev; |
| 2212 | rdev->bdev = NULL; |
| 2213 | if (!bdev) |
| 2214 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2215 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | void md_autodetect_dev(dev_t dev); |
| 2219 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2220 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | { |
| 2222 | char b[BDEVNAME_SIZE]; |
| 2223 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2224 | bdevname(rdev->bdev,b)); |
| 2225 | if (rdev->mddev) |
| 2226 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2227 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2229 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2230 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | #endif |
| 2232 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2233 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2236 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { |
| 2238 | unbind_rdev_from_array(rdev); |
| 2239 | export_rdev(rdev); |
| 2240 | } |
| 2241 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2242 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2244 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2246 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | if (!rdev->mddev) { |
| 2248 | MD_BUG(); |
| 2249 | continue; |
| 2250 | } |
| 2251 | kick_rdev_from_array(rdev); |
| 2252 | } |
| 2253 | if (!list_empty(&mddev->disks)) |
| 2254 | MD_BUG(); |
| 2255 | mddev->raid_disks = 0; |
| 2256 | mddev->major_version = 0; |
| 2257 | } |
| 2258 | |
| 2259 | static void print_desc(mdp_disk_t *desc) |
| 2260 | { |
| 2261 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2262 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2263 | } |
| 2264 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2265 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | { |
| 2267 | int i; |
| 2268 | |
| 2269 | printk(KERN_INFO |
| 2270 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2271 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2272 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2273 | sb->ctime); |
| 2274 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2275 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2276 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2277 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2278 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2279 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2280 | sb->failed_disks, sb->spare_disks, |
| 2281 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2282 | |
| 2283 | printk(KERN_INFO); |
| 2284 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2285 | mdp_disk_t *desc; |
| 2286 | |
| 2287 | desc = sb->disks + i; |
| 2288 | if (desc->number || desc->major || desc->minor || |
| 2289 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2290 | printk(" D %2d: ", i); |
| 2291 | print_desc(desc); |
| 2292 | } |
| 2293 | } |
| 2294 | printk(KERN_INFO "md: THIS: "); |
| 2295 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2298 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2299 | { |
| 2300 | __u8 *uuid; |
| 2301 | |
| 2302 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2303 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2304 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2305 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2306 | le32_to_cpu(sb->major_version), |
| 2307 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2308 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2309 | sb->set_name, |
| 2310 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2311 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2312 | |
| 2313 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2314 | printk(KERN_INFO |
| 2315 | "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] | 2316 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2317 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2318 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2319 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2320 | le32_to_cpu(sb->level), |
| 2321 | (unsigned long long)le64_to_cpu(sb->size), |
| 2322 | le32_to_cpu(sb->raid_disks), |
| 2323 | le32_to_cpu(sb->layout), |
| 2324 | le32_to_cpu(sb->chunksize), |
| 2325 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2326 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2327 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2328 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2329 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2330 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2331 | sb->devflags, |
| 2332 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2333 | (unsigned long long)le64_to_cpu(sb->events), |
| 2334 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2335 | le32_to_cpu(sb->sb_csum), |
| 2336 | le32_to_cpu(sb->max_dev) |
| 2337 | ); |
| 2338 | } |
| 2339 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2340 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | { |
| 2342 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2343 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2344 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2345 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2346 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2348 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2349 | switch (major_version) { |
| 2350 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2351 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2352 | break; |
| 2353 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2354 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2355 | break; |
| 2356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | } else |
| 2358 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2359 | } |
| 2360 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2361 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2363 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2364 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2365 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | char b[BDEVNAME_SIZE]; |
| 2367 | |
| 2368 | printk("\n"); |
| 2369 | printk("md: **********************************\n"); |
| 2370 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2371 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2372 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2374 | if (mddev->bitmap) |
| 2375 | bitmap_print_sb(mddev->bitmap); |
| 2376 | else |
| 2377 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2378 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2380 | printk("\n"); |
| 2381 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2382 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2383 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | } |
| 2385 | printk("md: **********************************\n"); |
| 2386 | printk("\n"); |
| 2387 | } |
| 2388 | |
| 2389 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2390 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2392 | /* Update each superblock (in-memory image), but |
| 2393 | * if we are allowed to, skip spares which already |
| 2394 | * have the right event counter, or have one earlier |
| 2395 | * (which would mean they aren't being marked as dirty |
| 2396 | * with the rest of the array) |
| 2397 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2398 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2399 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2400 | if (rdev->sb_events == mddev->events || |
| 2401 | (nospares && |
| 2402 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2403 | rdev->sb_events+1 == mddev->events)) { |
| 2404 | /* Don't update this superblock */ |
| 2405 | rdev->sb_loaded = 2; |
| 2406 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2407 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2408 | rdev->sb_loaded = 1; |
| 2409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2413 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2415 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2416 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2417 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2418 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2421 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2422 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2423 | if (rdev->raid_disk >= 0 && |
| 2424 | mddev->delta_disks >= 0 && |
| 2425 | !test_bit(In_sync, &rdev->flags) && |
| 2426 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2427 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2428 | |
| 2429 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2430 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2431 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2432 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2433 | if (!mddev->external) { |
NeilBrown | d97a41dc | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2434 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2435 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2436 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2437 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2438 | md_ack_all_badblocks(&rdev->badblocks); |
| 2439 | md_error(mddev, rdev); |
| 2440 | } |
| 2441 | clear_bit(Blocked, &rdev->flags); |
| 2442 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2443 | wake_up(&rdev->blocked_wait); |
| 2444 | } |
| 2445 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2446 | wake_up(&mddev->sb_wait); |
| 2447 | return; |
| 2448 | } |
| 2449 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2450 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2451 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2452 | mddev->utime = get_seconds(); |
| 2453 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2454 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2455 | force_change = 1; |
| 2456 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2457 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2458 | * though if events isn't the right even/odd, we will have to do |
| 2459 | * spares after all |
| 2460 | */ |
| 2461 | nospares = 1; |
| 2462 | if (force_change) |
| 2463 | nospares = 0; |
| 2464 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2465 | /* If the array is degraded, then skipping spares is both |
| 2466 | * dangerous and fairly pointless. |
| 2467 | * Dangerous because a device that was removed from the array |
| 2468 | * might have a event_count that still looks up-to-date, |
| 2469 | * so it can be re-added without a resync. |
| 2470 | * Pointless because if there are any spares to skip, |
| 2471 | * then a recovery will happen and soon that array won't |
| 2472 | * be degraded any more and the spare can go back to sleep then. |
| 2473 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2474 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2475 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2476 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2477 | |
| 2478 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2479 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2480 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2481 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2482 | && mddev->can_decrease_events |
| 2483 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2484 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2485 | mddev->can_decrease_events = 0; |
| 2486 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2487 | /* otherwise we have to go forward and ... */ |
| 2488 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2489 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
| 2492 | if (!mddev->events) { |
| 2493 | /* |
| 2494 | * oops, this 64-bit counter should never wrap. |
| 2495 | * Either we are in around ~1 trillion A.C., assuming |
| 2496 | * 1 reboot per second, or we have a bug: |
| 2497 | */ |
| 2498 | MD_BUG(); |
| 2499 | mddev->events --; |
| 2500 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2501 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2502 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2503 | if (rdev->badblocks.changed) |
| 2504 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2505 | if (test_bit(Faulty, &rdev->flags)) |
| 2506 | set_bit(FaultRecorded, &rdev->flags); |
| 2507 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2508 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2509 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2510 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2512 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2513 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2515 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2516 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2518 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2519 | if (rdev->sb_loaded != 1) |
| 2520 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2522 | if (!test_bit(Faulty, &rdev->flags) && |
| 2523 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2524 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2525 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2526 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2527 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2528 | bdevname(rdev->bdev, b), |
| 2529 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2530 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2531 | if (rdev->badblocks.size) { |
| 2532 | md_super_write(mddev, rdev, |
| 2533 | rdev->badblocks.sector, |
| 2534 | rdev->badblocks.size << 9, |
| 2535 | rdev->bb_page); |
| 2536 | rdev->badblocks.size = 0; |
| 2537 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2538 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2539 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2540 | pr_debug("md: %s (skipping faulty)\n", |
| 2541 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2542 | else |
| 2543 | pr_debug("(skipping incremental s/r "); |
| 2544 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2545 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | /* only need to write one superblock... */ |
| 2547 | break; |
| 2548 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2549 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2550 | /* 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] | 2551 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2552 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2553 | if (mddev->in_sync != sync_req || |
| 2554 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2555 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2556 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2557 | goto repeat; |
| 2558 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2559 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2560 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2561 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2562 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2563 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2564 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2565 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2566 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2567 | clear_bit(Blocked, &rdev->flags); |
| 2568 | |
| 2569 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2570 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2571 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2572 | wake_up(&rdev->blocked_wait); |
| 2573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2576 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2577 | * We want to accept with case. For this we use cmd_match. |
| 2578 | */ |
| 2579 | static int cmd_match(const char *cmd, const char *str) |
| 2580 | { |
| 2581 | /* See if cmd, written into a sysfs file, matches |
| 2582 | * str. They must either be the same, or cmd can |
| 2583 | * have a trailing newline |
| 2584 | */ |
| 2585 | while (*cmd && *str && *cmd == *str) { |
| 2586 | cmd++; |
| 2587 | str++; |
| 2588 | } |
| 2589 | if (*cmd == '\n') |
| 2590 | cmd++; |
| 2591 | if (*str || *cmd) |
| 2592 | return 0; |
| 2593 | return 1; |
| 2594 | } |
| 2595 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2596 | struct rdev_sysfs_entry { |
| 2597 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2598 | ssize_t (*show)(struct md_rdev *, char *); |
| 2599 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2600 | }; |
| 2601 | |
| 2602 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2603 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2604 | { |
| 2605 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2606 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2607 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2608 | if (test_bit(Faulty, &rdev->flags) || |
| 2609 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2610 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2611 | sep = ","; |
| 2612 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2613 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2614 | len += sprintf(page+len, "%sin_sync",sep); |
| 2615 | sep = ","; |
| 2616 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2617 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2618 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2619 | sep = ","; |
| 2620 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2621 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2622 | (rdev->badblocks.unacked_exist |
| 2623 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2624 | len += sprintf(page+len, "%sblocked", sep); |
| 2625 | sep = ","; |
| 2626 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2627 | if (!test_bit(Faulty, &rdev->flags) && |
| 2628 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2629 | len += sprintf(page+len, "%sspare", sep); |
| 2630 | sep = ","; |
| 2631 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2632 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2633 | len += sprintf(page+len, "%swrite_error", sep); |
| 2634 | sep = ","; |
| 2635 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2636 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2637 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2638 | sep = ","; |
| 2639 | } |
| 2640 | if (test_bit(Replacement, &rdev->flags)) { |
| 2641 | len += sprintf(page+len, "%sreplacement", sep); |
| 2642 | sep = ","; |
| 2643 | } |
| 2644 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2645 | return len+sprintf(page+len, "\n"); |
| 2646 | } |
| 2647 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2648 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2649 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2650 | { |
| 2651 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2652 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2653 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2654 | * writemostly - sets write_mostly |
| 2655 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2656 | * blocked - sets the Blocked flags |
| 2657 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2658 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2659 | * write_error - sets WriteErrorSeen |
| 2660 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2661 | */ |
| 2662 | int err = -EINVAL; |
| 2663 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2664 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2665 | if (test_bit(Faulty, &rdev->flags)) |
| 2666 | err = 0; |
| 2667 | else |
| 2668 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2669 | } else if (cmd_match(buf, "remove")) { |
| 2670 | if (rdev->raid_disk >= 0) |
| 2671 | err = -EBUSY; |
| 2672 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2673 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2674 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2675 | if (mddev->pers) |
| 2676 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2677 | md_new_event(mddev); |
| 2678 | err = 0; |
| 2679 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2680 | } else if (cmd_match(buf, "writemostly")) { |
| 2681 | set_bit(WriteMostly, &rdev->flags); |
| 2682 | err = 0; |
| 2683 | } else if (cmd_match(buf, "-writemostly")) { |
| 2684 | clear_bit(WriteMostly, &rdev->flags); |
| 2685 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2686 | } else if (cmd_match(buf, "blocked")) { |
| 2687 | set_bit(Blocked, &rdev->flags); |
| 2688 | err = 0; |
| 2689 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2690 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2691 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2692 | /* metadata handler doesn't understand badblocks, |
| 2693 | * so we need to fail the device |
| 2694 | */ |
| 2695 | md_error(rdev->mddev, rdev); |
| 2696 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2697 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2698 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2699 | wake_up(&rdev->blocked_wait); |
| 2700 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2701 | md_wakeup_thread(rdev->mddev->thread); |
| 2702 | |
| 2703 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2704 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2705 | set_bit(In_sync, &rdev->flags); |
| 2706 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2707 | } else if (cmd_match(buf, "write_error")) { |
| 2708 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2709 | err = 0; |
| 2710 | } else if (cmd_match(buf, "-write_error")) { |
| 2711 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2712 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2713 | } else if (cmd_match(buf, "want_replacement")) { |
| 2714 | /* Any non-spare device that is not a replacement can |
| 2715 | * become want_replacement at any time, but we then need to |
| 2716 | * check if recovery is needed. |
| 2717 | */ |
| 2718 | if (rdev->raid_disk >= 0 && |
| 2719 | !test_bit(Replacement, &rdev->flags)) |
| 2720 | set_bit(WantReplacement, &rdev->flags); |
| 2721 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2722 | md_wakeup_thread(rdev->mddev->thread); |
| 2723 | err = 0; |
| 2724 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2725 | /* Clearing 'want_replacement' is always allowed. |
| 2726 | * Once replacements starts it is too late though. |
| 2727 | */ |
| 2728 | err = 0; |
| 2729 | clear_bit(WantReplacement, &rdev->flags); |
| 2730 | } else if (cmd_match(buf, "replacement")) { |
| 2731 | /* Can only set a device as a replacement when array has not |
| 2732 | * yet been started. Once running, replacement is automatic |
| 2733 | * from spares, or by assigning 'slot'. |
| 2734 | */ |
| 2735 | if (rdev->mddev->pers) |
| 2736 | err = -EBUSY; |
| 2737 | else { |
| 2738 | set_bit(Replacement, &rdev->flags); |
| 2739 | err = 0; |
| 2740 | } |
| 2741 | } else if (cmd_match(buf, "-replacement")) { |
| 2742 | /* Similarly, can only clear Replacement before start */ |
| 2743 | if (rdev->mddev->pers) |
| 2744 | err = -EBUSY; |
| 2745 | else { |
| 2746 | clear_bit(Replacement, &rdev->flags); |
| 2747 | err = 0; |
| 2748 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2749 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2750 | if (!err) |
| 2751 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2752 | return err ? err : len; |
| 2753 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2754 | static struct rdev_sysfs_entry rdev_state = |
| 2755 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2756 | |
| 2757 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2758 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2759 | { |
| 2760 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2761 | } |
| 2762 | |
| 2763 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2764 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2765 | { |
| 2766 | char *e; |
| 2767 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2768 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2769 | atomic_set(&rdev->corrected_errors, n); |
| 2770 | return len; |
| 2771 | } |
| 2772 | return -EINVAL; |
| 2773 | } |
| 2774 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2775 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2776 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2777 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2778 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2779 | { |
| 2780 | if (rdev->raid_disk < 0) |
| 2781 | return sprintf(page, "none\n"); |
| 2782 | else |
| 2783 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2784 | } |
| 2785 | |
| 2786 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2787 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2788 | { |
| 2789 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2790 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2791 | int slot = simple_strtoul(buf, &e, 10); |
| 2792 | if (strncmp(buf, "none", 4)==0) |
| 2793 | slot = -1; |
| 2794 | else if (e==buf || (*e && *e!= '\n')) |
| 2795 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2796 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2797 | /* Setting 'slot' on an active array requires also |
| 2798 | * updating the 'rd%d' link, and communicating |
| 2799 | * with the personality with ->hot_*_disk. |
| 2800 | * For now we only support removing |
| 2801 | * failed/spare devices. This normally happens automatically, |
| 2802 | * but not when the metadata is externally managed. |
| 2803 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2804 | if (rdev->raid_disk == -1) |
| 2805 | return -EEXIST; |
| 2806 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2807 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2808 | return -EINVAL; |
| 2809 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2810 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2811 | if (err) |
| 2812 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2813 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2814 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2815 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2816 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2817 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2818 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2819 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2820 | */ |
| 2821 | |
| 2822 | if (rdev->raid_disk != -1) |
| 2823 | return -EBUSY; |
| 2824 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2825 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2826 | return -EBUSY; |
| 2827 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2828 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2829 | return -EINVAL; |
| 2830 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2831 | if (slot >= rdev->mddev->raid_disks && |
| 2832 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2833 | return -ENOSPC; |
| 2834 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2835 | rdev->raid_disk = slot; |
| 2836 | if (test_bit(In_sync, &rdev->flags)) |
| 2837 | rdev->saved_raid_disk = slot; |
| 2838 | else |
| 2839 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2840 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2841 | err = rdev->mddev->pers-> |
| 2842 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2843 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2844 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2845 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2846 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2847 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2848 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2849 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2850 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2851 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2852 | if (slot >= rdev->mddev->raid_disks && |
| 2853 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2854 | return -ENOSPC; |
| 2855 | rdev->raid_disk = slot; |
| 2856 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2857 | clear_bit(Faulty, &rdev->flags); |
| 2858 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2859 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2860 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2861 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2862 | return len; |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2867 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2868 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2869 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2870 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2871 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2872 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2876 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2877 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2878 | unsigned long long offset; |
| 2879 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2880 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2881 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2882 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2883 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2884 | /* Must set offset before size, so overlap checks |
| 2885 | * can be sane */ |
| 2886 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2887 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2888 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2889 | return len; |
| 2890 | } |
| 2891 | |
| 2892 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2893 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2894 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2895 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2896 | { |
| 2897 | return sprintf(page, "%llu\n", |
| 2898 | (unsigned long long)rdev->new_data_offset); |
| 2899 | } |
| 2900 | |
| 2901 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2902 | const char *buf, size_t len) |
| 2903 | { |
| 2904 | unsigned long long new_offset; |
| 2905 | struct mddev *mddev = rdev->mddev; |
| 2906 | |
| 2907 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2908 | return -EINVAL; |
| 2909 | |
| 2910 | if (mddev->sync_thread) |
| 2911 | return -EBUSY; |
| 2912 | if (new_offset == rdev->data_offset) |
| 2913 | /* reset is always permitted */ |
| 2914 | ; |
| 2915 | else if (new_offset > rdev->data_offset) { |
| 2916 | /* must not push array size beyond rdev_sectors */ |
| 2917 | if (new_offset - rdev->data_offset |
| 2918 | + mddev->dev_sectors > rdev->sectors) |
| 2919 | return -E2BIG; |
| 2920 | } |
| 2921 | /* Metadata worries about other space details. */ |
| 2922 | |
| 2923 | /* decreasing the offset is inconsistent with a backwards |
| 2924 | * reshape. |
| 2925 | */ |
| 2926 | if (new_offset < rdev->data_offset && |
| 2927 | mddev->reshape_backwards) |
| 2928 | return -EINVAL; |
| 2929 | /* Increasing offset is inconsistent with forwards |
| 2930 | * reshape. reshape_direction should be set to |
| 2931 | * 'backwards' first. |
| 2932 | */ |
| 2933 | if (new_offset > rdev->data_offset && |
| 2934 | !mddev->reshape_backwards) |
| 2935 | return -EINVAL; |
| 2936 | |
| 2937 | if (mddev->pers && mddev->persistent && |
| 2938 | !super_types[mddev->major_version] |
| 2939 | .allow_new_offset(rdev, new_offset)) |
| 2940 | return -E2BIG; |
| 2941 | rdev->new_data_offset = new_offset; |
| 2942 | if (new_offset > rdev->data_offset) |
| 2943 | mddev->reshape_backwards = 1; |
| 2944 | else if (new_offset < rdev->data_offset) |
| 2945 | mddev->reshape_backwards = 0; |
| 2946 | |
| 2947 | return len; |
| 2948 | } |
| 2949 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2950 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2951 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2952 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2953 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2954 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2955 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2958 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2959 | { |
| 2960 | /* check if two start/length pairs overlap */ |
| 2961 | if (s1+l1 <= s2) |
| 2962 | return 0; |
| 2963 | if (s2+l2 <= s1) |
| 2964 | return 0; |
| 2965 | return 1; |
| 2966 | } |
| 2967 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2968 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2969 | { |
| 2970 | unsigned long long blocks; |
| 2971 | sector_t new; |
| 2972 | |
| 2973 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2974 | return -EINVAL; |
| 2975 | |
| 2976 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2977 | return -EINVAL; /* sector conversion overflow */ |
| 2978 | |
| 2979 | new = blocks * 2; |
| 2980 | if (new != blocks * 2) |
| 2981 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2982 | |
| 2983 | *sectors = new; |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2987 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2988 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2989 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2990 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2991 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2992 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2993 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2994 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2995 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2996 | if (rdev->data_offset != rdev->new_data_offset) |
| 2997 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2998 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2999 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3000 | sectors = super_types[my_mddev->major_version]. |
| 3001 | rdev_size_change(rdev, sectors); |
| 3002 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3003 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3004 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3005 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3006 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3007 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3008 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3009 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3010 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3011 | rdev->sectors = sectors; |
| 3012 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3013 | /* need to check that all other rdevs with the same ->bdev |
| 3014 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3015 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3016 | * we have to change it back, we will have the lock again. |
| 3017 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3018 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3019 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3020 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3021 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3022 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3023 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3024 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3025 | |
| 3026 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3027 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3028 | if (rdev->bdev == rdev2->bdev && |
| 3029 | rdev != rdev2 && |
| 3030 | overlaps(rdev->data_offset, rdev->sectors, |
| 3031 | rdev2->data_offset, |
| 3032 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3033 | overlap = 1; |
| 3034 | break; |
| 3035 | } |
| 3036 | mddev_unlock(mddev); |
| 3037 | if (overlap) { |
| 3038 | mddev_put(mddev); |
| 3039 | break; |
| 3040 | } |
| 3041 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3042 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3043 | if (overlap) { |
| 3044 | /* Someone else could have slipped in a size |
| 3045 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3046 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3047 | * safe, and trust userspace not to race with |
| 3048 | * itself |
| 3049 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3050 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3051 | return -EBUSY; |
| 3052 | } |
| 3053 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3054 | return len; |
| 3055 | } |
| 3056 | |
| 3057 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3058 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3059 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3060 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3061 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3062 | { |
| 3063 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3064 | |
| 3065 | if (test_bit(In_sync, &rdev->flags) || |
| 3066 | recovery_start == MaxSector) |
| 3067 | return sprintf(page, "none\n"); |
| 3068 | |
| 3069 | return sprintf(page, "%llu\n", recovery_start); |
| 3070 | } |
| 3071 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3072 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3073 | { |
| 3074 | unsigned long long recovery_start; |
| 3075 | |
| 3076 | if (cmd_match(buf, "none")) |
| 3077 | recovery_start = MaxSector; |
| 3078 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3079 | return -EINVAL; |
| 3080 | |
| 3081 | if (rdev->mddev->pers && |
| 3082 | rdev->raid_disk >= 0) |
| 3083 | return -EBUSY; |
| 3084 | |
| 3085 | rdev->recovery_offset = recovery_start; |
| 3086 | if (recovery_start == MaxSector) |
| 3087 | set_bit(In_sync, &rdev->flags); |
| 3088 | else |
| 3089 | clear_bit(In_sync, &rdev->flags); |
| 3090 | return len; |
| 3091 | } |
| 3092 | |
| 3093 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3094 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3095 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3096 | |
| 3097 | static ssize_t |
| 3098 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3099 | static ssize_t |
| 3100 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3101 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3102 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3103 | { |
| 3104 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3105 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3106 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3107 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3108 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3109 | /* Maybe that ack was all we needed */ |
| 3110 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3111 | wake_up(&rdev->blocked_wait); |
| 3112 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3113 | } |
| 3114 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3115 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3116 | |
| 3117 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3118 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3119 | { |
| 3120 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3121 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3122 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3123 | { |
| 3124 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3125 | } |
| 3126 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3127 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3128 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3129 | static struct attribute *rdev_default_attrs[] = { |
| 3130 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3131 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3132 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3133 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3134 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3135 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3136 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3137 | &rdev_bad_blocks.attr, |
| 3138 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3139 | NULL, |
| 3140 | }; |
| 3141 | static ssize_t |
| 3142 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3143 | { |
| 3144 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3145 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3146 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3147 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3148 | |
| 3149 | if (!entry->show) |
| 3150 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3151 | |
| 3152 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3153 | if (!rv) { |
| 3154 | if (rdev->mddev == NULL) |
| 3155 | rv = -EBUSY; |
| 3156 | else |
| 3157 | rv = entry->show(rdev, page); |
| 3158 | mddev_unlock(mddev); |
| 3159 | } |
| 3160 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | static ssize_t |
| 3164 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3165 | const char *page, size_t length) |
| 3166 | { |
| 3167 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3168 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3169 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3170 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3171 | |
| 3172 | if (!entry->store) |
| 3173 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3174 | if (!capable(CAP_SYS_ADMIN)) |
| 3175 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3176 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3177 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3178 | if (rdev->mddev == NULL) |
| 3179 | rv = -EBUSY; |
| 3180 | else |
| 3181 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3182 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3183 | } |
| 3184 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | static void rdev_free(struct kobject *ko) |
| 3188 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3189 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3190 | kfree(rdev); |
| 3191 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3192 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3193 | .show = rdev_attr_show, |
| 3194 | .store = rdev_attr_store, |
| 3195 | }; |
| 3196 | static struct kobj_type rdev_ktype = { |
| 3197 | .release = rdev_free, |
| 3198 | .sysfs_ops = &rdev_sysfs_ops, |
| 3199 | .default_attrs = rdev_default_attrs, |
| 3200 | }; |
| 3201 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3202 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3203 | { |
| 3204 | rdev->desc_nr = -1; |
| 3205 | rdev->saved_raid_disk = -1; |
| 3206 | rdev->raid_disk = -1; |
| 3207 | rdev->flags = 0; |
| 3208 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3209 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3210 | rdev->sb_events = 0; |
| 3211 | rdev->last_read_error.tv_sec = 0; |
| 3212 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3213 | rdev->sb_loaded = 0; |
| 3214 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3215 | atomic_set(&rdev->nr_pending, 0); |
| 3216 | atomic_set(&rdev->read_errors, 0); |
| 3217 | atomic_set(&rdev->corrected_errors, 0); |
| 3218 | |
| 3219 | INIT_LIST_HEAD(&rdev->same_set); |
| 3220 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3221 | |
| 3222 | /* Add space to store bad block list. |
| 3223 | * This reserves the space even on arrays where it cannot |
| 3224 | * be used - I wonder if that matters |
| 3225 | */ |
| 3226 | rdev->badblocks.count = 0; |
| 3227 | rdev->badblocks.shift = 0; |
| 3228 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3229 | seqlock_init(&rdev->badblocks.lock); |
| 3230 | if (rdev->badblocks.page == NULL) |
| 3231 | return -ENOMEM; |
| 3232 | |
| 3233 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3234 | } |
| 3235 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | /* |
| 3237 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3238 | * |
| 3239 | * mark the device faulty if: |
| 3240 | * |
| 3241 | * - the device is nonexistent (zero size) |
| 3242 | * - the device has no valid superblock |
| 3243 | * |
| 3244 | * a faulty rdev _never_ has rdev->sb set. |
| 3245 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3246 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | { |
| 3248 | char b[BDEVNAME_SIZE]; |
| 3249 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3250 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | sector_t size; |
| 3252 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3253 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | if (!rdev) { |
| 3255 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3256 | return ERR_PTR(-ENOMEM); |
| 3257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3259 | err = md_rdev_init(rdev); |
| 3260 | if (err) |
| 3261 | goto abort_free; |
| 3262 | err = alloc_disk_sb(rdev); |
| 3263 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | goto abort_free; |
| 3265 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3266 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | if (err) |
| 3268 | goto abort_free; |
| 3269 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3270 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3271 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3272 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | if (!size) { |
| 3274 | printk(KERN_WARNING |
| 3275 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3276 | bdevname(rdev->bdev,b)); |
| 3277 | err = -EINVAL; |
| 3278 | goto abort_free; |
| 3279 | } |
| 3280 | |
| 3281 | if (super_format >= 0) { |
| 3282 | err = super_types[super_format]. |
| 3283 | load_super(rdev, NULL, super_minor); |
| 3284 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3285 | printk(KERN_WARNING |
| 3286 | "md: %s does not have a valid v%d.%d " |
| 3287 | "superblock, not importing!\n", |
| 3288 | bdevname(rdev->bdev,b), |
| 3289 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | goto abort_free; |
| 3291 | } |
| 3292 | if (err < 0) { |
| 3293 | printk(KERN_WARNING |
| 3294 | "md: could not read %s's sb, not importing!\n", |
| 3295 | bdevname(rdev->bdev,b)); |
| 3296 | goto abort_free; |
| 3297 | } |
| 3298 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3299 | if (super_format == -1) |
| 3300 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3301 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | return rdev; |
| 3304 | |
| 3305 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3306 | if (rdev->bdev) |
| 3307 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3308 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | kfree(rdev); |
| 3310 | return ERR_PTR(err); |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * Check a full RAID array for plausibility |
| 3315 | */ |
| 3316 | |
| 3317 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3318 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | { |
| 3320 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3321 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | char b[BDEVNAME_SIZE]; |
| 3323 | |
| 3324 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3325 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | switch (super_types[mddev->major_version]. |
| 3327 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3328 | case 1: |
| 3329 | freshest = rdev; |
| 3330 | break; |
| 3331 | case 0: |
| 3332 | break; |
| 3333 | default: |
| 3334 | printk( KERN_ERR \ |
| 3335 | "md: fatal superblock inconsistency in %s" |
| 3336 | " -- removing from array\n", |
| 3337 | bdevname(rdev->bdev,b)); |
| 3338 | kick_rdev_from_array(rdev); |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | super_types[mddev->major_version]. |
| 3343 | validate_super(mddev, freshest); |
| 3344 | |
| 3345 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3346 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3347 | if (mddev->max_disks && |
| 3348 | (rdev->desc_nr >= mddev->max_disks || |
| 3349 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3350 | printk(KERN_WARNING |
| 3351 | "md: %s: %s: only %d devices permitted\n", |
| 3352 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3353 | mddev->max_disks); |
| 3354 | kick_rdev_from_array(rdev); |
| 3355 | continue; |
| 3356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | if (rdev != freshest) |
| 3358 | if (super_types[mddev->major_version]. |
| 3359 | validate_super(mddev, rdev)) { |
| 3360 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3361 | " from array!\n", |
| 3362 | bdevname(rdev->bdev,b)); |
| 3363 | kick_rdev_from_array(rdev); |
| 3364 | continue; |
| 3365 | } |
| 3366 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3367 | rdev->desc_nr = i++; |
| 3368 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3369 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3370 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3371 | rdev->raid_disk = -1; |
| 3372 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
| 3374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3377 | /* Read a fixed-point number. |
| 3378 | * Numbers in sysfs attributes should be in "standard" units where |
| 3379 | * possible, so time should be in seconds. |
| 3380 | * However we internally use a a much smaller unit such as |
| 3381 | * milliseconds or jiffies. |
| 3382 | * This function takes a decimal number with a possible fractional |
| 3383 | * component, and produces an integer which is the result of |
| 3384 | * multiplying that number by 10^'scale'. |
| 3385 | * all without any floating-point arithmetic. |
| 3386 | */ |
| 3387 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3388 | { |
| 3389 | unsigned long result = 0; |
| 3390 | long decimals = -1; |
| 3391 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3392 | if (*cp == '.') |
| 3393 | decimals = 0; |
| 3394 | else if (decimals < scale) { |
| 3395 | unsigned int value; |
| 3396 | value = *cp - '0'; |
| 3397 | result = result * 10 + value; |
| 3398 | if (decimals >= 0) |
| 3399 | decimals++; |
| 3400 | } |
| 3401 | cp++; |
| 3402 | } |
| 3403 | if (*cp == '\n') |
| 3404 | cp++; |
| 3405 | if (*cp) |
| 3406 | return -EINVAL; |
| 3407 | if (decimals < 0) |
| 3408 | decimals = 0; |
| 3409 | while (decimals < scale) { |
| 3410 | result *= 10; |
| 3411 | decimals ++; |
| 3412 | } |
| 3413 | *res = result; |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3418 | static void md_safemode_timeout(unsigned long data); |
| 3419 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3421 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3422 | { |
| 3423 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3424 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3425 | } |
| 3426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3427 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3428 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3429 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3430 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3431 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3432 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3433 | if (msec == 0) |
| 3434 | mddev->safemode_delay = 0; |
| 3435 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3436 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3437 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3438 | if (mddev->safemode_delay == 0) |
| 3439 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3440 | if (mddev->safemode_delay < old_delay) |
| 3441 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3442 | } |
| 3443 | return len; |
| 3444 | } |
| 3445 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3446 | __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] | 3447 | |
| 3448 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3449 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3450 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3451 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3452 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3453 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3454 | else if (mddev->clevel[0]) |
| 3455 | return sprintf(page, "%s\n", mddev->clevel); |
| 3456 | else if (mddev->level != LEVEL_NONE) |
| 3457 | return sprintf(page, "%d\n", mddev->level); |
| 3458 | else |
| 3459 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3462 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3463 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3464 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3465 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3466 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3467 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3468 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3469 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3470 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3471 | |
| 3472 | if (mddev->pers == NULL) { |
| 3473 | if (len == 0) |
| 3474 | return 0; |
| 3475 | if (len >= sizeof(mddev->clevel)) |
| 3476 | return -ENOSPC; |
| 3477 | strncpy(mddev->clevel, buf, len); |
| 3478 | if (mddev->clevel[len-1] == '\n') |
| 3479 | len--; |
| 3480 | mddev->clevel[len] = 0; |
| 3481 | mddev->level = LEVEL_NONE; |
| 3482 | return rv; |
| 3483 | } |
| 3484 | |
| 3485 | /* request to change the personality. Need to ensure: |
| 3486 | * - array is not engaged in resync/recovery/reshape |
| 3487 | * - old personality can be suspended |
| 3488 | * - new personality will access other array. |
| 3489 | */ |
| 3490 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3491 | if (mddev->sync_thread || |
| 3492 | mddev->reshape_position != MaxSector || |
| 3493 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3494 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3495 | |
| 3496 | if (!mddev->pers->quiesce) { |
| 3497 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3498 | mdname(mddev), mddev->pers->name); |
| 3499 | return -EINVAL; |
| 3500 | } |
| 3501 | |
| 3502 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3503 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3504 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3505 | strncpy(clevel, buf, len); |
| 3506 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3507 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3508 | clevel[len] = 0; |
| 3509 | if (strict_strtol(clevel, 10, &level)) |
| 3510 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3511 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3512 | if (request_module("md-%s", clevel) != 0) |
| 3513 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3514 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3515 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3516 | if (!pers || !try_module_get(pers->owner)) { |
| 3517 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3518 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3519 | return -EINVAL; |
| 3520 | } |
| 3521 | spin_unlock(&pers_lock); |
| 3522 | |
| 3523 | if (pers == mddev->pers) { |
| 3524 | /* Nothing to do! */ |
| 3525 | module_put(pers->owner); |
| 3526 | return rv; |
| 3527 | } |
| 3528 | if (!pers->takeover) { |
| 3529 | module_put(pers->owner); |
| 3530 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3531 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3532 | return -EINVAL; |
| 3533 | } |
| 3534 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3535 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3536 | rdev->new_raid_disk = rdev->raid_disk; |
| 3537 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3538 | /* ->takeover must set new_* and/or delta_disks |
| 3539 | * if it succeeds, and may set them when it fails. |
| 3540 | */ |
| 3541 | priv = pers->takeover(mddev); |
| 3542 | if (IS_ERR(priv)) { |
| 3543 | mddev->new_level = mddev->level; |
| 3544 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3545 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3546 | mddev->raid_disks -= mddev->delta_disks; |
| 3547 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3548 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3549 | module_put(pers->owner); |
| 3550 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3551 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3552 | return PTR_ERR(priv); |
| 3553 | } |
| 3554 | |
| 3555 | /* Looks like we have a winner */ |
| 3556 | mddev_suspend(mddev); |
| 3557 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3558 | |
| 3559 | if (mddev->pers->sync_request == NULL && |
| 3560 | pers->sync_request != NULL) { |
| 3561 | /* need to add the md_redundancy_group */ |
| 3562 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3563 | printk(KERN_WARNING |
| 3564 | "md: cannot register extra attributes for %s\n", |
| 3565 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3566 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3567 | } |
| 3568 | if (mddev->pers->sync_request != NULL && |
| 3569 | pers->sync_request == NULL) { |
| 3570 | /* need to remove the md_redundancy_group */ |
| 3571 | if (mddev->to_remove == NULL) |
| 3572 | mddev->to_remove = &md_redundancy_group; |
| 3573 | } |
| 3574 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3575 | if (mddev->pers->sync_request == NULL && |
| 3576 | mddev->external) { |
| 3577 | /* We are converting from a no-redundancy array |
| 3578 | * to a redundancy array and metadata is managed |
| 3579 | * externally so we need to be sure that writes |
| 3580 | * won't block due to a need to transition |
| 3581 | * clean->dirty |
| 3582 | * until external management is started. |
| 3583 | */ |
| 3584 | mddev->in_sync = 0; |
| 3585 | mddev->safemode_delay = 0; |
| 3586 | mddev->safemode = 0; |
| 3587 | } |
| 3588 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3589 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3590 | if (rdev->raid_disk < 0) |
| 3591 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3592 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3593 | rdev->new_raid_disk = -1; |
| 3594 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3595 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3596 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3597 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3598 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3599 | if (rdev->raid_disk < 0) |
| 3600 | continue; |
| 3601 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3602 | continue; |
| 3603 | rdev->raid_disk = rdev->new_raid_disk; |
| 3604 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3605 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3606 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3607 | if (sysfs_link_rdev(mddev, rdev)) |
| 3608 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3609 | " for %s after level change\n", |
| 3610 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3611 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3615 | mddev->pers = pers; |
| 3616 | mddev->private = priv; |
| 3617 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3618 | mddev->level = mddev->new_level; |
| 3619 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3620 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3621 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3622 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3623 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3624 | if (mddev->pers->sync_request == NULL) { |
| 3625 | /* this is now an array without redundancy, so |
| 3626 | * it must always be in_sync |
| 3627 | */ |
| 3628 | mddev->in_sync = 1; |
| 3629 | del_timer_sync(&mddev->safemode_timer); |
| 3630 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3631 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3632 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3633 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3634 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3635 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3636 | return rv; |
| 3637 | } |
| 3638 | |
| 3639 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3640 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3641 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3642 | |
| 3643 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3644 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3645 | { |
| 3646 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3647 | if (mddev->reshape_position != MaxSector && |
| 3648 | mddev->layout != mddev->new_layout) |
| 3649 | return sprintf(page, "%d (%d)\n", |
| 3650 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3651 | return sprintf(page, "%d\n", mddev->layout); |
| 3652 | } |
| 3653 | |
| 3654 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3655 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3656 | { |
| 3657 | char *e; |
| 3658 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3659 | |
| 3660 | if (!*buf || (*e && *e != '\n')) |
| 3661 | return -EINVAL; |
| 3662 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3663 | if (mddev->pers) { |
| 3664 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3665 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3666 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3667 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3668 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3669 | if (err) { |
| 3670 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3671 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3672 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3673 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3674 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3675 | if (mddev->reshape_position == MaxSector) |
| 3676 | mddev->layout = n; |
| 3677 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3678 | return len; |
| 3679 | } |
| 3680 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3681 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3682 | |
| 3683 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3684 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3685 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3686 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3687 | if (mddev->raid_disks == 0) |
| 3688 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3689 | if (mddev->reshape_position != MaxSector && |
| 3690 | mddev->delta_disks != 0) |
| 3691 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3692 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3693 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3694 | } |
| 3695 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3696 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3697 | |
| 3698 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3699 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3700 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3701 | char *e; |
| 3702 | int rv = 0; |
| 3703 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3704 | |
| 3705 | if (!*buf || (*e && *e != '\n')) |
| 3706 | return -EINVAL; |
| 3707 | |
| 3708 | if (mddev->pers) |
| 3709 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3710 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3711 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3712 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3713 | |
| 3714 | rdev_for_each(rdev, mddev) { |
| 3715 | if (olddisks < n && |
| 3716 | rdev->data_offset < rdev->new_data_offset) |
| 3717 | return -EINVAL; |
| 3718 | if (olddisks > n && |
| 3719 | rdev->data_offset > rdev->new_data_offset) |
| 3720 | return -EINVAL; |
| 3721 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3722 | mddev->delta_disks = n - olddisks; |
| 3723 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3724 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3725 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3726 | mddev->raid_disks = n; |
| 3727 | return rv ? rv : len; |
| 3728 | } |
| 3729 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3730 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3731 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3732 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3733 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3734 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3735 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3736 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3737 | return sprintf(page, "%d (%d)\n", |
| 3738 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3739 | mddev->chunk_sectors << 9); |
| 3740 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3745 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3746 | char *e; |
| 3747 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3748 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3749 | if (!*buf || (*e && *e != '\n')) |
| 3750 | return -EINVAL; |
| 3751 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3752 | if (mddev->pers) { |
| 3753 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3754 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3755 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3756 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3757 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3758 | if (err) { |
| 3759 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3760 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3761 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3762 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3763 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3764 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3765 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3766 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | return len; |
| 3768 | } |
| 3769 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3770 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3771 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3772 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3773 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3774 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3775 | if (mddev->recovery_cp == MaxSector) |
| 3776 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3777 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3778 | } |
| 3779 | |
| 3780 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3781 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3782 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3783 | char *e; |
| 3784 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3785 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3786 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3787 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3788 | if (cmd_match(buf, "none")) |
| 3789 | n = MaxSector; |
| 3790 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3791 | return -EINVAL; |
| 3792 | |
| 3793 | mddev->recovery_cp = n; |
| 3794 | return len; |
| 3795 | } |
| 3796 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3797 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3798 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3799 | /* |
| 3800 | * The array state can be: |
| 3801 | * |
| 3802 | * clear |
| 3803 | * No devices, no size, no level |
| 3804 | * Equivalent to STOP_ARRAY ioctl |
| 3805 | * inactive |
| 3806 | * May have some settings, but array is not active |
| 3807 | * all IO results in error |
| 3808 | * When written, doesn't tear down array, but just stops it |
| 3809 | * suspended (not supported yet) |
| 3810 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3811 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3812 | * readonly |
| 3813 | * no resync can happen. no superblocks get written. |
| 3814 | * write requests fail |
| 3815 | * read-auto |
| 3816 | * like readonly, but behaves like 'clean' on a write request. |
| 3817 | * |
| 3818 | * clean - no pending writes, but otherwise active. |
| 3819 | * When written to inactive array, starts without resync |
| 3820 | * If a write request arrives then |
| 3821 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3822 | * if not known, block and switch to write-pending |
| 3823 | * If written to an active array that has pending writes, then fails. |
| 3824 | * active |
| 3825 | * fully active: IO and resync can be happening. |
| 3826 | * When written to inactive array, starts with resync |
| 3827 | * |
| 3828 | * write-pending |
| 3829 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3830 | * |
| 3831 | * active-idle |
| 3832 | * like active, but no writes have been seen for a while (100msec). |
| 3833 | * |
| 3834 | */ |
| 3835 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3836 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3837 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3838 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3839 | "write-pending", "active-idle", NULL }; |
| 3840 | |
| 3841 | static int match_word(const char *word, char **list) |
| 3842 | { |
| 3843 | int n; |
| 3844 | for (n=0; list[n]; n++) |
| 3845 | if (cmd_match(word, list[n])) |
| 3846 | break; |
| 3847 | return n; |
| 3848 | } |
| 3849 | |
| 3850 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3851 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3852 | { |
| 3853 | enum array_state st = inactive; |
| 3854 | |
| 3855 | if (mddev->pers) |
| 3856 | switch(mddev->ro) { |
| 3857 | case 1: |
| 3858 | st = readonly; |
| 3859 | break; |
| 3860 | case 2: |
| 3861 | st = read_auto; |
| 3862 | break; |
| 3863 | case 0: |
| 3864 | if (mddev->in_sync) |
| 3865 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3866 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3867 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3868 | else if (mddev->safemode) |
| 3869 | st = active_idle; |
| 3870 | else |
| 3871 | st = active; |
| 3872 | } |
| 3873 | else { |
| 3874 | if (list_empty(&mddev->disks) && |
| 3875 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3876 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3877 | st = clear; |
| 3878 | else |
| 3879 | st = inactive; |
| 3880 | } |
| 3881 | return sprintf(page, "%s\n", array_states[st]); |
| 3882 | } |
| 3883 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3884 | static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev); |
| 3885 | static int md_set_readonly(struct mddev * mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3886 | static int do_md_run(struct mddev * mddev); |
| 3887 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3888 | |
| 3889 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3890 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3891 | { |
| 3892 | int err = -EINVAL; |
| 3893 | enum array_state st = match_word(buf, array_states); |
| 3894 | switch(st) { |
| 3895 | case bad_word: |
| 3896 | break; |
| 3897 | case clear: |
| 3898 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3899 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3900 | break; |
| 3901 | case inactive: |
| 3902 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3903 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3904 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3905 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3906 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3907 | break; |
| 3908 | case suspended: |
| 3909 | break; /* not supported yet */ |
| 3910 | case readonly: |
| 3911 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3912 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3913 | else { |
| 3914 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3915 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3916 | err = do_md_run(mddev); |
| 3917 | } |
| 3918 | break; |
| 3919 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3920 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3921 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3922 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3923 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3924 | err = restart_array(mddev); |
| 3925 | if (err == 0) { |
| 3926 | mddev->ro = 2; |
| 3927 | set_disk_ro(mddev->gendisk, 0); |
| 3928 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3929 | } else { |
| 3930 | mddev->ro = 2; |
| 3931 | err = do_md_run(mddev); |
| 3932 | } |
| 3933 | break; |
| 3934 | case clean: |
| 3935 | if (mddev->pers) { |
| 3936 | restart_array(mddev); |
| 3937 | spin_lock_irq(&mddev->write_lock); |
| 3938 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3939 | if (mddev->in_sync == 0) { |
| 3940 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3941 | if (mddev->safemode == 1) |
| 3942 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3943 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3944 | } |
| 3945 | err = 0; |
| 3946 | } else |
| 3947 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3948 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3949 | } else |
| 3950 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3951 | break; |
| 3952 | case active: |
| 3953 | if (mddev->pers) { |
| 3954 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3955 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3956 | wake_up(&mddev->sb_wait); |
| 3957 | err = 0; |
| 3958 | } else { |
| 3959 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3960 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3961 | err = do_md_run(mddev); |
| 3962 | } |
| 3963 | break; |
| 3964 | case write_pending: |
| 3965 | case active_idle: |
| 3966 | /* these cannot be set */ |
| 3967 | break; |
| 3968 | } |
| 3969 | if (err) |
| 3970 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3971 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3972 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3973 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3974 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3975 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3976 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3978 | static struct md_sysfs_entry md_array_state = |
| 3979 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3980 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3981 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3982 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3983 | return sprintf(page, "%d\n", |
| 3984 | atomic_read(&mddev->max_corr_read_errors)); |
| 3985 | } |
| 3986 | |
| 3987 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3988 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3989 | { |
| 3990 | char *e; |
| 3991 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3992 | |
| 3993 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3994 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3995 | return len; |
| 3996 | } |
| 3997 | return -EINVAL; |
| 3998 | } |
| 3999 | |
| 4000 | static struct md_sysfs_entry max_corr_read_errors = |
| 4001 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4002 | max_corrected_read_errors_store); |
| 4003 | |
| 4004 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4005 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4006 | { |
| 4007 | return -EINVAL; |
| 4008 | } |
| 4009 | |
| 4010 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4011 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4012 | { |
| 4013 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4014 | /* The new device is added to the array. |
| 4015 | * If the array has a persistent superblock, we read the |
| 4016 | * superblock to initialise info and check validity. |
| 4017 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4018 | * which mainly checks size. |
| 4019 | */ |
| 4020 | char *e; |
| 4021 | int major = simple_strtoul(buf, &e, 10); |
| 4022 | int minor; |
| 4023 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4024 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4025 | int err; |
| 4026 | |
| 4027 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4028 | return -EINVAL; |
| 4029 | minor = simple_strtoul(e+1, &e, 10); |
| 4030 | if (*e && *e != '\n') |
| 4031 | return -EINVAL; |
| 4032 | dev = MKDEV(major, minor); |
| 4033 | if (major != MAJOR(dev) || |
| 4034 | minor != MINOR(dev)) |
| 4035 | return -EOVERFLOW; |
| 4036 | |
| 4037 | |
| 4038 | if (mddev->persistent) { |
| 4039 | rdev = md_import_device(dev, mddev->major_version, |
| 4040 | mddev->minor_version); |
| 4041 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4042 | struct md_rdev *rdev0 |
| 4043 | = list_entry(mddev->disks.next, |
| 4044 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4045 | err = super_types[mddev->major_version] |
| 4046 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4047 | if (err < 0) |
| 4048 | goto out; |
| 4049 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4050 | } else if (mddev->external) |
| 4051 | rdev = md_import_device(dev, -2, -1); |
| 4052 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4053 | rdev = md_import_device(dev, -1, -1); |
| 4054 | |
| 4055 | if (IS_ERR(rdev)) |
| 4056 | return PTR_ERR(rdev); |
| 4057 | err = bind_rdev_to_array(rdev, mddev); |
| 4058 | out: |
| 4059 | if (err) |
| 4060 | export_rdev(rdev); |
| 4061 | return err ? err : len; |
| 4062 | } |
| 4063 | |
| 4064 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4065 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4066 | |
| 4067 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4068 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4069 | { |
| 4070 | char *end; |
| 4071 | unsigned long chunk, end_chunk; |
| 4072 | |
| 4073 | if (!mddev->bitmap) |
| 4074 | goto out; |
| 4075 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4076 | while (*buf) { |
| 4077 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4078 | if (buf == end) break; |
| 4079 | if (*end == '-') { /* range */ |
| 4080 | buf = end + 1; |
| 4081 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4082 | if (buf == end) break; |
| 4083 | } |
| 4084 | if (*end && !isspace(*end)) break; |
| 4085 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4086 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4087 | } |
| 4088 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4089 | out: |
| 4090 | return len; |
| 4091 | } |
| 4092 | |
| 4093 | static struct md_sysfs_entry md_bitmap = |
| 4094 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4095 | |
| 4096 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4097 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4098 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4099 | return sprintf(page, "%llu\n", |
| 4100 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4103 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4104 | |
| 4105 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4106 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4107 | { |
| 4108 | /* If array is inactive, we can reduce the component size, but |
| 4109 | * not increase it (except from 0). |
| 4110 | * If array is active, we can try an on-line resize |
| 4111 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4112 | sector_t sectors; |
| 4113 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4114 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4115 | if (err < 0) |
| 4116 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4117 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4118 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4119 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4120 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4121 | if (mddev->dev_sectors == 0 || |
| 4122 | mddev->dev_sectors > sectors) |
| 4123 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4124 | else |
| 4125 | err = -ENOSPC; |
| 4126 | } |
| 4127 | return err ? err : len; |
| 4128 | } |
| 4129 | |
| 4130 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4131 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4132 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4133 | |
| 4134 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4135 | * This is one of |
| 4136 | * 'none' for arrays with no metadata (good luck...) |
| 4137 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4138 | * or N.M for internally known formats |
| 4139 | */ |
| 4140 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4141 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4142 | { |
| 4143 | if (mddev->persistent) |
| 4144 | return sprintf(page, "%d.%d\n", |
| 4145 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4146 | else if (mddev->external) |
| 4147 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4148 | else |
| 4149 | return sprintf(page, "none\n"); |
| 4150 | } |
| 4151 | |
| 4152 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4153 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4154 | { |
| 4155 | int major, minor; |
| 4156 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4157 | /* Changing the details of 'external' metadata is |
| 4158 | * always permitted. Otherwise there must be |
| 4159 | * no devices attached to the array. |
| 4160 | */ |
| 4161 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4162 | ; |
| 4163 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4164 | return -EBUSY; |
| 4165 | |
| 4166 | if (cmd_match(buf, "none")) { |
| 4167 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4168 | mddev->external = 0; |
| 4169 | mddev->major_version = 0; |
| 4170 | mddev->minor_version = 90; |
| 4171 | return len; |
| 4172 | } |
| 4173 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4174 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4175 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4176 | namelen = sizeof(mddev->metadata_type)-1; |
| 4177 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4178 | mddev->metadata_type[namelen] = 0; |
| 4179 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4180 | mddev->metadata_type[--namelen] = 0; |
| 4181 | mddev->persistent = 0; |
| 4182 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4183 | mddev->major_version = 0; |
| 4184 | mddev->minor_version = 90; |
| 4185 | return len; |
| 4186 | } |
| 4187 | major = simple_strtoul(buf, &e, 10); |
| 4188 | if (e==buf || *e != '.') |
| 4189 | return -EINVAL; |
| 4190 | buf = e+1; |
| 4191 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4192 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4193 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4194 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4195 | return -ENOENT; |
| 4196 | mddev->major_version = major; |
| 4197 | mddev->minor_version = minor; |
| 4198 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4199 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4200 | return len; |
| 4201 | } |
| 4202 | |
| 4203 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4204 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4205 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4206 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4207 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4208 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4209 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4210 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4211 | type = "frozen"; |
| 4212 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4213 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4214 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4215 | type = "reshape"; |
| 4216 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4217 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4218 | type = "resync"; |
| 4219 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4220 | type = "check"; |
| 4221 | else |
| 4222 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4223 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4224 | type = "recover"; |
| 4225 | } |
| 4226 | return sprintf(page, "%s\n", type); |
| 4227 | } |
| 4228 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4229 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4230 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4231 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4232 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4233 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4234 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4235 | return -EINVAL; |
| 4236 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4237 | if (cmd_match(page, "frozen")) |
| 4238 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4239 | else |
| 4240 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4241 | |
| 4242 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4243 | if (mddev->sync_thread) { |
| 4244 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4245 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4246 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4247 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4248 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4249 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4250 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4251 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4252 | else if (cmd_match(page, "recover")) { |
| 4253 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4254 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4255 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4256 | int err; |
| 4257 | if (mddev->pers->start_reshape == NULL) |
| 4258 | return -EINVAL; |
| 4259 | err = mddev->pers->start_reshape(mddev); |
| 4260 | if (err) |
| 4261 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4262 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4263 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4264 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4265 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4266 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4267 | return -EINVAL; |
| 4268 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4269 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4270 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4271 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4272 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4273 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4274 | return len; |
| 4275 | } |
| 4276 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4277 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4278 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4279 | { |
| 4280 | return sprintf(page, "%llu\n", |
| 4281 | (unsigned long long) mddev->resync_mismatches); |
| 4282 | } |
| 4283 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4284 | static struct md_sysfs_entry md_scan_mode = |
| 4285 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4286 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4287 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4288 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4289 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4290 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4291 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4292 | { |
| 4293 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4294 | mddev->sync_speed_min ? "local": "system"); |
| 4295 | } |
| 4296 | |
| 4297 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4298 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4299 | { |
| 4300 | int min; |
| 4301 | char *e; |
| 4302 | if (strncmp(buf, "system", 6)==0) { |
| 4303 | mddev->sync_speed_min = 0; |
| 4304 | return len; |
| 4305 | } |
| 4306 | min = simple_strtoul(buf, &e, 10); |
| 4307 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4308 | return -EINVAL; |
| 4309 | mddev->sync_speed_min = min; |
| 4310 | return len; |
| 4311 | } |
| 4312 | |
| 4313 | static struct md_sysfs_entry md_sync_min = |
| 4314 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4315 | |
| 4316 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4317 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4318 | { |
| 4319 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4320 | mddev->sync_speed_max ? "local": "system"); |
| 4321 | } |
| 4322 | |
| 4323 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4324 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4325 | { |
| 4326 | int max; |
| 4327 | char *e; |
| 4328 | if (strncmp(buf, "system", 6)==0) { |
| 4329 | mddev->sync_speed_max = 0; |
| 4330 | return len; |
| 4331 | } |
| 4332 | max = simple_strtoul(buf, &e, 10); |
| 4333 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4334 | return -EINVAL; |
| 4335 | mddev->sync_speed_max = max; |
| 4336 | return len; |
| 4337 | } |
| 4338 | |
| 4339 | static struct md_sysfs_entry md_sync_max = |
| 4340 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4341 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4342 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4343 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4344 | { |
| 4345 | return sprintf(page, "%d\n", mddev->degraded); |
| 4346 | } |
| 4347 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4348 | |
| 4349 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4350 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4351 | { |
| 4352 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4353 | } |
| 4354 | |
| 4355 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4356 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4357 | { |
| 4358 | long n; |
| 4359 | |
| 4360 | if (strict_strtol(buf, 10, &n)) |
| 4361 | return -EINVAL; |
| 4362 | |
| 4363 | if (n != 0 && n != 1) |
| 4364 | return -EINVAL; |
| 4365 | |
| 4366 | mddev->parallel_resync = n; |
| 4367 | |
| 4368 | if (mddev->sync_thread) |
| 4369 | wake_up(&resync_wait); |
| 4370 | |
| 4371 | return len; |
| 4372 | } |
| 4373 | |
| 4374 | /* force parallel resync, even with shared block devices */ |
| 4375 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4376 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4377 | sync_force_parallel_show, sync_force_parallel_store); |
| 4378 | |
| 4379 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4380 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4381 | { |
| 4382 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4383 | if (mddev->curr_resync == 0) |
| 4384 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4385 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4386 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4387 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4388 | db = resync - mddev->resync_mark_cnt; |
| 4389 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4390 | } |
| 4391 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4392 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4393 | |
| 4394 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4395 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4396 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4397 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4398 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4399 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4400 | return sprintf(page, "none\n"); |
| 4401 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4402 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4403 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4404 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4405 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4406 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4407 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4408 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4409 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4412 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4413 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4414 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4415 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4416 | { |
| 4417 | return sprintf(page, "%llu\n", |
| 4418 | (unsigned long long)mddev->resync_min); |
| 4419 | } |
| 4420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4421 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4422 | { |
| 4423 | unsigned long long min; |
| 4424 | if (strict_strtoull(buf, 10, &min)) |
| 4425 | return -EINVAL; |
| 4426 | if (min > mddev->resync_max) |
| 4427 | return -EINVAL; |
| 4428 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4429 | return -EBUSY; |
| 4430 | |
| 4431 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4432 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4433 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4434 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4435 | return -EINVAL; |
| 4436 | } |
| 4437 | mddev->resync_min = min; |
| 4438 | |
| 4439 | return len; |
| 4440 | } |
| 4441 | |
| 4442 | static struct md_sysfs_entry md_min_sync = |
| 4443 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4444 | |
| 4445 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4446 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4447 | { |
| 4448 | if (mddev->resync_max == MaxSector) |
| 4449 | return sprintf(page, "max\n"); |
| 4450 | else |
| 4451 | return sprintf(page, "%llu\n", |
| 4452 | (unsigned long long)mddev->resync_max); |
| 4453 | } |
| 4454 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4455 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4456 | { |
| 4457 | if (strncmp(buf, "max", 3) == 0) |
| 4458 | mddev->resync_max = MaxSector; |
| 4459 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4460 | unsigned long long max; |
| 4461 | if (strict_strtoull(buf, 10, &max)) |
| 4462 | return -EINVAL; |
| 4463 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4464 | return -EINVAL; |
| 4465 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4466 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4467 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4468 | return -EBUSY; |
| 4469 | |
| 4470 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4471 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4472 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4473 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4474 | return -EINVAL; |
| 4475 | } |
| 4476 | mddev->resync_max = max; |
| 4477 | } |
| 4478 | wake_up(&mddev->recovery_wait); |
| 4479 | return len; |
| 4480 | } |
| 4481 | |
| 4482 | static struct md_sysfs_entry md_max_sync = |
| 4483 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4484 | |
| 4485 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4486 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4487 | { |
| 4488 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4489 | } |
| 4490 | |
| 4491 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4492 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4493 | { |
| 4494 | char *e; |
| 4495 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4496 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4497 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4498 | if (mddev->pers == NULL || |
| 4499 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4500 | return -EINVAL; |
| 4501 | if (buf == e || (*e && *e != '\n')) |
| 4502 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4503 | |
| 4504 | mddev->suspend_lo = new; |
| 4505 | if (new >= old) |
| 4506 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4507 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4508 | else { |
| 4509 | /* Expanding suspended region - need to wait */ |
| 4510 | mddev->pers->quiesce(mddev, 1); |
| 4511 | mddev->pers->quiesce(mddev, 0); |
| 4512 | } |
| 4513 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4514 | } |
| 4515 | static struct md_sysfs_entry md_suspend_lo = |
| 4516 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4517 | |
| 4518 | |
| 4519 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4520 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4521 | { |
| 4522 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4523 | } |
| 4524 | |
| 4525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4526 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4527 | { |
| 4528 | char *e; |
| 4529 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4530 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4531 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4532 | if (mddev->pers == NULL || |
| 4533 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4534 | return -EINVAL; |
| 4535 | if (buf == e || (*e && *e != '\n')) |
| 4536 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4537 | |
| 4538 | mddev->suspend_hi = new; |
| 4539 | if (new <= old) |
| 4540 | /* Shrinking suspended region */ |
| 4541 | mddev->pers->quiesce(mddev, 2); |
| 4542 | else { |
| 4543 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4544 | mddev->pers->quiesce(mddev, 1); |
| 4545 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4546 | } |
| 4547 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4548 | } |
| 4549 | static struct md_sysfs_entry md_suspend_hi = |
| 4550 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4551 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4552 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4553 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4554 | { |
| 4555 | if (mddev->reshape_position != MaxSector) |
| 4556 | return sprintf(page, "%llu\n", |
| 4557 | (unsigned long long)mddev->reshape_position); |
| 4558 | strcpy(page, "none\n"); |
| 4559 | return 5; |
| 4560 | } |
| 4561 | |
| 4562 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4563 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4564 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4565 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4566 | char *e; |
| 4567 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4568 | if (mddev->pers) |
| 4569 | return -EBUSY; |
| 4570 | if (buf == e || (*e && *e != '\n')) |
| 4571 | return -EINVAL; |
| 4572 | mddev->reshape_position = new; |
| 4573 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4574 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4575 | mddev->new_level = mddev->level; |
| 4576 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4577 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4578 | rdev_for_each(rdev, mddev) |
| 4579 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4580 | return len; |
| 4581 | } |
| 4582 | |
| 4583 | static struct md_sysfs_entry md_reshape_position = |
| 4584 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4585 | reshape_position_store); |
| 4586 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4587 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4588 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4589 | { |
| 4590 | return sprintf(page, "%s\n", |
| 4591 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4592 | } |
| 4593 | |
| 4594 | static ssize_t |
| 4595 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4596 | { |
| 4597 | int backwards = 0; |
| 4598 | if (cmd_match(buf, "forwards")) |
| 4599 | backwards = 0; |
| 4600 | else if (cmd_match(buf, "backwards")) |
| 4601 | backwards = 1; |
| 4602 | else |
| 4603 | return -EINVAL; |
| 4604 | if (mddev->reshape_backwards == backwards) |
| 4605 | return len; |
| 4606 | |
| 4607 | /* check if we are allowed to change */ |
| 4608 | if (mddev->delta_disks) |
| 4609 | return -EBUSY; |
| 4610 | |
| 4611 | if (mddev->persistent && |
| 4612 | mddev->major_version == 0) |
| 4613 | return -EINVAL; |
| 4614 | |
| 4615 | mddev->reshape_backwards = backwards; |
| 4616 | return len; |
| 4617 | } |
| 4618 | |
| 4619 | static struct md_sysfs_entry md_reshape_direction = |
| 4620 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4621 | reshape_direction_store); |
| 4622 | |
| 4623 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4624 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4625 | { |
| 4626 | if (mddev->external_size) |
| 4627 | return sprintf(page, "%llu\n", |
| 4628 | (unsigned long long)mddev->array_sectors/2); |
| 4629 | else |
| 4630 | return sprintf(page, "default\n"); |
| 4631 | } |
| 4632 | |
| 4633 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4634 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4635 | { |
| 4636 | sector_t sectors; |
| 4637 | |
| 4638 | if (strncmp(buf, "default", 7) == 0) { |
| 4639 | if (mddev->pers) |
| 4640 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4641 | else |
| 4642 | sectors = mddev->array_sectors; |
| 4643 | |
| 4644 | mddev->external_size = 0; |
| 4645 | } else { |
| 4646 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4647 | return -EINVAL; |
| 4648 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4649 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4650 | |
| 4651 | mddev->external_size = 1; |
| 4652 | } |
| 4653 | |
| 4654 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4655 | if (mddev->pers) { |
| 4656 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4657 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4658 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4659 | return len; |
| 4660 | } |
| 4661 | |
| 4662 | static struct md_sysfs_entry md_array_size = |
| 4663 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4664 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4665 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4666 | static struct attribute *md_default_attrs[] = { |
| 4667 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4668 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4669 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4670 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4671 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4672 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4673 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4674 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4675 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4676 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4677 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4678 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4679 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4680 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4681 | NULL, |
| 4682 | }; |
| 4683 | |
| 4684 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4685 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4686 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4687 | &md_sync_min.attr, |
| 4688 | &md_sync_max.attr, |
| 4689 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4690 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4691 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4692 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4693 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4694 | &md_suspend_lo.attr, |
| 4695 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4696 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4697 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4698 | NULL, |
| 4699 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4700 | static struct attribute_group md_redundancy_group = { |
| 4701 | .name = NULL, |
| 4702 | .attrs = md_redundancy_attrs, |
| 4703 | }; |
| 4704 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4705 | |
| 4706 | static ssize_t |
| 4707 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4708 | { |
| 4709 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4710 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4711 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4712 | |
| 4713 | if (!entry->show) |
| 4714 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4715 | spin_lock(&all_mddevs_lock); |
| 4716 | if (list_empty(&mddev->all_mddevs)) { |
| 4717 | spin_unlock(&all_mddevs_lock); |
| 4718 | return -EBUSY; |
| 4719 | } |
| 4720 | mddev_get(mddev); |
| 4721 | spin_unlock(&all_mddevs_lock); |
| 4722 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4723 | rv = mddev_lock(mddev); |
| 4724 | if (!rv) { |
| 4725 | rv = entry->show(mddev, page); |
| 4726 | mddev_unlock(mddev); |
| 4727 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4728 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4729 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | static ssize_t |
| 4733 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4734 | const char *page, size_t length) |
| 4735 | { |
| 4736 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4737 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4738 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4739 | |
| 4740 | if (!entry->store) |
| 4741 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4742 | if (!capable(CAP_SYS_ADMIN)) |
| 4743 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4744 | spin_lock(&all_mddevs_lock); |
| 4745 | if (list_empty(&mddev->all_mddevs)) { |
| 4746 | spin_unlock(&all_mddevs_lock); |
| 4747 | return -EBUSY; |
| 4748 | } |
| 4749 | mddev_get(mddev); |
| 4750 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4751 | rv = mddev_lock(mddev); |
| 4752 | if (!rv) { |
| 4753 | rv = entry->store(mddev, page, length); |
| 4754 | mddev_unlock(mddev); |
| 4755 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4756 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4757 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | static void md_free(struct kobject *ko) |
| 4761 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4762 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4763 | |
| 4764 | if (mddev->sysfs_state) |
| 4765 | sysfs_put(mddev->sysfs_state); |
| 4766 | |
| 4767 | if (mddev->gendisk) { |
| 4768 | del_gendisk(mddev->gendisk); |
| 4769 | put_disk(mddev->gendisk); |
| 4770 | } |
| 4771 | if (mddev->queue) |
| 4772 | blk_cleanup_queue(mddev->queue); |
| 4773 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4774 | kfree(mddev); |
| 4775 | } |
| 4776 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4777 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4778 | .show = md_attr_show, |
| 4779 | .store = md_attr_store, |
| 4780 | }; |
| 4781 | static struct kobj_type md_ktype = { |
| 4782 | .release = md_free, |
| 4783 | .sysfs_ops = &md_sysfs_ops, |
| 4784 | .default_attrs = md_default_attrs, |
| 4785 | }; |
| 4786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | int mdp_major = 0; |
| 4788 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4789 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4790 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4791 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4792 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4793 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4794 | kobject_del(&mddev->kobj); |
| 4795 | kobject_put(&mddev->kobj); |
| 4796 | } |
| 4797 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4798 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4800 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4801 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4803 | int partitioned; |
| 4804 | int shift; |
| 4805 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4806 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
| 4808 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4809 | return -ENODEV; |
| 4810 | |
| 4811 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4812 | shift = partitioned ? MdpMinorShift : 0; |
| 4813 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4815 | /* wait for any previous instance of this device to be |
| 4816 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4817 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4818 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4819 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4820 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4821 | error = -EEXIST; |
| 4822 | if (mddev->gendisk) |
| 4823 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4824 | |
| 4825 | if (name) { |
| 4826 | /* Need to ensure that 'name' is not a duplicate. |
| 4827 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4828 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4829 | spin_lock(&all_mddevs_lock); |
| 4830 | |
| 4831 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4832 | if (mddev2->gendisk && |
| 4833 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4834 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4835 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4836 | } |
| 4837 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4839 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4840 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4841 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4842 | if (!mddev->queue) |
| 4843 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4844 | mddev->queue->queuedata = mddev; |
| 4845 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4846 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4847 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | disk = alloc_disk(1 << shift); |
| 4850 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4851 | blk_cleanup_queue(mddev->queue); |
| 4852 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4853 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4855 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4857 | if (name) |
| 4858 | strcpy(disk->disk_name, name); |
| 4859 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4861 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | disk->fops = &md_fops; |
| 4864 | disk->private_data = mddev; |
| 4865 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4866 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4867 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4868 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4869 | * remove it now. |
| 4870 | */ |
| 4871 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4873 | /* As soon as we call add_disk(), another thread could get |
| 4874 | * through to md_open, so make sure it doesn't get too far |
| 4875 | */ |
| 4876 | mutex_lock(&mddev->open_mutex); |
| 4877 | add_disk(disk); |
| 4878 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4879 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4880 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4881 | if (error) { |
| 4882 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4883 | * __must_check, we must do something with the result |
| 4884 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4885 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4886 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4887 | error = 0; |
| 4888 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4889 | if (mddev->kobj.sd && |
| 4890 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4891 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4892 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4893 | abort: |
| 4894 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4895 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4896 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4897 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4898 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4899 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4900 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4904 | { |
| 4905 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | return NULL; |
| 4907 | } |
| 4908 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4909 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4910 | { |
| 4911 | /* val must be "md_*" where * is not all digits. |
| 4912 | * We allocate an array with a large free minor number, and |
| 4913 | * set the name to val. val must not already be an active name. |
| 4914 | */ |
| 4915 | int len = strlen(val); |
| 4916 | char buf[DISK_NAME_LEN]; |
| 4917 | |
| 4918 | while (len && val[len-1] == '\n') |
| 4919 | len--; |
| 4920 | if (len >= DISK_NAME_LEN) |
| 4921 | return -E2BIG; |
| 4922 | strlcpy(buf, val, len+1); |
| 4923 | if (strncmp(buf, "md_", 3) != 0) |
| 4924 | return -EINVAL; |
| 4925 | return md_alloc(0, buf); |
| 4926 | } |
| 4927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | static void md_safemode_timeout(unsigned long data) |
| 4929 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4930 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4932 | if (!atomic_read(&mddev->writes_pending)) { |
| 4933 | mddev->safemode = 1; |
| 4934 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4935 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | md_wakeup_thread(mddev->thread); |
| 4938 | } |
| 4939 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4940 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4942 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4944 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4945 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4946 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4948 | if (list_empty(&mddev->disks)) |
| 4949 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
| 4952 | if (mddev->pers) |
| 4953 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4954 | /* Cannot run until previous stop completes properly */ |
| 4955 | if (mddev->sysfs_active) |
| 4956 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | /* |
| 4959 | * Analyze all RAID superblock(s) |
| 4960 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4961 | if (!mddev->raid_disks) { |
| 4962 | if (!mddev->persistent) |
| 4963 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4964 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4967 | if (mddev->level != LEVEL_NONE) |
| 4968 | request_module("md-level-%d", mddev->level); |
| 4969 | else if (mddev->clevel[0]) |
| 4970 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | |
| 4972 | /* |
| 4973 | * Drop all container device buffers, from now on |
| 4974 | * the only valid external interface is through the md |
| 4975 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4977 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4978 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | continue; |
| 4980 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4981 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4982 | |
| 4983 | /* perform some consistency tests on the device. |
| 4984 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4985 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4986 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4987 | if (rdev->meta_bdev) { |
| 4988 | /* Nothing to check */; |
| 4989 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4990 | if (mddev->dev_sectors && |
| 4991 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4992 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4993 | printk("md: %s: data overlaps metadata\n", |
| 4994 | mdname(mddev)); |
| 4995 | return -EINVAL; |
| 4996 | } |
| 4997 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4998 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4999 | > rdev->data_offset) { |
| 5000 | printk("md: %s: metadata overlaps data\n", |
| 5001 | mdname(mddev)); |
| 5002 | return -EINVAL; |
| 5003 | } |
| 5004 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5005 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5008 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 5009 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5010 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5013 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5014 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5016 | if (mddev->level != LEVEL_NONE) |
| 5017 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5018 | mddev->level); |
| 5019 | else |
| 5020 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5021 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | return -EINVAL; |
| 5023 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5024 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5026 | if (mddev->level != pers->level) { |
| 5027 | mddev->level = pers->level; |
| 5028 | mddev->new_level = pers->level; |
| 5029 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5030 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5032 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5033 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5034 | /* This personality cannot handle reshaping... */ |
| 5035 | mddev->pers = NULL; |
| 5036 | module_put(pers->owner); |
| 5037 | return -EINVAL; |
| 5038 | } |
| 5039 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5040 | if (pers->sync_request) { |
| 5041 | /* Warn if this is a potentially silly |
| 5042 | * configuration. |
| 5043 | */ |
| 5044 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5045 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5046 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5047 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5048 | rdev_for_each(rdev, mddev) |
| 5049 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5050 | if (rdev < rdev2 && |
| 5051 | rdev->bdev->bd_contains == |
| 5052 | rdev2->bdev->bd_contains) { |
| 5053 | printk(KERN_WARNING |
| 5054 | "%s: WARNING: %s appears to be" |
| 5055 | " on the same physical disk as" |
| 5056 | " %s.\n", |
| 5057 | mdname(mddev), |
| 5058 | bdevname(rdev->bdev,b), |
| 5059 | bdevname(rdev2->bdev,b2)); |
| 5060 | warned = 1; |
| 5061 | } |
| 5062 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5063 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5064 | if (warned) |
| 5065 | printk(KERN_WARNING |
| 5066 | "True protection against single-disk" |
| 5067 | " failure might be compromised.\n"); |
| 5068 | } |
| 5069 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5070 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5071 | /* may be over-ridden by personality */ |
| 5072 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5073 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5074 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5076 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5077 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5078 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5079 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5080 | if (err) |
| 5081 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5082 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5083 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5084 | " but 'external_size' not in effect?\n", __func__); |
| 5085 | printk(KERN_ERR |
| 5086 | "md: invalid array_size %llu > default size %llu\n", |
| 5087 | (unsigned long long)mddev->array_sectors / 2, |
| 5088 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5089 | err = -EINVAL; |
| 5090 | mddev->pers->stop(mddev); |
| 5091 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5092 | if (err == 0 && mddev->pers->sync_request && |
| 5093 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5094 | err = bitmap_create(mddev); |
| 5095 | if (err) { |
| 5096 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5097 | mdname(mddev), err); |
| 5098 | mddev->pers->stop(mddev); |
| 5099 | } |
| 5100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | module_put(mddev->pers->owner); |
| 5103 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5104 | bitmap_destroy(mddev); |
| 5105 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5107 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5108 | if (mddev->kobj.sd && |
| 5109 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5110 | printk(KERN_WARNING |
| 5111 | "md: cannot register extra attributes for %s\n", |
| 5112 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5113 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5114 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5115 | mddev->ro = 0; |
| 5116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5118 | atomic_set(&mddev->max_corr_read_errors, |
| 5119 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | mddev->safemode = 0; |
| 5121 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5122 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5123 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5125 | smp_wmb(); |
| 5126 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5127 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5128 | if (rdev->raid_disk >= 0) |
| 5129 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5130 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | |
| 5132 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5133 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5134 | if (mddev->flags) |
| 5135 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5137 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5138 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5139 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5140 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | return 0; |
| 5142 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5143 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5145 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5146 | { |
| 5147 | int err; |
| 5148 | |
| 5149 | err = md_run(mddev); |
| 5150 | if (err) |
| 5151 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5152 | err = bitmap_load(mddev); |
| 5153 | if (err) { |
| 5154 | bitmap_destroy(mddev); |
| 5155 | goto out; |
| 5156 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5157 | |
| 5158 | md_wakeup_thread(mddev->thread); |
| 5159 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5160 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5161 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5162 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5163 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5164 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5165 | out: |
| 5166 | return err; |
| 5167 | } |
| 5168 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5169 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | { |
| 5171 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5173 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5175 | return -ENXIO; |
| 5176 | if (!mddev->pers) |
| 5177 | return -EINVAL; |
| 5178 | if (!mddev->ro) |
| 5179 | return -EBUSY; |
| 5180 | mddev->safemode = 0; |
| 5181 | mddev->ro = 0; |
| 5182 | set_disk_ro(disk, 0); |
| 5183 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5184 | mdname(mddev)); |
| 5185 | /* Kick recovery or resync if necessary */ |
| 5186 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5187 | md_wakeup_thread(mddev->thread); |
| 5188 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5189 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5190 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5193 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5194 | * to the file ourselves */ |
| 5195 | static int deny_bitmap_write_access(struct file * file) |
| 5196 | { |
| 5197 | struct inode *inode = file->f_mapping->host; |
| 5198 | |
| 5199 | spin_lock(&inode->i_lock); |
| 5200 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5201 | spin_unlock(&inode->i_lock); |
| 5202 | return -ETXTBSY; |
| 5203 | } |
| 5204 | atomic_set(&inode->i_writecount, -1); |
| 5205 | spin_unlock(&inode->i_lock); |
| 5206 | |
| 5207 | return 0; |
| 5208 | } |
| 5209 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5210 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5211 | { |
| 5212 | struct inode *inode = file->f_mapping->host; |
| 5213 | |
| 5214 | spin_lock(&inode->i_lock); |
| 5215 | atomic_set(&inode->i_writecount, 1); |
| 5216 | spin_unlock(&inode->i_lock); |
| 5217 | } |
| 5218 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5219 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5220 | { |
| 5221 | mddev->array_sectors = 0; |
| 5222 | mddev->external_size = 0; |
| 5223 | mddev->dev_sectors = 0; |
| 5224 | mddev->raid_disks = 0; |
| 5225 | mddev->recovery_cp = 0; |
| 5226 | mddev->resync_min = 0; |
| 5227 | mddev->resync_max = MaxSector; |
| 5228 | mddev->reshape_position = MaxSector; |
| 5229 | mddev->external = 0; |
| 5230 | mddev->persistent = 0; |
| 5231 | mddev->level = LEVEL_NONE; |
| 5232 | mddev->clevel[0] = 0; |
| 5233 | mddev->flags = 0; |
| 5234 | mddev->ro = 0; |
| 5235 | mddev->metadata_type[0] = 0; |
| 5236 | mddev->chunk_sectors = 0; |
| 5237 | mddev->ctime = mddev->utime = 0; |
| 5238 | mddev->layout = 0; |
| 5239 | mddev->max_disks = 0; |
| 5240 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5241 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5242 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5243 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5244 | mddev->new_level = LEVEL_NONE; |
| 5245 | mddev->new_layout = 0; |
| 5246 | mddev->new_chunk_sectors = 0; |
| 5247 | mddev->curr_resync = 0; |
| 5248 | mddev->resync_mismatches = 0; |
| 5249 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5250 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5251 | mddev->recovery = 0; |
| 5252 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5253 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5254 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5255 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5256 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5257 | mddev->bitmap_info.offset = 0; |
| 5258 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5259 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5260 | mddev->bitmap_info.chunksize = 0; |
| 5261 | mddev->bitmap_info.daemon_sleep = 0; |
| 5262 | mddev->bitmap_info.max_write_behind = 0; |
| 5263 | } |
| 5264 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5265 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5266 | { |
| 5267 | if (mddev->sync_thread) { |
| 5268 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5269 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5270 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | del_timer_sync(&mddev->safemode_timer); |
| 5274 | |
| 5275 | bitmap_flush(mddev); |
| 5276 | md_super_wait(mddev); |
| 5277 | |
| 5278 | if (!mddev->in_sync || mddev->flags) { |
| 5279 | /* mark array as shutdown cleanly */ |
| 5280 | mddev->in_sync = 1; |
| 5281 | md_update_sb(mddev, 1); |
| 5282 | } |
| 5283 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5284 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5285 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5286 | { |
| 5287 | mddev_lock(mddev); |
| 5288 | __md_stop_writes(mddev); |
| 5289 | mddev_unlock(mddev); |
| 5290 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5291 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5292 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5293 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5294 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5295 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5296 | mddev->pers->stop(mddev); |
| 5297 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5298 | mddev->to_remove = &md_redundancy_group; |
| 5299 | module_put(mddev->pers->owner); |
| 5300 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5301 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5302 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5303 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5304 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5305 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5306 | { |
| 5307 | int err = 0; |
| 5308 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5309 | if (atomic_read(&mddev->openers) > !!bdev) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5310 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5311 | err = -EBUSY; |
| 5312 | goto out; |
| 5313 | } |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5314 | if (bdev) |
| 5315 | sync_blockdev(bdev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5316 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5317 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5318 | |
| 5319 | err = -ENXIO; |
| 5320 | if (mddev->ro==1) |
| 5321 | goto out; |
| 5322 | mddev->ro = 1; |
| 5323 | set_disk_ro(mddev->gendisk, 1); |
| 5324 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5325 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5326 | err = 0; |
| 5327 | } |
| 5328 | out: |
| 5329 | mutex_unlock(&mddev->open_mutex); |
| 5330 | return err; |
| 5331 | } |
| 5332 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5333 | /* mode: |
| 5334 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5335 | * 2 - stop but do not disassemble array |
| 5336 | */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5337 | static int do_md_stop(struct mddev * mddev, int mode, |
| 5338 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5341 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5343 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5344 | if (atomic_read(&mddev->openers) > !!bdev || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5345 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5346 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5347 | mutex_unlock(&mddev->open_mutex); |
| 5348 | return -EBUSY; |
| 5349 | } |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5350 | if (bdev) |
| 5351 | /* It is possible IO was issued on some other |
| 5352 | * open file which was closed before we took ->open_mutex. |
| 5353 | * As that was not the last close __blkdev_put will not |
| 5354 | * have called sync_blockdev, so we must. |
| 5355 | */ |
| 5356 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5358 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5359 | if (mddev->ro) |
| 5360 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5361 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5362 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5363 | md_stop(mddev); |
| 5364 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5365 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5366 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5367 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5368 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5369 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5370 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5371 | if (rdev->raid_disk >= 0) |
| 5372 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5373 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5374 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5375 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5376 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5377 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5378 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5379 | if (mddev->ro) |
| 5380 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5381 | } else |
| 5382 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | /* |
| 5384 | * Free resources if final stop |
| 5385 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5386 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5388 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5389 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5390 | if (mddev->bitmap_info.file) { |
| 5391 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5392 | fput(mddev->bitmap_info.file); |
| 5393 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5394 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5395 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | export_array(mddev); |
| 5398 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5399 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5400 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5401 | if (mddev->hold_active == UNTIL_STOP) |
| 5402 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5403 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5404 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5405 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5406 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5407 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5410 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5411 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5413 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | int err; |
| 5415 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5416 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | |
| 5419 | printk(KERN_INFO "md: running: "); |
| 5420 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5421 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | char b[BDEVNAME_SIZE]; |
| 5423 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5424 | } |
| 5425 | printk("\n"); |
| 5426 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5427 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | if (err) { |
| 5429 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5430 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * lets try to run arrays based on all disks that have arrived |
| 5436 | * until now. (those are in pending_raid_disks) |
| 5437 | * |
| 5438 | * the method: pick the first pending disk, collect all disks with |
| 5439 | * the same UUID, remove all from the pending list and put them into |
| 5440 | * the 'same_array' list. Then order this list based on superblock |
| 5441 | * update time (freshest comes first), kick out 'old' disks and |
| 5442 | * compare superblocks. If everything's fine then run it. |
| 5443 | * |
| 5444 | * If "unit" is allocated, then bump its reference count |
| 5445 | */ |
| 5446 | static void autorun_devices(int part) |
| 5447 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5448 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5449 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | char b[BDEVNAME_SIZE]; |
| 5451 | |
| 5452 | printk(KERN_INFO "md: autorun ...\n"); |
| 5453 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5454 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5456 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5458 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | |
| 5460 | printk(KERN_INFO "md: considering %s ...\n", |
| 5461 | bdevname(rdev0->bdev,b)); |
| 5462 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5463 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5465 | printk(KERN_INFO "md: adding %s ...\n", |
| 5466 | bdevname(rdev->bdev,b)); |
| 5467 | list_move(&rdev->same_set, &candidates); |
| 5468 | } |
| 5469 | /* |
| 5470 | * now we have a set of devices, with all of them having |
| 5471 | * mostly sane superblocks. It's time to allocate the |
| 5472 | * mddev. |
| 5473 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5474 | if (part) { |
| 5475 | dev = MKDEV(mdp_major, |
| 5476 | rdev0->preferred_minor << MdpMinorShift); |
| 5477 | unit = MINOR(dev) >> MdpMinorShift; |
| 5478 | } else { |
| 5479 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5480 | unit = MINOR(dev); |
| 5481 | } |
| 5482 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5484 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5485 | break; |
| 5486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | |
| 5488 | md_probe(dev, NULL, NULL); |
| 5489 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5490 | if (!mddev || !mddev->gendisk) { |
| 5491 | if (mddev) |
| 5492 | mddev_put(mddev); |
| 5493 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | "md: cannot allocate memory for md drive.\n"); |
| 5495 | break; |
| 5496 | } |
| 5497 | if (mddev_lock(mddev)) |
| 5498 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5499 | mdname(mddev)); |
| 5500 | else if (mddev->raid_disks || mddev->major_version |
| 5501 | || !list_empty(&mddev->disks)) { |
| 5502 | printk(KERN_WARNING |
| 5503 | "md: %s already running, cannot run %s\n", |
| 5504 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5505 | mddev_unlock(mddev); |
| 5506 | } else { |
| 5507 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5508 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5509 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | list_del_init(&rdev->same_set); |
| 5511 | if (bind_rdev_to_array(rdev, mddev)) |
| 5512 | export_rdev(rdev); |
| 5513 | } |
| 5514 | autorun_array(mddev); |
| 5515 | mddev_unlock(mddev); |
| 5516 | } |
| 5517 | /* on success, candidates will be empty, on error |
| 5518 | * it won't... |
| 5519 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5520 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5521 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | mddev_put(mddev); |
| 5525 | } |
| 5526 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5527 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5528 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | static int get_version(void __user * arg) |
| 5531 | { |
| 5532 | mdu_version_t ver; |
| 5533 | |
| 5534 | ver.major = MD_MAJOR_VERSION; |
| 5535 | ver.minor = MD_MINOR_VERSION; |
| 5536 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5537 | |
| 5538 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5539 | return -EFAULT; |
| 5540 | |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5544 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | { |
| 5546 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5547 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5548 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5550 | nr=working=insync=failed=spare=0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5551 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5553 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | failed++; |
| 5555 | else { |
| 5556 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5557 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5558 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | else |
| 5560 | spare++; |
| 5561 | } |
| 5562 | } |
| 5563 | |
| 5564 | info.major_version = mddev->major_version; |
| 5565 | info.minor_version = mddev->minor_version; |
| 5566 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5567 | info.ctime = mddev->ctime; |
| 5568 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5569 | info.size = mddev->dev_sectors / 2; |
| 5570 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5571 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | info.nr_disks = nr; |
| 5573 | info.raid_disks = mddev->raid_disks; |
| 5574 | info.md_minor = mddev->md_minor; |
| 5575 | info.not_persistent= !mddev->persistent; |
| 5576 | |
| 5577 | info.utime = mddev->utime; |
| 5578 | info.state = 0; |
| 5579 | if (mddev->in_sync) |
| 5580 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5581 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5582 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5583 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | info.working_disks = working; |
| 5585 | info.failed_disks = failed; |
| 5586 | info.spare_disks = spare; |
| 5587 | |
| 5588 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5589 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | |
| 5591 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5592 | return -EFAULT; |
| 5593 | |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5597 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5598 | { |
| 5599 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5600 | char *ptr, *buf = NULL; |
| 5601 | int err = -ENOMEM; |
| 5602 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5603 | if (md_allow_write(mddev)) |
| 5604 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5605 | else |
| 5606 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5607 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5608 | if (!file) |
| 5609 | goto out; |
| 5610 | |
| 5611 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5612 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5613 | file->pathname[0] = '\0'; |
| 5614 | goto copy_out; |
| 5615 | } |
| 5616 | |
| 5617 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5618 | if (!buf) |
| 5619 | goto out; |
| 5620 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5621 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5622 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5623 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5624 | goto out; |
| 5625 | |
| 5626 | strcpy(file->pathname, ptr); |
| 5627 | |
| 5628 | copy_out: |
| 5629 | err = 0; |
| 5630 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5631 | err = -EFAULT; |
| 5632 | out: |
| 5633 | kfree(buf); |
| 5634 | kfree(file); |
| 5635 | return err; |
| 5636 | } |
| 5637 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5638 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | { |
| 5640 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5641 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
| 5643 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5644 | return -EFAULT; |
| 5645 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5646 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | if (rdev) { |
| 5648 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5649 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5650 | info.raid_disk = rdev->raid_disk; |
| 5651 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5652 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5654 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5656 | info.state |= (1<<MD_DISK_SYNC); |
| 5657 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5658 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5659 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | } else { |
| 5661 | info.major = info.minor = 0; |
| 5662 | info.raid_disk = -1; |
| 5663 | info.state = (1<<MD_DISK_REMOVED); |
| 5664 | } |
| 5665 | |
| 5666 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5667 | return -EFAULT; |
| 5668 | |
| 5669 | return 0; |
| 5670 | } |
| 5671 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5672 | static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | { |
| 5674 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5675 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | dev_t dev = MKDEV(info->major,info->minor); |
| 5677 | |
| 5678 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5679 | return -EOVERFLOW; |
| 5680 | |
| 5681 | if (!mddev->raid_disks) { |
| 5682 | int err; |
| 5683 | /* expecting a device which has a superblock */ |
| 5684 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5685 | if (IS_ERR(rdev)) { |
| 5686 | printk(KERN_WARNING |
| 5687 | "md: md_import_device returned %ld\n", |
| 5688 | PTR_ERR(rdev)); |
| 5689 | return PTR_ERR(rdev); |
| 5690 | } |
| 5691 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5692 | struct md_rdev *rdev0 |
| 5693 | = list_entry(mddev->disks.next, |
| 5694 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5695 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5697 | if (err < 0) { |
| 5698 | printk(KERN_WARNING |
| 5699 | "md: %s has different UUID to %s\n", |
| 5700 | bdevname(rdev->bdev,b), |
| 5701 | bdevname(rdev0->bdev,b2)); |
| 5702 | export_rdev(rdev); |
| 5703 | return -EINVAL; |
| 5704 | } |
| 5705 | } |
| 5706 | err = bind_rdev_to_array(rdev, mddev); |
| 5707 | if (err) |
| 5708 | export_rdev(rdev); |
| 5709 | return err; |
| 5710 | } |
| 5711 | |
| 5712 | /* |
| 5713 | * add_new_disk can be used once the array is assembled |
| 5714 | * to add "hot spares". They must already have a superblock |
| 5715 | * written |
| 5716 | */ |
| 5717 | if (mddev->pers) { |
| 5718 | int err; |
| 5719 | if (!mddev->pers->hot_add_disk) { |
| 5720 | printk(KERN_WARNING |
| 5721 | "%s: personality does not support diskops!\n", |
| 5722 | mdname(mddev)); |
| 5723 | return -EINVAL; |
| 5724 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5725 | if (mddev->persistent) |
| 5726 | rdev = md_import_device(dev, mddev->major_version, |
| 5727 | mddev->minor_version); |
| 5728 | else |
| 5729 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | if (IS_ERR(rdev)) { |
| 5731 | printk(KERN_WARNING |
| 5732 | "md: md_import_device returned %ld\n", |
| 5733 | PTR_ERR(rdev)); |
| 5734 | return PTR_ERR(rdev); |
| 5735 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5736 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5737 | if (!mddev->persistent) { |
| 5738 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5739 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5740 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5741 | set_bit(In_sync, &rdev->flags); |
| 5742 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5743 | rdev->raid_disk = -1; |
| 5744 | } else |
| 5745 | super_types[mddev->major_version]. |
| 5746 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5747 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5748 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5749 | /* This was a hot-add request, but events doesn't |
| 5750 | * match, so reject it. |
| 5751 | */ |
| 5752 | export_rdev(rdev); |
| 5753 | return -EINVAL; |
| 5754 | } |
| 5755 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5756 | if (test_bit(In_sync, &rdev->flags)) |
| 5757 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5758 | else |
| 5759 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5760 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5761 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5762 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5763 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5764 | else |
| 5765 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | rdev->raid_disk = -1; |
| 5768 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5769 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5770 | /* If there is hot_add_disk but no hot_remove_disk |
| 5771 | * then added disks for geometry changes, |
| 5772 | * and should be added immediately. |
| 5773 | */ |
| 5774 | super_types[mddev->major_version]. |
| 5775 | validate_super(mddev, rdev); |
| 5776 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5777 | if (err) |
| 5778 | unbind_rdev_from_array(rdev); |
| 5779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | if (err) |
| 5781 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5782 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5783 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5784 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5785 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5786 | if (mddev->degraded) |
| 5787 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5788 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5789 | if (!err) |
| 5790 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5791 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | return err; |
| 5793 | } |
| 5794 | |
| 5795 | /* otherwise, add_new_disk is only allowed |
| 5796 | * for major_version==0 superblocks |
| 5797 | */ |
| 5798 | if (mddev->major_version != 0) { |
| 5799 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5800 | mdname(mddev)); |
| 5801 | return -EINVAL; |
| 5802 | } |
| 5803 | |
| 5804 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5805 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5806 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | if (IS_ERR(rdev)) { |
| 5808 | printk(KERN_WARNING |
| 5809 | "md: error, md_import_device() returned %ld\n", |
| 5810 | PTR_ERR(rdev)); |
| 5811 | return PTR_ERR(rdev); |
| 5812 | } |
| 5813 | rdev->desc_nr = info->number; |
| 5814 | if (info->raid_disk < mddev->raid_disks) |
| 5815 | rdev->raid_disk = info->raid_disk; |
| 5816 | else |
| 5817 | rdev->raid_disk = -1; |
| 5818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5820 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5821 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5823 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5824 | set_bit(WriteMostly, &rdev->flags); |
| 5825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | if (!mddev->persistent) { |
| 5827 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5828 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5829 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5830 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5831 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5833 | err = bind_rdev_to_array(rdev, mddev); |
| 5834 | if (err) { |
| 5835 | export_rdev(rdev); |
| 5836 | return err; |
| 5837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | return 0; |
| 5841 | } |
| 5842 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5843 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | { |
| 5845 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5846 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | rdev = find_rdev(mddev, dev); |
| 5849 | if (!rdev) |
| 5850 | return -ENXIO; |
| 5851 | |
| 5852 | if (rdev->raid_disk >= 0) |
| 5853 | goto busy; |
| 5854 | |
| 5855 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5856 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5857 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | |
| 5859 | return 0; |
| 5860 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5861 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5863 | return -EBUSY; |
| 5864 | } |
| 5865 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5866 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | { |
| 5868 | char b[BDEVNAME_SIZE]; |
| 5869 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5870 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | |
| 5872 | if (!mddev->pers) |
| 5873 | return -ENODEV; |
| 5874 | |
| 5875 | if (mddev->major_version != 0) { |
| 5876 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5877 | " version-0 superblocks.\n", |
| 5878 | mdname(mddev)); |
| 5879 | return -EINVAL; |
| 5880 | } |
| 5881 | if (!mddev->pers->hot_add_disk) { |
| 5882 | printk(KERN_WARNING |
| 5883 | "%s: personality does not support diskops!\n", |
| 5884 | mdname(mddev)); |
| 5885 | return -EINVAL; |
| 5886 | } |
| 5887 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5888 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | if (IS_ERR(rdev)) { |
| 5890 | printk(KERN_WARNING |
| 5891 | "md: error, md_import_device() returned %ld\n", |
| 5892 | PTR_ERR(rdev)); |
| 5893 | return -EINVAL; |
| 5894 | } |
| 5895 | |
| 5896 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5897 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5899 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5901 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5903 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | printk(KERN_WARNING |
| 5905 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5906 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5907 | err = -EINVAL; |
| 5908 | goto abort_export; |
| 5909 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5910 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5912 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5913 | err = bind_rdev_to_array(rdev, mddev); |
| 5914 | if (err) |
| 5915 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | |
| 5917 | /* |
| 5918 | * The rest should better be atomic, we can have disk failures |
| 5919 | * noticed in interrupt contexts ... |
| 5920 | */ |
| 5921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | rdev->raid_disk = -1; |
| 5923 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5924 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | |
| 5926 | /* |
| 5927 | * Kick recovery, maybe this spare has to be added to the |
| 5928 | * array immediately. |
| 5929 | */ |
| 5930 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5931 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5932 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | return 0; |
| 5934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | abort_export: |
| 5936 | export_rdev(rdev); |
| 5937 | return err; |
| 5938 | } |
| 5939 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5940 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5941 | { |
| 5942 | int err; |
| 5943 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5944 | if (mddev->pers) { |
| 5945 | if (!mddev->pers->quiesce) |
| 5946 | return -EBUSY; |
| 5947 | if (mddev->recovery || mddev->sync_thread) |
| 5948 | return -EBUSY; |
| 5949 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5950 | } |
| 5951 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5952 | |
| 5953 | if (fd >= 0) { |
| 5954 | if (mddev->bitmap) |
| 5955 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5956 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5957 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5958 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5959 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5960 | mdname(mddev)); |
| 5961 | return -EBADF; |
| 5962 | } |
| 5963 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5964 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5965 | if (err) { |
| 5966 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5967 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5968 | fput(mddev->bitmap_info.file); |
| 5969 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5970 | return err; |
| 5971 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5972 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5973 | } else if (mddev->bitmap == NULL) |
| 5974 | return -ENOENT; /* cannot remove what isn't there */ |
| 5975 | err = 0; |
| 5976 | if (mddev->pers) { |
| 5977 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5978 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5979 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5980 | if (!err) |
| 5981 | err = bitmap_load(mddev); |
| 5982 | } |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5983 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5984 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5985 | fd = -1; /* make sure to put the file */ |
| 5986 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5987 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5988 | } |
| 5989 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5990 | if (mddev->bitmap_info.file) { |
| 5991 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5992 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5993 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5994 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5995 | } |
| 5996 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5997 | return err; |
| 5998 | } |
| 5999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | /* |
| 6001 | * set_array_info is used two different ways |
| 6002 | * The original usage is when creating a new array. |
| 6003 | * In this usage, raid_disks is > 0 and it together with |
| 6004 | * level, size, not_persistent,layout,chunksize determine the |
| 6005 | * shape of the array. |
| 6006 | * This will always create an array with a type-0.90.0 superblock. |
| 6007 | * The newer usage is when assembling an array. |
| 6008 | * In this case raid_disks will be 0, and the major_version field is |
| 6009 | * use to determine which style super-blocks are to be found on the devices. |
| 6010 | * The minor and patch _version numbers are also kept incase the |
| 6011 | * super_block handler wishes to interpret them. |
| 6012 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6013 | static int set_array_info(struct mddev * mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | { |
| 6015 | |
| 6016 | if (info->raid_disks == 0) { |
| 6017 | /* just setting version number for superblock loading */ |
| 6018 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6019 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | super_types[info->major_version].name == NULL) { |
| 6021 | /* maybe try to auto-load a module? */ |
| 6022 | printk(KERN_INFO |
| 6023 | "md: superblock version %d not known\n", |
| 6024 | info->major_version); |
| 6025 | return -EINVAL; |
| 6026 | } |
| 6027 | mddev->major_version = info->major_version; |
| 6028 | mddev->minor_version = info->minor_version; |
| 6029 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6030 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6031 | /* ensure mddev_put doesn't delete this now that there |
| 6032 | * is some minimal configuration. |
| 6033 | */ |
| 6034 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | return 0; |
| 6036 | } |
| 6037 | mddev->major_version = MD_MAJOR_VERSION; |
| 6038 | mddev->minor_version = MD_MINOR_VERSION; |
| 6039 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6040 | mddev->ctime = get_seconds(); |
| 6041 | |
| 6042 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6043 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6044 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | mddev->raid_disks = info->raid_disks; |
| 6046 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6047 | * openned |
| 6048 | */ |
| 6049 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6050 | mddev->recovery_cp = MaxSector; |
| 6051 | else |
| 6052 | mddev->recovery_cp = 0; |
| 6053 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6054 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | |
| 6056 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6057 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | |
| 6059 | mddev->max_disks = MD_SB_DISKS; |
| 6060 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6061 | if (mddev->persistent) |
| 6062 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6063 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6065 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6066 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6067 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6068 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6069 | mddev->reshape_position = MaxSector; |
| 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | /* |
| 6072 | * Generate a 128 bit UUID |
| 6073 | */ |
| 6074 | get_random_bytes(mddev->uuid, 16); |
| 6075 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6076 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6077 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6078 | mddev->new_layout = mddev->layout; |
| 6079 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6080 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | return 0; |
| 6083 | } |
| 6084 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6085 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6086 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6087 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6088 | |
| 6089 | if (mddev->external_size) |
| 6090 | return; |
| 6091 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6092 | mddev->array_sectors = array_sectors; |
| 6093 | } |
| 6094 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6095 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6096 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6097 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6098 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6099 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6100 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6101 | |
| 6102 | if (mddev->pers->resize == NULL) |
| 6103 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6104 | /* The "num_sectors" is the number of sectors of each device that |
| 6105 | * is used. This can only make sense for arrays with redundancy. |
| 6106 | * linear and raid0 always use whatever space is available. We can only |
| 6107 | * consider changing this number if no resync or reconstruction is |
| 6108 | * 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] | 6109 | * 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] | 6110 | * of each device. If num_sectors is zero, we find the largest size |
| 6111 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6112 | */ |
| 6113 | if (mddev->sync_thread) |
| 6114 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6115 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6116 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6117 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6118 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6119 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6120 | num_sectors = avail; |
| 6121 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6122 | return -ENOSPC; |
| 6123 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6124 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6125 | if (!rv) |
| 6126 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6127 | return rv; |
| 6128 | } |
| 6129 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6130 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6131 | { |
| 6132 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6133 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6134 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6135 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6136 | return -EINVAL; |
| 6137 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6138 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6139 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6140 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6141 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6142 | |
| 6143 | rdev_for_each(rdev, mddev) { |
| 6144 | if (mddev->raid_disks < raid_disks && |
| 6145 | rdev->data_offset < rdev->new_data_offset) |
| 6146 | return -EINVAL; |
| 6147 | if (mddev->raid_disks > raid_disks && |
| 6148 | rdev->data_offset > rdev->new_data_offset) |
| 6149 | return -EINVAL; |
| 6150 | } |
| 6151 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6152 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6153 | if (mddev->delta_disks < 0) |
| 6154 | mddev->reshape_backwards = 1; |
| 6155 | else if (mddev->delta_disks > 0) |
| 6156 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6157 | |
| 6158 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6159 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6160 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6161 | mddev->reshape_backwards = 0; |
| 6162 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6163 | return rv; |
| 6164 | } |
| 6165 | |
| 6166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | /* |
| 6168 | * update_array_info is used to change the configuration of an |
| 6169 | * on-line array. |
| 6170 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6171 | * fields in the info are checked against the array. |
| 6172 | * Any differences that cannot be handled will cause an error. |
| 6173 | * Normally, only one change can be managed at a time. |
| 6174 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6175 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | { |
| 6177 | int rv = 0; |
| 6178 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6179 | int state = 0; |
| 6180 | |
| 6181 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6182 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6183 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | |
| 6185 | if (mddev->major_version != info->major_version || |
| 6186 | mddev->minor_version != info->minor_version || |
| 6187 | /* mddev->patch_version != info->patch_version || */ |
| 6188 | mddev->ctime != info->ctime || |
| 6189 | mddev->level != info->level || |
| 6190 | /* mddev->layout != info->layout || */ |
| 6191 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6192 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6193 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6194 | ((state^info->state) & 0xfffffe00) |
| 6195 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | return -EINVAL; |
| 6197 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6198 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6199 | cnt++; |
| 6200 | if (mddev->raid_disks != info->raid_disks) |
| 6201 | cnt++; |
| 6202 | if (mddev->layout != info->layout) |
| 6203 | cnt++; |
| 6204 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6205 | cnt++; |
| 6206 | if (cnt == 0) |
| 6207 | return 0; |
| 6208 | if (cnt > 1) |
| 6209 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | |
| 6211 | if (mddev->layout != info->layout) { |
| 6212 | /* Change layout |
| 6213 | * we don't need to do anything at the md level, the |
| 6214 | * personality will take care of it all. |
| 6215 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6216 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6218 | else { |
| 6219 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6220 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6221 | if (rv) |
| 6222 | mddev->new_layout = mddev->layout; |
| 6223 | return rv; |
| 6224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6226 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6227 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6229 | if (mddev->raid_disks != info->raid_disks) |
| 6230 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6232 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6233 | if (mddev->pers->quiesce == NULL) |
| 6234 | return -EINVAL; |
| 6235 | if (mddev->recovery || mddev->sync_thread) |
| 6236 | return -EBUSY; |
| 6237 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6238 | /* add the bitmap */ |
| 6239 | if (mddev->bitmap) |
| 6240 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6241 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6242 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6243 | mddev->bitmap_info.offset = |
| 6244 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6245 | mddev->bitmap_info.space = |
| 6246 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6247 | mddev->pers->quiesce(mddev, 1); |
| 6248 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6249 | if (!rv) |
| 6250 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6251 | if (rv) |
| 6252 | bitmap_destroy(mddev); |
| 6253 | mddev->pers->quiesce(mddev, 0); |
| 6254 | } else { |
| 6255 | /* remove the bitmap */ |
| 6256 | if (!mddev->bitmap) |
| 6257 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6258 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6259 | return -EINVAL; |
| 6260 | mddev->pers->quiesce(mddev, 1); |
| 6261 | bitmap_destroy(mddev); |
| 6262 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6263 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6264 | } |
| 6265 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6266 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | return rv; |
| 6268 | } |
| 6269 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6270 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6272 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | |
| 6274 | if (mddev->pers == NULL) |
| 6275 | return -ENODEV; |
| 6276 | |
| 6277 | rdev = find_rdev(mddev, dev); |
| 6278 | if (!rdev) |
| 6279 | return -ENODEV; |
| 6280 | |
| 6281 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6282 | if (!test_bit(Faulty, &rdev->flags)) |
| 6283 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | return 0; |
| 6285 | } |
| 6286 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6287 | /* |
| 6288 | * We have a problem here : there is no easy way to give a CHS |
| 6289 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6290 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6291 | * dosfs just mad... ;-) |
| 6292 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6293 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6294 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6295 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6296 | |
| 6297 | geo->heads = 2; |
| 6298 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6299 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6300 | return 0; |
| 6301 | } |
| 6302 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6303 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | unsigned int cmd, unsigned long arg) |
| 6305 | { |
| 6306 | int err = 0; |
| 6307 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6308 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6309 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6311 | switch (cmd) { |
| 6312 | case RAID_VERSION: |
| 6313 | case GET_ARRAY_INFO: |
| 6314 | case GET_DISK_INFO: |
| 6315 | break; |
| 6316 | default: |
| 6317 | if (!capable(CAP_SYS_ADMIN)) |
| 6318 | return -EACCES; |
| 6319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | |
| 6321 | /* |
| 6322 | * Commands dealing with the RAID driver but not any |
| 6323 | * particular array: |
| 6324 | */ |
| 6325 | switch (cmd) |
| 6326 | { |
| 6327 | case RAID_VERSION: |
| 6328 | err = get_version(argp); |
| 6329 | goto done; |
| 6330 | |
| 6331 | case PRINT_RAID_DEBUG: |
| 6332 | err = 0; |
| 6333 | md_print_devices(); |
| 6334 | goto done; |
| 6335 | |
| 6336 | #ifndef MODULE |
| 6337 | case RAID_AUTORUN: |
| 6338 | err = 0; |
| 6339 | autostart_arrays(arg); |
| 6340 | goto done; |
| 6341 | #endif |
| 6342 | default:; |
| 6343 | } |
| 6344 | |
| 6345 | /* |
| 6346 | * Commands creating/starting a new array: |
| 6347 | */ |
| 6348 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6349 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | |
| 6351 | if (!mddev) { |
| 6352 | BUG(); |
| 6353 | goto abort; |
| 6354 | } |
| 6355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | err = mddev_lock(mddev); |
| 6357 | if (err) { |
| 6358 | printk(KERN_INFO |
| 6359 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6360 | err, cmd); |
| 6361 | goto abort; |
| 6362 | } |
| 6363 | |
| 6364 | switch (cmd) |
| 6365 | { |
| 6366 | case SET_ARRAY_INFO: |
| 6367 | { |
| 6368 | mdu_array_info_t info; |
| 6369 | if (!arg) |
| 6370 | memset(&info, 0, sizeof(info)); |
| 6371 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6372 | err = -EFAULT; |
| 6373 | goto abort_unlock; |
| 6374 | } |
| 6375 | if (mddev->pers) { |
| 6376 | err = update_array_info(mddev, &info); |
| 6377 | if (err) { |
| 6378 | printk(KERN_WARNING "md: couldn't update" |
| 6379 | " array info. %d\n", err); |
| 6380 | goto abort_unlock; |
| 6381 | } |
| 6382 | goto done_unlock; |
| 6383 | } |
| 6384 | if (!list_empty(&mddev->disks)) { |
| 6385 | printk(KERN_WARNING |
| 6386 | "md: array %s already has disks!\n", |
| 6387 | mdname(mddev)); |
| 6388 | err = -EBUSY; |
| 6389 | goto abort_unlock; |
| 6390 | } |
| 6391 | if (mddev->raid_disks) { |
| 6392 | printk(KERN_WARNING |
| 6393 | "md: array %s already initialised!\n", |
| 6394 | mdname(mddev)); |
| 6395 | err = -EBUSY; |
| 6396 | goto abort_unlock; |
| 6397 | } |
| 6398 | err = set_array_info(mddev, &info); |
| 6399 | if (err) { |
| 6400 | printk(KERN_WARNING "md: couldn't set" |
| 6401 | " array info. %d\n", err); |
| 6402 | goto abort_unlock; |
| 6403 | } |
| 6404 | } |
| 6405 | goto done_unlock; |
| 6406 | |
| 6407 | default:; |
| 6408 | } |
| 6409 | |
| 6410 | /* |
| 6411 | * Commands querying/configuring an existing array: |
| 6412 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6413 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6414 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6415 | if ((!mddev->raid_disks && !mddev->external) |
| 6416 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6417 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6418 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | err = -ENODEV; |
| 6420 | goto abort_unlock; |
| 6421 | } |
| 6422 | |
| 6423 | /* |
| 6424 | * Commands even a read-only array can execute: |
| 6425 | */ |
| 6426 | switch (cmd) |
| 6427 | { |
| 6428 | case GET_ARRAY_INFO: |
| 6429 | err = get_array_info(mddev, argp); |
| 6430 | goto done_unlock; |
| 6431 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6432 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6433 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6434 | goto done_unlock; |
| 6435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | case GET_DISK_INFO: |
| 6437 | err = get_disk_info(mddev, argp); |
| 6438 | goto done_unlock; |
| 6439 | |
| 6440 | case RESTART_ARRAY_RW: |
| 6441 | err = restart_array(mddev); |
| 6442 | goto done_unlock; |
| 6443 | |
| 6444 | case STOP_ARRAY: |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6445 | err = do_md_stop(mddev, 0, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | goto done_unlock; |
| 6447 | |
| 6448 | case STOP_ARRAY_RO: |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6449 | err = md_set_readonly(mddev, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | goto done_unlock; |
| 6451 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6452 | case BLKROSET: |
| 6453 | if (get_user(ro, (int __user *)(arg))) { |
| 6454 | err = -EFAULT; |
| 6455 | goto done_unlock; |
| 6456 | } |
| 6457 | err = -EINVAL; |
| 6458 | |
| 6459 | /* if the bdev is going readonly the value of mddev->ro |
| 6460 | * does not matter, no writes are coming |
| 6461 | */ |
| 6462 | if (ro) |
| 6463 | goto done_unlock; |
| 6464 | |
| 6465 | /* are we are already prepared for writes? */ |
| 6466 | if (mddev->ro != 1) |
| 6467 | goto done_unlock; |
| 6468 | |
| 6469 | /* transitioning to readauto need only happen for |
| 6470 | * arrays that call md_write_start |
| 6471 | */ |
| 6472 | if (mddev->pers) { |
| 6473 | err = restart_array(mddev); |
| 6474 | if (err == 0) { |
| 6475 | mddev->ro = 2; |
| 6476 | set_disk_ro(mddev->gendisk, 0); |
| 6477 | } |
| 6478 | } |
| 6479 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | /* |
| 6483 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6484 | * superblock, so we do not allow them on read-only arrays. |
| 6485 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6486 | * here and hit the 'default' below, so only disallow |
| 6487 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6489 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6490 | if (mddev->ro == 2) { |
| 6491 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6492 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6493 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6494 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6495 | } else { |
| 6496 | err = -EROFS; |
| 6497 | goto abort_unlock; |
| 6498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | switch (cmd) |
| 6502 | { |
| 6503 | case ADD_NEW_DISK: |
| 6504 | { |
| 6505 | mdu_disk_info_t info; |
| 6506 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6507 | err = -EFAULT; |
| 6508 | else |
| 6509 | err = add_new_disk(mddev, &info); |
| 6510 | goto done_unlock; |
| 6511 | } |
| 6512 | |
| 6513 | case HOT_REMOVE_DISK: |
| 6514 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6515 | goto done_unlock; |
| 6516 | |
| 6517 | case HOT_ADD_DISK: |
| 6518 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6519 | goto done_unlock; |
| 6520 | |
| 6521 | case SET_DISK_FAULTY: |
| 6522 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6523 | goto done_unlock; |
| 6524 | |
| 6525 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6526 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | goto done_unlock; |
| 6528 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6529 | case SET_BITMAP_FILE: |
| 6530 | err = set_bitmap_file(mddev, (int)arg); |
| 6531 | goto done_unlock; |
| 6532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | err = -EINVAL; |
| 6535 | goto abort_unlock; |
| 6536 | } |
| 6537 | |
| 6538 | done_unlock: |
| 6539 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6540 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6541 | err != -EINVAL) |
| 6542 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | mddev_unlock(mddev); |
| 6544 | |
| 6545 | return err; |
| 6546 | done: |
| 6547 | if (err) |
| 6548 | MD_BUG(); |
| 6549 | abort: |
| 6550 | return err; |
| 6551 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6552 | #ifdef CONFIG_COMPAT |
| 6553 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6554 | unsigned int cmd, unsigned long arg) |
| 6555 | { |
| 6556 | switch (cmd) { |
| 6557 | case HOT_REMOVE_DISK: |
| 6558 | case HOT_ADD_DISK: |
| 6559 | case SET_DISK_FAULTY: |
| 6560 | case SET_BITMAP_FILE: |
| 6561 | /* These take in integer arg, do not convert */ |
| 6562 | break; |
| 6563 | default: |
| 6564 | arg = (unsigned long)compat_ptr(arg); |
| 6565 | break; |
| 6566 | } |
| 6567 | |
| 6568 | return md_ioctl(bdev, mode, cmd, arg); |
| 6569 | } |
| 6570 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6571 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6572 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | { |
| 6574 | /* |
| 6575 | * Succeed if we can lock the mddev, which confirms that |
| 6576 | * it isn't being stopped right now. |
| 6577 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6578 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | int err; |
| 6580 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6581 | if (!mddev) |
| 6582 | return -ENODEV; |
| 6583 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6584 | if (mddev->gendisk != bdev->bd_disk) { |
| 6585 | /* we are racing with mddev_put which is discarding this |
| 6586 | * bd_disk. |
| 6587 | */ |
| 6588 | mddev_put(mddev); |
| 6589 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6590 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6591 | /* Then retry the open from the top */ |
| 6592 | return -ERESTARTSYS; |
| 6593 | } |
| 6594 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6595 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6596 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | goto out; |
| 6598 | |
| 6599 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6600 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6601 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6603 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | out: |
| 6605 | return err; |
| 6606 | } |
| 6607 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6608 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6610 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6612 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6613 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | mddev_put(mddev); |
| 6615 | |
| 6616 | return 0; |
| 6617 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6618 | |
| 6619 | static int md_media_changed(struct gendisk *disk) |
| 6620 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6621 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6622 | |
| 6623 | return mddev->changed; |
| 6624 | } |
| 6625 | |
| 6626 | static int md_revalidate(struct gendisk *disk) |
| 6627 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6628 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6629 | |
| 6630 | mddev->changed = 0; |
| 6631 | return 0; |
| 6632 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6633 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | { |
| 6635 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6636 | .open = md_open, |
| 6637 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6638 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6639 | #ifdef CONFIG_COMPAT |
| 6640 | .compat_ioctl = md_compat_ioctl, |
| 6641 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6642 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6643 | .media_changed = md_media_changed, |
| 6644 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | }; |
| 6646 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6647 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6649 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | /* |
| 6652 | * md_thread is a 'system-thread', it's priority should be very |
| 6653 | * high. We avoid resource deadlocks individually in each |
| 6654 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6655 | * the very same RT priority as kswapd, thus we will never get |
| 6656 | * into a priority inversion deadlock. |
| 6657 | * |
| 6658 | * we definitely have to have equal or higher priority than |
| 6659 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6660 | * many dirty RAID5 blocks. |
| 6661 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6663 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6664 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6666 | /* We need to wait INTERRUPTIBLE so that |
| 6667 | * we don't add to the load-average. |
| 6668 | * That means we need to be sure no signals are |
| 6669 | * pending |
| 6670 | */ |
| 6671 | if (signal_pending(current)) |
| 6672 | flush_signals(current); |
| 6673 | |
| 6674 | wait_event_interruptible_timeout |
| 6675 | (thread->wqueue, |
| 6676 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6677 | || kthread_should_stop(), |
| 6678 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6680 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6681 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6682 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | return 0; |
| 6686 | } |
| 6687 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6688 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6689 | { |
| 6690 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6691 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6693 | wake_up(&thread->wqueue); |
| 6694 | } |
| 6695 | } |
| 6696 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6697 | struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | const char *name) |
| 6699 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6700 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6702 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | if (!thread) |
| 6704 | return NULL; |
| 6705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | init_waitqueue_head(&thread->wqueue); |
| 6707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | thread->run = run; |
| 6709 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6710 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6711 | thread->tsk = kthread_run(md_thread, thread, |
| 6712 | "%s_%s", |
| 6713 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6714 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6715 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | kfree(thread); |
| 6717 | return NULL; |
| 6718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | return thread; |
| 6720 | } |
| 6721 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6722 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6724 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6725 | if (!thread) |
| 6726 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6727 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6728 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6729 | * non-existent thread |
| 6730 | */ |
| 6731 | spin_lock(&pers_lock); |
| 6732 | *threadp = NULL; |
| 6733 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6734 | |
| 6735 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | kfree(thread); |
| 6737 | } |
| 6738 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6739 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | { |
| 6741 | if (!mddev) { |
| 6742 | MD_BUG(); |
| 6743 | return; |
| 6744 | } |
| 6745 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6746 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6748 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6749 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | return; |
| 6751 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6752 | if (mddev->degraded) |
| 6753 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6754 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6756 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6757 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6758 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6759 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6760 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | /* seq_file implementation /proc/mdstat */ |
| 6764 | |
| 6765 | static void status_unused(struct seq_file *seq) |
| 6766 | { |
| 6767 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6768 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | |
| 6770 | seq_printf(seq, "unused devices: "); |
| 6771 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6772 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | char b[BDEVNAME_SIZE]; |
| 6774 | i++; |
| 6775 | seq_printf(seq, "%s ", |
| 6776 | bdevname(rdev->bdev,b)); |
| 6777 | } |
| 6778 | if (!i) |
| 6779 | seq_printf(seq, "<none>"); |
| 6780 | |
| 6781 | seq_printf(seq, "\n"); |
| 6782 | } |
| 6783 | |
| 6784 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6785 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6787 | sector_t max_sectors, resync, res; |
| 6788 | unsigned long dt, db; |
| 6789 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6790 | int scale; |
| 6791 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6793 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6795 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6796 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6797 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6799 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | |
| 6801 | /* |
| 6802 | * Should not happen. |
| 6803 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6804 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | MD_BUG(); |
| 6806 | return; |
| 6807 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6808 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6809 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6810 | * u32, as those are the requirements for sector_div. |
| 6811 | * Thus 'scale' must be at least 10 |
| 6812 | */ |
| 6813 | scale = 10; |
| 6814 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6815 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6816 | scale++; |
| 6817 | } |
| 6818 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6819 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6820 | |
| 6821 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6823 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | seq_printf(seq, "["); |
| 6825 | for (i = 0; i < x; i++) |
| 6826 | seq_printf(seq, "="); |
| 6827 | seq_printf(seq, ">"); |
| 6828 | for (i = 0; i < y; i++) |
| 6829 | seq_printf(seq, "."); |
| 6830 | seq_printf(seq, "] "); |
| 6831 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6832 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6833 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6834 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6835 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6836 | "check" : |
| 6837 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6838 | "resync" : "recovery"))), |
| 6839 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6840 | (unsigned long long) resync/2, |
| 6841 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | |
| 6843 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | * dt: time from mark until now |
| 6845 | * db: blocks written from mark until now |
| 6846 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6847 | * |
| 6848 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6849 | * So we divide before multiply in case it is 32bit and close |
| 6850 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6851 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6852 | * near the end of resync when the number of remaining sectors |
| 6853 | * is close to 'db'. |
| 6854 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6855 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | */ |
| 6857 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6858 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6859 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6860 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6862 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6863 | sector_div(rt, db/32+1); |
| 6864 | rt *= dt; |
| 6865 | rt >>= 5; |
| 6866 | |
| 6867 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6868 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6870 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6874 | { |
| 6875 | struct list_head *tmp; |
| 6876 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6877 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | |
| 6879 | if (l >= 0x10000) |
| 6880 | return NULL; |
| 6881 | if (!l--) |
| 6882 | /* header */ |
| 6883 | return (void*)1; |
| 6884 | |
| 6885 | spin_lock(&all_mddevs_lock); |
| 6886 | list_for_each(tmp,&all_mddevs) |
| 6887 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6888 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | mddev_get(mddev); |
| 6890 | spin_unlock(&all_mddevs_lock); |
| 6891 | return mddev; |
| 6892 | } |
| 6893 | spin_unlock(&all_mddevs_lock); |
| 6894 | if (!l--) |
| 6895 | return (void*)2;/* tail */ |
| 6896 | return NULL; |
| 6897 | } |
| 6898 | |
| 6899 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6900 | { |
| 6901 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6902 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | |
| 6904 | ++*pos; |
| 6905 | if (v == (void*)2) |
| 6906 | return NULL; |
| 6907 | |
| 6908 | spin_lock(&all_mddevs_lock); |
| 6909 | if (v == (void*)1) |
| 6910 | tmp = all_mddevs.next; |
| 6911 | else |
| 6912 | tmp = mddev->all_mddevs.next; |
| 6913 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6914 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | else { |
| 6916 | next_mddev = (void*)2; |
| 6917 | *pos = 0x10000; |
| 6918 | } |
| 6919 | spin_unlock(&all_mddevs_lock); |
| 6920 | |
| 6921 | if (v != (void*)1) |
| 6922 | mddev_put(mddev); |
| 6923 | return next_mddev; |
| 6924 | |
| 6925 | } |
| 6926 | |
| 6927 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6928 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6929 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | |
| 6931 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6932 | mddev_put(mddev); |
| 6933 | } |
| 6934 | |
| 6935 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6936 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6937 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6938 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6939 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | |
| 6941 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6942 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | seq_printf(seq, "Personalities : "); |
| 6944 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6945 | list_for_each_entry(pers, &pers_list, list) |
| 6946 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | |
| 6948 | spin_unlock(&pers_lock); |
| 6949 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6950 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | return 0; |
| 6952 | } |
| 6953 | if (v == (void*)2) { |
| 6954 | status_unused(seq); |
| 6955 | return 0; |
| 6956 | } |
| 6957 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6958 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6962 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6963 | mddev->pers ? "" : "in"); |
| 6964 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6965 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6967 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6968 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | seq_printf(seq, " %s", mddev->pers->name); |
| 6970 | } |
| 6971 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6972 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6973 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | char b[BDEVNAME_SIZE]; |
| 6975 | seq_printf(seq, " %s[%d]", |
| 6976 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6977 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6978 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6979 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | seq_printf(seq, "(F)"); |
| 6981 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6982 | } |
| 6983 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6984 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6985 | if (test_bit(Replacement, &rdev->flags)) |
| 6986 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6987 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | if (!list_empty(&mddev->disks)) { |
| 6991 | if (mddev->pers) |
| 6992 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6993 | (unsigned long long) |
| 6994 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6995 | else |
| 6996 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6997 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6999 | if (mddev->persistent) { |
| 7000 | if (mddev->major_version != 0 || |
| 7001 | mddev->minor_version != 90) { |
| 7002 | seq_printf(seq," super %d.%d", |
| 7003 | mddev->major_version, |
| 7004 | mddev->minor_version); |
| 7005 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7006 | } else if (mddev->external) |
| 7007 | seq_printf(seq, " super external:%s", |
| 7008 | mddev->metadata_type); |
| 7009 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7010 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7011 | |
| 7012 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7013 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7015 | if (mddev->pers->sync_request) { |
| 7016 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7017 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7018 | seq_printf(seq, "\n "); |
| 7019 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 7020 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7021 | else if (mddev->recovery_cp < MaxSector) |
| 7022 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7023 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7024 | } else |
| 7025 | seq_printf(seq, "\n "); |
| 7026 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7027 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | |
| 7029 | seq_printf(seq, "\n"); |
| 7030 | } |
| 7031 | mddev_unlock(mddev); |
| 7032 | |
| 7033 | return 0; |
| 7034 | } |
| 7035 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7036 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | .start = md_seq_start, |
| 7038 | .next = md_seq_next, |
| 7039 | .stop = md_seq_stop, |
| 7040 | .show = md_seq_show, |
| 7041 | }; |
| 7042 | |
| 7043 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7044 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7045 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | int error; |
| 7047 | |
| 7048 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7049 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7050 | return error; |
| 7051 | |
| 7052 | seq = file->private_data; |
| 7053 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | return error; |
| 7055 | } |
| 7056 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7057 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7058 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7059 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7060 | int mask; |
| 7061 | |
| 7062 | poll_wait(filp, &md_event_waiters, wait); |
| 7063 | |
| 7064 | /* always allow read */ |
| 7065 | mask = POLLIN | POLLRDNORM; |
| 7066 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7067 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7068 | mask |= POLLERR | POLLPRI; |
| 7069 | return mask; |
| 7070 | } |
| 7071 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7072 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7073 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | .open = md_seq_open, |
| 7075 | .read = seq_read, |
| 7076 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7077 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7078 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | }; |
| 7080 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7081 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7082 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7083 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7084 | list_add_tail(&p->list, &pers_list); |
| 7085 | 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] | 7086 | spin_unlock(&pers_lock); |
| 7087 | return 0; |
| 7088 | } |
| 7089 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7090 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7092 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7094 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | spin_unlock(&pers_lock); |
| 7096 | return 0; |
| 7097 | } |
| 7098 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7099 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7101 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7103 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | |
| 7105 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7106 | rcu_read_lock(); |
| 7107 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7109 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7110 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7111 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7112 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7113 | * as sync_io is counted when a request starts, and |
| 7114 | * disk_stats is counted when it completes. |
| 7115 | * So resync activity will cause curr_events to be smaller than |
| 7116 | * when there was no such activity. |
| 7117 | * non-sync IO will cause disk_stat to increase without |
| 7118 | * increasing sync_io so curr_events will (eventually) |
| 7119 | * be larger than it was before. Once it becomes |
| 7120 | * substantially larger, the test below will cause |
| 7121 | * the array to appear non-idle, and resync will slow |
| 7122 | * down. |
| 7123 | * If there is a lot of outstanding resync activity when |
| 7124 | * we set last_event to curr_events, then all that activity |
| 7125 | * completing might cause the array to appear non-idle |
| 7126 | * and resync will be slowed down even though there might |
| 7127 | * not have been non-resync activity. This will only |
| 7128 | * happen once though. 'last_events' will soon reflect |
| 7129 | * the state where there is little or no outstanding |
| 7130 | * resync requests, and further resync activity will |
| 7131 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7132 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7134 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | rdev->last_events = curr_events; |
| 7136 | idle = 0; |
| 7137 | } |
| 7138 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7139 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | return idle; |
| 7141 | } |
| 7142 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7143 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7144 | { |
| 7145 | /* another "blocks" (512byte) blocks have been synced */ |
| 7146 | atomic_sub(blocks, &mddev->recovery_active); |
| 7147 | wake_up(&mddev->recovery_wait); |
| 7148 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7149 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | md_wakeup_thread(mddev->thread); |
| 7151 | // stop recovery, signal do_sync .... |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7156 | /* md_write_start(mddev, bi) |
| 7157 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7158 | * in superblock) before writing, schedule a superblock update |
| 7159 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7160 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7161 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7163 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7164 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7165 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7166 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7167 | BUG_ON(mddev->ro == 1); |
| 7168 | if (mddev->ro == 2) { |
| 7169 | /* need to switch to read/write */ |
| 7170 | mddev->ro = 0; |
| 7171 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7172 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7173 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7174 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7175 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7176 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7177 | if (mddev->safemode == 1) |
| 7178 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7179 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7180 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7181 | if (mddev->in_sync) { |
| 7182 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7183 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7184 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7185 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7186 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7187 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7188 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7189 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7190 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7191 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7192 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7193 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | } |
| 7195 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7196 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | { |
| 7198 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7199 | if (mddev->safemode == 2) |
| 7200 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7201 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7203 | } |
| 7204 | } |
| 7205 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7206 | /* md_allow_write(mddev) |
| 7207 | * Calling this ensures that the array is marked 'active' so that writes |
| 7208 | * may proceed without blocking. It is important to call this before |
| 7209 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7210 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7211 | * |
| 7212 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7213 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7214 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7215 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7216 | { |
| 7217 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7218 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7219 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7220 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7221 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7222 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7223 | |
| 7224 | spin_lock_irq(&mddev->write_lock); |
| 7225 | if (mddev->in_sync) { |
| 7226 | mddev->in_sync = 0; |
| 7227 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7228 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7229 | if (mddev->safemode_delay && |
| 7230 | mddev->safemode == 0) |
| 7231 | mddev->safemode = 1; |
| 7232 | spin_unlock_irq(&mddev->write_lock); |
| 7233 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7234 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7235 | } else |
| 7236 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7237 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7238 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7239 | return -EAGAIN; |
| 7240 | else |
| 7241 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7242 | } |
| 7243 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | #define SYNC_MARKS 10 |
| 7246 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7247 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7249 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | unsigned int currspeed = 0, |
| 7251 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7252 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | unsigned long mark[SYNC_MARKS]; |
| 7254 | sector_t mark_cnt[SYNC_MARKS]; |
| 7255 | int last_mark,m; |
| 7256 | struct list_head *tmp; |
| 7257 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7258 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7259 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7260 | char *desc; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7261 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | |
| 7263 | /* just incase thread restarts... */ |
| 7264 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7265 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7266 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7267 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7269 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7270 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7271 | desc = "data-check"; |
| 7272 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7273 | desc = "requested-resync"; |
| 7274 | else |
| 7275 | desc = "resync"; |
| 7276 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7277 | desc = "reshape"; |
| 7278 | else |
| 7279 | desc = "recovery"; |
| 7280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | /* we overload curr_resync somewhat here. |
| 7282 | * 0 == not engaged in resync at all |
| 7283 | * 2 == checking that there is no conflict with another sync |
| 7284 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7285 | * commense |
| 7286 | * other == active in resync - this many blocks |
| 7287 | * |
| 7288 | * Before starting a resync we must have set curr_resync to |
| 7289 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7290 | * less than ours. When we find one that is the same or higher |
| 7291 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7292 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7293 | * This will mean we have to start checking from the beginning again. |
| 7294 | * |
| 7295 | */ |
| 7296 | |
| 7297 | do { |
| 7298 | mddev->curr_resync = 2; |
| 7299 | |
| 7300 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7301 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7302 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7303 | |
| 7304 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7306 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | if (mddev2 == mddev) |
| 7308 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7309 | if (!mddev->parallel_resync |
| 7310 | && mddev2->curr_resync |
| 7311 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | DEFINE_WAIT(wq); |
| 7313 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7314 | /* arbitrarily yield */ |
| 7315 | mddev->curr_resync = 1; |
| 7316 | wake_up(&resync_wait); |
| 7317 | } |
| 7318 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7319 | /* no need to wait here, we can wait the next |
| 7320 | * time 'round when curr_resync == 2 |
| 7321 | */ |
| 7322 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7323 | /* We need to wait 'interruptible' so as not to |
| 7324 | * contribute to the load average, and not to |
| 7325 | * be caught by 'softlockup' |
| 7326 | */ |
| 7327 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7328 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7329 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7330 | printk(KERN_INFO "md: delaying %s of %s" |
| 7331 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7333 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7335 | if (signal_pending(current)) |
| 7336 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | schedule(); |
| 7338 | finish_wait(&resync_wait, &wq); |
| 7339 | goto try_again; |
| 7340 | } |
| 7341 | finish_wait(&resync_wait, &wq); |
| 7342 | } |
| 7343 | } |
| 7344 | } while (mddev->curr_resync < 2); |
| 7345 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7346 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7347 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7349 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7350 | */ |
| 7351 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7352 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7353 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7354 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7355 | j = mddev->resync_min; |
| 7356 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7357 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7358 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7359 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7360 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7361 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7363 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7364 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7365 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7366 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7367 | if (rdev->raid_disk >= 0 && |
| 7368 | !test_bit(Faulty, &rdev->flags) && |
| 7369 | !test_bit(In_sync, &rdev->flags) && |
| 7370 | rdev->recovery_offset < j) |
| 7371 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7372 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7375 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7376 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7377 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7378 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7379 | "(but not more than %d KB/sec) for %s.\n", |
| 7380 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7382 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7383 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7384 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7386 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7387 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7388 | } |
| 7389 | last_mark = 0; |
| 7390 | mddev->resync_mark = mark[last_mark]; |
| 7391 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7392 | |
| 7393 | /* |
| 7394 | * Tune reconstruction: |
| 7395 | */ |
| 7396 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7397 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7398 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
| 7400 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7401 | last_check = 0; |
| 7402 | |
| 7403 | if (j>2) { |
| 7404 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7405 | "md: resuming %s of %s from checkpoint.\n", |
| 7406 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | mddev->curr_resync = j; |
| 7408 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7409 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7411 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7413 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7415 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7416 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7417 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7418 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7419 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7420 | > (max_sectors >> 4)) || |
| 7421 | (j - mddev->curr_resync_completed)*2 |
| 7422 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7423 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7424 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7425 | wait_event(mddev->recovery_wait, |
| 7426 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7427 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7428 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7429 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7430 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7431 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7432 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7433 | /* As this condition is controlled by user-space, |
| 7434 | * we can block indefinitely, so use '_interruptible' |
| 7435 | * to avoid triggering warnings. |
| 7436 | */ |
| 7437 | flush_signals(current); /* just in case */ |
| 7438 | wait_event_interruptible(mddev->recovery_wait, |
| 7439 | mddev->resync_max > j |
| 7440 | || kthread_should_stop()); |
| 7441 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7442 | |
| 7443 | if (kthread_should_stop()) |
| 7444 | goto interrupted; |
| 7445 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7446 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7447 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7448 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7449 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7450 | goto out; |
| 7451 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7452 | |
| 7453 | if (!skipped) { /* actual IO requested */ |
| 7454 | io_sectors += sectors; |
| 7455 | atomic_add(sectors, &mddev->recovery_active); |
| 7456 | } |
| 7457 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7458 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7459 | break; |
| 7460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7461 | j += sectors; |
| 7462 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7463 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7464 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7465 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7466 | * visible in /proc/mdstat |
| 7467 | */ |
| 7468 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7469 | |
| 7470 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | continue; |
| 7472 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7473 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | repeat: |
| 7475 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7476 | /* step marks */ |
| 7477 | int next = (last_mark+1) % SYNC_MARKS; |
| 7478 | |
| 7479 | mddev->resync_mark = mark[next]; |
| 7480 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7481 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7482 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7483 | last_mark = next; |
| 7484 | } |
| 7485 | |
| 7486 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7487 | if (kthread_should_stop()) |
| 7488 | goto interrupted; |
| 7489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | |
| 7491 | /* |
| 7492 | * this loop exits only if either when we are slower than |
| 7493 | * the 'hard' speed limit, or the system was IO-idle for |
| 7494 | * a jiffy. |
| 7495 | * the system might be non-idle CPU-wise, but we only care |
| 7496 | * about not overloading the IO subsystem. (things like an |
| 7497 | * e2fsck being done on the RAID array should execute fast) |
| 7498 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | cond_resched(); |
| 7500 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7501 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7502 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7504 | if (currspeed > speed_min(mddev)) { |
| 7505 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7506 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7507 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7508 | goto repeat; |
| 7509 | } |
| 7510 | } |
| 7511 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7512 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | /* |
| 7514 | * this also signals 'finished resyncing' to md_stop |
| 7515 | */ |
| 7516 | out: |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7517 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7519 | |
| 7520 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7521 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7523 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7524 | mddev->curr_resync > 2) { |
| 7525 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7526 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7527 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7528 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7529 | "md: checkpointing %s of %s.\n", |
| 7530 | desc, mdname(mddev)); |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7531 | mddev->recovery_cp = |
| 7532 | mddev->curr_resync_completed; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7533 | } |
| 7534 | } else |
| 7535 | mddev->recovery_cp = MaxSector; |
| 7536 | } else { |
| 7537 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7538 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7539 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7540 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7541 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7542 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7543 | !test_bit(Faulty, &rdev->flags) && |
| 7544 | !test_bit(In_sync, &rdev->flags) && |
| 7545 | rdev->recovery_offset < mddev->curr_resync) |
| 7546 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7547 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7549 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7550 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7551 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7552 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7553 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7554 | /* We completed so min/max setting can be forgotten if used. */ |
| 7555 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7556 | mddev->resync_min = 0; |
| 7557 | mddev->resync_max = MaxSector; |
| 7558 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7559 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7560 | mddev->curr_resync = 0; |
| 7561 | wake_up(&resync_wait); |
| 7562 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7563 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7564 | return; |
| 7565 | |
| 7566 | interrupted: |
| 7567 | /* |
| 7568 | * got a signal, exit. |
| 7569 | */ |
| 7570 | printk(KERN_INFO |
| 7571 | "md: md_do_sync() got signal ... exiting\n"); |
| 7572 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7573 | goto out; |
| 7574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7575 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7576 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7577 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7578 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7579 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7580 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7581 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7582 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7583 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7584 | mddev->curr_resync_completed = 0; |
| 7585 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7586 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7587 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7588 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7589 | (test_bit(Faulty, &rdev->flags) || |
| 7590 | ! test_bit(In_sync, &rdev->flags)) && |
| 7591 | atomic_read(&rdev->nr_pending)==0) { |
| 7592 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7593 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7594 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7595 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7596 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7597 | } |
| 7598 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7599 | if (removed) |
| 7600 | sysfs_notify(&mddev->kobj, NULL, |
| 7601 | "degraded"); |
| 7602 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7603 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7604 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7605 | if (rdev->raid_disk >= 0 && |
| 7606 | !test_bit(In_sync, &rdev->flags) && |
| 7607 | !test_bit(Faulty, &rdev->flags)) |
| 7608 | spares++; |
| 7609 | if (rdev->raid_disk < 0 |
| 7610 | && !test_bit(Faulty, &rdev->flags)) { |
| 7611 | rdev->recovery_offset = 0; |
| 7612 | if (mddev->pers-> |
| 7613 | hot_add_disk(mddev, rdev) == 0) { |
| 7614 | if (sysfs_link_rdev(mddev, rdev)) |
| 7615 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7616 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7617 | md_new_event(mddev); |
| 7618 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7619 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7620 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7621 | } |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame^] | 7622 | if (removed) |
| 7623 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7624 | return spares; |
| 7625 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7626 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7627 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7628 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7629 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7630 | |
| 7631 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7632 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7633 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7634 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7635 | /* success...*/ |
| 7636 | /* activate any spares */ |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame^] | 7637 | if (mddev->pers->spare_active(mddev)) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7638 | sysfs_notify(&mddev->kobj, NULL, |
| 7639 | "degraded"); |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame^] | 7640 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7641 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7642 | } |
| 7643 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7644 | mddev->pers->finish_reshape) |
| 7645 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7646 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7647 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7648 | * information must be scrapped. Also if any device is now |
| 7649 | * In_sync we must scrape the saved_raid_disk for that device |
| 7650 | * do the superblock for an incrementally recovered device |
| 7651 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7652 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7653 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7654 | if (!mddev->degraded || |
| 7655 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7656 | rdev->saved_raid_disk = -1; |
| 7657 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7658 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7659 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7660 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7661 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7662 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7663 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7664 | /* flag recovery needed just to double check */ |
| 7665 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7666 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7667 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7668 | if (mddev->event_work.func) |
| 7669 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7670 | } |
| 7671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7672 | /* |
| 7673 | * This routine is regularly called by all per-raid-array threads to |
| 7674 | * deal with generic issues like resync and super-block update. |
| 7675 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7676 | * need this as they never do any recovery or update the superblock. |
| 7677 | * |
| 7678 | * It does not do any resync itself, but rather "forks" off other threads |
| 7679 | * to do that as needed. |
| 7680 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7681 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7682 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 | * and wakeups up this thread which will reap the thread and finish up. |
| 7684 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7685 | * |
| 7686 | * The overall approach is: |
| 7687 | * 1/ if the superblock needs updating, update it. |
| 7688 | * 2/ If a recovery thread is running, don't do anything else. |
| 7689 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7690 | * 4/ If there are any faulty devices, remove them. |
| 7691 | * 5/ If array is degraded, try to add spares devices |
| 7692 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7693 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7694 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7695 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7696 | if (mddev->suspended) |
| 7697 | return; |
| 7698 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7699 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7700 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7701 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7702 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7703 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7704 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7705 | mdname(mddev)); |
| 7706 | mddev->safemode = 2; |
| 7707 | } |
| 7708 | flush_signals(current); |
| 7709 | } |
| 7710 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7711 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7712 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7713 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7714 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7716 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7717 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7718 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7719 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7720 | )) |
| 7721 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7722 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7723 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7724 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7725 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7726 | if (mddev->ro) { |
| 7727 | /* Only thing we do on a ro array is remove |
| 7728 | * failed devices. |
| 7729 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7730 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7731 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7732 | if (rdev->raid_disk >= 0 && |
| 7733 | !test_bit(Blocked, &rdev->flags) && |
| 7734 | test_bit(Faulty, &rdev->flags) && |
| 7735 | atomic_read(&rdev->nr_pending)==0) { |
| 7736 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7737 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7738 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7739 | rdev->raid_disk = -1; |
| 7740 | } |
| 7741 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7742 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7743 | goto unlock; |
| 7744 | } |
| 7745 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7746 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7747 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7748 | spin_lock_irq(&mddev->write_lock); |
| 7749 | if (mddev->safemode && |
| 7750 | !atomic_read(&mddev->writes_pending) && |
| 7751 | !mddev->in_sync && |
| 7752 | mddev->recovery_cp == MaxSector) { |
| 7753 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7754 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7755 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7756 | } |
| 7757 | if (mddev->safemode == 1) |
| 7758 | mddev->safemode = 0; |
| 7759 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7760 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7761 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7762 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7763 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7764 | if (mddev->flags) |
| 7765 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7767 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7768 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7769 | /* resync/recovery still happening */ |
| 7770 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7771 | goto unlock; |
| 7772 | } |
| 7773 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7774 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | goto unlock; |
| 7776 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7777 | /* Set RUNNING before clearing NEEDED to avoid |
| 7778 | * any transients in the value of "sync_action". |
| 7779 | */ |
| 7780 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7781 | /* Clear some bits that don't mean anything, but |
| 7782 | * might be left set |
| 7783 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7784 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7785 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7787 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7788 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7789 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7790 | /* no recovery is running. |
| 7791 | * remove any failed drives, then |
| 7792 | * add spares if possible. |
| 7793 | * Spare are also removed and re-added, to allow |
| 7794 | * the personality to fail the re-add. |
| 7795 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7796 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7797 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7798 | if (mddev->pers->check_reshape == NULL || |
| 7799 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7800 | /* Cannot proceed */ |
| 7801 | goto unlock; |
| 7802 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7803 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7804 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7805 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7806 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7807 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7808 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7809 | } else if (mddev->recovery_cp < MaxSector) { |
| 7810 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7811 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7812 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7813 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7814 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7816 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7817 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7818 | /* We are adding a device or devices to an array |
| 7819 | * which has the bitmap stored on all devices. |
| 7820 | * So make sure all bitmap pages get written |
| 7821 | */ |
| 7822 | bitmap_write_all(mddev->bitmap); |
| 7823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7824 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7825 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7826 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7827 | if (!mddev->sync_thread) { |
| 7828 | printk(KERN_ERR "%s: could not start resync" |
| 7829 | " thread...\n", |
| 7830 | mdname(mddev)); |
| 7831 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7832 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7833 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7834 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7835 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7836 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7837 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7838 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7839 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7840 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7841 | } |
| 7842 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7843 | if (!mddev->sync_thread) { |
| 7844 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7845 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7846 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7847 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7848 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7850 | mddev_unlock(mddev); |
| 7851 | } |
| 7852 | } |
| 7853 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7854 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7855 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7856 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7857 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7858 | !test_bit(Blocked, &rdev->flags) && |
| 7859 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7860 | msecs_to_jiffies(5000)); |
| 7861 | rdev_dec_pending(rdev, mddev); |
| 7862 | } |
| 7863 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7864 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7865 | void md_finish_reshape(struct mddev *mddev) |
| 7866 | { |
| 7867 | /* called be personality module when reshape completes. */ |
| 7868 | struct md_rdev *rdev; |
| 7869 | |
| 7870 | rdev_for_each(rdev, mddev) { |
| 7871 | if (rdev->data_offset > rdev->new_data_offset) |
| 7872 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7873 | else |
| 7874 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7875 | rdev->data_offset = rdev->new_data_offset; |
| 7876 | } |
| 7877 | } |
| 7878 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7879 | |
| 7880 | /* Bad block management. |
| 7881 | * We can record which blocks on each device are 'bad' and so just |
| 7882 | * fail those blocks, or that stripe, rather than the whole device. |
| 7883 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7884 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7885 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7886 | * A 'shift' can be set so that larger blocks are tracked and |
| 7887 | * consequently larger devices can be covered. |
| 7888 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7889 | * |
| 7890 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7891 | * might need to retry if it is very unlucky. |
| 7892 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7893 | * so we use the write_seqlock_irq variant. |
| 7894 | * |
| 7895 | * When looking for a bad block we specify a range and want to |
| 7896 | * know if any block in the range is bad. So we binary-search |
| 7897 | * to the last range that starts at-or-before the given endpoint, |
| 7898 | * (or "before the sector after the target range") |
| 7899 | * then see if it ends after the given start. |
| 7900 | * We return |
| 7901 | * 0 if there are no known bad blocks in the range |
| 7902 | * 1 if there are known bad block which are all acknowledged |
| 7903 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7904 | * plus the start/length of the first bad section we overlap. |
| 7905 | */ |
| 7906 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7907 | sector_t *first_bad, int *bad_sectors) |
| 7908 | { |
| 7909 | int hi; |
| 7910 | int lo = 0; |
| 7911 | u64 *p = bb->page; |
| 7912 | int rv = 0; |
| 7913 | sector_t target = s + sectors; |
| 7914 | unsigned seq; |
| 7915 | |
| 7916 | if (bb->shift > 0) { |
| 7917 | /* round the start down, and the end up */ |
| 7918 | s >>= bb->shift; |
| 7919 | target += (1<<bb->shift) - 1; |
| 7920 | target >>= bb->shift; |
| 7921 | sectors = target - s; |
| 7922 | } |
| 7923 | /* 'target' is now the first block after the bad range */ |
| 7924 | |
| 7925 | retry: |
| 7926 | seq = read_seqbegin(&bb->lock); |
| 7927 | |
| 7928 | hi = bb->count; |
| 7929 | |
| 7930 | /* Binary search between lo and hi for 'target' |
| 7931 | * i.e. for the last range that starts before 'target' |
| 7932 | */ |
| 7933 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7934 | * are known not to be the last range before target. |
| 7935 | * VARIANT: hi-lo is the number of possible |
| 7936 | * ranges, and decreases until it reaches 1 |
| 7937 | */ |
| 7938 | while (hi - lo > 1) { |
| 7939 | int mid = (lo + hi) / 2; |
| 7940 | sector_t a = BB_OFFSET(p[mid]); |
| 7941 | if (a < target) |
| 7942 | /* This could still be the one, earlier ranges |
| 7943 | * could not. */ |
| 7944 | lo = mid; |
| 7945 | else |
| 7946 | /* This and later ranges are definitely out. */ |
| 7947 | hi = mid; |
| 7948 | } |
| 7949 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7950 | if (hi > lo) { |
| 7951 | /* need to check all range that end after 's' to see if |
| 7952 | * any are unacknowledged. |
| 7953 | */ |
| 7954 | while (lo >= 0 && |
| 7955 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7956 | if (BB_OFFSET(p[lo]) < target) { |
| 7957 | /* starts before the end, and finishes after |
| 7958 | * the start, so they must overlap |
| 7959 | */ |
| 7960 | if (rv != -1 && BB_ACK(p[lo])) |
| 7961 | rv = 1; |
| 7962 | else |
| 7963 | rv = -1; |
| 7964 | *first_bad = BB_OFFSET(p[lo]); |
| 7965 | *bad_sectors = BB_LEN(p[lo]); |
| 7966 | } |
| 7967 | lo--; |
| 7968 | } |
| 7969 | } |
| 7970 | |
| 7971 | if (read_seqretry(&bb->lock, seq)) |
| 7972 | goto retry; |
| 7973 | |
| 7974 | return rv; |
| 7975 | } |
| 7976 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7977 | |
| 7978 | /* |
| 7979 | * Add a range of bad blocks to the table. |
| 7980 | * This might extend the table, or might contract it |
| 7981 | * if two adjacent ranges can be merged. |
| 7982 | * We binary-search to find the 'insertion' point, then |
| 7983 | * decide how best to handle it. |
| 7984 | */ |
| 7985 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7986 | int acknowledged) |
| 7987 | { |
| 7988 | u64 *p; |
| 7989 | int lo, hi; |
| 7990 | int rv = 1; |
| 7991 | |
| 7992 | if (bb->shift < 0) |
| 7993 | /* badblocks are disabled */ |
| 7994 | return 0; |
| 7995 | |
| 7996 | if (bb->shift) { |
| 7997 | /* round the start down, and the end up */ |
| 7998 | sector_t next = s + sectors; |
| 7999 | s >>= bb->shift; |
| 8000 | next += (1<<bb->shift) - 1; |
| 8001 | next >>= bb->shift; |
| 8002 | sectors = next - s; |
| 8003 | } |
| 8004 | |
| 8005 | write_seqlock_irq(&bb->lock); |
| 8006 | |
| 8007 | p = bb->page; |
| 8008 | lo = 0; |
| 8009 | hi = bb->count; |
| 8010 | /* Find the last range that starts at-or-before 's' */ |
| 8011 | while (hi - lo > 1) { |
| 8012 | int mid = (lo + hi) / 2; |
| 8013 | sector_t a = BB_OFFSET(p[mid]); |
| 8014 | if (a <= s) |
| 8015 | lo = mid; |
| 8016 | else |
| 8017 | hi = mid; |
| 8018 | } |
| 8019 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8020 | hi = lo; |
| 8021 | |
| 8022 | if (hi > lo) { |
| 8023 | /* we found a range that might merge with the start |
| 8024 | * of our new range |
| 8025 | */ |
| 8026 | sector_t a = BB_OFFSET(p[lo]); |
| 8027 | sector_t e = a + BB_LEN(p[lo]); |
| 8028 | int ack = BB_ACK(p[lo]); |
| 8029 | if (e >= s) { |
| 8030 | /* Yes, we can merge with a previous range */ |
| 8031 | if (s == a && s + sectors >= e) |
| 8032 | /* new range covers old */ |
| 8033 | ack = acknowledged; |
| 8034 | else |
| 8035 | ack = ack && acknowledged; |
| 8036 | |
| 8037 | if (e < s + sectors) |
| 8038 | e = s + sectors; |
| 8039 | if (e - a <= BB_MAX_LEN) { |
| 8040 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8041 | s = e; |
| 8042 | } else { |
| 8043 | /* does not all fit in one range, |
| 8044 | * make p[lo] maximal |
| 8045 | */ |
| 8046 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8047 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8048 | s = a + BB_MAX_LEN; |
| 8049 | } |
| 8050 | sectors = e - s; |
| 8051 | } |
| 8052 | } |
| 8053 | if (sectors && hi < bb->count) { |
| 8054 | /* 'hi' points to the first range that starts after 's'. |
| 8055 | * Maybe we can merge with the start of that range */ |
| 8056 | sector_t a = BB_OFFSET(p[hi]); |
| 8057 | sector_t e = a + BB_LEN(p[hi]); |
| 8058 | int ack = BB_ACK(p[hi]); |
| 8059 | if (a <= s + sectors) { |
| 8060 | /* merging is possible */ |
| 8061 | if (e <= s + sectors) { |
| 8062 | /* full overlap */ |
| 8063 | e = s + sectors; |
| 8064 | ack = acknowledged; |
| 8065 | } else |
| 8066 | ack = ack && acknowledged; |
| 8067 | |
| 8068 | a = s; |
| 8069 | if (e - a <= BB_MAX_LEN) { |
| 8070 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8071 | s = e; |
| 8072 | } else { |
| 8073 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8074 | s = a + BB_MAX_LEN; |
| 8075 | } |
| 8076 | sectors = e - s; |
| 8077 | lo = hi; |
| 8078 | hi++; |
| 8079 | } |
| 8080 | } |
| 8081 | if (sectors == 0 && hi < bb->count) { |
| 8082 | /* we might be able to combine lo and hi */ |
| 8083 | /* Note: 's' is at the end of 'lo' */ |
| 8084 | sector_t a = BB_OFFSET(p[hi]); |
| 8085 | int lolen = BB_LEN(p[lo]); |
| 8086 | int hilen = BB_LEN(p[hi]); |
| 8087 | int newlen = lolen + hilen - (s - a); |
| 8088 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8089 | /* yes, we can combine them */ |
| 8090 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8091 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8092 | memmove(p + hi, p + hi + 1, |
| 8093 | (bb->count - hi - 1) * 8); |
| 8094 | bb->count--; |
| 8095 | } |
| 8096 | } |
| 8097 | while (sectors) { |
| 8098 | /* didn't merge (it all). |
| 8099 | * Need to add a range just before 'hi' */ |
| 8100 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8101 | /* No room for more */ |
| 8102 | rv = 0; |
| 8103 | break; |
| 8104 | } else { |
| 8105 | int this_sectors = sectors; |
| 8106 | memmove(p + hi + 1, p + hi, |
| 8107 | (bb->count - hi) * 8); |
| 8108 | bb->count++; |
| 8109 | |
| 8110 | if (this_sectors > BB_MAX_LEN) |
| 8111 | this_sectors = BB_MAX_LEN; |
| 8112 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8113 | sectors -= this_sectors; |
| 8114 | s += this_sectors; |
| 8115 | } |
| 8116 | } |
| 8117 | |
| 8118 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8119 | if (!acknowledged) |
| 8120 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8121 | write_sequnlock_irq(&bb->lock); |
| 8122 | |
| 8123 | return rv; |
| 8124 | } |
| 8125 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8126 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8127 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8128 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8129 | int rv; |
| 8130 | if (is_new) |
| 8131 | s += rdev->new_data_offset; |
| 8132 | else |
| 8133 | s += rdev->data_offset; |
| 8134 | rv = md_set_badblocks(&rdev->badblocks, |
| 8135 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8136 | if (rv) { |
| 8137 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8138 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8139 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8140 | md_wakeup_thread(rdev->mddev->thread); |
| 8141 | } |
| 8142 | return rv; |
| 8143 | } |
| 8144 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8145 | |
| 8146 | /* |
| 8147 | * Remove a range of bad blocks from the table. |
| 8148 | * This may involve extending the table if we spilt a region, |
| 8149 | * but it must not fail. So if the table becomes full, we just |
| 8150 | * drop the remove request. |
| 8151 | */ |
| 8152 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8153 | { |
| 8154 | u64 *p; |
| 8155 | int lo, hi; |
| 8156 | sector_t target = s + sectors; |
| 8157 | int rv = 0; |
| 8158 | |
| 8159 | if (bb->shift > 0) { |
| 8160 | /* When clearing we round the start up and the end down. |
| 8161 | * This should not matter as the shift should align with |
| 8162 | * the block size and no rounding should ever be needed. |
| 8163 | * However it is better the think a block is bad when it |
| 8164 | * isn't than to think a block is not bad when it is. |
| 8165 | */ |
| 8166 | s += (1<<bb->shift) - 1; |
| 8167 | s >>= bb->shift; |
| 8168 | target >>= bb->shift; |
| 8169 | sectors = target - s; |
| 8170 | } |
| 8171 | |
| 8172 | write_seqlock_irq(&bb->lock); |
| 8173 | |
| 8174 | p = bb->page; |
| 8175 | lo = 0; |
| 8176 | hi = bb->count; |
| 8177 | /* Find the last range that starts before 'target' */ |
| 8178 | while (hi - lo > 1) { |
| 8179 | int mid = (lo + hi) / 2; |
| 8180 | sector_t a = BB_OFFSET(p[mid]); |
| 8181 | if (a < target) |
| 8182 | lo = mid; |
| 8183 | else |
| 8184 | hi = mid; |
| 8185 | } |
| 8186 | if (hi > lo) { |
| 8187 | /* p[lo] is the last range that could overlap the |
| 8188 | * current range. Earlier ranges could also overlap, |
| 8189 | * but only this one can overlap the end of the range. |
| 8190 | */ |
| 8191 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8192 | /* Partial overlap, leave the tail of this range */ |
| 8193 | int ack = BB_ACK(p[lo]); |
| 8194 | sector_t a = BB_OFFSET(p[lo]); |
| 8195 | sector_t end = a + BB_LEN(p[lo]); |
| 8196 | |
| 8197 | if (a < s) { |
| 8198 | /* we need to split this range */ |
| 8199 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8200 | rv = 0; |
| 8201 | goto out; |
| 8202 | } |
| 8203 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8204 | bb->count++; |
| 8205 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8206 | lo++; |
| 8207 | } |
| 8208 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8209 | /* there is no longer an overlap */ |
| 8210 | hi = lo; |
| 8211 | lo--; |
| 8212 | } |
| 8213 | while (lo >= 0 && |
| 8214 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8215 | /* This range does overlap */ |
| 8216 | if (BB_OFFSET(p[lo]) < s) { |
| 8217 | /* Keep the early parts of this range. */ |
| 8218 | int ack = BB_ACK(p[lo]); |
| 8219 | sector_t start = BB_OFFSET(p[lo]); |
| 8220 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8221 | /* now low doesn't overlap, so.. */ |
| 8222 | break; |
| 8223 | } |
| 8224 | lo--; |
| 8225 | } |
| 8226 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8227 | * anything between needs to be discarded |
| 8228 | */ |
| 8229 | if (hi - lo > 1) { |
| 8230 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8231 | bb->count -= (hi - lo - 1); |
| 8232 | } |
| 8233 | } |
| 8234 | |
| 8235 | bb->changed = 1; |
| 8236 | out: |
| 8237 | write_sequnlock_irq(&bb->lock); |
| 8238 | return rv; |
| 8239 | } |
| 8240 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8241 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8242 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8243 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8244 | if (is_new) |
| 8245 | s += rdev->new_data_offset; |
| 8246 | else |
| 8247 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8248 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8249 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8250 | } |
| 8251 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8252 | |
| 8253 | /* |
| 8254 | * Acknowledge all bad blocks in a list. |
| 8255 | * This only succeeds if ->changed is clear. It is used by |
| 8256 | * in-kernel metadata updates |
| 8257 | */ |
| 8258 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8259 | { |
| 8260 | if (bb->page == NULL || bb->changed) |
| 8261 | /* no point even trying */ |
| 8262 | return; |
| 8263 | write_seqlock_irq(&bb->lock); |
| 8264 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8265 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8266 | u64 *p = bb->page; |
| 8267 | int i; |
| 8268 | for (i = 0; i < bb->count ; i++) { |
| 8269 | if (!BB_ACK(p[i])) { |
| 8270 | sector_t start = BB_OFFSET(p[i]); |
| 8271 | int len = BB_LEN(p[i]); |
| 8272 | p[i] = BB_MAKE(start, len, 1); |
| 8273 | } |
| 8274 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8275 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8276 | } |
| 8277 | write_sequnlock_irq(&bb->lock); |
| 8278 | } |
| 8279 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8280 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8281 | /* sysfs access to bad-blocks list. |
| 8282 | * We present two files. |
| 8283 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8284 | * are recorded as bad. The list is truncated to fit within |
| 8285 | * the one-page limit of sysfs. |
| 8286 | * Writing "sector length" to this file adds an acknowledged |
| 8287 | * bad block list. |
| 8288 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8289 | * been acknowledged. Writing to this file adds bad blocks |
| 8290 | * without acknowledging them. This is largely for testing. |
| 8291 | */ |
| 8292 | |
| 8293 | static ssize_t |
| 8294 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8295 | { |
| 8296 | size_t len; |
| 8297 | int i; |
| 8298 | u64 *p = bb->page; |
| 8299 | unsigned seq; |
| 8300 | |
| 8301 | if (bb->shift < 0) |
| 8302 | return 0; |
| 8303 | |
| 8304 | retry: |
| 8305 | seq = read_seqbegin(&bb->lock); |
| 8306 | |
| 8307 | len = 0; |
| 8308 | i = 0; |
| 8309 | |
| 8310 | while (len < PAGE_SIZE && i < bb->count) { |
| 8311 | sector_t s = BB_OFFSET(p[i]); |
| 8312 | unsigned int length = BB_LEN(p[i]); |
| 8313 | int ack = BB_ACK(p[i]); |
| 8314 | i++; |
| 8315 | |
| 8316 | if (unack && ack) |
| 8317 | continue; |
| 8318 | |
| 8319 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8320 | (unsigned long long)s << bb->shift, |
| 8321 | length << bb->shift); |
| 8322 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8323 | if (unack && len == 0) |
| 8324 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8325 | |
| 8326 | if (read_seqretry(&bb->lock, seq)) |
| 8327 | goto retry; |
| 8328 | |
| 8329 | return len; |
| 8330 | } |
| 8331 | |
| 8332 | #define DO_DEBUG 1 |
| 8333 | |
| 8334 | static ssize_t |
| 8335 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8336 | { |
| 8337 | unsigned long long sector; |
| 8338 | int length; |
| 8339 | char newline; |
| 8340 | #ifdef DO_DEBUG |
| 8341 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8342 | * Normally only a successful write may clear a badblock |
| 8343 | */ |
| 8344 | int clear = 0; |
| 8345 | if (page[0] == '-') { |
| 8346 | clear = 1; |
| 8347 | page++; |
| 8348 | } |
| 8349 | #endif /* DO_DEBUG */ |
| 8350 | |
| 8351 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8352 | case 3: |
| 8353 | if (newline != '\n') |
| 8354 | return -EINVAL; |
| 8355 | case 2: |
| 8356 | if (length <= 0) |
| 8357 | return -EINVAL; |
| 8358 | break; |
| 8359 | default: |
| 8360 | return -EINVAL; |
| 8361 | } |
| 8362 | |
| 8363 | #ifdef DO_DEBUG |
| 8364 | if (clear) { |
| 8365 | md_clear_badblocks(bb, sector, length); |
| 8366 | return len; |
| 8367 | } |
| 8368 | #endif /* DO_DEBUG */ |
| 8369 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8370 | return len; |
| 8371 | else |
| 8372 | return -ENOSPC; |
| 8373 | } |
| 8374 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8375 | static int md_notify_reboot(struct notifier_block *this, |
| 8376 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8377 | { |
| 8378 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8379 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8380 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8381 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8382 | for_each_mddev(mddev, tmp) { |
| 8383 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8384 | if (mddev->pers) |
| 8385 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8386 | mddev->safemode = 2; |
| 8387 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8388 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8389 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8390 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8391 | /* |
| 8392 | * certain more exotic SCSI devices are known to be |
| 8393 | * volatile wrt too early system reboots. While the |
| 8394 | * right place to handle this issue is the given |
| 8395 | * driver, we do want to have a safe RAID driver ... |
| 8396 | */ |
| 8397 | if (need_delay) |
| 8398 | mdelay(1000*1); |
| 8399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8400 | return NOTIFY_DONE; |
| 8401 | } |
| 8402 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8403 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8404 | .notifier_call = md_notify_reboot, |
| 8405 | .next = NULL, |
| 8406 | .priority = INT_MAX, /* before any real devices */ |
| 8407 | }; |
| 8408 | |
| 8409 | static void md_geninit(void) |
| 8410 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8411 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8412 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8413 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8414 | } |
| 8415 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8416 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8417 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8418 | int ret = -ENOMEM; |
| 8419 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8420 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8421 | if (!md_wq) |
| 8422 | goto err_wq; |
| 8423 | |
| 8424 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8425 | if (!md_misc_wq) |
| 8426 | goto err_misc_wq; |
| 8427 | |
| 8428 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8429 | goto err_md; |
| 8430 | |
| 8431 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8432 | goto err_mdp; |
| 8433 | mdp_major = ret; |
| 8434 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8435 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8436 | md_probe, NULL, NULL); |
| 8437 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8438 | md_probe, NULL, NULL); |
| 8439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8440 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8441 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8442 | |
| 8443 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8444 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8445 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8446 | err_mdp: |
| 8447 | unregister_blkdev(MD_MAJOR, "md"); |
| 8448 | err_md: |
| 8449 | destroy_workqueue(md_misc_wq); |
| 8450 | err_misc_wq: |
| 8451 | destroy_workqueue(md_wq); |
| 8452 | err_wq: |
| 8453 | return ret; |
| 8454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8455 | |
| 8456 | #ifndef MODULE |
| 8457 | |
| 8458 | /* |
| 8459 | * Searches all registered partitions for autorun RAID arrays |
| 8460 | * at boot time. |
| 8461 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8462 | |
| 8463 | static LIST_HEAD(all_detected_devices); |
| 8464 | struct detected_devices_node { |
| 8465 | struct list_head list; |
| 8466 | dev_t dev; |
| 8467 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8468 | |
| 8469 | void md_autodetect_dev(dev_t dev) |
| 8470 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8471 | struct detected_devices_node *node_detected_dev; |
| 8472 | |
| 8473 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8474 | if (node_detected_dev) { |
| 8475 | node_detected_dev->dev = dev; |
| 8476 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8477 | } else { |
| 8478 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8479 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8481 | } |
| 8482 | |
| 8483 | |
| 8484 | static void autostart_arrays(int part) |
| 8485 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8486 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8487 | struct detected_devices_node *node_detected_dev; |
| 8488 | dev_t dev; |
| 8489 | int i_scanned, i_passed; |
| 8490 | |
| 8491 | i_scanned = 0; |
| 8492 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8493 | |
| 8494 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8495 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8496 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8497 | i_scanned++; |
| 8498 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8499 | struct detected_devices_node, list); |
| 8500 | list_del(&node_detected_dev->list); |
| 8501 | dev = node_detected_dev->dev; |
| 8502 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8503 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 | if (IS_ERR(rdev)) |
| 8505 | continue; |
| 8506 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8507 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8508 | MD_BUG(); |
| 8509 | continue; |
| 8510 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8511 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8512 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8513 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8514 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8515 | |
| 8516 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8517 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8518 | |
| 8519 | autorun_devices(part); |
| 8520 | } |
| 8521 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8522 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8523 | |
| 8524 | static __exit void md_exit(void) |
| 8525 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8526 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8527 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8528 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8529 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8530 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8531 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8532 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8533 | unregister_blkdev(mdp_major, "mdp"); |
| 8534 | unregister_reboot_notifier(&md_notifier); |
| 8535 | unregister_sysctl_table(raid_table_header); |
| 8536 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8537 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8538 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8539 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8540 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8541 | destroy_workqueue(md_misc_wq); |
| 8542 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8543 | } |
| 8544 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8545 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8546 | module_exit(md_exit) |
| 8547 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8548 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8549 | { |
| 8550 | return sprintf(buffer, "%d", start_readonly); |
| 8551 | } |
| 8552 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8553 | { |
| 8554 | char *e; |
| 8555 | int num = simple_strtoul(val, &e, 10); |
| 8556 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8557 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8558 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8559 | } |
| 8560 | return -EINVAL; |
| 8561 | } |
| 8562 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8563 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8564 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8565 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8566 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8568 | EXPORT_SYMBOL(register_md_personality); |
| 8569 | EXPORT_SYMBOL(unregister_md_personality); |
| 8570 | EXPORT_SYMBOL(md_error); |
| 8571 | EXPORT_SYMBOL(md_done_sync); |
| 8572 | EXPORT_SYMBOL(md_write_start); |
| 8573 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8574 | EXPORT_SYMBOL(md_register_thread); |
| 8575 | EXPORT_SYMBOL(md_unregister_thread); |
| 8576 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8577 | EXPORT_SYMBOL(md_check_recovery); |
| 8578 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8579 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8580 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8581 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |