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 | |
| 405 | md_wakeup_thread(mddev->thread); |
| 406 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 407 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 408 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 409 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 410 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 411 | { |
| 412 | return mddev->suspended; |
| 413 | } |
| 414 | EXPORT_SYMBOL(mddev_congested); |
| 415 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 417 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | */ |
| 419 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 420 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 422 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 423 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 424 | |
| 425 | rdev_dec_pending(rdev, mddev); |
| 426 | |
| 427 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 429 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | } |
| 431 | bio_put(bio); |
| 432 | } |
| 433 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 434 | static void md_submit_flush_data(struct work_struct *ws); |
| 435 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 436 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 437 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 438 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 439 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 440 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 441 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 442 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 443 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 444 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 445 | if (rdev->raid_disk >= 0 && |
| 446 | !test_bit(Faulty, &rdev->flags)) { |
| 447 | /* Take two references, one is dropped |
| 448 | * when request finishes, one after |
| 449 | * we reclaim rcu_read_lock |
| 450 | */ |
| 451 | struct bio *bi; |
| 452 | atomic_inc(&rdev->nr_pending); |
| 453 | atomic_inc(&rdev->nr_pending); |
| 454 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 455 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 456 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 457 | bi->bi_private = rdev; |
| 458 | bi->bi_bdev = rdev->bdev; |
| 459 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 460 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 461 | rcu_read_lock(); |
| 462 | rdev_dec_pending(rdev, mddev); |
| 463 | } |
| 464 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 465 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 466 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 467 | } |
| 468 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 469 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 470 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 471 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 472 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 473 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 474 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 475 | /* an empty barrier - all done */ |
| 476 | bio_endio(bio, 0); |
| 477 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 478 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 479 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 480 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 481 | |
| 482 | mddev->flush_bio = NULL; |
| 483 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 484 | } |
| 485 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 486 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 487 | { |
| 488 | spin_lock_irq(&mddev->write_lock); |
| 489 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 490 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 491 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 492 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 493 | spin_unlock_irq(&mddev->write_lock); |
| 494 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 495 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 496 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 497 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 498 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 499 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 500 | /* Support for plugging. |
| 501 | * This mirrors the plugging support in request_queue, but does not |
| 502 | * require having a whole queue or request structures. |
| 503 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 504 | * plugged on. This links tot the private plug_handle structure in the |
| 505 | * personality data where we keep a count of the number of outstanding |
| 506 | * plugs so other code can see if a plug is active. |
| 507 | */ |
| 508 | struct md_plug_cb { |
| 509 | struct blk_plug_cb cb; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 510 | struct mddev *mddev; |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 514 | { |
| 515 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 516 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 517 | md_wakeup_thread(mdcb->mddev->thread); |
| 518 | kfree(mdcb); |
| 519 | } |
| 520 | |
| 521 | /* Check that an unplug wakeup will come shortly. |
| 522 | * If not, wakeup the md thread immediately |
| 523 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 524 | int mddev_check_plugged(struct mddev *mddev) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 525 | { |
| 526 | struct blk_plug *plug = current->plug; |
| 527 | struct md_plug_cb *mdcb; |
| 528 | |
| 529 | if (!plug) |
| 530 | return 0; |
| 531 | |
| 532 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 533 | if (mdcb->cb.callback == plugger_unplug && |
| 534 | mdcb->mddev == mddev) { |
| 535 | /* Already on the list, move to top */ |
| 536 | if (mdcb != list_first_entry(&plug->cb_list, |
| 537 | struct md_plug_cb, |
| 538 | cb.list)) |
| 539 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 540 | return 1; |
| 541 | } |
| 542 | } |
| 543 | /* Not currently on the callback list */ |
| 544 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 545 | if (!mdcb) |
| 546 | return 0; |
| 547 | |
| 548 | mdcb->mddev = mddev; |
| 549 | mdcb->cb.callback = plugger_unplug; |
| 550 | atomic_inc(&mddev->plug_cnt); |
| 551 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 552 | return 1; |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 555 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 556 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | atomic_inc(&mddev->active); |
| 559 | return mddev; |
| 560 | } |
| 561 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 562 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 563 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 564 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 566 | struct bio_set *bs = NULL; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 569 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 570 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 571 | mddev->ctime == 0 && !mddev->hold_active) { |
| 572 | /* Array is not configured at all, and not held active, |
| 573 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 574 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 575 | bs = mddev->bio_set; |
| 576 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 577 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 578 | /* We did a probe so need to clean up. Call |
| 579 | * queue_work inside the spinlock so that |
| 580 | * flush_workqueue() after mddev_find will |
| 581 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 582 | */ |
| 583 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 584 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 585 | } else |
| 586 | kfree(mddev); |
| 587 | } |
| 588 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 589 | if (bs) |
| 590 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 593 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 594 | { |
| 595 | mutex_init(&mddev->open_mutex); |
| 596 | mutex_init(&mddev->reconfig_mutex); |
| 597 | mutex_init(&mddev->bitmap_info.mutex); |
| 598 | INIT_LIST_HEAD(&mddev->disks); |
| 599 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 600 | init_timer(&mddev->safemode_timer); |
| 601 | atomic_set(&mddev->active, 1); |
| 602 | atomic_set(&mddev->openers, 0); |
| 603 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 604 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 605 | spin_lock_init(&mddev->write_lock); |
| 606 | atomic_set(&mddev->flush_pending, 0); |
| 607 | init_waitqueue_head(&mddev->sb_wait); |
| 608 | init_waitqueue_head(&mddev->recovery_wait); |
| 609 | mddev->reshape_position = MaxSector; |
| 610 | mddev->resync_min = 0; |
| 611 | mddev->resync_max = MaxSector; |
| 612 | mddev->level = LEVEL_NONE; |
| 613 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 614 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 615 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 616 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 618 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 620 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 621 | unit &= ~((1<<MdpMinorShift)-1); |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | retry: |
| 624 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 626 | if (unit) { |
| 627 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 628 | if (mddev->unit == unit) { |
| 629 | mddev_get(mddev); |
| 630 | spin_unlock(&all_mddevs_lock); |
| 631 | kfree(new); |
| 632 | return mddev; |
| 633 | } |
| 634 | |
| 635 | if (new) { |
| 636 | list_add(&new->all_mddevs, &all_mddevs); |
| 637 | spin_unlock(&all_mddevs_lock); |
| 638 | new->hold_active = UNTIL_IOCTL; |
| 639 | return new; |
| 640 | } |
| 641 | } else if (new) { |
| 642 | /* find an unused unit number */ |
| 643 | static int next_minor = 512; |
| 644 | int start = next_minor; |
| 645 | int is_free = 0; |
| 646 | int dev = 0; |
| 647 | while (!is_free) { |
| 648 | dev = MKDEV(MD_MAJOR, next_minor); |
| 649 | next_minor++; |
| 650 | if (next_minor > MINORMASK) |
| 651 | next_minor = 0; |
| 652 | if (next_minor == start) { |
| 653 | /* Oh dear, all in use. */ |
| 654 | spin_unlock(&all_mddevs_lock); |
| 655 | kfree(new); |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | is_free = 1; |
| 660 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 661 | if (mddev->unit == dev) { |
| 662 | is_free = 0; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | new->unit = dev; |
| 667 | new->md_minor = MINOR(dev); |
| 668 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | list_add(&new->all_mddevs, &all_mddevs); |
| 670 | spin_unlock(&all_mddevs_lock); |
| 671 | return new; |
| 672 | } |
| 673 | spin_unlock(&all_mddevs_lock); |
| 674 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 675 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (!new) |
| 677 | return NULL; |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | new->unit = unit; |
| 680 | if (MAJOR(unit) == MD_MAJOR) |
| 681 | new->md_minor = MINOR(unit); |
| 682 | else |
| 683 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 684 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 685 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | goto retry; |
| 688 | } |
| 689 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 690 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 692 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 695 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 696 | { |
| 697 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 698 | } |
| 699 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 700 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 702 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 705 | static struct attribute_group md_redundancy_group; |
| 706 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 707 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 709 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 710 | /* These cannot be removed under reconfig_mutex as |
| 711 | * an access to the files will try to take reconfig_mutex |
| 712 | * while holding the file unremovable, which leads to |
| 713 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 714 | * So hold set sysfs_active while the remove in happeing, |
| 715 | * and anything else which might set ->to_remove or my |
| 716 | * otherwise change the sysfs namespace will fail with |
| 717 | * -EBUSY if sysfs_active is still set. |
| 718 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 719 | * test it under the same mutex to ensure its correct value |
| 720 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 721 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 722 | struct attribute_group *to_remove = mddev->to_remove; |
| 723 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 724 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 725 | mutex_unlock(&mddev->reconfig_mutex); |
| 726 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 727 | if (mddev->kobj.sd) { |
| 728 | if (to_remove != &md_redundancy_group) |
| 729 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 730 | if (mddev->pers == NULL || |
| 731 | mddev->pers->sync_request == NULL) { |
| 732 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 733 | if (mddev->sysfs_action) |
| 734 | sysfs_put(mddev->sysfs_action); |
| 735 | mddev->sysfs_action = NULL; |
| 736 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 737 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 738 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 739 | } else |
| 740 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 742 | /* As we've dropped the mutex we need a spinlock to |
| 743 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 744 | */ |
| 745 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 746 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 747 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 750 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 752 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 754 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (rdev->desc_nr == nr) |
| 756 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return NULL; |
| 759 | } |
| 760 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 761 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 763 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 765 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | if (rdev->bdev->bd_dev == dev) |
| 767 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return NULL; |
| 770 | } |
| 771 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 772 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 773 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 774 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 775 | list_for_each_entry(pers, &pers_list, list) { |
| 776 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 777 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 778 | if (strcmp(pers->name, clevel)==0) |
| 779 | return pers; |
| 780 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 781 | return NULL; |
| 782 | } |
| 783 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 784 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 785 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 787 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 788 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 791 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
| 793 | if (rdev->sb_page) |
| 794 | MD_BUG(); |
| 795 | |
| 796 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 797 | if (!rdev->sb_page) { |
| 798 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 799 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 805 | static void free_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
| 807 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 808 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | rdev->sb_loaded = 0; |
| 810 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 811 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 812 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 814 | if (rdev->bb_page) { |
| 815 | put_page(rdev->bb_page); |
| 816 | rdev->bb_page = NULL; |
| 817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 821 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 822 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 823 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 824 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 825 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 826 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 827 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 828 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 829 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 830 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 831 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 832 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 833 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 834 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 835 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 836 | } |
| 837 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 838 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 839 | sector_t sector, int size, struct page *page) |
| 840 | { |
| 841 | /* write first size bytes of page to sector of rdev |
| 842 | * Increment mddev->pending_writes before returning |
| 843 | * and decrement it on completion, waking up sb_wait |
| 844 | * if zero is reached. |
| 845 | * If an error occurred, call md_error |
| 846 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 847 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 848 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 849 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 850 | bio->bi_sector = sector; |
| 851 | bio_add_page(bio, page, size, 0); |
| 852 | bio->bi_private = rdev; |
| 853 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 854 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 855 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 856 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 857 | } |
| 858 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 859 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 860 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 861 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 862 | DEFINE_WAIT(wq); |
| 863 | for(;;) { |
| 864 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 865 | if (atomic_read(&mddev->pending_writes)==0) |
| 866 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 867 | schedule(); |
| 868 | } |
| 869 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 870 | } |
| 871 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 872 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 877 | 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] | 878 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 880 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | struct completion event; |
| 882 | int ret; |
| 883 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 884 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 886 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 887 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 888 | if (metadata_op) |
| 889 | bio->bi_sector = sector + rdev->sb_start; |
| 890 | else |
| 891 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | bio_add_page(bio, page, size, 0); |
| 893 | init_completion(&event); |
| 894 | bio->bi_private = &event; |
| 895 | bio->bi_end_io = bi_complete; |
| 896 | submit_bio(rw, bio); |
| 897 | wait_for_completion(&event); |
| 898 | |
| 899 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 900 | bio_put(bio); |
| 901 | return ret; |
| 902 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 903 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 905 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
| 907 | char b[BDEVNAME_SIZE]; |
| 908 | if (!rdev->sb_page) { |
| 909 | MD_BUG(); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | if (rdev->sb_loaded) |
| 913 | return 0; |
| 914 | |
| 915 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 916 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | goto fail; |
| 918 | rdev->sb_loaded = 1; |
| 919 | return 0; |
| 920 | |
| 921 | fail: |
| 922 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 923 | bdevname(rdev->bdev,b)); |
| 924 | return -EINVAL; |
| 925 | } |
| 926 | |
| 927 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 928 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 929 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 930 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 931 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 932 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 936 | { |
| 937 | int ret; |
| 938 | mdp_super_t *tmp1, *tmp2; |
| 939 | |
| 940 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 941 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 942 | |
| 943 | if (!tmp1 || !tmp2) { |
| 944 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 945 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto abort; |
| 947 | } |
| 948 | |
| 949 | *tmp1 = *sb1; |
| 950 | *tmp2 = *sb2; |
| 951 | |
| 952 | /* |
| 953 | * nr_disks is not constant |
| 954 | */ |
| 955 | tmp1->nr_disks = 0; |
| 956 | tmp2->nr_disks = 0; |
| 957 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 958 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 960 | kfree(tmp1); |
| 961 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return ret; |
| 963 | } |
| 964 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 965 | |
| 966 | static u32 md_csum_fold(u32 csum) |
| 967 | { |
| 968 | csum = (csum & 0xffff) + (csum >> 16); |
| 969 | return (csum & 0xffff) + (csum >> 16); |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 973 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 974 | u64 newcsum = 0; |
| 975 | u32 *sb32 = (u32*)sb; |
| 976 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | unsigned int disk_csum, csum; |
| 978 | |
| 979 | disk_csum = sb->sb_csum; |
| 980 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 981 | |
| 982 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 983 | newcsum += sb32[i]; |
| 984 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 985 | |
| 986 | |
| 987 | #ifdef CONFIG_ALPHA |
| 988 | /* This used to use csum_partial, which was wrong for several |
| 989 | * reasons including that different results are returned on |
| 990 | * different architectures. It isn't critical that we get exactly |
| 991 | * the same return value as before (we always csum_fold before |
| 992 | * testing, and that removes any differences). However as we |
| 993 | * know that csum_partial always returned a 16bit value on |
| 994 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 995 | */ |
| 996 | sb->sb_csum = md_csum_fold(disk_csum); |
| 997 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 999 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return csum; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | /* |
| 1005 | * Handle superblock details. |
| 1006 | * We want to be able to handle multiple superblock formats |
| 1007 | * so we have a common interface to them all, and an array of |
| 1008 | * different handlers. |
| 1009 | * We rely on user-space to write the initial superblock, and support |
| 1010 | * reading and updating of superblocks. |
| 1011 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1012 | * 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] | 1013 | * loads and validates a superblock on dev. |
| 1014 | * if refdev != NULL, compare superblocks on both devices |
| 1015 | * Return: |
| 1016 | * 0 - dev has a superblock that is compatible with refdev |
| 1017 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1018 | * so dev should be used as the refdev in future |
| 1019 | * -EINVAL superblock incompatible or invalid |
| 1020 | * -othererror e.g. -EIO |
| 1021 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1022 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | * Verify that dev is acceptable into mddev. |
| 1024 | * The first time, mddev->raid_disks will be 0, and data from |
| 1025 | * dev should be merged in. Subsequent calls check that dev |
| 1026 | * is new enough. Return 0 or -EINVAL |
| 1027 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1028 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | * Update the superblock for rdev with data in mddev |
| 1030 | * This does not write to disc. |
| 1031 | * |
| 1032 | */ |
| 1033 | |
| 1034 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1035 | char *name; |
| 1036 | struct module *owner; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1037 | int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1038 | int minor_version); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1039 | int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev); |
| 1040 | void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1041 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1042 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | }; |
| 1044 | |
| 1045 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1046 | * Check that the given mddev has no bitmap. |
| 1047 | * |
| 1048 | * This function is called from the run method of all personalities that do not |
| 1049 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1050 | * has a bitmap. Otherwise, it returns 0. |
| 1051 | * |
| 1052 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1053 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1054 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1055 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1056 | return 0; |
| 1057 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1058 | mdname(mddev), mddev->pers->name); |
| 1059 | return 1; |
| 1060 | } |
| 1061 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1062 | |
| 1063 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | * load_super for 0.90.0 |
| 1065 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1066 | 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] | 1067 | { |
| 1068 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1069 | mdp_super_t *sb; |
| 1070 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
| 1072 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1073 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | * it's at the end of the disk. |
| 1075 | * |
| 1076 | * It also happens to be a multiple of 4Kb. |
| 1077 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1078 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1080 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (ret) return ret; |
| 1082 | |
| 1083 | ret = -EINVAL; |
| 1084 | |
| 1085 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1086 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
| 1088 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1089 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1090 | b); |
| 1091 | goto abort; |
| 1092 | } |
| 1093 | |
| 1094 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1095 | sb->minor_version < 90 || |
| 1096 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1098 | sb->major_version, sb->minor_version, |
| 1099 | b); |
| 1100 | goto abort; |
| 1101 | } |
| 1102 | |
| 1103 | if (sb->raid_disks <= 0) |
| 1104 | goto abort; |
| 1105 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1106 | 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] | 1107 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1108 | b); |
| 1109 | goto abort; |
| 1110 | } |
| 1111 | |
| 1112 | rdev->preferred_minor = sb->md_minor; |
| 1113 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1114 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1115 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
| 1117 | if (sb->level == LEVEL_MULTIPATH) |
| 1118 | rdev->desc_nr = -1; |
| 1119 | else |
| 1120 | rdev->desc_nr = sb->this_disk.number; |
| 1121 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1122 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1124 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1126 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | if (!uuid_equal(refsb, sb)) { |
| 1128 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1129 | b, bdevname(refdev->bdev,b2)); |
| 1130 | goto abort; |
| 1131 | } |
| 1132 | if (!sb_equal(refsb, sb)) { |
| 1133 | printk(KERN_WARNING "md: %s has same UUID" |
| 1134 | " but different superblock to %s\n", |
| 1135 | b, bdevname(refdev->bdev, b2)); |
| 1136 | goto abort; |
| 1137 | } |
| 1138 | ev1 = md_event(sb); |
| 1139 | ev2 = md_event(refsb); |
| 1140 | if (ev1 > ev2) |
| 1141 | ret = 1; |
| 1142 | else |
| 1143 | ret = 0; |
| 1144 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1145 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1146 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1147 | if (rdev->sectors >= (2ULL << 32)) |
| 1148 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1150 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1151 | /* "this cannot possibly happen" ... */ |
| 1152 | ret = -EINVAL; |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | abort: |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * validate_super for 0.90.0 |
| 1160 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1161 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | { |
| 1163 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1164 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1165 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1167 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1168 | clear_bit(Faulty, &rdev->flags); |
| 1169 | clear_bit(In_sync, &rdev->flags); |
| 1170 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (mddev->raid_disks == 0) { |
| 1173 | mddev->major_version = 0; |
| 1174 | mddev->minor_version = sb->minor_version; |
| 1175 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1176 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1177 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | mddev->ctime = sb->ctime; |
| 1179 | mddev->utime = sb->utime; |
| 1180 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1181 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | mddev->layout = sb->layout; |
| 1183 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1184 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1185 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1186 | mddev->bitmap_info.offset = 0; |
| 1187 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1189 | if (mddev->minor_version >= 91) { |
| 1190 | mddev->reshape_position = sb->reshape_position; |
| 1191 | mddev->delta_disks = sb->delta_disks; |
| 1192 | mddev->new_level = sb->new_level; |
| 1193 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1194 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1195 | } else { |
| 1196 | mddev->reshape_position = MaxSector; |
| 1197 | mddev->delta_disks = 0; |
| 1198 | mddev->new_level = mddev->level; |
| 1199 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1200 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1204 | mddev->recovery_cp = MaxSector; |
| 1205 | else { |
| 1206 | if (sb->events_hi == sb->cp_events_hi && |
| 1207 | sb->events_lo == sb->cp_events_lo) { |
| 1208 | mddev->recovery_cp = sb->recovery_cp; |
| 1209 | } else |
| 1210 | mddev->recovery_cp = 0; |
| 1211 | } |
| 1212 | |
| 1213 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1214 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1215 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1216 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1217 | |
| 1218 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1219 | |
| 1220 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1221 | mddev->bitmap_info.file == NULL) |
| 1222 | mddev->bitmap_info.offset = |
| 1223 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1224 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1225 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1226 | /* Insist on good event counter while assembling, except |
| 1227 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1229 | if (sb->disks[rdev->desc_nr].state & ( |
| 1230 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1231 | if (ev1 < mddev->events) |
| 1232 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1233 | } else if (mddev->bitmap) { |
| 1234 | /* if adding to array with a bitmap, then we can accept an |
| 1235 | * older device ... but not too old. |
| 1236 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1237 | if (ev1 < mddev->bitmap->events_cleared) |
| 1238 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1239 | } else { |
| 1240 | if (ev1 < mddev->events) |
| 1241 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1242 | return 0; |
| 1243 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | desc = sb->disks + rdev->desc_nr; |
| 1247 | |
| 1248 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1249 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1250 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1251 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1252 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1254 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1255 | /* active but not in sync implies recovery up to |
| 1256 | * reshape position. We don't know exactly where |
| 1257 | * that is, so set to zero for now */ |
| 1258 | if (mddev->minor_version >= 91) { |
| 1259 | rdev->recovery_offset = 0; |
| 1260 | rdev->raid_disk = desc->raid_disk; |
| 1261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1263 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1264 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1265 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1266 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * sync_super for 0.90.0 |
| 1272 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1273 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { |
| 1275 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1276 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | /* make rdev->sb match mddev data.. |
| 1281 | * |
| 1282 | * 1/ zero out disks |
| 1283 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1284 | * 3/ any empty disks < next_spare become removed |
| 1285 | * |
| 1286 | * disks[0] gets initialised to REMOVED because |
| 1287 | * we cannot be sure from other fields if it has |
| 1288 | * been initialised or not. |
| 1289 | */ |
| 1290 | int i; |
| 1291 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1292 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1293 | rdev->sb_size = MD_SB_BYTES; |
| 1294 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1295 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | memset(sb, 0, sizeof(*sb)); |
| 1298 | |
| 1299 | sb->md_magic = MD_SB_MAGIC; |
| 1300 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | sb->patch_version = mddev->patch_version; |
| 1302 | sb->gvalid_words = 0; /* ignored */ |
| 1303 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1304 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1305 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1306 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1307 | |
| 1308 | sb->ctime = mddev->ctime; |
| 1309 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1310 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | sb->raid_disks = mddev->raid_disks; |
| 1312 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1313 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | sb->utime = mddev->utime; |
| 1315 | sb->state = 0; |
| 1316 | sb->events_hi = (mddev->events>>32); |
| 1317 | sb->events_lo = (u32)mddev->events; |
| 1318 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1319 | if (mddev->reshape_position == MaxSector) |
| 1320 | sb->minor_version = 90; |
| 1321 | else { |
| 1322 | sb->minor_version = 91; |
| 1323 | sb->reshape_position = mddev->reshape_position; |
| 1324 | sb->new_level = mddev->new_level; |
| 1325 | sb->delta_disks = mddev->delta_disks; |
| 1326 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1327 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1328 | } |
| 1329 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | if (mddev->in_sync) |
| 1331 | { |
| 1332 | sb->recovery_cp = mddev->recovery_cp; |
| 1333 | sb->cp_events_hi = (mddev->events>>32); |
| 1334 | sb->cp_events_lo = (u32)mddev->events; |
| 1335 | if (mddev->recovery_cp == MaxSector) |
| 1336 | sb->state = (1<< MD_SB_CLEAN); |
| 1337 | } else |
| 1338 | sb->recovery_cp = 0; |
| 1339 | |
| 1340 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1341 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1343 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1344 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1347 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1349 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1350 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1351 | |
| 1352 | if (rdev2->raid_disk >= 0 && |
| 1353 | sb->minor_version >= 91) |
| 1354 | /* we have nowhere to store the recovery_offset, |
| 1355 | * but if it is not below the reshape_position, |
| 1356 | * we can piggy-back on that. |
| 1357 | */ |
| 1358 | is_active = 1; |
| 1359 | if (rdev2->raid_disk < 0 || |
| 1360 | test_bit(Faulty, &rdev2->flags)) |
| 1361 | is_active = 0; |
| 1362 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1363 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1365 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1366 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | d = &sb->disks[rdev2->desc_nr]; |
| 1368 | nr_disks++; |
| 1369 | d->number = rdev2->desc_nr; |
| 1370 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1371 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1372 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | d->raid_disk = rdev2->raid_disk; |
| 1374 | else |
| 1375 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1376 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1378 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1380 | if (test_bit(In_sync, &rdev2->flags)) |
| 1381 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | active++; |
| 1383 | working++; |
| 1384 | } else { |
| 1385 | d->state = 0; |
| 1386 | spare++; |
| 1387 | working++; |
| 1388 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1389 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1390 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1393 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1394 | mdp_disk_t *d = &sb->disks[i]; |
| 1395 | if (d->state == 0 && d->number == 0) { |
| 1396 | d->number = i; |
| 1397 | d->raid_disk = i; |
| 1398 | d->state = (1<<MD_DISK_REMOVED); |
| 1399 | d->state |= (1<<MD_DISK_FAULTY); |
| 1400 | failed++; |
| 1401 | } |
| 1402 | } |
| 1403 | sb->nr_disks = nr_disks; |
| 1404 | sb->active_disks = active; |
| 1405 | sb->working_disks = working; |
| 1406 | sb->failed_disks = failed; |
| 1407 | sb->spare_disks = spare; |
| 1408 | |
| 1409 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1410 | sb->sb_csum = calc_sb_csum(sb); |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1414 | * rdev_size_change for 0.90.0 |
| 1415 | */ |
| 1416 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1417 | 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] | 1418 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1419 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1420 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1421 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1422 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1423 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1424 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1425 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1426 | /* Limit to 4TB as metadata cannot record more than that. |
| 1427 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1428 | */ |
| 1429 | if (num_sectors >= (2ULL << 32)) |
| 1430 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1431 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1432 | rdev->sb_page); |
| 1433 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1434 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | * version 1 superblock |
| 1440 | */ |
| 1441 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1442 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1444 | __le32 disk_csum; |
| 1445 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | unsigned long long newcsum; |
| 1447 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1448 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | int i; |
| 1450 | |
| 1451 | disk_csum = sb->sb_csum; |
| 1452 | sb->sb_csum = 0; |
| 1453 | newcsum = 0; |
| 1454 | for (i=0; size>=4; size -= 4 ) |
| 1455 | newcsum += le32_to_cpu(*isuper++); |
| 1456 | |
| 1457 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1458 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
| 1460 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1461 | sb->sb_csum = disk_csum; |
| 1462 | return cpu_to_le32(csum); |
| 1463 | } |
| 1464 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1465 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1466 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1467 | 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] | 1468 | { |
| 1469 | struct mdp_superblock_1 *sb; |
| 1470 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1471 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1473 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
| 1475 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1476 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | * It is always aligned to a 4K boundary and |
| 1478 | * depeding on minor_version, it can be: |
| 1479 | * 0: At least 8K, but less than 12K, from end of device |
| 1480 | * 1: At start of device |
| 1481 | * 2: 4K from start of device. |
| 1482 | */ |
| 1483 | switch(minor_version) { |
| 1484 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1485 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1486 | sb_start -= 8*2; |
| 1487 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | break; |
| 1489 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1490 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | break; |
| 1492 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1493 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | break; |
| 1495 | default: |
| 1496 | return -EINVAL; |
| 1497 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1498 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1500 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1501 | * and it is safe to read 4k, so we do that |
| 1502 | */ |
| 1503 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | if (ret) return ret; |
| 1505 | |
| 1506 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1507 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
| 1509 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1510 | sb->major_version != cpu_to_le32(1) || |
| 1511 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1512 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1513 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | return -EINVAL; |
| 1515 | |
| 1516 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1517 | printk("md: invalid superblock checksum on %s\n", |
| 1518 | bdevname(rdev->bdev,b)); |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1522 | printk("md: data_size too small on %s\n", |
| 1523 | bdevname(rdev->bdev,b)); |
| 1524 | return -EINVAL; |
| 1525 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | rdev->preferred_minor = 0xffff; |
| 1528 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1529 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1531 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1532 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1533 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1534 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1535 | |
| 1536 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1537 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1538 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1539 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1540 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1541 | rdev->desc_nr = -1; |
| 1542 | else |
| 1543 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1544 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1545 | if (!rdev->bb_page) { |
| 1546 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1547 | if (!rdev->bb_page) |
| 1548 | return -ENOMEM; |
| 1549 | } |
| 1550 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1551 | rdev->badblocks.count == 0) { |
| 1552 | /* need to load the bad block list. |
| 1553 | * Currently we limit it to one page. |
| 1554 | */ |
| 1555 | s32 offset; |
| 1556 | sector_t bb_sector; |
| 1557 | u64 *bbp; |
| 1558 | int i; |
| 1559 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1560 | if (sectors > (PAGE_SIZE / 512)) |
| 1561 | return -EINVAL; |
| 1562 | offset = le32_to_cpu(sb->bblog_offset); |
| 1563 | if (offset == 0) |
| 1564 | return -EINVAL; |
| 1565 | bb_sector = (long long)offset; |
| 1566 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1567 | rdev->bb_page, READ, true)) |
| 1568 | return -EIO; |
| 1569 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1570 | rdev->badblocks.shift = sb->bblog_shift; |
| 1571 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1572 | u64 bb = le64_to_cpu(*bbp); |
| 1573 | int count = bb & (0x3ff); |
| 1574 | u64 sector = bb >> 10; |
| 1575 | sector <<= sb->bblog_shift; |
| 1576 | count <<= sb->bblog_shift; |
| 1577 | if (bb + 1 == 0) |
| 1578 | break; |
| 1579 | if (md_set_badblocks(&rdev->badblocks, |
| 1580 | sector, count, 1) == 0) |
| 1581 | return -EINVAL; |
| 1582 | } |
| 1583 | } else if (sb->bblog_offset == 0) |
| 1584 | rdev->badblocks.shift = -1; |
| 1585 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1586 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1587 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1588 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1590 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
| 1592 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1593 | sb->level != refsb->level || |
| 1594 | sb->layout != refsb->layout || |
| 1595 | sb->chunksize != refsb->chunksize) { |
| 1596 | printk(KERN_WARNING "md: %s has strangely different" |
| 1597 | " superblock to %s\n", |
| 1598 | bdevname(rdev->bdev,b), |
| 1599 | bdevname(refdev->bdev,b2)); |
| 1600 | return -EINVAL; |
| 1601 | } |
| 1602 | ev1 = le64_to_cpu(sb->events); |
| 1603 | ev2 = le64_to_cpu(refsb->events); |
| 1604 | |
| 1605 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1606 | ret = 1; |
| 1607 | else |
| 1608 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1610 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1611 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1612 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1614 | rdev->sectors = rdev->sb_start; |
| 1615 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1617 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1618 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1619 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1620 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1623 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | { |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1625 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1626 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1628 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1629 | clear_bit(Faulty, &rdev->flags); |
| 1630 | clear_bit(In_sync, &rdev->flags); |
| 1631 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | if (mddev->raid_disks == 0) { |
| 1634 | mddev->major_version = 1; |
| 1635 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1636 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1637 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1639 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1640 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1641 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | mddev->layout = le32_to_cpu(sb->layout); |
| 1643 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1644 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1645 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1646 | mddev->bitmap_info.offset = 0; |
| 1647 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
| 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 | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1655 | mddev->bitmap_info.file == NULL ) |
| 1656 | mddev->bitmap_info.offset = |
| 1657 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1658 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1659 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1660 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1661 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1662 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1663 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1664 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1665 | } else { |
| 1666 | mddev->reshape_position = MaxSector; |
| 1667 | mddev->delta_disks = 0; |
| 1668 | mddev->new_level = mddev->level; |
| 1669 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1670 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1673 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1674 | /* Insist of good event counter while assembling, except for |
| 1675 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1677 | if (rdev->desc_nr >= 0 && |
| 1678 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1679 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1680 | if (ev1 < mddev->events) |
| 1681 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1682 | } else if (mddev->bitmap) { |
| 1683 | /* If adding to array with a bitmap, then we can accept an |
| 1684 | * older device, but not too old. |
| 1685 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1686 | if (ev1 < mddev->bitmap->events_cleared) |
| 1687 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1688 | } else { |
| 1689 | if (ev1 < mddev->events) |
| 1690 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1691 | return 0; |
| 1692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1694 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1695 | if (rdev->desc_nr < 0 || |
| 1696 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1697 | role = 0xffff; |
| 1698 | rdev->desc_nr = -1; |
| 1699 | } else |
| 1700 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | switch(role) { |
| 1702 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | break; |
| 1704 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1705 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | break; |
| 1707 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1708 | if ((le32_to_cpu(sb->feature_map) & |
| 1709 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1710 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1711 | else |
| 1712 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | rdev->raid_disk = role; |
| 1714 | break; |
| 1715 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1716 | if (sb->devflags & WriteMostly1) |
| 1717 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1718 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1719 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1720 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1721 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1726 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
| 1728 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1729 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | int max_dev, i; |
| 1731 | /* make rdev->sb match mddev and rdev data. */ |
| 1732 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1733 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
| 1735 | sb->feature_map = 0; |
| 1736 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1737 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1740 | |
| 1741 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1742 | sb->events = cpu_to_le64(mddev->events); |
| 1743 | if (mddev->in_sync) |
| 1744 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1745 | else |
| 1746 | sb->resync_offset = cpu_to_le64(0); |
| 1747 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1748 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1749 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1750 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1751 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1752 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1753 | sb->level = cpu_to_le32(mddev->level); |
| 1754 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1755 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1756 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1757 | sb->devflags |= WriteMostly1; |
| 1758 | else |
| 1759 | sb->devflags &= ~WriteMostly1; |
| 1760 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1761 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1762 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1763 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1764 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1767 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1768 | sb->feature_map |= |
| 1769 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1770 | sb->recovery_offset = |
| 1771 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1772 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1773 | if (test_bit(Replacement, &rdev->flags)) |
| 1774 | sb->feature_map |= |
| 1775 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1776 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1777 | if (mddev->reshape_position != MaxSector) { |
| 1778 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1779 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1780 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1781 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1782 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1783 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1784 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1785 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1786 | if (rdev->badblocks.count == 0) |
| 1787 | /* Nothing to do for bad blocks*/ ; |
| 1788 | else if (sb->bblog_offset == 0) |
| 1789 | /* Cannot record bad blocks on this device */ |
| 1790 | md_error(mddev, rdev); |
| 1791 | else { |
| 1792 | struct badblocks *bb = &rdev->badblocks; |
| 1793 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1794 | u64 *p = bb->page; |
| 1795 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1796 | if (bb->changed) { |
| 1797 | unsigned seq; |
| 1798 | |
| 1799 | retry: |
| 1800 | seq = read_seqbegin(&bb->lock); |
| 1801 | |
| 1802 | memset(bbp, 0xff, PAGE_SIZE); |
| 1803 | |
| 1804 | for (i = 0 ; i < bb->count ; i++) { |
| 1805 | u64 internal_bb = *p++; |
| 1806 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1807 | | BB_LEN(internal_bb)); |
| 1808 | *bbp++ = cpu_to_le64(store_bb); |
| 1809 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1810 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1811 | if (read_seqretry(&bb->lock, seq)) |
| 1812 | goto retry; |
| 1813 | |
| 1814 | bb->sector = (rdev->sb_start + |
| 1815 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1816 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1821 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | if (rdev2->desc_nr+1 > max_dev) |
| 1823 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1824 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1825 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1826 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1827 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1828 | rdev->sb_size = max_dev * 2 + 256; |
| 1829 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1830 | if (rdev->sb_size & bmask) |
| 1831 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1832 | } else |
| 1833 | max_dev = le32_to_cpu(sb->max_dev); |
| 1834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | for (i=0; i<max_dev;i++) |
| 1836 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1837 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1838 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1840 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1842 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1844 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1845 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | else |
| 1847 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1848 | } |
| 1849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1851 | } |
| 1852 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1853 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1854 | 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] | 1855 | { |
| 1856 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1857 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1858 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1859 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1860 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1861 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1862 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1863 | max_sectors -= rdev->data_offset; |
| 1864 | if (!num_sectors || num_sectors > max_sectors) |
| 1865 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1866 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1867 | /* minor version 0 with bitmap we can't move */ |
| 1868 | return 0; |
| 1869 | } else { |
| 1870 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1871 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1872 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1873 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1874 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1875 | if (!num_sectors || num_sectors > max_sectors) |
| 1876 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1877 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1878 | } |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1879 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1880 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1881 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1882 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1883 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1884 | rdev->sb_page); |
| 1885 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1886 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1889 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | [0] = { |
| 1891 | .name = "0.90.0", |
| 1892 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1893 | .load_super = super_90_load, |
| 1894 | .validate_super = super_90_validate, |
| 1895 | .sync_super = super_90_sync, |
| 1896 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | }, |
| 1898 | [1] = { |
| 1899 | .name = "md-1", |
| 1900 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1901 | .load_super = super_1_load, |
| 1902 | .validate_super = super_1_validate, |
| 1903 | .sync_super = super_1_sync, |
| 1904 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | }, |
| 1906 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1908 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1909 | { |
| 1910 | if (mddev->sync_super) { |
| 1911 | mddev->sync_super(mddev, rdev); |
| 1912 | return; |
| 1913 | } |
| 1914 | |
| 1915 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1916 | |
| 1917 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1918 | } |
| 1919 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1920 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1922 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1924 | rcu_read_lock(); |
| 1925 | rdev_for_each_rcu(rdev, mddev1) |
| 1926 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1927 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1928 | rdev2->bdev->bd_contains) { |
| 1929 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1930 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1931 | } |
| 1932 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | static LIST_HEAD(pending_raid_disks); |
| 1937 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1938 | /* |
| 1939 | * Try to register data integrity profile for an mddev |
| 1940 | * |
| 1941 | * This is called when an array is started and after a disk has been kicked |
| 1942 | * from the array. It only succeeds if all working and active component devices |
| 1943 | * are integrity capable with matching profiles. |
| 1944 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1945 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1946 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1947 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1948 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1949 | if (list_empty(&mddev->disks)) |
| 1950 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1951 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1952 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1953 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1954 | /* skip spares and non-functional disks */ |
| 1955 | if (test_bit(Faulty, &rdev->flags)) |
| 1956 | continue; |
| 1957 | if (rdev->raid_disk < 0) |
| 1958 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1959 | if (!reference) { |
| 1960 | /* Use the first rdev as the reference */ |
| 1961 | reference = rdev; |
| 1962 | continue; |
| 1963 | } |
| 1964 | /* does this rdev's profile match the reference profile? */ |
| 1965 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1966 | rdev->bdev->bd_disk) < 0) |
| 1967 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1968 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1969 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1970 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1971 | /* |
| 1972 | * All component devices are integrity capable and have matching |
| 1973 | * profiles, register the common profile for the md device. |
| 1974 | */ |
| 1975 | if (blk_integrity_register(mddev->gendisk, |
| 1976 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1977 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1978 | mdname(mddev)); |
| 1979 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1980 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1981 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1982 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1983 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1984 | mdname(mddev)); |
| 1985 | return -EINVAL; |
| 1986 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1987 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1988 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1989 | EXPORT_SYMBOL(md_integrity_register); |
| 1990 | |
| 1991 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1992 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1993 | { |
| 1994 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1995 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1996 | |
| 1997 | if (!bi_mddev) /* nothing to do */ |
| 1998 | return; |
| 1999 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2000 | return; |
| 2001 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2002 | rdev->bdev->bd_disk) >= 0) |
| 2003 | return; |
| 2004 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2005 | blk_integrity_unregister(mddev->gendisk); |
| 2006 | } |
| 2007 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2008 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2009 | 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] | 2010 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2011 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2012 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2013 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2014 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
| 2016 | if (rdev->mddev) { |
| 2017 | MD_BUG(); |
| 2018 | return -EINVAL; |
| 2019 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2020 | |
| 2021 | /* prevent duplicates */ |
| 2022 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2023 | return -EEXIST; |
| 2024 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2025 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2026 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2027 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2028 | if (mddev->pers) { |
| 2029 | /* Cannot change size, so fail |
| 2030 | * If mddev->level <= 0, then we don't care |
| 2031 | * about aligning sizes (e.g. linear) |
| 2032 | */ |
| 2033 | if (mddev->level > 0) |
| 2034 | return -ENOSPC; |
| 2035 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2036 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | |
| 2039 | /* Verify rdev->desc_nr is unique. |
| 2040 | * If it is -1, assign a free number, else |
| 2041 | * check number is not in use |
| 2042 | */ |
| 2043 | if (rdev->desc_nr < 0) { |
| 2044 | int choice = 0; |
| 2045 | if (mddev->pers) choice = mddev->raid_disks; |
| 2046 | while (find_rdev_nr(mddev, choice)) |
| 2047 | choice++; |
| 2048 | rdev->desc_nr = choice; |
| 2049 | } else { |
| 2050 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2051 | return -EBUSY; |
| 2052 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2053 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2054 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2055 | mdname(mddev), mddev->max_disks); |
| 2056 | return -EBUSY; |
| 2057 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2058 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2059 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2060 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2063 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2064 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2065 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2066 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2067 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2068 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2069 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2070 | /* failure here is OK */; |
| 2071 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2072 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2073 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2074 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2075 | |
| 2076 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2077 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2080 | |
| 2081 | fail: |
| 2082 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2083 | b, mdname(mddev)); |
| 2084 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2087 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2088 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2089 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2090 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2091 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2094 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | { |
| 2096 | char b[BDEVNAME_SIZE]; |
| 2097 | if (!rdev->mddev) { |
| 2098 | MD_BUG(); |
| 2099 | return; |
| 2100 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2101 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2102 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2104 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2105 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2106 | sysfs_put(rdev->sysfs_state); |
| 2107 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2108 | kfree(rdev->badblocks.page); |
| 2109 | rdev->badblocks.count = 0; |
| 2110 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2111 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2112 | * writing to 'remove' to "dev/state". We also need |
| 2113 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2114 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2115 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2116 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2117 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2118 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * prevent the device from being mounted, repartitioned or |
| 2123 | * otherwise reused by a RAID array (or any other kernel |
| 2124 | * subsystem), by bd_claiming the device. |
| 2125 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2126 | 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] | 2127 | { |
| 2128 | int err = 0; |
| 2129 | struct block_device *bdev; |
| 2130 | char b[BDEVNAME_SIZE]; |
| 2131 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2132 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2133 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | if (IS_ERR(bdev)) { |
| 2135 | printk(KERN_ERR "md: could not open %s.\n", |
| 2136 | __bdevname(dev, b)); |
| 2137 | return PTR_ERR(bdev); |
| 2138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | rdev->bdev = bdev; |
| 2140 | return err; |
| 2141 | } |
| 2142 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2143 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | { |
| 2145 | struct block_device *bdev = rdev->bdev; |
| 2146 | rdev->bdev = NULL; |
| 2147 | if (!bdev) |
| 2148 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2149 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | void md_autodetect_dev(dev_t dev); |
| 2153 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2154 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
| 2156 | char b[BDEVNAME_SIZE]; |
| 2157 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2158 | bdevname(rdev->bdev,b)); |
| 2159 | if (rdev->mddev) |
| 2160 | MD_BUG(); |
| 2161 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2163 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2164 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | #endif |
| 2166 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2167 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2170 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
| 2172 | unbind_rdev_from_array(rdev); |
| 2173 | export_rdev(rdev); |
| 2174 | } |
| 2175 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2176 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2178 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2180 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | if (!rdev->mddev) { |
| 2182 | MD_BUG(); |
| 2183 | continue; |
| 2184 | } |
| 2185 | kick_rdev_from_array(rdev); |
| 2186 | } |
| 2187 | if (!list_empty(&mddev->disks)) |
| 2188 | MD_BUG(); |
| 2189 | mddev->raid_disks = 0; |
| 2190 | mddev->major_version = 0; |
| 2191 | } |
| 2192 | |
| 2193 | static void print_desc(mdp_disk_t *desc) |
| 2194 | { |
| 2195 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2196 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2197 | } |
| 2198 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2199 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | { |
| 2201 | int i; |
| 2202 | |
| 2203 | printk(KERN_INFO |
| 2204 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2205 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2206 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2207 | sb->ctime); |
| 2208 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2209 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2210 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2211 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2212 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2213 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2214 | sb->failed_disks, sb->spare_disks, |
| 2215 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2216 | |
| 2217 | printk(KERN_INFO); |
| 2218 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2219 | mdp_disk_t *desc; |
| 2220 | |
| 2221 | desc = sb->disks + i; |
| 2222 | if (desc->number || desc->major || desc->minor || |
| 2223 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2224 | printk(" D %2d: ", i); |
| 2225 | print_desc(desc); |
| 2226 | } |
| 2227 | } |
| 2228 | printk(KERN_INFO "md: THIS: "); |
| 2229 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2232 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2233 | { |
| 2234 | __u8 *uuid; |
| 2235 | |
| 2236 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2237 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2238 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2239 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2240 | le32_to_cpu(sb->major_version), |
| 2241 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2242 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2243 | sb->set_name, |
| 2244 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2245 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2246 | |
| 2247 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2248 | printk(KERN_INFO |
| 2249 | "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] | 2250 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2251 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2252 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2253 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2254 | le32_to_cpu(sb->level), |
| 2255 | (unsigned long long)le64_to_cpu(sb->size), |
| 2256 | le32_to_cpu(sb->raid_disks), |
| 2257 | le32_to_cpu(sb->layout), |
| 2258 | le32_to_cpu(sb->chunksize), |
| 2259 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2260 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2261 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2262 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2263 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2264 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2265 | sb->devflags, |
| 2266 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2267 | (unsigned long long)le64_to_cpu(sb->events), |
| 2268 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2269 | le32_to_cpu(sb->sb_csum), |
| 2270 | le32_to_cpu(sb->max_dev) |
| 2271 | ); |
| 2272 | } |
| 2273 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2274 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | { |
| 2276 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2277 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2278 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2279 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2280 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2282 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2283 | switch (major_version) { |
| 2284 | case 0: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2285 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2286 | break; |
| 2287 | case 1: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2288 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2289 | break; |
| 2290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | } else |
| 2292 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2293 | } |
| 2294 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2295 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2297 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2298 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2299 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | char b[BDEVNAME_SIZE]; |
| 2301 | |
| 2302 | printk("\n"); |
| 2303 | printk("md: **********************************\n"); |
| 2304 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2305 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2306 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2308 | if (mddev->bitmap) |
| 2309 | bitmap_print_sb(mddev->bitmap); |
| 2310 | else |
| 2311 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2312 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2314 | printk("\n"); |
| 2315 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2316 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2317 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | } |
| 2319 | printk("md: **********************************\n"); |
| 2320 | printk("\n"); |
| 2321 | } |
| 2322 | |
| 2323 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2324 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2326 | /* Update each superblock (in-memory image), but |
| 2327 | * if we are allowed to, skip spares which already |
| 2328 | * have the right event counter, or have one earlier |
| 2329 | * (which would mean they aren't being marked as dirty |
| 2330 | * with the rest of the array) |
| 2331 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2332 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2333 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2334 | if (rdev->sb_events == mddev->events || |
| 2335 | (nospares && |
| 2336 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2337 | rdev->sb_events+1 == mddev->events)) { |
| 2338 | /* Don't update this superblock */ |
| 2339 | rdev->sb_loaded = 2; |
| 2340 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2341 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2342 | rdev->sb_loaded = 1; |
| 2343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2347 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2349 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2350 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2351 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2352 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2355 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2356 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2357 | if (rdev->raid_disk >= 0 && |
| 2358 | mddev->delta_disks >= 0 && |
| 2359 | !test_bit(In_sync, &rdev->flags) && |
| 2360 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2361 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2362 | |
| 2363 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2364 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2365 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2366 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2367 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2368 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2369 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2370 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2371 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2372 | md_ack_all_badblocks(&rdev->badblocks); |
| 2373 | md_error(mddev, rdev); |
| 2374 | } |
| 2375 | clear_bit(Blocked, &rdev->flags); |
| 2376 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2377 | wake_up(&rdev->blocked_wait); |
| 2378 | } |
| 2379 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2380 | wake_up(&mddev->sb_wait); |
| 2381 | return; |
| 2382 | } |
| 2383 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2384 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2385 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2386 | mddev->utime = get_seconds(); |
| 2387 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2388 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2389 | force_change = 1; |
| 2390 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2391 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2392 | * though if events isn't the right even/odd, we will have to do |
| 2393 | * spares after all |
| 2394 | */ |
| 2395 | nospares = 1; |
| 2396 | if (force_change) |
| 2397 | nospares = 0; |
| 2398 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2399 | /* If the array is degraded, then skipping spares is both |
| 2400 | * dangerous and fairly pointless. |
| 2401 | * Dangerous because a device that was removed from the array |
| 2402 | * might have a event_count that still looks up-to-date, |
| 2403 | * so it can be re-added without a resync. |
| 2404 | * Pointless because if there are any spares to skip, |
| 2405 | * then a recovery will happen and soon that array won't |
| 2406 | * be degraded any more and the spare can go back to sleep then. |
| 2407 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2408 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2409 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2410 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | |
| 2412 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2413 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2414 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2415 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2416 | && mddev->can_decrease_events |
| 2417 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2418 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2419 | mddev->can_decrease_events = 0; |
| 2420 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2421 | /* otherwise we have to go forward and ... */ |
| 2422 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2423 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | |
| 2426 | if (!mddev->events) { |
| 2427 | /* |
| 2428 | * oops, this 64-bit counter should never wrap. |
| 2429 | * Either we are in around ~1 trillion A.C., assuming |
| 2430 | * 1 reboot per second, or we have a bug: |
| 2431 | */ |
| 2432 | MD_BUG(); |
| 2433 | mddev->events --; |
| 2434 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2435 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2436 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2437 | if (rdev->badblocks.changed) |
| 2438 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2439 | if (test_bit(Faulty, &rdev->flags)) |
| 2440 | set_bit(FaultRecorded, &rdev->flags); |
| 2441 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2442 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2443 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2444 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2446 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2447 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2449 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2450 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2452 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2453 | if (rdev->sb_loaded != 1) |
| 2454 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2456 | if (!test_bit(Faulty, &rdev->flags) && |
| 2457 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2458 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2459 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2460 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2461 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2462 | bdevname(rdev->bdev, b), |
| 2463 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2464 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2465 | if (rdev->badblocks.size) { |
| 2466 | md_super_write(mddev, rdev, |
| 2467 | rdev->badblocks.sector, |
| 2468 | rdev->badblocks.size << 9, |
| 2469 | rdev->bb_page); |
| 2470 | rdev->badblocks.size = 0; |
| 2471 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2472 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2473 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2474 | pr_debug("md: %s (skipping faulty)\n", |
| 2475 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2476 | else |
| 2477 | pr_debug("(skipping incremental s/r "); |
| 2478 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2479 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | /* only need to write one superblock... */ |
| 2481 | break; |
| 2482 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2483 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2484 | /* 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] | 2485 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2486 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2487 | if (mddev->in_sync != sync_req || |
| 2488 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2489 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2490 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2491 | goto repeat; |
| 2492 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2493 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2494 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2495 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2496 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2497 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2498 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2499 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2500 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2501 | clear_bit(Blocked, &rdev->flags); |
| 2502 | |
| 2503 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2504 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2505 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2506 | wake_up(&rdev->blocked_wait); |
| 2507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2510 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2511 | * We want to accept with case. For this we use cmd_match. |
| 2512 | */ |
| 2513 | static int cmd_match(const char *cmd, const char *str) |
| 2514 | { |
| 2515 | /* See if cmd, written into a sysfs file, matches |
| 2516 | * str. They must either be the same, or cmd can |
| 2517 | * have a trailing newline |
| 2518 | */ |
| 2519 | while (*cmd && *str && *cmd == *str) { |
| 2520 | cmd++; |
| 2521 | str++; |
| 2522 | } |
| 2523 | if (*cmd == '\n') |
| 2524 | cmd++; |
| 2525 | if (*str || *cmd) |
| 2526 | return 0; |
| 2527 | return 1; |
| 2528 | } |
| 2529 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2530 | struct rdev_sysfs_entry { |
| 2531 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2532 | ssize_t (*show)(struct md_rdev *, char *); |
| 2533 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2534 | }; |
| 2535 | |
| 2536 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2537 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2538 | { |
| 2539 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2540 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2541 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2542 | if (test_bit(Faulty, &rdev->flags) || |
| 2543 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2544 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2545 | sep = ","; |
| 2546 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2547 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2548 | len += sprintf(page+len, "%sin_sync",sep); |
| 2549 | sep = ","; |
| 2550 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2551 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2552 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2553 | sep = ","; |
| 2554 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2555 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2556 | (rdev->badblocks.unacked_exist |
| 2557 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2558 | len += sprintf(page+len, "%sblocked", sep); |
| 2559 | sep = ","; |
| 2560 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2561 | if (!test_bit(Faulty, &rdev->flags) && |
| 2562 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2563 | len += sprintf(page+len, "%sspare", sep); |
| 2564 | sep = ","; |
| 2565 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2566 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2567 | len += sprintf(page+len, "%swrite_error", sep); |
| 2568 | sep = ","; |
| 2569 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2570 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2571 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2572 | sep = ","; |
| 2573 | } |
| 2574 | if (test_bit(Replacement, &rdev->flags)) { |
| 2575 | len += sprintf(page+len, "%sreplacement", sep); |
| 2576 | sep = ","; |
| 2577 | } |
| 2578 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2579 | return len+sprintf(page+len, "\n"); |
| 2580 | } |
| 2581 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2582 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2583 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2584 | { |
| 2585 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2586 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2587 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2588 | * writemostly - sets write_mostly |
| 2589 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2590 | * blocked - sets the Blocked flags |
| 2591 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2592 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2593 | * write_error - sets WriteErrorSeen |
| 2594 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2595 | */ |
| 2596 | int err = -EINVAL; |
| 2597 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2598 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2599 | if (test_bit(Faulty, &rdev->flags)) |
| 2600 | err = 0; |
| 2601 | else |
| 2602 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2603 | } else if (cmd_match(buf, "remove")) { |
| 2604 | if (rdev->raid_disk >= 0) |
| 2605 | err = -EBUSY; |
| 2606 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2607 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2608 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2609 | if (mddev->pers) |
| 2610 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2611 | md_new_event(mddev); |
| 2612 | err = 0; |
| 2613 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2614 | } else if (cmd_match(buf, "writemostly")) { |
| 2615 | set_bit(WriteMostly, &rdev->flags); |
| 2616 | err = 0; |
| 2617 | } else if (cmd_match(buf, "-writemostly")) { |
| 2618 | clear_bit(WriteMostly, &rdev->flags); |
| 2619 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2620 | } else if (cmd_match(buf, "blocked")) { |
| 2621 | set_bit(Blocked, &rdev->flags); |
| 2622 | err = 0; |
| 2623 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2624 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2625 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2626 | /* metadata handler doesn't understand badblocks, |
| 2627 | * so we need to fail the device |
| 2628 | */ |
| 2629 | md_error(rdev->mddev, rdev); |
| 2630 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2631 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2632 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2633 | wake_up(&rdev->blocked_wait); |
| 2634 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2635 | md_wakeup_thread(rdev->mddev->thread); |
| 2636 | |
| 2637 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2638 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2639 | set_bit(In_sync, &rdev->flags); |
| 2640 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2641 | } else if (cmd_match(buf, "write_error")) { |
| 2642 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2643 | err = 0; |
| 2644 | } else if (cmd_match(buf, "-write_error")) { |
| 2645 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2646 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2647 | } else if (cmd_match(buf, "want_replacement")) { |
| 2648 | /* Any non-spare device that is not a replacement can |
| 2649 | * become want_replacement at any time, but we then need to |
| 2650 | * check if recovery is needed. |
| 2651 | */ |
| 2652 | if (rdev->raid_disk >= 0 && |
| 2653 | !test_bit(Replacement, &rdev->flags)) |
| 2654 | set_bit(WantReplacement, &rdev->flags); |
| 2655 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2656 | md_wakeup_thread(rdev->mddev->thread); |
| 2657 | err = 0; |
| 2658 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2659 | /* Clearing 'want_replacement' is always allowed. |
| 2660 | * Once replacements starts it is too late though. |
| 2661 | */ |
| 2662 | err = 0; |
| 2663 | clear_bit(WantReplacement, &rdev->flags); |
| 2664 | } else if (cmd_match(buf, "replacement")) { |
| 2665 | /* Can only set a device as a replacement when array has not |
| 2666 | * yet been started. Once running, replacement is automatic |
| 2667 | * from spares, or by assigning 'slot'. |
| 2668 | */ |
| 2669 | if (rdev->mddev->pers) |
| 2670 | err = -EBUSY; |
| 2671 | else { |
| 2672 | set_bit(Replacement, &rdev->flags); |
| 2673 | err = 0; |
| 2674 | } |
| 2675 | } else if (cmd_match(buf, "-replacement")) { |
| 2676 | /* Similarly, can only clear Replacement before start */ |
| 2677 | if (rdev->mddev->pers) |
| 2678 | err = -EBUSY; |
| 2679 | else { |
| 2680 | clear_bit(Replacement, &rdev->flags); |
| 2681 | err = 0; |
| 2682 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2683 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2684 | if (!err) |
| 2685 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2686 | return err ? err : len; |
| 2687 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2688 | static struct rdev_sysfs_entry rdev_state = |
| 2689 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2690 | |
| 2691 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2692 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2693 | { |
| 2694 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2695 | } |
| 2696 | |
| 2697 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2698 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2699 | { |
| 2700 | char *e; |
| 2701 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2702 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2703 | atomic_set(&rdev->corrected_errors, n); |
| 2704 | return len; |
| 2705 | } |
| 2706 | return -EINVAL; |
| 2707 | } |
| 2708 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2709 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2710 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2711 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2712 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2713 | { |
| 2714 | if (rdev->raid_disk < 0) |
| 2715 | return sprintf(page, "none\n"); |
| 2716 | else |
| 2717 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2718 | } |
| 2719 | |
| 2720 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2721 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2722 | { |
| 2723 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2724 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2725 | int slot = simple_strtoul(buf, &e, 10); |
| 2726 | if (strncmp(buf, "none", 4)==0) |
| 2727 | slot = -1; |
| 2728 | else if (e==buf || (*e && *e!= '\n')) |
| 2729 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2730 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2731 | /* Setting 'slot' on an active array requires also |
| 2732 | * updating the 'rd%d' link, and communicating |
| 2733 | * with the personality with ->hot_*_disk. |
| 2734 | * For now we only support removing |
| 2735 | * failed/spare devices. This normally happens automatically, |
| 2736 | * but not when the metadata is externally managed. |
| 2737 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2738 | if (rdev->raid_disk == -1) |
| 2739 | return -EEXIST; |
| 2740 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2741 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2742 | return -EINVAL; |
| 2743 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2744 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2745 | if (err) |
| 2746 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2747 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2748 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2749 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2750 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2751 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2752 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2753 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2754 | */ |
| 2755 | |
| 2756 | if (rdev->raid_disk != -1) |
| 2757 | return -EBUSY; |
| 2758 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2759 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2760 | return -EBUSY; |
| 2761 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2762 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2763 | return -EINVAL; |
| 2764 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2765 | if (slot >= rdev->mddev->raid_disks && |
| 2766 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2767 | return -ENOSPC; |
| 2768 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2769 | rdev->raid_disk = slot; |
| 2770 | if (test_bit(In_sync, &rdev->flags)) |
| 2771 | rdev->saved_raid_disk = slot; |
| 2772 | else |
| 2773 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2774 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2775 | err = rdev->mddev->pers-> |
| 2776 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2777 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2778 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2779 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2780 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2781 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2782 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2783 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2784 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2785 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2786 | if (slot >= rdev->mddev->raid_disks && |
| 2787 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2788 | return -ENOSPC; |
| 2789 | rdev->raid_disk = slot; |
| 2790 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2791 | clear_bit(Faulty, &rdev->flags); |
| 2792 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2793 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2794 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2795 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2796 | return len; |
| 2797 | } |
| 2798 | |
| 2799 | |
| 2800 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2801 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2802 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2803 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2804 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2805 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2806 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2810 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2811 | { |
| 2812 | char *e; |
| 2813 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2814 | if (e==buf || (*e && *e != '\n')) |
| 2815 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2816 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2817 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2818 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2819 | /* Must set offset before size, so overlap checks |
| 2820 | * can be sane */ |
| 2821 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2822 | rdev->data_offset = offset; |
| 2823 | return len; |
| 2824 | } |
| 2825 | |
| 2826 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2827 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2828 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2829 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2830 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2831 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2832 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2835 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2836 | { |
| 2837 | /* check if two start/length pairs overlap */ |
| 2838 | if (s1+l1 <= s2) |
| 2839 | return 0; |
| 2840 | if (s2+l2 <= s1) |
| 2841 | return 0; |
| 2842 | return 1; |
| 2843 | } |
| 2844 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2845 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2846 | { |
| 2847 | unsigned long long blocks; |
| 2848 | sector_t new; |
| 2849 | |
| 2850 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2851 | return -EINVAL; |
| 2852 | |
| 2853 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2854 | return -EINVAL; /* sector conversion overflow */ |
| 2855 | |
| 2856 | new = blocks * 2; |
| 2857 | if (new != blocks * 2) |
| 2858 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2859 | |
| 2860 | *sectors = new; |
| 2861 | return 0; |
| 2862 | } |
| 2863 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2864 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2865 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2866 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2867 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2868 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2869 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2870 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2871 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2872 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2873 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2874 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2875 | sectors = super_types[my_mddev->major_version]. |
| 2876 | rdev_size_change(rdev, sectors); |
| 2877 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2878 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2879 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2880 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2881 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2882 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2883 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2884 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2885 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2886 | rdev->sectors = sectors; |
| 2887 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2888 | /* need to check that all other rdevs with the same ->bdev |
| 2889 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2890 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2891 | * we have to change it back, we will have the lock again. |
| 2892 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2893 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2894 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2895 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2896 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2897 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2898 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2899 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2900 | |
| 2901 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2902 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2903 | if (rdev->bdev == rdev2->bdev && |
| 2904 | rdev != rdev2 && |
| 2905 | overlaps(rdev->data_offset, rdev->sectors, |
| 2906 | rdev2->data_offset, |
| 2907 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2908 | overlap = 1; |
| 2909 | break; |
| 2910 | } |
| 2911 | mddev_unlock(mddev); |
| 2912 | if (overlap) { |
| 2913 | mddev_put(mddev); |
| 2914 | break; |
| 2915 | } |
| 2916 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2917 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2918 | if (overlap) { |
| 2919 | /* Someone else could have slipped in a size |
| 2920 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2921 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2922 | * safe, and trust userspace not to race with |
| 2923 | * itself |
| 2924 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2925 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2926 | return -EBUSY; |
| 2927 | } |
| 2928 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2929 | return len; |
| 2930 | } |
| 2931 | |
| 2932 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2933 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2934 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2935 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2936 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2937 | { |
| 2938 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2939 | |
| 2940 | if (test_bit(In_sync, &rdev->flags) || |
| 2941 | recovery_start == MaxSector) |
| 2942 | return sprintf(page, "none\n"); |
| 2943 | |
| 2944 | return sprintf(page, "%llu\n", recovery_start); |
| 2945 | } |
| 2946 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2947 | 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] | 2948 | { |
| 2949 | unsigned long long recovery_start; |
| 2950 | |
| 2951 | if (cmd_match(buf, "none")) |
| 2952 | recovery_start = MaxSector; |
| 2953 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2954 | return -EINVAL; |
| 2955 | |
| 2956 | if (rdev->mddev->pers && |
| 2957 | rdev->raid_disk >= 0) |
| 2958 | return -EBUSY; |
| 2959 | |
| 2960 | rdev->recovery_offset = recovery_start; |
| 2961 | if (recovery_start == MaxSector) |
| 2962 | set_bit(In_sync, &rdev->flags); |
| 2963 | else |
| 2964 | clear_bit(In_sync, &rdev->flags); |
| 2965 | return len; |
| 2966 | } |
| 2967 | |
| 2968 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2969 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2970 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2971 | |
| 2972 | static ssize_t |
| 2973 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2974 | static ssize_t |
| 2975 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2976 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2977 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2978 | { |
| 2979 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2980 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2981 | 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] | 2982 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2983 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2984 | /* Maybe that ack was all we needed */ |
| 2985 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2986 | wake_up(&rdev->blocked_wait); |
| 2987 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2988 | } |
| 2989 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2990 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2991 | |
| 2992 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2993 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2994 | { |
| 2995 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2996 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2997 | 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] | 2998 | { |
| 2999 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3000 | } |
| 3001 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3002 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3003 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3004 | static struct attribute *rdev_default_attrs[] = { |
| 3005 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3006 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3007 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3008 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3009 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3010 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3011 | &rdev_bad_blocks.attr, |
| 3012 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3013 | NULL, |
| 3014 | }; |
| 3015 | static ssize_t |
| 3016 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3017 | { |
| 3018 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3019 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3020 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3021 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3022 | |
| 3023 | if (!entry->show) |
| 3024 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3025 | |
| 3026 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3027 | if (!rv) { |
| 3028 | if (rdev->mddev == NULL) |
| 3029 | rv = -EBUSY; |
| 3030 | else |
| 3031 | rv = entry->show(rdev, page); |
| 3032 | mddev_unlock(mddev); |
| 3033 | } |
| 3034 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | static ssize_t |
| 3038 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3039 | const char *page, size_t length) |
| 3040 | { |
| 3041 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3042 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3043 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3044 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3045 | |
| 3046 | if (!entry->store) |
| 3047 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3048 | if (!capable(CAP_SYS_ADMIN)) |
| 3049 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3050 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3051 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3052 | if (rdev->mddev == NULL) |
| 3053 | rv = -EBUSY; |
| 3054 | else |
| 3055 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3056 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3057 | } |
| 3058 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | static void rdev_free(struct kobject *ko) |
| 3062 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3063 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3064 | kfree(rdev); |
| 3065 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3066 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3067 | .show = rdev_attr_show, |
| 3068 | .store = rdev_attr_store, |
| 3069 | }; |
| 3070 | static struct kobj_type rdev_ktype = { |
| 3071 | .release = rdev_free, |
| 3072 | .sysfs_ops = &rdev_sysfs_ops, |
| 3073 | .default_attrs = rdev_default_attrs, |
| 3074 | }; |
| 3075 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3076 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3077 | { |
| 3078 | rdev->desc_nr = -1; |
| 3079 | rdev->saved_raid_disk = -1; |
| 3080 | rdev->raid_disk = -1; |
| 3081 | rdev->flags = 0; |
| 3082 | rdev->data_offset = 0; |
| 3083 | rdev->sb_events = 0; |
| 3084 | rdev->last_read_error.tv_sec = 0; |
| 3085 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3086 | rdev->sb_loaded = 0; |
| 3087 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3088 | atomic_set(&rdev->nr_pending, 0); |
| 3089 | atomic_set(&rdev->read_errors, 0); |
| 3090 | atomic_set(&rdev->corrected_errors, 0); |
| 3091 | |
| 3092 | INIT_LIST_HEAD(&rdev->same_set); |
| 3093 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3094 | |
| 3095 | /* Add space to store bad block list. |
| 3096 | * This reserves the space even on arrays where it cannot |
| 3097 | * be used - I wonder if that matters |
| 3098 | */ |
| 3099 | rdev->badblocks.count = 0; |
| 3100 | rdev->badblocks.shift = 0; |
| 3101 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3102 | seqlock_init(&rdev->badblocks.lock); |
| 3103 | if (rdev->badblocks.page == NULL) |
| 3104 | return -ENOMEM; |
| 3105 | |
| 3106 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3107 | } |
| 3108 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | /* |
| 3110 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3111 | * |
| 3112 | * mark the device faulty if: |
| 3113 | * |
| 3114 | * - the device is nonexistent (zero size) |
| 3115 | * - the device has no valid superblock |
| 3116 | * |
| 3117 | * a faulty rdev _never_ has rdev->sb set. |
| 3118 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3119 | 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] | 3120 | { |
| 3121 | char b[BDEVNAME_SIZE]; |
| 3122 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3123 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | sector_t size; |
| 3125 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3126 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | if (!rdev) { |
| 3128 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3129 | return ERR_PTR(-ENOMEM); |
| 3130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3132 | err = md_rdev_init(rdev); |
| 3133 | if (err) |
| 3134 | goto abort_free; |
| 3135 | err = alloc_disk_sb(rdev); |
| 3136 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | goto abort_free; |
| 3138 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3139 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | if (err) |
| 3141 | goto abort_free; |
| 3142 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3143 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3144 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3145 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | if (!size) { |
| 3147 | printk(KERN_WARNING |
| 3148 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3149 | bdevname(rdev->bdev,b)); |
| 3150 | err = -EINVAL; |
| 3151 | goto abort_free; |
| 3152 | } |
| 3153 | |
| 3154 | if (super_format >= 0) { |
| 3155 | err = super_types[super_format]. |
| 3156 | load_super(rdev, NULL, super_minor); |
| 3157 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3158 | printk(KERN_WARNING |
| 3159 | "md: %s does not have a valid v%d.%d " |
| 3160 | "superblock, not importing!\n", |
| 3161 | bdevname(rdev->bdev,b), |
| 3162 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | goto abort_free; |
| 3164 | } |
| 3165 | if (err < 0) { |
| 3166 | printk(KERN_WARNING |
| 3167 | "md: could not read %s's sb, not importing!\n", |
| 3168 | bdevname(rdev->bdev,b)); |
| 3169 | goto abort_free; |
| 3170 | } |
| 3171 | } |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3172 | if (super_format == -1) |
| 3173 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3174 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | return rdev; |
| 3177 | |
| 3178 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3179 | if (rdev->bdev) |
| 3180 | unlock_rdev(rdev); |
| 3181 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3182 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | kfree(rdev); |
| 3184 | return ERR_PTR(err); |
| 3185 | } |
| 3186 | |
| 3187 | /* |
| 3188 | * Check a full RAID array for plausibility |
| 3189 | */ |
| 3190 | |
| 3191 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3192 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | { |
| 3194 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3195 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | char b[BDEVNAME_SIZE]; |
| 3197 | |
| 3198 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3199 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | switch (super_types[mddev->major_version]. |
| 3201 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3202 | case 1: |
| 3203 | freshest = rdev; |
| 3204 | break; |
| 3205 | case 0: |
| 3206 | break; |
| 3207 | default: |
| 3208 | printk( KERN_ERR \ |
| 3209 | "md: fatal superblock inconsistency in %s" |
| 3210 | " -- removing from array\n", |
| 3211 | bdevname(rdev->bdev,b)); |
| 3212 | kick_rdev_from_array(rdev); |
| 3213 | } |
| 3214 | |
| 3215 | |
| 3216 | super_types[mddev->major_version]. |
| 3217 | validate_super(mddev, freshest); |
| 3218 | |
| 3219 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3220 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3221 | if (mddev->max_disks && |
| 3222 | (rdev->desc_nr >= mddev->max_disks || |
| 3223 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3224 | printk(KERN_WARNING |
| 3225 | "md: %s: %s: only %d devices permitted\n", |
| 3226 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3227 | mddev->max_disks); |
| 3228 | kick_rdev_from_array(rdev); |
| 3229 | continue; |
| 3230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | if (rdev != freshest) |
| 3232 | if (super_types[mddev->major_version]. |
| 3233 | validate_super(mddev, rdev)) { |
| 3234 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3235 | " from array!\n", |
| 3236 | bdevname(rdev->bdev,b)); |
| 3237 | kick_rdev_from_array(rdev); |
| 3238 | continue; |
| 3239 | } |
| 3240 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3241 | rdev->desc_nr = i++; |
| 3242 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3243 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3244 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3245 | rdev->raid_disk = -1; |
| 3246 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | } |
| 3248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | } |
| 3250 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3251 | /* Read a fixed-point number. |
| 3252 | * Numbers in sysfs attributes should be in "standard" units where |
| 3253 | * possible, so time should be in seconds. |
| 3254 | * However we internally use a a much smaller unit such as |
| 3255 | * milliseconds or jiffies. |
| 3256 | * This function takes a decimal number with a possible fractional |
| 3257 | * component, and produces an integer which is the result of |
| 3258 | * multiplying that number by 10^'scale'. |
| 3259 | * all without any floating-point arithmetic. |
| 3260 | */ |
| 3261 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3262 | { |
| 3263 | unsigned long result = 0; |
| 3264 | long decimals = -1; |
| 3265 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3266 | if (*cp == '.') |
| 3267 | decimals = 0; |
| 3268 | else if (decimals < scale) { |
| 3269 | unsigned int value; |
| 3270 | value = *cp - '0'; |
| 3271 | result = result * 10 + value; |
| 3272 | if (decimals >= 0) |
| 3273 | decimals++; |
| 3274 | } |
| 3275 | cp++; |
| 3276 | } |
| 3277 | if (*cp == '\n') |
| 3278 | cp++; |
| 3279 | if (*cp) |
| 3280 | return -EINVAL; |
| 3281 | if (decimals < 0) |
| 3282 | decimals = 0; |
| 3283 | while (decimals < scale) { |
| 3284 | result *= 10; |
| 3285 | decimals ++; |
| 3286 | } |
| 3287 | *res = result; |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
| 3291 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3292 | static void md_safemode_timeout(unsigned long data); |
| 3293 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3294 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3295 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3296 | { |
| 3297 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3298 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3299 | } |
| 3300 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3301 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3302 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3303 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3304 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3305 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3306 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3307 | if (msec == 0) |
| 3308 | mddev->safemode_delay = 0; |
| 3309 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3310 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3311 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3312 | if (mddev->safemode_delay == 0) |
| 3313 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3314 | if (mddev->safemode_delay < old_delay) |
| 3315 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3316 | } |
| 3317 | return len; |
| 3318 | } |
| 3319 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3320 | __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] | 3321 | |
| 3322 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3323 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3324 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3325 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3326 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3327 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3328 | else if (mddev->clevel[0]) |
| 3329 | return sprintf(page, "%s\n", mddev->clevel); |
| 3330 | else if (mddev->level != LEVEL_NONE) |
| 3331 | return sprintf(page, "%d\n", mddev->level); |
| 3332 | else |
| 3333 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3336 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3337 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3338 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3339 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3340 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3341 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3342 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3343 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3344 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3345 | |
| 3346 | if (mddev->pers == NULL) { |
| 3347 | if (len == 0) |
| 3348 | return 0; |
| 3349 | if (len >= sizeof(mddev->clevel)) |
| 3350 | return -ENOSPC; |
| 3351 | strncpy(mddev->clevel, buf, len); |
| 3352 | if (mddev->clevel[len-1] == '\n') |
| 3353 | len--; |
| 3354 | mddev->clevel[len] = 0; |
| 3355 | mddev->level = LEVEL_NONE; |
| 3356 | return rv; |
| 3357 | } |
| 3358 | |
| 3359 | /* request to change the personality. Need to ensure: |
| 3360 | * - array is not engaged in resync/recovery/reshape |
| 3361 | * - old personality can be suspended |
| 3362 | * - new personality will access other array. |
| 3363 | */ |
| 3364 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3365 | if (mddev->sync_thread || |
| 3366 | mddev->reshape_position != MaxSector || |
| 3367 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3368 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3369 | |
| 3370 | if (!mddev->pers->quiesce) { |
| 3371 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3372 | mdname(mddev), mddev->pers->name); |
| 3373 | return -EINVAL; |
| 3374 | } |
| 3375 | |
| 3376 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3377 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3378 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3379 | strncpy(clevel, buf, len); |
| 3380 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3381 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3382 | clevel[len] = 0; |
| 3383 | if (strict_strtol(clevel, 10, &level)) |
| 3384 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3385 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3386 | if (request_module("md-%s", clevel) != 0) |
| 3387 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3388 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3389 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3390 | if (!pers || !try_module_get(pers->owner)) { |
| 3391 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3392 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3393 | return -EINVAL; |
| 3394 | } |
| 3395 | spin_unlock(&pers_lock); |
| 3396 | |
| 3397 | if (pers == mddev->pers) { |
| 3398 | /* Nothing to do! */ |
| 3399 | module_put(pers->owner); |
| 3400 | return rv; |
| 3401 | } |
| 3402 | if (!pers->takeover) { |
| 3403 | module_put(pers->owner); |
| 3404 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3405 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3406 | return -EINVAL; |
| 3407 | } |
| 3408 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3409 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3410 | rdev->new_raid_disk = rdev->raid_disk; |
| 3411 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3412 | /* ->takeover must set new_* and/or delta_disks |
| 3413 | * if it succeeds, and may set them when it fails. |
| 3414 | */ |
| 3415 | priv = pers->takeover(mddev); |
| 3416 | if (IS_ERR(priv)) { |
| 3417 | mddev->new_level = mddev->level; |
| 3418 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3419 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3420 | mddev->raid_disks -= mddev->delta_disks; |
| 3421 | mddev->delta_disks = 0; |
| 3422 | module_put(pers->owner); |
| 3423 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3424 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3425 | return PTR_ERR(priv); |
| 3426 | } |
| 3427 | |
| 3428 | /* Looks like we have a winner */ |
| 3429 | mddev_suspend(mddev); |
| 3430 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3431 | |
| 3432 | if (mddev->pers->sync_request == NULL && |
| 3433 | pers->sync_request != NULL) { |
| 3434 | /* need to add the md_redundancy_group */ |
| 3435 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3436 | printk(KERN_WARNING |
| 3437 | "md: cannot register extra attributes for %s\n", |
| 3438 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3439 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3440 | } |
| 3441 | if (mddev->pers->sync_request != NULL && |
| 3442 | pers->sync_request == NULL) { |
| 3443 | /* need to remove the md_redundancy_group */ |
| 3444 | if (mddev->to_remove == NULL) |
| 3445 | mddev->to_remove = &md_redundancy_group; |
| 3446 | } |
| 3447 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3448 | if (mddev->pers->sync_request == NULL && |
| 3449 | mddev->external) { |
| 3450 | /* We are converting from a no-redundancy array |
| 3451 | * to a redundancy array and metadata is managed |
| 3452 | * externally so we need to be sure that writes |
| 3453 | * won't block due to a need to transition |
| 3454 | * clean->dirty |
| 3455 | * until external management is started. |
| 3456 | */ |
| 3457 | mddev->in_sync = 0; |
| 3458 | mddev->safemode_delay = 0; |
| 3459 | mddev->safemode = 0; |
| 3460 | } |
| 3461 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3462 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3463 | if (rdev->raid_disk < 0) |
| 3464 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3465 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3466 | rdev->new_raid_disk = -1; |
| 3467 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3468 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3469 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3470 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3471 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3472 | if (rdev->raid_disk < 0) |
| 3473 | continue; |
| 3474 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3475 | continue; |
| 3476 | rdev->raid_disk = rdev->new_raid_disk; |
| 3477 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3478 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3479 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3480 | if (sysfs_link_rdev(mddev, rdev)) |
| 3481 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3482 | " for %s after level change\n", |
| 3483 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3484 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3488 | mddev->pers = pers; |
| 3489 | mddev->private = priv; |
| 3490 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3491 | mddev->level = mddev->new_level; |
| 3492 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3493 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3494 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3495 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3496 | if (mddev->pers->sync_request == NULL) { |
| 3497 | /* this is now an array without redundancy, so |
| 3498 | * it must always be in_sync |
| 3499 | */ |
| 3500 | mddev->in_sync = 1; |
| 3501 | del_timer_sync(&mddev->safemode_timer); |
| 3502 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3503 | pers->run(mddev); |
| 3504 | mddev_resume(mddev); |
| 3505 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3506 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3507 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3508 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3509 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3510 | return rv; |
| 3511 | } |
| 3512 | |
| 3513 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3514 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3515 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3516 | |
| 3517 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3518 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3519 | { |
| 3520 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3521 | if (mddev->reshape_position != MaxSector && |
| 3522 | mddev->layout != mddev->new_layout) |
| 3523 | return sprintf(page, "%d (%d)\n", |
| 3524 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3525 | return sprintf(page, "%d\n", mddev->layout); |
| 3526 | } |
| 3527 | |
| 3528 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3529 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3530 | { |
| 3531 | char *e; |
| 3532 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3533 | |
| 3534 | if (!*buf || (*e && *e != '\n')) |
| 3535 | return -EINVAL; |
| 3536 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3537 | if (mddev->pers) { |
| 3538 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3539 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3540 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3541 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3542 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3543 | if (err) { |
| 3544 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3545 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3546 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3547 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3548 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3549 | if (mddev->reshape_position == MaxSector) |
| 3550 | mddev->layout = n; |
| 3551 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3552 | return len; |
| 3553 | } |
| 3554 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3555 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3556 | |
| 3557 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3558 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3559 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3560 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3561 | if (mddev->raid_disks == 0) |
| 3562 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3563 | if (mddev->reshape_position != MaxSector && |
| 3564 | mddev->delta_disks != 0) |
| 3565 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3566 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3567 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3568 | } |
| 3569 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3570 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3571 | |
| 3572 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3573 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3574 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3575 | char *e; |
| 3576 | int rv = 0; |
| 3577 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3578 | |
| 3579 | if (!*buf || (*e && *e != '\n')) |
| 3580 | return -EINVAL; |
| 3581 | |
| 3582 | if (mddev->pers) |
| 3583 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3584 | else if (mddev->reshape_position != MaxSector) { |
| 3585 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3586 | mddev->delta_disks = n - olddisks; |
| 3587 | mddev->raid_disks = n; |
| 3588 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3589 | mddev->raid_disks = n; |
| 3590 | return rv ? rv : len; |
| 3591 | } |
| 3592 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3593 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3594 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3595 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3596 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3597 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3598 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3599 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3600 | return sprintf(page, "%d (%d)\n", |
| 3601 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3602 | mddev->chunk_sectors << 9); |
| 3603 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3607 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3608 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3609 | char *e; |
| 3610 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3611 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3612 | if (!*buf || (*e && *e != '\n')) |
| 3613 | return -EINVAL; |
| 3614 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3615 | if (mddev->pers) { |
| 3616 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3617 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3618 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3619 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3620 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3621 | if (err) { |
| 3622 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3623 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3624 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3625 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3626 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3627 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3628 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3629 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3630 | return len; |
| 3631 | } |
| 3632 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3633 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3634 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3635 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3636 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3637 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3638 | if (mddev->recovery_cp == MaxSector) |
| 3639 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3640 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3641 | } |
| 3642 | |
| 3643 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3644 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3645 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3646 | char *e; |
| 3647 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3648 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3649 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3650 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3651 | if (cmd_match(buf, "none")) |
| 3652 | n = MaxSector; |
| 3653 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3654 | return -EINVAL; |
| 3655 | |
| 3656 | mddev->recovery_cp = n; |
| 3657 | return len; |
| 3658 | } |
| 3659 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3660 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3661 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3662 | /* |
| 3663 | * The array state can be: |
| 3664 | * |
| 3665 | * clear |
| 3666 | * No devices, no size, no level |
| 3667 | * Equivalent to STOP_ARRAY ioctl |
| 3668 | * inactive |
| 3669 | * May have some settings, but array is not active |
| 3670 | * all IO results in error |
| 3671 | * When written, doesn't tear down array, but just stops it |
| 3672 | * suspended (not supported yet) |
| 3673 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3674 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3675 | * readonly |
| 3676 | * no resync can happen. no superblocks get written. |
| 3677 | * write requests fail |
| 3678 | * read-auto |
| 3679 | * like readonly, but behaves like 'clean' on a write request. |
| 3680 | * |
| 3681 | * clean - no pending writes, but otherwise active. |
| 3682 | * When written to inactive array, starts without resync |
| 3683 | * If a write request arrives then |
| 3684 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3685 | * if not known, block and switch to write-pending |
| 3686 | * If written to an active array that has pending writes, then fails. |
| 3687 | * active |
| 3688 | * fully active: IO and resync can be happening. |
| 3689 | * When written to inactive array, starts with resync |
| 3690 | * |
| 3691 | * write-pending |
| 3692 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3693 | * |
| 3694 | * active-idle |
| 3695 | * like active, but no writes have been seen for a while (100msec). |
| 3696 | * |
| 3697 | */ |
| 3698 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3699 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3700 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3701 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3702 | "write-pending", "active-idle", NULL }; |
| 3703 | |
| 3704 | static int match_word(const char *word, char **list) |
| 3705 | { |
| 3706 | int n; |
| 3707 | for (n=0; list[n]; n++) |
| 3708 | if (cmd_match(word, list[n])) |
| 3709 | break; |
| 3710 | return n; |
| 3711 | } |
| 3712 | |
| 3713 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3714 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3715 | { |
| 3716 | enum array_state st = inactive; |
| 3717 | |
| 3718 | if (mddev->pers) |
| 3719 | switch(mddev->ro) { |
| 3720 | case 1: |
| 3721 | st = readonly; |
| 3722 | break; |
| 3723 | case 2: |
| 3724 | st = read_auto; |
| 3725 | break; |
| 3726 | case 0: |
| 3727 | if (mddev->in_sync) |
| 3728 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3729 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3730 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3731 | else if (mddev->safemode) |
| 3732 | st = active_idle; |
| 3733 | else |
| 3734 | st = active; |
| 3735 | } |
| 3736 | else { |
| 3737 | if (list_empty(&mddev->disks) && |
| 3738 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3739 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3740 | st = clear; |
| 3741 | else |
| 3742 | st = inactive; |
| 3743 | } |
| 3744 | return sprintf(page, "%s\n", array_states[st]); |
| 3745 | } |
| 3746 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3747 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3748 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3749 | static int do_md_run(struct mddev * mddev); |
| 3750 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3751 | |
| 3752 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3753 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3754 | { |
| 3755 | int err = -EINVAL; |
| 3756 | enum array_state st = match_word(buf, array_states); |
| 3757 | switch(st) { |
| 3758 | case bad_word: |
| 3759 | break; |
| 3760 | case clear: |
| 3761 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3762 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3763 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3764 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3765 | break; |
| 3766 | case inactive: |
| 3767 | /* stopping an active array */ |
| 3768 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3769 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3770 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3771 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3772 | } else |
| 3773 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3774 | break; |
| 3775 | case suspended: |
| 3776 | break; /* not supported yet */ |
| 3777 | case readonly: |
| 3778 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3779 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3780 | else { |
| 3781 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3782 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3783 | err = do_md_run(mddev); |
| 3784 | } |
| 3785 | break; |
| 3786 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3787 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3788 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3789 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3790 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3791 | err = restart_array(mddev); |
| 3792 | if (err == 0) { |
| 3793 | mddev->ro = 2; |
| 3794 | set_disk_ro(mddev->gendisk, 0); |
| 3795 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3796 | } else { |
| 3797 | mddev->ro = 2; |
| 3798 | err = do_md_run(mddev); |
| 3799 | } |
| 3800 | break; |
| 3801 | case clean: |
| 3802 | if (mddev->pers) { |
| 3803 | restart_array(mddev); |
| 3804 | spin_lock_irq(&mddev->write_lock); |
| 3805 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3806 | if (mddev->in_sync == 0) { |
| 3807 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3808 | if (mddev->safemode == 1) |
| 3809 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3810 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3811 | } |
| 3812 | err = 0; |
| 3813 | } else |
| 3814 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3815 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3816 | } else |
| 3817 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3818 | break; |
| 3819 | case active: |
| 3820 | if (mddev->pers) { |
| 3821 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3822 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3823 | wake_up(&mddev->sb_wait); |
| 3824 | err = 0; |
| 3825 | } else { |
| 3826 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3827 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3828 | err = do_md_run(mddev); |
| 3829 | } |
| 3830 | break; |
| 3831 | case write_pending: |
| 3832 | case active_idle: |
| 3833 | /* these cannot be set */ |
| 3834 | break; |
| 3835 | } |
| 3836 | if (err) |
| 3837 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3838 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3839 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3840 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3841 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3842 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3843 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3844 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3845 | static struct md_sysfs_entry md_array_state = |
| 3846 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3847 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3848 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3849 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3850 | return sprintf(page, "%d\n", |
| 3851 | atomic_read(&mddev->max_corr_read_errors)); |
| 3852 | } |
| 3853 | |
| 3854 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3855 | 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] | 3856 | { |
| 3857 | char *e; |
| 3858 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3859 | |
| 3860 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3861 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3862 | return len; |
| 3863 | } |
| 3864 | return -EINVAL; |
| 3865 | } |
| 3866 | |
| 3867 | static struct md_sysfs_entry max_corr_read_errors = |
| 3868 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3869 | max_corrected_read_errors_store); |
| 3870 | |
| 3871 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3872 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3873 | { |
| 3874 | return -EINVAL; |
| 3875 | } |
| 3876 | |
| 3877 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3878 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3879 | { |
| 3880 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3881 | /* The new device is added to the array. |
| 3882 | * If the array has a persistent superblock, we read the |
| 3883 | * superblock to initialise info and check validity. |
| 3884 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3885 | * which mainly checks size. |
| 3886 | */ |
| 3887 | char *e; |
| 3888 | int major = simple_strtoul(buf, &e, 10); |
| 3889 | int minor; |
| 3890 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3891 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3892 | int err; |
| 3893 | |
| 3894 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3895 | return -EINVAL; |
| 3896 | minor = simple_strtoul(e+1, &e, 10); |
| 3897 | if (*e && *e != '\n') |
| 3898 | return -EINVAL; |
| 3899 | dev = MKDEV(major, minor); |
| 3900 | if (major != MAJOR(dev) || |
| 3901 | minor != MINOR(dev)) |
| 3902 | return -EOVERFLOW; |
| 3903 | |
| 3904 | |
| 3905 | if (mddev->persistent) { |
| 3906 | rdev = md_import_device(dev, mddev->major_version, |
| 3907 | mddev->minor_version); |
| 3908 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3909 | struct md_rdev *rdev0 |
| 3910 | = list_entry(mddev->disks.next, |
| 3911 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3912 | err = super_types[mddev->major_version] |
| 3913 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3914 | if (err < 0) |
| 3915 | goto out; |
| 3916 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3917 | } else if (mddev->external) |
| 3918 | rdev = md_import_device(dev, -2, -1); |
| 3919 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3920 | rdev = md_import_device(dev, -1, -1); |
| 3921 | |
| 3922 | if (IS_ERR(rdev)) |
| 3923 | return PTR_ERR(rdev); |
| 3924 | err = bind_rdev_to_array(rdev, mddev); |
| 3925 | out: |
| 3926 | if (err) |
| 3927 | export_rdev(rdev); |
| 3928 | return err ? err : len; |
| 3929 | } |
| 3930 | |
| 3931 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3932 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3933 | |
| 3934 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3935 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3936 | { |
| 3937 | char *end; |
| 3938 | unsigned long chunk, end_chunk; |
| 3939 | |
| 3940 | if (!mddev->bitmap) |
| 3941 | goto out; |
| 3942 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3943 | while (*buf) { |
| 3944 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3945 | if (buf == end) break; |
| 3946 | if (*end == '-') { /* range */ |
| 3947 | buf = end + 1; |
| 3948 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3949 | if (buf == end) break; |
| 3950 | } |
| 3951 | if (*end && !isspace(*end)) break; |
| 3952 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3953 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3954 | } |
| 3955 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3956 | out: |
| 3957 | return len; |
| 3958 | } |
| 3959 | |
| 3960 | static struct md_sysfs_entry md_bitmap = |
| 3961 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3962 | |
| 3963 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3964 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3965 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3966 | return sprintf(page, "%llu\n", |
| 3967 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3968 | } |
| 3969 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3970 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3971 | |
| 3972 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3973 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3974 | { |
| 3975 | /* If array is inactive, we can reduce the component size, but |
| 3976 | * not increase it (except from 0). |
| 3977 | * If array is active, we can try an on-line resize |
| 3978 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3979 | sector_t sectors; |
| 3980 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3981 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3982 | if (err < 0) |
| 3983 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3984 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3985 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3986 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3987 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3988 | if (mddev->dev_sectors == 0 || |
| 3989 | mddev->dev_sectors > sectors) |
| 3990 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3991 | else |
| 3992 | err = -ENOSPC; |
| 3993 | } |
| 3994 | return err ? err : len; |
| 3995 | } |
| 3996 | |
| 3997 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3998 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3999 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4000 | |
| 4001 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4002 | * This is one of |
| 4003 | * 'none' for arrays with no metadata (good luck...) |
| 4004 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4005 | * or N.M for internally known formats |
| 4006 | */ |
| 4007 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4008 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4009 | { |
| 4010 | if (mddev->persistent) |
| 4011 | return sprintf(page, "%d.%d\n", |
| 4012 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4013 | else if (mddev->external) |
| 4014 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4015 | else |
| 4016 | return sprintf(page, "none\n"); |
| 4017 | } |
| 4018 | |
| 4019 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4020 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4021 | { |
| 4022 | int major, minor; |
| 4023 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4024 | /* Changing the details of 'external' metadata is |
| 4025 | * always permitted. Otherwise there must be |
| 4026 | * no devices attached to the array. |
| 4027 | */ |
| 4028 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4029 | ; |
| 4030 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4031 | return -EBUSY; |
| 4032 | |
| 4033 | if (cmd_match(buf, "none")) { |
| 4034 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4035 | mddev->external = 0; |
| 4036 | mddev->major_version = 0; |
| 4037 | mddev->minor_version = 90; |
| 4038 | return len; |
| 4039 | } |
| 4040 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4041 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4042 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4043 | namelen = sizeof(mddev->metadata_type)-1; |
| 4044 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4045 | mddev->metadata_type[namelen] = 0; |
| 4046 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4047 | mddev->metadata_type[--namelen] = 0; |
| 4048 | mddev->persistent = 0; |
| 4049 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4050 | mddev->major_version = 0; |
| 4051 | mddev->minor_version = 90; |
| 4052 | return len; |
| 4053 | } |
| 4054 | major = simple_strtoul(buf, &e, 10); |
| 4055 | if (e==buf || *e != '.') |
| 4056 | return -EINVAL; |
| 4057 | buf = e+1; |
| 4058 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4059 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4060 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4061 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4062 | return -ENOENT; |
| 4063 | mddev->major_version = major; |
| 4064 | mddev->minor_version = minor; |
| 4065 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4066 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4067 | return len; |
| 4068 | } |
| 4069 | |
| 4070 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4071 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4072 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4073 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4074 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4075 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4076 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4077 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4078 | type = "frozen"; |
| 4079 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4080 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4081 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4082 | type = "reshape"; |
| 4083 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4084 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4085 | type = "resync"; |
| 4086 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4087 | type = "check"; |
| 4088 | else |
| 4089 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4090 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4091 | type = "recover"; |
| 4092 | } |
| 4093 | return sprintf(page, "%s\n", type); |
| 4094 | } |
| 4095 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4096 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4097 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4098 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4099 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4100 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4101 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4102 | return -EINVAL; |
| 4103 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4104 | if (cmd_match(page, "frozen")) |
| 4105 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4106 | else |
| 4107 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4108 | |
| 4109 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4110 | if (mddev->sync_thread) { |
| 4111 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4112 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4113 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4114 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4115 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4116 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4117 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4118 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4119 | else if (cmd_match(page, "recover")) { |
| 4120 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4121 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4122 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4123 | int err; |
| 4124 | if (mddev->pers->start_reshape == NULL) |
| 4125 | return -EINVAL; |
| 4126 | err = mddev->pers->start_reshape(mddev); |
| 4127 | if (err) |
| 4128 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4129 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4130 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4131 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4132 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4133 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4134 | return -EINVAL; |
| 4135 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4136 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4137 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4138 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4139 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4140 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4141 | return len; |
| 4142 | } |
| 4143 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4144 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4145 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4146 | { |
| 4147 | return sprintf(page, "%llu\n", |
| 4148 | (unsigned long long) mddev->resync_mismatches); |
| 4149 | } |
| 4150 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4151 | static struct md_sysfs_entry md_scan_mode = |
| 4152 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4153 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4154 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4155 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4156 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4157 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4158 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4159 | { |
| 4160 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4161 | mddev->sync_speed_min ? "local": "system"); |
| 4162 | } |
| 4163 | |
| 4164 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4165 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4166 | { |
| 4167 | int min; |
| 4168 | char *e; |
| 4169 | if (strncmp(buf, "system", 6)==0) { |
| 4170 | mddev->sync_speed_min = 0; |
| 4171 | return len; |
| 4172 | } |
| 4173 | min = simple_strtoul(buf, &e, 10); |
| 4174 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4175 | return -EINVAL; |
| 4176 | mddev->sync_speed_min = min; |
| 4177 | return len; |
| 4178 | } |
| 4179 | |
| 4180 | static struct md_sysfs_entry md_sync_min = |
| 4181 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4182 | |
| 4183 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4184 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4185 | { |
| 4186 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4187 | mddev->sync_speed_max ? "local": "system"); |
| 4188 | } |
| 4189 | |
| 4190 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4191 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4192 | { |
| 4193 | int max; |
| 4194 | char *e; |
| 4195 | if (strncmp(buf, "system", 6)==0) { |
| 4196 | mddev->sync_speed_max = 0; |
| 4197 | return len; |
| 4198 | } |
| 4199 | max = simple_strtoul(buf, &e, 10); |
| 4200 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4201 | return -EINVAL; |
| 4202 | mddev->sync_speed_max = max; |
| 4203 | return len; |
| 4204 | } |
| 4205 | |
| 4206 | static struct md_sysfs_entry md_sync_max = |
| 4207 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4208 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4209 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4210 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4211 | { |
| 4212 | return sprintf(page, "%d\n", mddev->degraded); |
| 4213 | } |
| 4214 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4215 | |
| 4216 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4217 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4218 | { |
| 4219 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4220 | } |
| 4221 | |
| 4222 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4223 | 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] | 4224 | { |
| 4225 | long n; |
| 4226 | |
| 4227 | if (strict_strtol(buf, 10, &n)) |
| 4228 | return -EINVAL; |
| 4229 | |
| 4230 | if (n != 0 && n != 1) |
| 4231 | return -EINVAL; |
| 4232 | |
| 4233 | mddev->parallel_resync = n; |
| 4234 | |
| 4235 | if (mddev->sync_thread) |
| 4236 | wake_up(&resync_wait); |
| 4237 | |
| 4238 | return len; |
| 4239 | } |
| 4240 | |
| 4241 | /* force parallel resync, even with shared block devices */ |
| 4242 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4243 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4244 | sync_force_parallel_show, sync_force_parallel_store); |
| 4245 | |
| 4246 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4247 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4248 | { |
| 4249 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4250 | if (mddev->curr_resync == 0) |
| 4251 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4252 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4253 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4254 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4255 | db = resync - mddev->resync_mark_cnt; |
| 4256 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4257 | } |
| 4258 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4259 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4260 | |
| 4261 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4262 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4263 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4264 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4265 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4266 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4267 | return sprintf(page, "none\n"); |
| 4268 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4269 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4270 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4271 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4272 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4273 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4274 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4275 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4276 | } |
| 4277 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4278 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4279 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4280 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4281 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4282 | { |
| 4283 | return sprintf(page, "%llu\n", |
| 4284 | (unsigned long long)mddev->resync_min); |
| 4285 | } |
| 4286 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4287 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4288 | { |
| 4289 | unsigned long long min; |
| 4290 | if (strict_strtoull(buf, 10, &min)) |
| 4291 | return -EINVAL; |
| 4292 | if (min > mddev->resync_max) |
| 4293 | return -EINVAL; |
| 4294 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4295 | return -EBUSY; |
| 4296 | |
| 4297 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4298 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4299 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4300 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4301 | return -EINVAL; |
| 4302 | } |
| 4303 | mddev->resync_min = min; |
| 4304 | |
| 4305 | return len; |
| 4306 | } |
| 4307 | |
| 4308 | static struct md_sysfs_entry md_min_sync = |
| 4309 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4310 | |
| 4311 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4312 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4313 | { |
| 4314 | if (mddev->resync_max == MaxSector) |
| 4315 | return sprintf(page, "max\n"); |
| 4316 | else |
| 4317 | return sprintf(page, "%llu\n", |
| 4318 | (unsigned long long)mddev->resync_max); |
| 4319 | } |
| 4320 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4321 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4322 | { |
| 4323 | if (strncmp(buf, "max", 3) == 0) |
| 4324 | mddev->resync_max = MaxSector; |
| 4325 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4326 | unsigned long long max; |
| 4327 | if (strict_strtoull(buf, 10, &max)) |
| 4328 | return -EINVAL; |
| 4329 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4330 | return -EINVAL; |
| 4331 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4332 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4333 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4334 | return -EBUSY; |
| 4335 | |
| 4336 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4337 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4338 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4339 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4340 | return -EINVAL; |
| 4341 | } |
| 4342 | mddev->resync_max = max; |
| 4343 | } |
| 4344 | wake_up(&mddev->recovery_wait); |
| 4345 | return len; |
| 4346 | } |
| 4347 | |
| 4348 | static struct md_sysfs_entry md_max_sync = |
| 4349 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4350 | |
| 4351 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4352 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4353 | { |
| 4354 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4355 | } |
| 4356 | |
| 4357 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4358 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4359 | { |
| 4360 | char *e; |
| 4361 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4362 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4363 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4364 | if (mddev->pers == NULL || |
| 4365 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4366 | return -EINVAL; |
| 4367 | if (buf == e || (*e && *e != '\n')) |
| 4368 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4369 | |
| 4370 | mddev->suspend_lo = new; |
| 4371 | if (new >= old) |
| 4372 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4373 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4374 | else { |
| 4375 | /* Expanding suspended region - need to wait */ |
| 4376 | mddev->pers->quiesce(mddev, 1); |
| 4377 | mddev->pers->quiesce(mddev, 0); |
| 4378 | } |
| 4379 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4380 | } |
| 4381 | static struct md_sysfs_entry md_suspend_lo = |
| 4382 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4383 | |
| 4384 | |
| 4385 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4386 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4387 | { |
| 4388 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4389 | } |
| 4390 | |
| 4391 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4392 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4393 | { |
| 4394 | char *e; |
| 4395 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4396 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4397 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4398 | if (mddev->pers == NULL || |
| 4399 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4400 | return -EINVAL; |
| 4401 | if (buf == e || (*e && *e != '\n')) |
| 4402 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4403 | |
| 4404 | mddev->suspend_hi = new; |
| 4405 | if (new <= old) |
| 4406 | /* Shrinking suspended region */ |
| 4407 | mddev->pers->quiesce(mddev, 2); |
| 4408 | else { |
| 4409 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4410 | mddev->pers->quiesce(mddev, 1); |
| 4411 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4412 | } |
| 4413 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4414 | } |
| 4415 | static struct md_sysfs_entry md_suspend_hi = |
| 4416 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4417 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4418 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4419 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4420 | { |
| 4421 | if (mddev->reshape_position != MaxSector) |
| 4422 | return sprintf(page, "%llu\n", |
| 4423 | (unsigned long long)mddev->reshape_position); |
| 4424 | strcpy(page, "none\n"); |
| 4425 | return 5; |
| 4426 | } |
| 4427 | |
| 4428 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4429 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4430 | { |
| 4431 | char *e; |
| 4432 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4433 | if (mddev->pers) |
| 4434 | return -EBUSY; |
| 4435 | if (buf == e || (*e && *e != '\n')) |
| 4436 | return -EINVAL; |
| 4437 | mddev->reshape_position = new; |
| 4438 | mddev->delta_disks = 0; |
| 4439 | mddev->new_level = mddev->level; |
| 4440 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4441 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4442 | return len; |
| 4443 | } |
| 4444 | |
| 4445 | static struct md_sysfs_entry md_reshape_position = |
| 4446 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4447 | reshape_position_store); |
| 4448 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4449 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4450 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4451 | { |
| 4452 | if (mddev->external_size) |
| 4453 | return sprintf(page, "%llu\n", |
| 4454 | (unsigned long long)mddev->array_sectors/2); |
| 4455 | else |
| 4456 | return sprintf(page, "default\n"); |
| 4457 | } |
| 4458 | |
| 4459 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4460 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4461 | { |
| 4462 | sector_t sectors; |
| 4463 | |
| 4464 | if (strncmp(buf, "default", 7) == 0) { |
| 4465 | if (mddev->pers) |
| 4466 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4467 | else |
| 4468 | sectors = mddev->array_sectors; |
| 4469 | |
| 4470 | mddev->external_size = 0; |
| 4471 | } else { |
| 4472 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4473 | return -EINVAL; |
| 4474 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4475 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4476 | |
| 4477 | mddev->external_size = 1; |
| 4478 | } |
| 4479 | |
| 4480 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4481 | if (mddev->pers) { |
| 4482 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4483 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4484 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4485 | return len; |
| 4486 | } |
| 4487 | |
| 4488 | static struct md_sysfs_entry md_array_size = |
| 4489 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4490 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4491 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4492 | static struct attribute *md_default_attrs[] = { |
| 4493 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4494 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4495 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4496 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4497 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4498 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4499 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4500 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4501 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4502 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4503 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4504 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4505 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4506 | NULL, |
| 4507 | }; |
| 4508 | |
| 4509 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4510 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4511 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4512 | &md_sync_min.attr, |
| 4513 | &md_sync_max.attr, |
| 4514 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4515 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4516 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4517 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4518 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4519 | &md_suspend_lo.attr, |
| 4520 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4521 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4522 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4523 | NULL, |
| 4524 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4525 | static struct attribute_group md_redundancy_group = { |
| 4526 | .name = NULL, |
| 4527 | .attrs = md_redundancy_attrs, |
| 4528 | }; |
| 4529 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4530 | |
| 4531 | static ssize_t |
| 4532 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4533 | { |
| 4534 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4535 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4536 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4537 | |
| 4538 | if (!entry->show) |
| 4539 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4540 | spin_lock(&all_mddevs_lock); |
| 4541 | if (list_empty(&mddev->all_mddevs)) { |
| 4542 | spin_unlock(&all_mddevs_lock); |
| 4543 | return -EBUSY; |
| 4544 | } |
| 4545 | mddev_get(mddev); |
| 4546 | spin_unlock(&all_mddevs_lock); |
| 4547 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4548 | rv = mddev_lock(mddev); |
| 4549 | if (!rv) { |
| 4550 | rv = entry->show(mddev, page); |
| 4551 | mddev_unlock(mddev); |
| 4552 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4553 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4554 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | static ssize_t |
| 4558 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4559 | const char *page, size_t length) |
| 4560 | { |
| 4561 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4562 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4563 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4564 | |
| 4565 | if (!entry->store) |
| 4566 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4567 | if (!capable(CAP_SYS_ADMIN)) |
| 4568 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4569 | spin_lock(&all_mddevs_lock); |
| 4570 | if (list_empty(&mddev->all_mddevs)) { |
| 4571 | spin_unlock(&all_mddevs_lock); |
| 4572 | return -EBUSY; |
| 4573 | } |
| 4574 | mddev_get(mddev); |
| 4575 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4576 | rv = mddev_lock(mddev); |
| 4577 | if (!rv) { |
| 4578 | rv = entry->store(mddev, page, length); |
| 4579 | mddev_unlock(mddev); |
| 4580 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4581 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4582 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | static void md_free(struct kobject *ko) |
| 4586 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4587 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4588 | |
| 4589 | if (mddev->sysfs_state) |
| 4590 | sysfs_put(mddev->sysfs_state); |
| 4591 | |
| 4592 | if (mddev->gendisk) { |
| 4593 | del_gendisk(mddev->gendisk); |
| 4594 | put_disk(mddev->gendisk); |
| 4595 | } |
| 4596 | if (mddev->queue) |
| 4597 | blk_cleanup_queue(mddev->queue); |
| 4598 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4599 | kfree(mddev); |
| 4600 | } |
| 4601 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4602 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4603 | .show = md_attr_show, |
| 4604 | .store = md_attr_store, |
| 4605 | }; |
| 4606 | static struct kobj_type md_ktype = { |
| 4607 | .release = md_free, |
| 4608 | .sysfs_ops = &md_sysfs_ops, |
| 4609 | .default_attrs = md_default_attrs, |
| 4610 | }; |
| 4611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | int mdp_major = 0; |
| 4613 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4614 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4615 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4616 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4617 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4618 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4619 | kobject_del(&mddev->kobj); |
| 4620 | kobject_put(&mddev->kobj); |
| 4621 | } |
| 4622 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4623 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4625 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4626 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4628 | int partitioned; |
| 4629 | int shift; |
| 4630 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4631 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | |
| 4633 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4634 | return -ENODEV; |
| 4635 | |
| 4636 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4637 | shift = partitioned ? MdpMinorShift : 0; |
| 4638 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4640 | /* wait for any previous instance of this device to be |
| 4641 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4642 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4643 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4644 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4645 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4646 | error = -EEXIST; |
| 4647 | if (mddev->gendisk) |
| 4648 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4649 | |
| 4650 | if (name) { |
| 4651 | /* Need to ensure that 'name' is not a duplicate. |
| 4652 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4653 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4654 | spin_lock(&all_mddevs_lock); |
| 4655 | |
| 4656 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4657 | if (mddev2->gendisk && |
| 4658 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4659 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4660 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4661 | } |
| 4662 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4664 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4665 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4666 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4667 | if (!mddev->queue) |
| 4668 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4669 | mddev->queue->queuedata = mddev; |
| 4670 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4671 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4672 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | disk = alloc_disk(1 << shift); |
| 4675 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4676 | blk_cleanup_queue(mddev->queue); |
| 4677 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4678 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4680 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4682 | if (name) |
| 4683 | strcpy(disk->disk_name, name); |
| 4684 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4686 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | disk->fops = &md_fops; |
| 4689 | disk->private_data = mddev; |
| 4690 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4691 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4692 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4693 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4694 | * remove it now. |
| 4695 | */ |
| 4696 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4698 | /* As soon as we call add_disk(), another thread could get |
| 4699 | * through to md_open, so make sure it doesn't get too far |
| 4700 | */ |
| 4701 | mutex_lock(&mddev->open_mutex); |
| 4702 | add_disk(disk); |
| 4703 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4704 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4705 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4706 | if (error) { |
| 4707 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4708 | * __must_check, we must do something with the result |
| 4709 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4710 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4711 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4712 | error = 0; |
| 4713 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4714 | if (mddev->kobj.sd && |
| 4715 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4716 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4717 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4718 | abort: |
| 4719 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4720 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4721 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4722 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4723 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4724 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4725 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4729 | { |
| 4730 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | return NULL; |
| 4732 | } |
| 4733 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4734 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4735 | { |
| 4736 | /* val must be "md_*" where * is not all digits. |
| 4737 | * We allocate an array with a large free minor number, and |
| 4738 | * set the name to val. val must not already be an active name. |
| 4739 | */ |
| 4740 | int len = strlen(val); |
| 4741 | char buf[DISK_NAME_LEN]; |
| 4742 | |
| 4743 | while (len && val[len-1] == '\n') |
| 4744 | len--; |
| 4745 | if (len >= DISK_NAME_LEN) |
| 4746 | return -E2BIG; |
| 4747 | strlcpy(buf, val, len+1); |
| 4748 | if (strncmp(buf, "md_", 3) != 0) |
| 4749 | return -EINVAL; |
| 4750 | return md_alloc(0, buf); |
| 4751 | } |
| 4752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | static void md_safemode_timeout(unsigned long data) |
| 4754 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4755 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4757 | if (!atomic_read(&mddev->writes_pending)) { |
| 4758 | mddev->safemode = 1; |
| 4759 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4760 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | md_wakeup_thread(mddev->thread); |
| 4763 | } |
| 4764 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4765 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4767 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4769 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4770 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4771 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4773 | if (list_empty(&mddev->disks)) |
| 4774 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | |
| 4777 | if (mddev->pers) |
| 4778 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4779 | /* Cannot run until previous stop completes properly */ |
| 4780 | if (mddev->sysfs_active) |
| 4781 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | /* |
| 4784 | * Analyze all RAID superblock(s) |
| 4785 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4786 | if (!mddev->raid_disks) { |
| 4787 | if (!mddev->persistent) |
| 4788 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4789 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4792 | if (mddev->level != LEVEL_NONE) |
| 4793 | request_module("md-level-%d", mddev->level); |
| 4794 | else if (mddev->clevel[0]) |
| 4795 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | |
| 4797 | /* |
| 4798 | * Drop all container device buffers, from now on |
| 4799 | * the only valid external interface is through the md |
| 4800 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4802 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4803 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | continue; |
| 4805 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4806 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4807 | |
| 4808 | /* perform some consistency tests on the device. |
| 4809 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4810 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4811 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4812 | if (rdev->meta_bdev) { |
| 4813 | /* Nothing to check */; |
| 4814 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4815 | if (mddev->dev_sectors && |
| 4816 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4817 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4818 | printk("md: %s: data overlaps metadata\n", |
| 4819 | mdname(mddev)); |
| 4820 | return -EINVAL; |
| 4821 | } |
| 4822 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4823 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4824 | > rdev->data_offset) { |
| 4825 | printk("md: %s: metadata overlaps data\n", |
| 4826 | mdname(mddev)); |
| 4827 | return -EINVAL; |
| 4828 | } |
| 4829 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4830 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | } |
| 4832 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4833 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4834 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4835 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4838 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4839 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4841 | if (mddev->level != LEVEL_NONE) |
| 4842 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4843 | mddev->level); |
| 4844 | else |
| 4845 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4846 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | return -EINVAL; |
| 4848 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4849 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4851 | if (mddev->level != pers->level) { |
| 4852 | mddev->level = pers->level; |
| 4853 | mddev->new_level = pers->level; |
| 4854 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4855 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4857 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4858 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4859 | /* This personality cannot handle reshaping... */ |
| 4860 | mddev->pers = NULL; |
| 4861 | module_put(pers->owner); |
| 4862 | return -EINVAL; |
| 4863 | } |
| 4864 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4865 | if (pers->sync_request) { |
| 4866 | /* Warn if this is a potentially silly |
| 4867 | * configuration. |
| 4868 | */ |
| 4869 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4870 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4871 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4872 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4873 | rdev_for_each(rdev, mddev) |
| 4874 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4875 | if (rdev < rdev2 && |
| 4876 | rdev->bdev->bd_contains == |
| 4877 | rdev2->bdev->bd_contains) { |
| 4878 | printk(KERN_WARNING |
| 4879 | "%s: WARNING: %s appears to be" |
| 4880 | " on the same physical disk as" |
| 4881 | " %s.\n", |
| 4882 | mdname(mddev), |
| 4883 | bdevname(rdev->bdev,b), |
| 4884 | bdevname(rdev2->bdev,b2)); |
| 4885 | warned = 1; |
| 4886 | } |
| 4887 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4888 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4889 | if (warned) |
| 4890 | printk(KERN_WARNING |
| 4891 | "True protection against single-disk" |
| 4892 | " failure might be compromised.\n"); |
| 4893 | } |
| 4894 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4895 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4896 | /* may be over-ridden by personality */ |
| 4897 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4898 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4899 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4901 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4902 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4903 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4904 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4905 | if (err) |
| 4906 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4907 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4908 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4909 | " but 'external_size' not in effect?\n", __func__); |
| 4910 | printk(KERN_ERR |
| 4911 | "md: invalid array_size %llu > default size %llu\n", |
| 4912 | (unsigned long long)mddev->array_sectors / 2, |
| 4913 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4914 | err = -EINVAL; |
| 4915 | mddev->pers->stop(mddev); |
| 4916 | } |
| 4917 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4918 | err = bitmap_create(mddev); |
| 4919 | if (err) { |
| 4920 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4921 | mdname(mddev), err); |
| 4922 | mddev->pers->stop(mddev); |
| 4923 | } |
| 4924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | module_put(mddev->pers->owner); |
| 4927 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4928 | bitmap_destroy(mddev); |
| 4929 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4931 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4932 | if (mddev->kobj.sd && |
| 4933 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4934 | printk(KERN_WARNING |
| 4935 | "md: cannot register extra attributes for %s\n", |
| 4936 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4937 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4938 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4939 | mddev->ro = 0; |
| 4940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4942 | atomic_set(&mddev->max_corr_read_errors, |
| 4943 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | mddev->safemode = 0; |
| 4945 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4946 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4947 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4949 | smp_wmb(); |
| 4950 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4951 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4952 | if (rdev->raid_disk >= 0) |
| 4953 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4954 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
| 4956 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4957 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4958 | if (mddev->flags) |
| 4959 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4961 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4962 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4963 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4964 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | return 0; |
| 4966 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4967 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4969 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4970 | { |
| 4971 | int err; |
| 4972 | |
| 4973 | err = md_run(mddev); |
| 4974 | if (err) |
| 4975 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4976 | err = bitmap_load(mddev); |
| 4977 | if (err) { |
| 4978 | bitmap_destroy(mddev); |
| 4979 | goto out; |
| 4980 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4981 | |
| 4982 | md_wakeup_thread(mddev->thread); |
| 4983 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4984 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4985 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4986 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4987 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4988 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4989 | out: |
| 4990 | return err; |
| 4991 | } |
| 4992 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4993 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | { |
| 4995 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4997 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4999 | return -ENXIO; |
| 5000 | if (!mddev->pers) |
| 5001 | return -EINVAL; |
| 5002 | if (!mddev->ro) |
| 5003 | return -EBUSY; |
| 5004 | mddev->safemode = 0; |
| 5005 | mddev->ro = 0; |
| 5006 | set_disk_ro(disk, 0); |
| 5007 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5008 | mdname(mddev)); |
| 5009 | /* Kick recovery or resync if necessary */ |
| 5010 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5011 | md_wakeup_thread(mddev->thread); |
| 5012 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5013 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5014 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5017 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5018 | * to the file ourselves */ |
| 5019 | static int deny_bitmap_write_access(struct file * file) |
| 5020 | { |
| 5021 | struct inode *inode = file->f_mapping->host; |
| 5022 | |
| 5023 | spin_lock(&inode->i_lock); |
| 5024 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5025 | spin_unlock(&inode->i_lock); |
| 5026 | return -ETXTBSY; |
| 5027 | } |
| 5028 | atomic_set(&inode->i_writecount, -1); |
| 5029 | spin_unlock(&inode->i_lock); |
| 5030 | |
| 5031 | return 0; |
| 5032 | } |
| 5033 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5034 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5035 | { |
| 5036 | struct inode *inode = file->f_mapping->host; |
| 5037 | |
| 5038 | spin_lock(&inode->i_lock); |
| 5039 | atomic_set(&inode->i_writecount, 1); |
| 5040 | spin_unlock(&inode->i_lock); |
| 5041 | } |
| 5042 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5043 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5044 | { |
| 5045 | mddev->array_sectors = 0; |
| 5046 | mddev->external_size = 0; |
| 5047 | mddev->dev_sectors = 0; |
| 5048 | mddev->raid_disks = 0; |
| 5049 | mddev->recovery_cp = 0; |
| 5050 | mddev->resync_min = 0; |
| 5051 | mddev->resync_max = MaxSector; |
| 5052 | mddev->reshape_position = MaxSector; |
| 5053 | mddev->external = 0; |
| 5054 | mddev->persistent = 0; |
| 5055 | mddev->level = LEVEL_NONE; |
| 5056 | mddev->clevel[0] = 0; |
| 5057 | mddev->flags = 0; |
| 5058 | mddev->ro = 0; |
| 5059 | mddev->metadata_type[0] = 0; |
| 5060 | mddev->chunk_sectors = 0; |
| 5061 | mddev->ctime = mddev->utime = 0; |
| 5062 | mddev->layout = 0; |
| 5063 | mddev->max_disks = 0; |
| 5064 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5065 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5066 | mddev->delta_disks = 0; |
| 5067 | mddev->new_level = LEVEL_NONE; |
| 5068 | mddev->new_layout = 0; |
| 5069 | mddev->new_chunk_sectors = 0; |
| 5070 | mddev->curr_resync = 0; |
| 5071 | mddev->resync_mismatches = 0; |
| 5072 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5073 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5074 | mddev->recovery = 0; |
| 5075 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5076 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5077 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5078 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5079 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5080 | mddev->bitmap_info.offset = 0; |
| 5081 | mddev->bitmap_info.default_offset = 0; |
| 5082 | mddev->bitmap_info.chunksize = 0; |
| 5083 | mddev->bitmap_info.daemon_sleep = 0; |
| 5084 | mddev->bitmap_info.max_write_behind = 0; |
| 5085 | } |
| 5086 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5087 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5088 | { |
| 5089 | if (mddev->sync_thread) { |
| 5090 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5091 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5092 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | del_timer_sync(&mddev->safemode_timer); |
| 5096 | |
| 5097 | bitmap_flush(mddev); |
| 5098 | md_super_wait(mddev); |
| 5099 | |
| 5100 | if (!mddev->in_sync || mddev->flags) { |
| 5101 | /* mark array as shutdown cleanly */ |
| 5102 | mddev->in_sync = 1; |
| 5103 | md_update_sb(mddev, 1); |
| 5104 | } |
| 5105 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5107 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5108 | { |
| 5109 | mddev_lock(mddev); |
| 5110 | __md_stop_writes(mddev); |
| 5111 | mddev_unlock(mddev); |
| 5112 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5113 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5114 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5115 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5116 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5117 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5118 | mddev->pers->stop(mddev); |
| 5119 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5120 | mddev->to_remove = &md_redundancy_group; |
| 5121 | module_put(mddev->pers->owner); |
| 5122 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5123 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5124 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5125 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5126 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5127 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5128 | { |
| 5129 | int err = 0; |
| 5130 | mutex_lock(&mddev->open_mutex); |
| 5131 | if (atomic_read(&mddev->openers) > is_open) { |
| 5132 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5133 | err = -EBUSY; |
| 5134 | goto out; |
| 5135 | } |
| 5136 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5137 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5138 | |
| 5139 | err = -ENXIO; |
| 5140 | if (mddev->ro==1) |
| 5141 | goto out; |
| 5142 | mddev->ro = 1; |
| 5143 | set_disk_ro(mddev->gendisk, 1); |
| 5144 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5145 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5146 | err = 0; |
| 5147 | } |
| 5148 | out: |
| 5149 | mutex_unlock(&mddev->open_mutex); |
| 5150 | return err; |
| 5151 | } |
| 5152 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5153 | /* mode: |
| 5154 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5155 | * 2 - stop but do not disassemble array |
| 5156 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5157 | static int do_md_stop(struct mddev * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5160 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5162 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5163 | if (atomic_read(&mddev->openers) > is_open || |
| 5164 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5165 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5166 | mutex_unlock(&mddev->open_mutex); |
| 5167 | return -EBUSY; |
| 5168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5170 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5171 | if (mddev->ro) |
| 5172 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5173 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5174 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5175 | md_stop(mddev); |
| 5176 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5177 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5178 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5179 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5180 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5181 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5182 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5183 | if (rdev->raid_disk >= 0) |
| 5184 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5185 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5186 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5187 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5188 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5189 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5190 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5191 | if (mddev->ro) |
| 5192 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5193 | } else |
| 5194 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | /* |
| 5196 | * Free resources if final stop |
| 5197 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5198 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5200 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5201 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5202 | if (mddev->bitmap_info.file) { |
| 5203 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5204 | fput(mddev->bitmap_info.file); |
| 5205 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5206 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5207 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | export_array(mddev); |
| 5210 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5211 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5212 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5213 | if (mddev->hold_active == UNTIL_STOP) |
| 5214 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5215 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5216 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5217 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5218 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5219 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | } |
| 5221 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5222 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5223 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5225 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | int err; |
| 5227 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5228 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | |
| 5231 | printk(KERN_INFO "md: running: "); |
| 5232 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5233 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | char b[BDEVNAME_SIZE]; |
| 5235 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5236 | } |
| 5237 | printk("\n"); |
| 5238 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5239 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | if (err) { |
| 5241 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5242 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | } |
| 5244 | } |
| 5245 | |
| 5246 | /* |
| 5247 | * lets try to run arrays based on all disks that have arrived |
| 5248 | * until now. (those are in pending_raid_disks) |
| 5249 | * |
| 5250 | * the method: pick the first pending disk, collect all disks with |
| 5251 | * the same UUID, remove all from the pending list and put them into |
| 5252 | * the 'same_array' list. Then order this list based on superblock |
| 5253 | * update time (freshest comes first), kick out 'old' disks and |
| 5254 | * compare superblocks. If everything's fine then run it. |
| 5255 | * |
| 5256 | * If "unit" is allocated, then bump its reference count |
| 5257 | */ |
| 5258 | static void autorun_devices(int part) |
| 5259 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5260 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5261 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | char b[BDEVNAME_SIZE]; |
| 5263 | |
| 5264 | printk(KERN_INFO "md: autorun ...\n"); |
| 5265 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5266 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5268 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5270 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
| 5272 | printk(KERN_INFO "md: considering %s ...\n", |
| 5273 | bdevname(rdev0->bdev,b)); |
| 5274 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5275 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5277 | printk(KERN_INFO "md: adding %s ...\n", |
| 5278 | bdevname(rdev->bdev,b)); |
| 5279 | list_move(&rdev->same_set, &candidates); |
| 5280 | } |
| 5281 | /* |
| 5282 | * now we have a set of devices, with all of them having |
| 5283 | * mostly sane superblocks. It's time to allocate the |
| 5284 | * mddev. |
| 5285 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5286 | if (part) { |
| 5287 | dev = MKDEV(mdp_major, |
| 5288 | rdev0->preferred_minor << MdpMinorShift); |
| 5289 | unit = MINOR(dev) >> MdpMinorShift; |
| 5290 | } else { |
| 5291 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5292 | unit = MINOR(dev); |
| 5293 | } |
| 5294 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5296 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5297 | break; |
| 5298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | |
| 5300 | md_probe(dev, NULL, NULL); |
| 5301 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5302 | if (!mddev || !mddev->gendisk) { |
| 5303 | if (mddev) |
| 5304 | mddev_put(mddev); |
| 5305 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | "md: cannot allocate memory for md drive.\n"); |
| 5307 | break; |
| 5308 | } |
| 5309 | if (mddev_lock(mddev)) |
| 5310 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5311 | mdname(mddev)); |
| 5312 | else if (mddev->raid_disks || mddev->major_version |
| 5313 | || !list_empty(&mddev->disks)) { |
| 5314 | printk(KERN_WARNING |
| 5315 | "md: %s already running, cannot run %s\n", |
| 5316 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5317 | mddev_unlock(mddev); |
| 5318 | } else { |
| 5319 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5320 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5321 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | list_del_init(&rdev->same_set); |
| 5323 | if (bind_rdev_to_array(rdev, mddev)) |
| 5324 | export_rdev(rdev); |
| 5325 | } |
| 5326 | autorun_array(mddev); |
| 5327 | mddev_unlock(mddev); |
| 5328 | } |
| 5329 | /* on success, candidates will be empty, on error |
| 5330 | * it won't... |
| 5331 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5332 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5333 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | mddev_put(mddev); |
| 5337 | } |
| 5338 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5339 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5340 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | static int get_version(void __user * arg) |
| 5343 | { |
| 5344 | mdu_version_t ver; |
| 5345 | |
| 5346 | ver.major = MD_MAJOR_VERSION; |
| 5347 | ver.minor = MD_MINOR_VERSION; |
| 5348 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5349 | |
| 5350 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5351 | return -EFAULT; |
| 5352 | |
| 5353 | return 0; |
| 5354 | } |
| 5355 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5356 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | { |
| 5358 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5359 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5360 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5362 | nr=working=insync=failed=spare=0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5363 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5365 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | failed++; |
| 5367 | else { |
| 5368 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5369 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5370 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | else |
| 5372 | spare++; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | info.major_version = mddev->major_version; |
| 5377 | info.minor_version = mddev->minor_version; |
| 5378 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5379 | info.ctime = mddev->ctime; |
| 5380 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5381 | info.size = mddev->dev_sectors / 2; |
| 5382 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5383 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | info.nr_disks = nr; |
| 5385 | info.raid_disks = mddev->raid_disks; |
| 5386 | info.md_minor = mddev->md_minor; |
| 5387 | info.not_persistent= !mddev->persistent; |
| 5388 | |
| 5389 | info.utime = mddev->utime; |
| 5390 | info.state = 0; |
| 5391 | if (mddev->in_sync) |
| 5392 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5393 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5394 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5395 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | info.working_disks = working; |
| 5397 | info.failed_disks = failed; |
| 5398 | info.spare_disks = spare; |
| 5399 | |
| 5400 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5401 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | |
| 5403 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5404 | return -EFAULT; |
| 5405 | |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5409 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5410 | { |
| 5411 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5412 | char *ptr, *buf = NULL; |
| 5413 | int err = -ENOMEM; |
| 5414 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5415 | if (md_allow_write(mddev)) |
| 5416 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5417 | else |
| 5418 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5419 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5420 | if (!file) |
| 5421 | goto out; |
| 5422 | |
| 5423 | /* bitmap disabled, zero the first byte and copy out */ |
| 5424 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5425 | file->pathname[0] = '\0'; |
| 5426 | goto copy_out; |
| 5427 | } |
| 5428 | |
| 5429 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5430 | if (!buf) |
| 5431 | goto out; |
| 5432 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5433 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5434 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5435 | goto out; |
| 5436 | |
| 5437 | strcpy(file->pathname, ptr); |
| 5438 | |
| 5439 | copy_out: |
| 5440 | err = 0; |
| 5441 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5442 | err = -EFAULT; |
| 5443 | out: |
| 5444 | kfree(buf); |
| 5445 | kfree(file); |
| 5446 | return err; |
| 5447 | } |
| 5448 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5449 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | { |
| 5451 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5452 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | |
| 5454 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5455 | return -EFAULT; |
| 5456 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5457 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | if (rdev) { |
| 5459 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5460 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5461 | info.raid_disk = rdev->raid_disk; |
| 5462 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5463 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5465 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5467 | info.state |= (1<<MD_DISK_SYNC); |
| 5468 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5469 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5470 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | } else { |
| 5472 | info.major = info.minor = 0; |
| 5473 | info.raid_disk = -1; |
| 5474 | info.state = (1<<MD_DISK_REMOVED); |
| 5475 | } |
| 5476 | |
| 5477 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5478 | return -EFAULT; |
| 5479 | |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5483 | 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] | 5484 | { |
| 5485 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5486 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | dev_t dev = MKDEV(info->major,info->minor); |
| 5488 | |
| 5489 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5490 | return -EOVERFLOW; |
| 5491 | |
| 5492 | if (!mddev->raid_disks) { |
| 5493 | int err; |
| 5494 | /* expecting a device which has a superblock */ |
| 5495 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5496 | if (IS_ERR(rdev)) { |
| 5497 | printk(KERN_WARNING |
| 5498 | "md: md_import_device returned %ld\n", |
| 5499 | PTR_ERR(rdev)); |
| 5500 | return PTR_ERR(rdev); |
| 5501 | } |
| 5502 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5503 | struct md_rdev *rdev0 |
| 5504 | = list_entry(mddev->disks.next, |
| 5505 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5506 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5508 | if (err < 0) { |
| 5509 | printk(KERN_WARNING |
| 5510 | "md: %s has different UUID to %s\n", |
| 5511 | bdevname(rdev->bdev,b), |
| 5512 | bdevname(rdev0->bdev,b2)); |
| 5513 | export_rdev(rdev); |
| 5514 | return -EINVAL; |
| 5515 | } |
| 5516 | } |
| 5517 | err = bind_rdev_to_array(rdev, mddev); |
| 5518 | if (err) |
| 5519 | export_rdev(rdev); |
| 5520 | return err; |
| 5521 | } |
| 5522 | |
| 5523 | /* |
| 5524 | * add_new_disk can be used once the array is assembled |
| 5525 | * to add "hot spares". They must already have a superblock |
| 5526 | * written |
| 5527 | */ |
| 5528 | if (mddev->pers) { |
| 5529 | int err; |
| 5530 | if (!mddev->pers->hot_add_disk) { |
| 5531 | printk(KERN_WARNING |
| 5532 | "%s: personality does not support diskops!\n", |
| 5533 | mdname(mddev)); |
| 5534 | return -EINVAL; |
| 5535 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5536 | if (mddev->persistent) |
| 5537 | rdev = md_import_device(dev, mddev->major_version, |
| 5538 | mddev->minor_version); |
| 5539 | else |
| 5540 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | if (IS_ERR(rdev)) { |
| 5542 | printk(KERN_WARNING |
| 5543 | "md: md_import_device returned %ld\n", |
| 5544 | PTR_ERR(rdev)); |
| 5545 | return PTR_ERR(rdev); |
| 5546 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5547 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5548 | if (!mddev->persistent) { |
| 5549 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5550 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5551 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5552 | set_bit(In_sync, &rdev->flags); |
| 5553 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5554 | rdev->raid_disk = -1; |
| 5555 | } else |
| 5556 | super_types[mddev->major_version]. |
| 5557 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5558 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5559 | (!test_bit(In_sync, &rdev->flags) || |
| 5560 | rdev->raid_disk != info->raid_disk)) { |
| 5561 | /* This was a hot-add request, but events doesn't |
| 5562 | * match, so reject it. |
| 5563 | */ |
| 5564 | export_rdev(rdev); |
| 5565 | return -EINVAL; |
| 5566 | } |
| 5567 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5568 | if (test_bit(In_sync, &rdev->flags)) |
| 5569 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5570 | else |
| 5571 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5572 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5573 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5574 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5575 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5576 | else |
| 5577 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | rdev->raid_disk = -1; |
| 5580 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5581 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5582 | /* If there is hot_add_disk but no hot_remove_disk |
| 5583 | * then added disks for geometry changes, |
| 5584 | * and should be added immediately. |
| 5585 | */ |
| 5586 | super_types[mddev->major_version]. |
| 5587 | validate_super(mddev, rdev); |
| 5588 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5589 | if (err) |
| 5590 | unbind_rdev_from_array(rdev); |
| 5591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | if (err) |
| 5593 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5594 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5595 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5596 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5597 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5598 | if (mddev->degraded) |
| 5599 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5600 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5601 | if (!err) |
| 5602 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5603 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | return err; |
| 5605 | } |
| 5606 | |
| 5607 | /* otherwise, add_new_disk is only allowed |
| 5608 | * for major_version==0 superblocks |
| 5609 | */ |
| 5610 | if (mddev->major_version != 0) { |
| 5611 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5612 | mdname(mddev)); |
| 5613 | return -EINVAL; |
| 5614 | } |
| 5615 | |
| 5616 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5617 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5618 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | if (IS_ERR(rdev)) { |
| 5620 | printk(KERN_WARNING |
| 5621 | "md: error, md_import_device() returned %ld\n", |
| 5622 | PTR_ERR(rdev)); |
| 5623 | return PTR_ERR(rdev); |
| 5624 | } |
| 5625 | rdev->desc_nr = info->number; |
| 5626 | if (info->raid_disk < mddev->raid_disks) |
| 5627 | rdev->raid_disk = info->raid_disk; |
| 5628 | else |
| 5629 | rdev->raid_disk = -1; |
| 5630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5632 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5633 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5635 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5636 | set_bit(WriteMostly, &rdev->flags); |
| 5637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | if (!mddev->persistent) { |
| 5639 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5640 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5641 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5642 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5643 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5645 | err = bind_rdev_to_array(rdev, mddev); |
| 5646 | if (err) { |
| 5647 | export_rdev(rdev); |
| 5648 | return err; |
| 5649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | return 0; |
| 5653 | } |
| 5654 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5655 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | { |
| 5657 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5658 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | rdev = find_rdev(mddev, dev); |
| 5661 | if (!rdev) |
| 5662 | return -ENXIO; |
| 5663 | |
| 5664 | if (rdev->raid_disk >= 0) |
| 5665 | goto busy; |
| 5666 | |
| 5667 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5668 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5669 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
| 5671 | return 0; |
| 5672 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5673 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5675 | return -EBUSY; |
| 5676 | } |
| 5677 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5678 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | { |
| 5680 | char b[BDEVNAME_SIZE]; |
| 5681 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5682 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | |
| 5684 | if (!mddev->pers) |
| 5685 | return -ENODEV; |
| 5686 | |
| 5687 | if (mddev->major_version != 0) { |
| 5688 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5689 | " version-0 superblocks.\n", |
| 5690 | mdname(mddev)); |
| 5691 | return -EINVAL; |
| 5692 | } |
| 5693 | if (!mddev->pers->hot_add_disk) { |
| 5694 | printk(KERN_WARNING |
| 5695 | "%s: personality does not support diskops!\n", |
| 5696 | mdname(mddev)); |
| 5697 | return -EINVAL; |
| 5698 | } |
| 5699 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5700 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | if (IS_ERR(rdev)) { |
| 5702 | printk(KERN_WARNING |
| 5703 | "md: error, md_import_device() returned %ld\n", |
| 5704 | PTR_ERR(rdev)); |
| 5705 | return -EINVAL; |
| 5706 | } |
| 5707 | |
| 5708 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5709 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5711 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5713 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5715 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | printk(KERN_WARNING |
| 5717 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5718 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5719 | err = -EINVAL; |
| 5720 | goto abort_export; |
| 5721 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5722 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5724 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5725 | err = bind_rdev_to_array(rdev, mddev); |
| 5726 | if (err) |
| 5727 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | |
| 5729 | /* |
| 5730 | * The rest should better be atomic, we can have disk failures |
| 5731 | * noticed in interrupt contexts ... |
| 5732 | */ |
| 5733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | rdev->raid_disk = -1; |
| 5735 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5736 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | |
| 5738 | /* |
| 5739 | * Kick recovery, maybe this spare has to be added to the |
| 5740 | * array immediately. |
| 5741 | */ |
| 5742 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5743 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5744 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | return 0; |
| 5746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | abort_export: |
| 5748 | export_rdev(rdev); |
| 5749 | return err; |
| 5750 | } |
| 5751 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5752 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5753 | { |
| 5754 | int err; |
| 5755 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5756 | if (mddev->pers) { |
| 5757 | if (!mddev->pers->quiesce) |
| 5758 | return -EBUSY; |
| 5759 | if (mddev->recovery || mddev->sync_thread) |
| 5760 | return -EBUSY; |
| 5761 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5762 | } |
| 5763 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5764 | |
| 5765 | if (fd >= 0) { |
| 5766 | if (mddev->bitmap) |
| 5767 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5768 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5769 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5770 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5771 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5772 | mdname(mddev)); |
| 5773 | return -EBADF; |
| 5774 | } |
| 5775 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5776 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5777 | if (err) { |
| 5778 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5779 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5780 | fput(mddev->bitmap_info.file); |
| 5781 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5782 | return err; |
| 5783 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5784 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5785 | } else if (mddev->bitmap == NULL) |
| 5786 | return -ENOENT; /* cannot remove what isn't there */ |
| 5787 | err = 0; |
| 5788 | if (mddev->pers) { |
| 5789 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5790 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5791 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5792 | if (!err) |
| 5793 | err = bitmap_load(mddev); |
| 5794 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5795 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5796 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5797 | fd = -1; /* make sure to put the file */ |
| 5798 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5799 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5800 | } |
| 5801 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5802 | if (mddev->bitmap_info.file) { |
| 5803 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5804 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5805 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5806 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5809 | return err; |
| 5810 | } |
| 5811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | /* |
| 5813 | * set_array_info is used two different ways |
| 5814 | * The original usage is when creating a new array. |
| 5815 | * In this usage, raid_disks is > 0 and it together with |
| 5816 | * level, size, not_persistent,layout,chunksize determine the |
| 5817 | * shape of the array. |
| 5818 | * This will always create an array with a type-0.90.0 superblock. |
| 5819 | * The newer usage is when assembling an array. |
| 5820 | * In this case raid_disks will be 0, and the major_version field is |
| 5821 | * use to determine which style super-blocks are to be found on the devices. |
| 5822 | * The minor and patch _version numbers are also kept incase the |
| 5823 | * super_block handler wishes to interpret them. |
| 5824 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5825 | 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] | 5826 | { |
| 5827 | |
| 5828 | if (info->raid_disks == 0) { |
| 5829 | /* just setting version number for superblock loading */ |
| 5830 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5831 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | super_types[info->major_version].name == NULL) { |
| 5833 | /* maybe try to auto-load a module? */ |
| 5834 | printk(KERN_INFO |
| 5835 | "md: superblock version %d not known\n", |
| 5836 | info->major_version); |
| 5837 | return -EINVAL; |
| 5838 | } |
| 5839 | mddev->major_version = info->major_version; |
| 5840 | mddev->minor_version = info->minor_version; |
| 5841 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5842 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5843 | /* ensure mddev_put doesn't delete this now that there |
| 5844 | * is some minimal configuration. |
| 5845 | */ |
| 5846 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | return 0; |
| 5848 | } |
| 5849 | mddev->major_version = MD_MAJOR_VERSION; |
| 5850 | mddev->minor_version = MD_MINOR_VERSION; |
| 5851 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5852 | mddev->ctime = get_seconds(); |
| 5853 | |
| 5854 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5855 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5856 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | mddev->raid_disks = info->raid_disks; |
| 5858 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5859 | * openned |
| 5860 | */ |
| 5861 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5862 | mddev->recovery_cp = MaxSector; |
| 5863 | else |
| 5864 | mddev->recovery_cp = 0; |
| 5865 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5866 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | |
| 5868 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5869 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | |
| 5871 | mddev->max_disks = MD_SB_DISKS; |
| 5872 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5873 | if (mddev->persistent) |
| 5874 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5875 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5877 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5878 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5879 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5880 | mddev->reshape_position = MaxSector; |
| 5881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | /* |
| 5883 | * Generate a 128 bit UUID |
| 5884 | */ |
| 5885 | get_random_bytes(mddev->uuid, 16); |
| 5886 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5887 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5888 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5889 | mddev->new_layout = mddev->layout; |
| 5890 | mddev->delta_disks = 0; |
| 5891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | return 0; |
| 5893 | } |
| 5894 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5895 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5896 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5897 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5898 | |
| 5899 | if (mddev->external_size) |
| 5900 | return; |
| 5901 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5902 | mddev->array_sectors = array_sectors; |
| 5903 | } |
| 5904 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5905 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5906 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5907 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5908 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5909 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5910 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5911 | |
| 5912 | if (mddev->pers->resize == NULL) |
| 5913 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5914 | /* The "num_sectors" is the number of sectors of each device that |
| 5915 | * is used. This can only make sense for arrays with redundancy. |
| 5916 | * linear and raid0 always use whatever space is available. We can only |
| 5917 | * consider changing this number if no resync or reconstruction is |
| 5918 | * 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] | 5919 | * 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] | 5920 | * of each device. If num_sectors is zero, we find the largest size |
| 5921 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5922 | */ |
| 5923 | if (mddev->sync_thread) |
| 5924 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5925 | if (mddev->bitmap) |
| 5926 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5927 | * grow, and re-add. |
| 5928 | */ |
| 5929 | return -EBUSY; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5930 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5931 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5932 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5933 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5934 | num_sectors = avail; |
| 5935 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5936 | return -ENOSPC; |
| 5937 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5938 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5939 | if (!rv) |
| 5940 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5941 | return rv; |
| 5942 | } |
| 5943 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5944 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5945 | { |
| 5946 | int rv; |
| 5947 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5948 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5949 | return -EINVAL; |
| 5950 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5951 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5952 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5953 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5954 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5955 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5956 | |
| 5957 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5958 | if (rv < 0) |
| 5959 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5960 | return rv; |
| 5961 | } |
| 5962 | |
| 5963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | /* |
| 5965 | * update_array_info is used to change the configuration of an |
| 5966 | * on-line array. |
| 5967 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5968 | * fields in the info are checked against the array. |
| 5969 | * Any differences that cannot be handled will cause an error. |
| 5970 | * Normally, only one change can be managed at a time. |
| 5971 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5972 | 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] | 5973 | { |
| 5974 | int rv = 0; |
| 5975 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5976 | int state = 0; |
| 5977 | |
| 5978 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5979 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5980 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | |
| 5982 | if (mddev->major_version != info->major_version || |
| 5983 | mddev->minor_version != info->minor_version || |
| 5984 | /* mddev->patch_version != info->patch_version || */ |
| 5985 | mddev->ctime != info->ctime || |
| 5986 | mddev->level != info->level || |
| 5987 | /* mddev->layout != info->layout || */ |
| 5988 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5989 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5990 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5991 | ((state^info->state) & 0xfffffe00) |
| 5992 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | return -EINVAL; |
| 5994 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5995 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5996 | cnt++; |
| 5997 | if (mddev->raid_disks != info->raid_disks) |
| 5998 | cnt++; |
| 5999 | if (mddev->layout != info->layout) |
| 6000 | cnt++; |
| 6001 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6002 | cnt++; |
| 6003 | if (cnt == 0) |
| 6004 | return 0; |
| 6005 | if (cnt > 1) |
| 6006 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | |
| 6008 | if (mddev->layout != info->layout) { |
| 6009 | /* Change layout |
| 6010 | * we don't need to do anything at the md level, the |
| 6011 | * personality will take care of it all. |
| 6012 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6013 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6015 | else { |
| 6016 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6017 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6018 | if (rv) |
| 6019 | mddev->new_layout = mddev->layout; |
| 6020 | return rv; |
| 6021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6023 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6024 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6026 | if (mddev->raid_disks != info->raid_disks) |
| 6027 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6029 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6030 | if (mddev->pers->quiesce == NULL) |
| 6031 | return -EINVAL; |
| 6032 | if (mddev->recovery || mddev->sync_thread) |
| 6033 | return -EBUSY; |
| 6034 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6035 | /* add the bitmap */ |
| 6036 | if (mddev->bitmap) |
| 6037 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6038 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6039 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6040 | mddev->bitmap_info.offset = |
| 6041 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6042 | mddev->pers->quiesce(mddev, 1); |
| 6043 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6044 | if (!rv) |
| 6045 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6046 | if (rv) |
| 6047 | bitmap_destroy(mddev); |
| 6048 | mddev->pers->quiesce(mddev, 0); |
| 6049 | } else { |
| 6050 | /* remove the bitmap */ |
| 6051 | if (!mddev->bitmap) |
| 6052 | return -ENOENT; |
| 6053 | if (mddev->bitmap->file) |
| 6054 | return -EINVAL; |
| 6055 | mddev->pers->quiesce(mddev, 1); |
| 6056 | bitmap_destroy(mddev); |
| 6057 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6058 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6059 | } |
| 6060 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6061 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | return rv; |
| 6063 | } |
| 6064 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6065 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6067 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
| 6069 | if (mddev->pers == NULL) |
| 6070 | return -ENODEV; |
| 6071 | |
| 6072 | rdev = find_rdev(mddev, dev); |
| 6073 | if (!rdev) |
| 6074 | return -ENODEV; |
| 6075 | |
| 6076 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6077 | if (!test_bit(Faulty, &rdev->flags)) |
| 6078 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | return 0; |
| 6080 | } |
| 6081 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6082 | /* |
| 6083 | * We have a problem here : there is no easy way to give a CHS |
| 6084 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6085 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6086 | * dosfs just mad... ;-) |
| 6087 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6088 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6089 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6090 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6091 | |
| 6092 | geo->heads = 2; |
| 6093 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6094 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6095 | return 0; |
| 6096 | } |
| 6097 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6098 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | unsigned int cmd, unsigned long arg) |
| 6100 | { |
| 6101 | int err = 0; |
| 6102 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6103 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6104 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6106 | switch (cmd) { |
| 6107 | case RAID_VERSION: |
| 6108 | case GET_ARRAY_INFO: |
| 6109 | case GET_DISK_INFO: |
| 6110 | break; |
| 6111 | default: |
| 6112 | if (!capable(CAP_SYS_ADMIN)) |
| 6113 | return -EACCES; |
| 6114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | |
| 6116 | /* |
| 6117 | * Commands dealing with the RAID driver but not any |
| 6118 | * particular array: |
| 6119 | */ |
| 6120 | switch (cmd) |
| 6121 | { |
| 6122 | case RAID_VERSION: |
| 6123 | err = get_version(argp); |
| 6124 | goto done; |
| 6125 | |
| 6126 | case PRINT_RAID_DEBUG: |
| 6127 | err = 0; |
| 6128 | md_print_devices(); |
| 6129 | goto done; |
| 6130 | |
| 6131 | #ifndef MODULE |
| 6132 | case RAID_AUTORUN: |
| 6133 | err = 0; |
| 6134 | autostart_arrays(arg); |
| 6135 | goto done; |
| 6136 | #endif |
| 6137 | default:; |
| 6138 | } |
| 6139 | |
| 6140 | /* |
| 6141 | * Commands creating/starting a new array: |
| 6142 | */ |
| 6143 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6144 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | |
| 6146 | if (!mddev) { |
| 6147 | BUG(); |
| 6148 | goto abort; |
| 6149 | } |
| 6150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | err = mddev_lock(mddev); |
| 6152 | if (err) { |
| 6153 | printk(KERN_INFO |
| 6154 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6155 | err, cmd); |
| 6156 | goto abort; |
| 6157 | } |
| 6158 | |
| 6159 | switch (cmd) |
| 6160 | { |
| 6161 | case SET_ARRAY_INFO: |
| 6162 | { |
| 6163 | mdu_array_info_t info; |
| 6164 | if (!arg) |
| 6165 | memset(&info, 0, sizeof(info)); |
| 6166 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6167 | err = -EFAULT; |
| 6168 | goto abort_unlock; |
| 6169 | } |
| 6170 | if (mddev->pers) { |
| 6171 | err = update_array_info(mddev, &info); |
| 6172 | if (err) { |
| 6173 | printk(KERN_WARNING "md: couldn't update" |
| 6174 | " array info. %d\n", err); |
| 6175 | goto abort_unlock; |
| 6176 | } |
| 6177 | goto done_unlock; |
| 6178 | } |
| 6179 | if (!list_empty(&mddev->disks)) { |
| 6180 | printk(KERN_WARNING |
| 6181 | "md: array %s already has disks!\n", |
| 6182 | mdname(mddev)); |
| 6183 | err = -EBUSY; |
| 6184 | goto abort_unlock; |
| 6185 | } |
| 6186 | if (mddev->raid_disks) { |
| 6187 | printk(KERN_WARNING |
| 6188 | "md: array %s already initialised!\n", |
| 6189 | mdname(mddev)); |
| 6190 | err = -EBUSY; |
| 6191 | goto abort_unlock; |
| 6192 | } |
| 6193 | err = set_array_info(mddev, &info); |
| 6194 | if (err) { |
| 6195 | printk(KERN_WARNING "md: couldn't set" |
| 6196 | " array info. %d\n", err); |
| 6197 | goto abort_unlock; |
| 6198 | } |
| 6199 | } |
| 6200 | goto done_unlock; |
| 6201 | |
| 6202 | default:; |
| 6203 | } |
| 6204 | |
| 6205 | /* |
| 6206 | * Commands querying/configuring an existing array: |
| 6207 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6208 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6209 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6210 | if ((!mddev->raid_disks && !mddev->external) |
| 6211 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6212 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6213 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | err = -ENODEV; |
| 6215 | goto abort_unlock; |
| 6216 | } |
| 6217 | |
| 6218 | /* |
| 6219 | * Commands even a read-only array can execute: |
| 6220 | */ |
| 6221 | switch (cmd) |
| 6222 | { |
| 6223 | case GET_ARRAY_INFO: |
| 6224 | err = get_array_info(mddev, argp); |
| 6225 | goto done_unlock; |
| 6226 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6227 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6228 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6229 | goto done_unlock; |
| 6230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | case GET_DISK_INFO: |
| 6232 | err = get_disk_info(mddev, argp); |
| 6233 | goto done_unlock; |
| 6234 | |
| 6235 | case RESTART_ARRAY_RW: |
| 6236 | err = restart_array(mddev); |
| 6237 | goto done_unlock; |
| 6238 | |
| 6239 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6240 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | goto done_unlock; |
| 6242 | |
| 6243 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6244 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | goto done_unlock; |
| 6246 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6247 | case BLKROSET: |
| 6248 | if (get_user(ro, (int __user *)(arg))) { |
| 6249 | err = -EFAULT; |
| 6250 | goto done_unlock; |
| 6251 | } |
| 6252 | err = -EINVAL; |
| 6253 | |
| 6254 | /* if the bdev is going readonly the value of mddev->ro |
| 6255 | * does not matter, no writes are coming |
| 6256 | */ |
| 6257 | if (ro) |
| 6258 | goto done_unlock; |
| 6259 | |
| 6260 | /* are we are already prepared for writes? */ |
| 6261 | if (mddev->ro != 1) |
| 6262 | goto done_unlock; |
| 6263 | |
| 6264 | /* transitioning to readauto need only happen for |
| 6265 | * arrays that call md_write_start |
| 6266 | */ |
| 6267 | if (mddev->pers) { |
| 6268 | err = restart_array(mddev); |
| 6269 | if (err == 0) { |
| 6270 | mddev->ro = 2; |
| 6271 | set_disk_ro(mddev->gendisk, 0); |
| 6272 | } |
| 6273 | } |
| 6274 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | /* |
| 6278 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6279 | * superblock, so we do not allow them on read-only arrays. |
| 6280 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6281 | * here and hit the 'default' below, so only disallow |
| 6282 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6284 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6285 | if (mddev->ro == 2) { |
| 6286 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6287 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6288 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6289 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6290 | } else { |
| 6291 | err = -EROFS; |
| 6292 | goto abort_unlock; |
| 6293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | switch (cmd) |
| 6297 | { |
| 6298 | case ADD_NEW_DISK: |
| 6299 | { |
| 6300 | mdu_disk_info_t info; |
| 6301 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6302 | err = -EFAULT; |
| 6303 | else |
| 6304 | err = add_new_disk(mddev, &info); |
| 6305 | goto done_unlock; |
| 6306 | } |
| 6307 | |
| 6308 | case HOT_REMOVE_DISK: |
| 6309 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6310 | goto done_unlock; |
| 6311 | |
| 6312 | case HOT_ADD_DISK: |
| 6313 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6314 | goto done_unlock; |
| 6315 | |
| 6316 | case SET_DISK_FAULTY: |
| 6317 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6318 | goto done_unlock; |
| 6319 | |
| 6320 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6321 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | goto done_unlock; |
| 6323 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6324 | case SET_BITMAP_FILE: |
| 6325 | err = set_bitmap_file(mddev, (int)arg); |
| 6326 | goto done_unlock; |
| 6327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6329 | err = -EINVAL; |
| 6330 | goto abort_unlock; |
| 6331 | } |
| 6332 | |
| 6333 | done_unlock: |
| 6334 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6335 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6336 | err != -EINVAL) |
| 6337 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | mddev_unlock(mddev); |
| 6339 | |
| 6340 | return err; |
| 6341 | done: |
| 6342 | if (err) |
| 6343 | MD_BUG(); |
| 6344 | abort: |
| 6345 | return err; |
| 6346 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6347 | #ifdef CONFIG_COMPAT |
| 6348 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6349 | unsigned int cmd, unsigned long arg) |
| 6350 | { |
| 6351 | switch (cmd) { |
| 6352 | case HOT_REMOVE_DISK: |
| 6353 | case HOT_ADD_DISK: |
| 6354 | case SET_DISK_FAULTY: |
| 6355 | case SET_BITMAP_FILE: |
| 6356 | /* These take in integer arg, do not convert */ |
| 6357 | break; |
| 6358 | default: |
| 6359 | arg = (unsigned long)compat_ptr(arg); |
| 6360 | break; |
| 6361 | } |
| 6362 | |
| 6363 | return md_ioctl(bdev, mode, cmd, arg); |
| 6364 | } |
| 6365 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6367 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | { |
| 6369 | /* |
| 6370 | * Succeed if we can lock the mddev, which confirms that |
| 6371 | * it isn't being stopped right now. |
| 6372 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6373 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | int err; |
| 6375 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6376 | if (mddev->gendisk != bdev->bd_disk) { |
| 6377 | /* we are racing with mddev_put which is discarding this |
| 6378 | * bd_disk. |
| 6379 | */ |
| 6380 | mddev_put(mddev); |
| 6381 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6382 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6383 | /* Then retry the open from the top */ |
| 6384 | return -ERESTARTSYS; |
| 6385 | } |
| 6386 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6387 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6388 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | goto out; |
| 6390 | |
| 6391 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6392 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6393 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6395 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | out: |
| 6397 | return err; |
| 6398 | } |
| 6399 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6400 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6402 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6404 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6405 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | mddev_put(mddev); |
| 6407 | |
| 6408 | return 0; |
| 6409 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6410 | |
| 6411 | static int md_media_changed(struct gendisk *disk) |
| 6412 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6413 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6414 | |
| 6415 | return mddev->changed; |
| 6416 | } |
| 6417 | |
| 6418 | static int md_revalidate(struct gendisk *disk) |
| 6419 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6420 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6421 | |
| 6422 | mddev->changed = 0; |
| 6423 | return 0; |
| 6424 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6425 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | { |
| 6427 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6428 | .open = md_open, |
| 6429 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6430 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6431 | #ifdef CONFIG_COMPAT |
| 6432 | .compat_ioctl = md_compat_ioctl, |
| 6433 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6434 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6435 | .media_changed = md_media_changed, |
| 6436 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | }; |
| 6438 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6439 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6441 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | /* |
| 6444 | * md_thread is a 'system-thread', it's priority should be very |
| 6445 | * high. We avoid resource deadlocks individually in each |
| 6446 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6447 | * the very same RT priority as kswapd, thus we will never get |
| 6448 | * into a priority inversion deadlock. |
| 6449 | * |
| 6450 | * we definitely have to have equal or higher priority than |
| 6451 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6452 | * many dirty RAID5 blocks. |
| 6453 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6455 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6456 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6458 | /* We need to wait INTERRUPTIBLE so that |
| 6459 | * we don't add to the load-average. |
| 6460 | * That means we need to be sure no signals are |
| 6461 | * pending |
| 6462 | */ |
| 6463 | if (signal_pending(current)) |
| 6464 | flush_signals(current); |
| 6465 | |
| 6466 | wait_event_interruptible_timeout |
| 6467 | (thread->wqueue, |
| 6468 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6469 | || kthread_should_stop(), |
| 6470 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6472 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6473 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6474 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | return 0; |
| 6478 | } |
| 6479 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6480 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | { |
| 6482 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6483 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6485 | wake_up(&thread->wqueue); |
| 6486 | } |
| 6487 | } |
| 6488 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6489 | 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] | 6490 | const char *name) |
| 6491 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6492 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6494 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | if (!thread) |
| 6496 | return NULL; |
| 6497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | init_waitqueue_head(&thread->wqueue); |
| 6499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | thread->run = run; |
| 6501 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6502 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6503 | thread->tsk = kthread_run(md_thread, thread, |
| 6504 | "%s_%s", |
| 6505 | mdname(thread->mddev), |
| 6506 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6507 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | kfree(thread); |
| 6509 | return NULL; |
| 6510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | return thread; |
| 6512 | } |
| 6513 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6514 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6516 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6517 | if (!thread) |
| 6518 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6519 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6520 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6521 | * non-existent thread |
| 6522 | */ |
| 6523 | spin_lock(&pers_lock); |
| 6524 | *threadp = NULL; |
| 6525 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6526 | |
| 6527 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | kfree(thread); |
| 6529 | } |
| 6530 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6531 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | { |
| 6533 | if (!mddev) { |
| 6534 | MD_BUG(); |
| 6535 | return; |
| 6536 | } |
| 6537 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6538 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6540 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6541 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | return; |
| 6543 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6544 | if (mddev->degraded) |
| 6545 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6546 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6548 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6549 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6550 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6551 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6552 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | } |
| 6554 | |
| 6555 | /* seq_file implementation /proc/mdstat */ |
| 6556 | |
| 6557 | static void status_unused(struct seq_file *seq) |
| 6558 | { |
| 6559 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6560 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | seq_printf(seq, "unused devices: "); |
| 6563 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6564 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | char b[BDEVNAME_SIZE]; |
| 6566 | i++; |
| 6567 | seq_printf(seq, "%s ", |
| 6568 | bdevname(rdev->bdev,b)); |
| 6569 | } |
| 6570 | if (!i) |
| 6571 | seq_printf(seq, "<none>"); |
| 6572 | |
| 6573 | seq_printf(seq, "\n"); |
| 6574 | } |
| 6575 | |
| 6576 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6577 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6579 | sector_t max_sectors, resync, res; |
| 6580 | unsigned long dt, db; |
| 6581 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6582 | int scale; |
| 6583 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6585 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | |
| 6587 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6588 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6590 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | |
| 6592 | /* |
| 6593 | * Should not happen. |
| 6594 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6595 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | MD_BUG(); |
| 6597 | return; |
| 6598 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6599 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6600 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6601 | * u32, as those are the requirements for sector_div. |
| 6602 | * Thus 'scale' must be at least 10 |
| 6603 | */ |
| 6604 | scale = 10; |
| 6605 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6606 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6607 | scale++; |
| 6608 | } |
| 6609 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6610 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6611 | |
| 6612 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6614 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | seq_printf(seq, "["); |
| 6616 | for (i = 0; i < x; i++) |
| 6617 | seq_printf(seq, "="); |
| 6618 | seq_printf(seq, ">"); |
| 6619 | for (i = 0; i < y; i++) |
| 6620 | seq_printf(seq, "."); |
| 6621 | seq_printf(seq, "] "); |
| 6622 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6623 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6624 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6625 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6626 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6627 | "check" : |
| 6628 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6629 | "resync" : "recovery"))), |
| 6630 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6631 | (unsigned long long) resync/2, |
| 6632 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | |
| 6634 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | * dt: time from mark until now |
| 6636 | * db: blocks written from mark until now |
| 6637 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6638 | * |
| 6639 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6640 | * So we divide before multiply in case it is 32bit and close |
| 6641 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6642 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6643 | * near the end of resync when the number of remaining sectors |
| 6644 | * is close to 'db'. |
| 6645 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6646 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | */ |
| 6648 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6649 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6650 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6651 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6653 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6654 | sector_div(rt, db/32+1); |
| 6655 | rt *= dt; |
| 6656 | rt >>= 5; |
| 6657 | |
| 6658 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6659 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6661 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6665 | { |
| 6666 | struct list_head *tmp; |
| 6667 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6668 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
| 6670 | if (l >= 0x10000) |
| 6671 | return NULL; |
| 6672 | if (!l--) |
| 6673 | /* header */ |
| 6674 | return (void*)1; |
| 6675 | |
| 6676 | spin_lock(&all_mddevs_lock); |
| 6677 | list_for_each(tmp,&all_mddevs) |
| 6678 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6679 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | mddev_get(mddev); |
| 6681 | spin_unlock(&all_mddevs_lock); |
| 6682 | return mddev; |
| 6683 | } |
| 6684 | spin_unlock(&all_mddevs_lock); |
| 6685 | if (!l--) |
| 6686 | return (void*)2;/* tail */ |
| 6687 | return NULL; |
| 6688 | } |
| 6689 | |
| 6690 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6691 | { |
| 6692 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6693 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6694 | |
| 6695 | ++*pos; |
| 6696 | if (v == (void*)2) |
| 6697 | return NULL; |
| 6698 | |
| 6699 | spin_lock(&all_mddevs_lock); |
| 6700 | if (v == (void*)1) |
| 6701 | tmp = all_mddevs.next; |
| 6702 | else |
| 6703 | tmp = mddev->all_mddevs.next; |
| 6704 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6705 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | else { |
| 6707 | next_mddev = (void*)2; |
| 6708 | *pos = 0x10000; |
| 6709 | } |
| 6710 | spin_unlock(&all_mddevs_lock); |
| 6711 | |
| 6712 | if (v != (void*)1) |
| 6713 | mddev_put(mddev); |
| 6714 | return next_mddev; |
| 6715 | |
| 6716 | } |
| 6717 | |
| 6718 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6719 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6720 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6721 | |
| 6722 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6723 | mddev_put(mddev); |
| 6724 | } |
| 6725 | |
| 6726 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6727 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6728 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6729 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6730 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | |
| 6732 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6733 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | seq_printf(seq, "Personalities : "); |
| 6735 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6736 | list_for_each_entry(pers, &pers_list, list) |
| 6737 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | |
| 6739 | spin_unlock(&pers_lock); |
| 6740 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6741 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | return 0; |
| 6743 | } |
| 6744 | if (v == (void*)2) { |
| 6745 | status_unused(seq); |
| 6746 | return 0; |
| 6747 | } |
| 6748 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6749 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6753 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6754 | mddev->pers ? "" : "in"); |
| 6755 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6756 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6757 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6758 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6759 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | seq_printf(seq, " %s", mddev->pers->name); |
| 6761 | } |
| 6762 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6763 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6764 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | char b[BDEVNAME_SIZE]; |
| 6766 | seq_printf(seq, " %s[%d]", |
| 6767 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6768 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6769 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6770 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | seq_printf(seq, "(F)"); |
| 6772 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6773 | } |
| 6774 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6775 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6776 | if (test_bit(Replacement, &rdev->flags)) |
| 6777 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6778 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | } |
| 6780 | |
| 6781 | if (!list_empty(&mddev->disks)) { |
| 6782 | if (mddev->pers) |
| 6783 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6784 | (unsigned long long) |
| 6785 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | else |
| 6787 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6788 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6790 | if (mddev->persistent) { |
| 6791 | if (mddev->major_version != 0 || |
| 6792 | mddev->minor_version != 90) { |
| 6793 | seq_printf(seq," super %d.%d", |
| 6794 | mddev->major_version, |
| 6795 | mddev->minor_version); |
| 6796 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6797 | } else if (mddev->external) |
| 6798 | seq_printf(seq, " super external:%s", |
| 6799 | mddev->metadata_type); |
| 6800 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6801 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
| 6803 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6804 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6806 | if (mddev->pers->sync_request) { |
| 6807 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6808 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6809 | seq_printf(seq, "\n "); |
| 6810 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6811 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6812 | else if (mddev->recovery_cp < MaxSector) |
| 6813 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6814 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6815 | } else |
| 6816 | seq_printf(seq, "\n "); |
| 6817 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 6818 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | |
| 6820 | seq_printf(seq, "\n"); |
| 6821 | } |
| 6822 | mddev_unlock(mddev); |
| 6823 | |
| 6824 | return 0; |
| 6825 | } |
| 6826 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6827 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | .start = md_seq_start, |
| 6829 | .next = md_seq_next, |
| 6830 | .stop = md_seq_stop, |
| 6831 | .show = md_seq_show, |
| 6832 | }; |
| 6833 | |
| 6834 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6835 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6836 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | int error; |
| 6838 | |
| 6839 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6840 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6841 | return error; |
| 6842 | |
| 6843 | seq = file->private_data; |
| 6844 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | return error; |
| 6846 | } |
| 6847 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6848 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6849 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6850 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6851 | int mask; |
| 6852 | |
| 6853 | poll_wait(filp, &md_event_waiters, wait); |
| 6854 | |
| 6855 | /* always allow read */ |
| 6856 | mask = POLLIN | POLLRDNORM; |
| 6857 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6858 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6859 | mask |= POLLERR | POLLPRI; |
| 6860 | return mask; |
| 6861 | } |
| 6862 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6863 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6864 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | .open = md_seq_open, |
| 6866 | .read = seq_read, |
| 6867 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6868 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6869 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | }; |
| 6871 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6872 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6875 | list_add_tail(&p->list, &pers_list); |
| 6876 | 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] | 6877 | spin_unlock(&pers_lock); |
| 6878 | return 0; |
| 6879 | } |
| 6880 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6881 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6883 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6885 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | spin_unlock(&pers_lock); |
| 6887 | return 0; |
| 6888 | } |
| 6889 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6890 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6892 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6894 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | |
| 6896 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6897 | rcu_read_lock(); |
| 6898 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6900 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6901 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6902 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6903 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6904 | * as sync_io is counted when a request starts, and |
| 6905 | * disk_stats is counted when it completes. |
| 6906 | * So resync activity will cause curr_events to be smaller than |
| 6907 | * when there was no such activity. |
| 6908 | * non-sync IO will cause disk_stat to increase without |
| 6909 | * increasing sync_io so curr_events will (eventually) |
| 6910 | * be larger than it was before. Once it becomes |
| 6911 | * substantially larger, the test below will cause |
| 6912 | * the array to appear non-idle, and resync will slow |
| 6913 | * down. |
| 6914 | * If there is a lot of outstanding resync activity when |
| 6915 | * we set last_event to curr_events, then all that activity |
| 6916 | * completing might cause the array to appear non-idle |
| 6917 | * and resync will be slowed down even though there might |
| 6918 | * not have been non-resync activity. This will only |
| 6919 | * happen once though. 'last_events' will soon reflect |
| 6920 | * the state where there is little or no outstanding |
| 6921 | * resync requests, and further resync activity will |
| 6922 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6923 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6925 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | rdev->last_events = curr_events; |
| 6927 | idle = 0; |
| 6928 | } |
| 6929 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6930 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | return idle; |
| 6932 | } |
| 6933 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6934 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | { |
| 6936 | /* another "blocks" (512byte) blocks have been synced */ |
| 6937 | atomic_sub(blocks, &mddev->recovery_active); |
| 6938 | wake_up(&mddev->recovery_wait); |
| 6939 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6940 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | md_wakeup_thread(mddev->thread); |
| 6942 | // stop recovery, signal do_sync .... |
| 6943 | } |
| 6944 | } |
| 6945 | |
| 6946 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6947 | /* md_write_start(mddev, bi) |
| 6948 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6949 | * in superblock) before writing, schedule a superblock update |
| 6950 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6951 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6952 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6954 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6955 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6956 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6957 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6958 | BUG_ON(mddev->ro == 1); |
| 6959 | if (mddev->ro == 2) { |
| 6960 | /* need to switch to read/write */ |
| 6961 | mddev->ro = 0; |
| 6962 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6963 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6964 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6965 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6966 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6967 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6968 | if (mddev->safemode == 1) |
| 6969 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6970 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6971 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6972 | if (mddev->in_sync) { |
| 6973 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6974 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6975 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6976 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6977 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6978 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6979 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6980 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6981 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6982 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6983 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6984 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | } |
| 6986 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6987 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | { |
| 6989 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6990 | if (mddev->safemode == 2) |
| 6991 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6992 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6994 | } |
| 6995 | } |
| 6996 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6997 | /* md_allow_write(mddev) |
| 6998 | * Calling this ensures that the array is marked 'active' so that writes |
| 6999 | * may proceed without blocking. It is important to call this before |
| 7000 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7001 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7002 | * |
| 7003 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7004 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7005 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7006 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7007 | { |
| 7008 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7009 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7010 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7011 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7012 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7013 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7014 | |
| 7015 | spin_lock_irq(&mddev->write_lock); |
| 7016 | if (mddev->in_sync) { |
| 7017 | mddev->in_sync = 0; |
| 7018 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7019 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7020 | if (mddev->safemode_delay && |
| 7021 | mddev->safemode == 0) |
| 7022 | mddev->safemode = 1; |
| 7023 | spin_unlock_irq(&mddev->write_lock); |
| 7024 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7025 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7026 | } else |
| 7027 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7028 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7029 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7030 | return -EAGAIN; |
| 7031 | else |
| 7032 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7033 | } |
| 7034 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | #define SYNC_MARKS 10 |
| 7037 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7038 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7040 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | unsigned int currspeed = 0, |
| 7042 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7043 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | unsigned long mark[SYNC_MARKS]; |
| 7045 | sector_t mark_cnt[SYNC_MARKS]; |
| 7046 | int last_mark,m; |
| 7047 | struct list_head *tmp; |
| 7048 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7049 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7050 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7051 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | |
| 7053 | /* just incase thread restarts... */ |
| 7054 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7055 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7056 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7057 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7059 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7060 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7061 | desc = "data-check"; |
| 7062 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7063 | desc = "requested-resync"; |
| 7064 | else |
| 7065 | desc = "resync"; |
| 7066 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7067 | desc = "reshape"; |
| 7068 | else |
| 7069 | desc = "recovery"; |
| 7070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7071 | /* we overload curr_resync somewhat here. |
| 7072 | * 0 == not engaged in resync at all |
| 7073 | * 2 == checking that there is no conflict with another sync |
| 7074 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7075 | * commense |
| 7076 | * other == active in resync - this many blocks |
| 7077 | * |
| 7078 | * Before starting a resync we must have set curr_resync to |
| 7079 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7080 | * less than ours. When we find one that is the same or higher |
| 7081 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7082 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7083 | * This will mean we have to start checking from the beginning again. |
| 7084 | * |
| 7085 | */ |
| 7086 | |
| 7087 | do { |
| 7088 | mddev->curr_resync = 2; |
| 7089 | |
| 7090 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7091 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7092 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7093 | |
| 7094 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7096 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | if (mddev2 == mddev) |
| 7098 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7099 | if (!mddev->parallel_resync |
| 7100 | && mddev2->curr_resync |
| 7101 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | DEFINE_WAIT(wq); |
| 7103 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7104 | /* arbitrarily yield */ |
| 7105 | mddev->curr_resync = 1; |
| 7106 | wake_up(&resync_wait); |
| 7107 | } |
| 7108 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7109 | /* no need to wait here, we can wait the next |
| 7110 | * time 'round when curr_resync == 2 |
| 7111 | */ |
| 7112 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7113 | /* We need to wait 'interruptible' so as not to |
| 7114 | * contribute to the load average, and not to |
| 7115 | * be caught by 'softlockup' |
| 7116 | */ |
| 7117 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7118 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7119 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7120 | printk(KERN_INFO "md: delaying %s of %s" |
| 7121 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7123 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7125 | if (signal_pending(current)) |
| 7126 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | schedule(); |
| 7128 | finish_wait(&resync_wait, &wq); |
| 7129 | goto try_again; |
| 7130 | } |
| 7131 | finish_wait(&resync_wait, &wq); |
| 7132 | } |
| 7133 | } |
| 7134 | } while (mddev->curr_resync < 2); |
| 7135 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7136 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7137 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7139 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | */ |
| 7141 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7142 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7143 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7144 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7145 | j = mddev->resync_min; |
| 7146 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7147 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7148 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7149 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7150 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7151 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7153 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7154 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7155 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7156 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7157 | if (rdev->raid_disk >= 0 && |
| 7158 | !test_bit(Faulty, &rdev->flags) && |
| 7159 | !test_bit(In_sync, &rdev->flags) && |
| 7160 | rdev->recovery_offset < j) |
| 7161 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7162 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7165 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7166 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7167 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7168 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7169 | "(but not more than %d KB/sec) for %s.\n", |
| 7170 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7172 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7173 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7174 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7176 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7177 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7178 | } |
| 7179 | last_mark = 0; |
| 7180 | mddev->resync_mark = mark[last_mark]; |
| 7181 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7182 | |
| 7183 | /* |
| 7184 | * Tune reconstruction: |
| 7185 | */ |
| 7186 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7187 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7188 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | |
| 7190 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7191 | last_check = 0; |
| 7192 | |
| 7193 | if (j>2) { |
| 7194 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7195 | "md: resuming %s of %s from checkpoint.\n", |
| 7196 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | mddev->curr_resync = j; |
| 7198 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7199 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | |
| 7201 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7202 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7204 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7205 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7206 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7207 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7208 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7209 | > (max_sectors >> 4)) || |
| 7210 | (j - mddev->curr_resync_completed)*2 |
| 7211 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7212 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7213 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7214 | wait_event(mddev->recovery_wait, |
| 7215 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7216 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7217 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7218 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7219 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7220 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7221 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7222 | /* As this condition is controlled by user-space, |
| 7223 | * we can block indefinitely, so use '_interruptible' |
| 7224 | * to avoid triggering warnings. |
| 7225 | */ |
| 7226 | flush_signals(current); /* just in case */ |
| 7227 | wait_event_interruptible(mddev->recovery_wait, |
| 7228 | mddev->resync_max > j |
| 7229 | || kthread_should_stop()); |
| 7230 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7231 | |
| 7232 | if (kthread_should_stop()) |
| 7233 | goto interrupted; |
| 7234 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7235 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7236 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7237 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7238 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | goto out; |
| 7240 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7241 | |
| 7242 | if (!skipped) { /* actual IO requested */ |
| 7243 | io_sectors += sectors; |
| 7244 | atomic_add(sectors, &mddev->recovery_active); |
| 7245 | } |
| 7246 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7247 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7248 | break; |
| 7249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | j += sectors; |
| 7251 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7252 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7253 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7254 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7255 | * visible in /proc/mdstat |
| 7256 | */ |
| 7257 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7258 | |
| 7259 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | continue; |
| 7261 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7262 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | repeat: |
| 7264 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7265 | /* step marks */ |
| 7266 | int next = (last_mark+1) % SYNC_MARKS; |
| 7267 | |
| 7268 | mddev->resync_mark = mark[next]; |
| 7269 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7270 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7271 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | last_mark = next; |
| 7273 | } |
| 7274 | |
| 7275 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7276 | if (kthread_should_stop()) |
| 7277 | goto interrupted; |
| 7278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | |
| 7280 | /* |
| 7281 | * this loop exits only if either when we are slower than |
| 7282 | * the 'hard' speed limit, or the system was IO-idle for |
| 7283 | * a jiffy. |
| 7284 | * the system might be non-idle CPU-wise, but we only care |
| 7285 | * about not overloading the IO subsystem. (things like an |
| 7286 | * e2fsck being done on the RAID array should execute fast) |
| 7287 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | cond_resched(); |
| 7289 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7290 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7291 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7293 | if (currspeed > speed_min(mddev)) { |
| 7294 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7295 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7296 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | goto repeat; |
| 7298 | } |
| 7299 | } |
| 7300 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7301 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7302 | /* |
| 7303 | * this also signals 'finished resyncing' to md_stop |
| 7304 | */ |
| 7305 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7307 | |
| 7308 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7309 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7311 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7312 | mddev->curr_resync > 2) { |
| 7313 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7314 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7315 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7316 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7317 | "md: checkpointing %s of %s.\n", |
| 7318 | desc, mdname(mddev)); |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7319 | mddev->recovery_cp = |
| 7320 | mddev->curr_resync_completed; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7321 | } |
| 7322 | } else |
| 7323 | mddev->recovery_cp = MaxSector; |
| 7324 | } else { |
| 7325 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7326 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7327 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7328 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7329 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7330 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7331 | !test_bit(Faulty, &rdev->flags) && |
| 7332 | !test_bit(In_sync, &rdev->flags) && |
| 7333 | rdev->recovery_offset < mddev->curr_resync) |
| 7334 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7335 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7338 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7339 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7341 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7342 | /* We completed so min/max setting can be forgotten if used. */ |
| 7343 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7344 | mddev->resync_min = 0; |
| 7345 | mddev->resync_max = MaxSector; |
| 7346 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7347 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | mddev->curr_resync = 0; |
| 7349 | wake_up(&resync_wait); |
| 7350 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7351 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7352 | return; |
| 7353 | |
| 7354 | interrupted: |
| 7355 | /* |
| 7356 | * got a signal, exit. |
| 7357 | */ |
| 7358 | printk(KERN_INFO |
| 7359 | "md: md_do_sync() got signal ... exiting\n"); |
| 7360 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7361 | goto out; |
| 7362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7364 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7366 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7367 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7368 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7369 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7370 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7371 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7372 | mddev->curr_resync_completed = 0; |
| 7373 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7374 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7375 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7376 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7377 | (test_bit(Faulty, &rdev->flags) || |
| 7378 | ! test_bit(In_sync, &rdev->flags)) && |
| 7379 | atomic_read(&rdev->nr_pending)==0) { |
| 7380 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7381 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7382 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7383 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7384 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7385 | } |
| 7386 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7387 | if (removed) |
| 7388 | sysfs_notify(&mddev->kobj, NULL, |
| 7389 | "degraded"); |
| 7390 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7391 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7392 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7393 | if (rdev->raid_disk >= 0 && |
| 7394 | !test_bit(In_sync, &rdev->flags) && |
| 7395 | !test_bit(Faulty, &rdev->flags)) |
| 7396 | spares++; |
| 7397 | if (rdev->raid_disk < 0 |
| 7398 | && !test_bit(Faulty, &rdev->flags)) { |
| 7399 | rdev->recovery_offset = 0; |
| 7400 | if (mddev->pers-> |
| 7401 | hot_add_disk(mddev, rdev) == 0) { |
| 7402 | if (sysfs_link_rdev(mddev, rdev)) |
| 7403 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7404 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7405 | md_new_event(mddev); |
| 7406 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7407 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7408 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7409 | } |
| 7410 | return spares; |
| 7411 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7412 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7413 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7414 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7415 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7416 | |
| 7417 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7418 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7419 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7420 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7421 | /* success...*/ |
| 7422 | /* activate any spares */ |
| 7423 | if (mddev->pers->spare_active(mddev)) |
| 7424 | sysfs_notify(&mddev->kobj, NULL, |
| 7425 | "degraded"); |
| 7426 | } |
| 7427 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7428 | mddev->pers->finish_reshape) |
| 7429 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7430 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7431 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7432 | * information must be scrapped. Also if any device is now |
| 7433 | * In_sync we must scrape the saved_raid_disk for that device |
| 7434 | * do the superblock for an incrementally recovered device |
| 7435 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7436 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7437 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7438 | if (!mddev->degraded || |
| 7439 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7440 | rdev->saved_raid_disk = -1; |
| 7441 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7442 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7443 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7444 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7445 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7446 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7447 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7448 | /* flag recovery needed just to double check */ |
| 7449 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7450 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7451 | md_new_event(mddev); |
Jonathan Brassow | 768e587 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7452 | if (mddev->event_work.func) |
| 7453 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7454 | } |
| 7455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | /* |
| 7457 | * This routine is regularly called by all per-raid-array threads to |
| 7458 | * deal with generic issues like resync and super-block update. |
| 7459 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7460 | * need this as they never do any recovery or update the superblock. |
| 7461 | * |
| 7462 | * It does not do any resync itself, but rather "forks" off other threads |
| 7463 | * to do that as needed. |
| 7464 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7465 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7466 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | * and wakeups up this thread which will reap the thread and finish up. |
| 7468 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7469 | * |
| 7470 | * The overall approach is: |
| 7471 | * 1/ if the superblock needs updating, update it. |
| 7472 | * 2/ If a recovery thread is running, don't do anything else. |
| 7473 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7474 | * 4/ If there are any faulty devices, remove them. |
| 7475 | * 5/ If array is degraded, try to add spares devices |
| 7476 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7477 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7478 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7480 | if (mddev->suspended) |
| 7481 | return; |
| 7482 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7483 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7484 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7485 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7486 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7487 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7488 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7489 | mdname(mddev)); |
| 7490 | mddev->safemode = 2; |
| 7491 | } |
| 7492 | flush_signals(current); |
| 7493 | } |
| 7494 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7495 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7496 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7497 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7498 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7500 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7501 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7502 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7503 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7504 | )) |
| 7505 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7506 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7507 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7508 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7509 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7510 | if (mddev->ro) { |
| 7511 | /* Only thing we do on a ro array is remove |
| 7512 | * failed devices. |
| 7513 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7514 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7515 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7516 | if (rdev->raid_disk >= 0 && |
| 7517 | !test_bit(Blocked, &rdev->flags) && |
| 7518 | test_bit(Faulty, &rdev->flags) && |
| 7519 | atomic_read(&rdev->nr_pending)==0) { |
| 7520 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7521 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7522 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7523 | rdev->raid_disk = -1; |
| 7524 | } |
| 7525 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7526 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7527 | goto unlock; |
| 7528 | } |
| 7529 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7530 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7531 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7532 | spin_lock_irq(&mddev->write_lock); |
| 7533 | if (mddev->safemode && |
| 7534 | !atomic_read(&mddev->writes_pending) && |
| 7535 | !mddev->in_sync && |
| 7536 | mddev->recovery_cp == MaxSector) { |
| 7537 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7538 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7539 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7540 | } |
| 7541 | if (mddev->safemode == 1) |
| 7542 | mddev->safemode = 0; |
| 7543 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7544 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7545 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7546 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7547 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7548 | if (mddev->flags) |
| 7549 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7552 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7553 | /* resync/recovery still happening */ |
| 7554 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7555 | goto unlock; |
| 7556 | } |
| 7557 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7558 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7559 | goto unlock; |
| 7560 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7561 | /* Set RUNNING before clearing NEEDED to avoid |
| 7562 | * any transients in the value of "sync_action". |
| 7563 | */ |
| 7564 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7565 | /* Clear some bits that don't mean anything, but |
| 7566 | * might be left set |
| 7567 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7568 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7569 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7570 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7571 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7572 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7573 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7574 | /* no recovery is running. |
| 7575 | * remove any failed drives, then |
| 7576 | * add spares if possible. |
| 7577 | * Spare are also removed and re-added, to allow |
| 7578 | * the personality to fail the re-add. |
| 7579 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7580 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7581 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7582 | if (mddev->pers->check_reshape == NULL || |
| 7583 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7584 | /* Cannot proceed */ |
| 7585 | goto unlock; |
| 7586 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7587 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7588 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7589 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7590 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7591 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7592 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7593 | } else if (mddev->recovery_cp < MaxSector) { |
| 7594 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7595 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7596 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7597 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7598 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7600 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7601 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7602 | /* We are adding a device or devices to an array |
| 7603 | * which has the bitmap stored on all devices. |
| 7604 | * So make sure all bitmap pages get written |
| 7605 | */ |
| 7606 | bitmap_write_all(mddev->bitmap); |
| 7607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7608 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7609 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7610 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7611 | if (!mddev->sync_thread) { |
| 7612 | printk(KERN_ERR "%s: could not start resync" |
| 7613 | " thread...\n", |
| 7614 | mdname(mddev)); |
| 7615 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7616 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7617 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7618 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7619 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7620 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7621 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7622 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7623 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7624 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | } |
| 7626 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7627 | if (!mddev->sync_thread) { |
| 7628 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7629 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7630 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7631 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7632 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7634 | mddev_unlock(mddev); |
| 7635 | } |
| 7636 | } |
| 7637 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7638 | 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] | 7639 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7640 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7641 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7642 | !test_bit(Blocked, &rdev->flags) && |
| 7643 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7644 | msecs_to_jiffies(5000)); |
| 7645 | rdev_dec_pending(rdev, mddev); |
| 7646 | } |
| 7647 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7648 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7649 | |
| 7650 | /* Bad block management. |
| 7651 | * We can record which blocks on each device are 'bad' and so just |
| 7652 | * fail those blocks, or that stripe, rather than the whole device. |
| 7653 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7654 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7655 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7656 | * A 'shift' can be set so that larger blocks are tracked and |
| 7657 | * consequently larger devices can be covered. |
| 7658 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7659 | * |
| 7660 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7661 | * might need to retry if it is very unlucky. |
| 7662 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7663 | * so we use the write_seqlock_irq variant. |
| 7664 | * |
| 7665 | * When looking for a bad block we specify a range and want to |
| 7666 | * know if any block in the range is bad. So we binary-search |
| 7667 | * to the last range that starts at-or-before the given endpoint, |
| 7668 | * (or "before the sector after the target range") |
| 7669 | * then see if it ends after the given start. |
| 7670 | * We return |
| 7671 | * 0 if there are no known bad blocks in the range |
| 7672 | * 1 if there are known bad block which are all acknowledged |
| 7673 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7674 | * plus the start/length of the first bad section we overlap. |
| 7675 | */ |
| 7676 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7677 | sector_t *first_bad, int *bad_sectors) |
| 7678 | { |
| 7679 | int hi; |
| 7680 | int lo = 0; |
| 7681 | u64 *p = bb->page; |
| 7682 | int rv = 0; |
| 7683 | sector_t target = s + sectors; |
| 7684 | unsigned seq; |
| 7685 | |
| 7686 | if (bb->shift > 0) { |
| 7687 | /* round the start down, and the end up */ |
| 7688 | s >>= bb->shift; |
| 7689 | target += (1<<bb->shift) - 1; |
| 7690 | target >>= bb->shift; |
| 7691 | sectors = target - s; |
| 7692 | } |
| 7693 | /* 'target' is now the first block after the bad range */ |
| 7694 | |
| 7695 | retry: |
| 7696 | seq = read_seqbegin(&bb->lock); |
| 7697 | |
| 7698 | hi = bb->count; |
| 7699 | |
| 7700 | /* Binary search between lo and hi for 'target' |
| 7701 | * i.e. for the last range that starts before 'target' |
| 7702 | */ |
| 7703 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7704 | * are known not to be the last range before target. |
| 7705 | * VARIANT: hi-lo is the number of possible |
| 7706 | * ranges, and decreases until it reaches 1 |
| 7707 | */ |
| 7708 | while (hi - lo > 1) { |
| 7709 | int mid = (lo + hi) / 2; |
| 7710 | sector_t a = BB_OFFSET(p[mid]); |
| 7711 | if (a < target) |
| 7712 | /* This could still be the one, earlier ranges |
| 7713 | * could not. */ |
| 7714 | lo = mid; |
| 7715 | else |
| 7716 | /* This and later ranges are definitely out. */ |
| 7717 | hi = mid; |
| 7718 | } |
| 7719 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7720 | if (hi > lo) { |
| 7721 | /* need to check all range that end after 's' to see if |
| 7722 | * any are unacknowledged. |
| 7723 | */ |
| 7724 | while (lo >= 0 && |
| 7725 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7726 | if (BB_OFFSET(p[lo]) < target) { |
| 7727 | /* starts before the end, and finishes after |
| 7728 | * the start, so they must overlap |
| 7729 | */ |
| 7730 | if (rv != -1 && BB_ACK(p[lo])) |
| 7731 | rv = 1; |
| 7732 | else |
| 7733 | rv = -1; |
| 7734 | *first_bad = BB_OFFSET(p[lo]); |
| 7735 | *bad_sectors = BB_LEN(p[lo]); |
| 7736 | } |
| 7737 | lo--; |
| 7738 | } |
| 7739 | } |
| 7740 | |
| 7741 | if (read_seqretry(&bb->lock, seq)) |
| 7742 | goto retry; |
| 7743 | |
| 7744 | return rv; |
| 7745 | } |
| 7746 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7747 | |
| 7748 | /* |
| 7749 | * Add a range of bad blocks to the table. |
| 7750 | * This might extend the table, or might contract it |
| 7751 | * if two adjacent ranges can be merged. |
| 7752 | * We binary-search to find the 'insertion' point, then |
| 7753 | * decide how best to handle it. |
| 7754 | */ |
| 7755 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7756 | int acknowledged) |
| 7757 | { |
| 7758 | u64 *p; |
| 7759 | int lo, hi; |
| 7760 | int rv = 1; |
| 7761 | |
| 7762 | if (bb->shift < 0) |
| 7763 | /* badblocks are disabled */ |
| 7764 | return 0; |
| 7765 | |
| 7766 | if (bb->shift) { |
| 7767 | /* round the start down, and the end up */ |
| 7768 | sector_t next = s + sectors; |
| 7769 | s >>= bb->shift; |
| 7770 | next += (1<<bb->shift) - 1; |
| 7771 | next >>= bb->shift; |
| 7772 | sectors = next - s; |
| 7773 | } |
| 7774 | |
| 7775 | write_seqlock_irq(&bb->lock); |
| 7776 | |
| 7777 | p = bb->page; |
| 7778 | lo = 0; |
| 7779 | hi = bb->count; |
| 7780 | /* Find the last range that starts at-or-before 's' */ |
| 7781 | while (hi - lo > 1) { |
| 7782 | int mid = (lo + hi) / 2; |
| 7783 | sector_t a = BB_OFFSET(p[mid]); |
| 7784 | if (a <= s) |
| 7785 | lo = mid; |
| 7786 | else |
| 7787 | hi = mid; |
| 7788 | } |
| 7789 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7790 | hi = lo; |
| 7791 | |
| 7792 | if (hi > lo) { |
| 7793 | /* we found a range that might merge with the start |
| 7794 | * of our new range |
| 7795 | */ |
| 7796 | sector_t a = BB_OFFSET(p[lo]); |
| 7797 | sector_t e = a + BB_LEN(p[lo]); |
| 7798 | int ack = BB_ACK(p[lo]); |
| 7799 | if (e >= s) { |
| 7800 | /* Yes, we can merge with a previous range */ |
| 7801 | if (s == a && s + sectors >= e) |
| 7802 | /* new range covers old */ |
| 7803 | ack = acknowledged; |
| 7804 | else |
| 7805 | ack = ack && acknowledged; |
| 7806 | |
| 7807 | if (e < s + sectors) |
| 7808 | e = s + sectors; |
| 7809 | if (e - a <= BB_MAX_LEN) { |
| 7810 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7811 | s = e; |
| 7812 | } else { |
| 7813 | /* does not all fit in one range, |
| 7814 | * make p[lo] maximal |
| 7815 | */ |
| 7816 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7817 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7818 | s = a + BB_MAX_LEN; |
| 7819 | } |
| 7820 | sectors = e - s; |
| 7821 | } |
| 7822 | } |
| 7823 | if (sectors && hi < bb->count) { |
| 7824 | /* 'hi' points to the first range that starts after 's'. |
| 7825 | * Maybe we can merge with the start of that range */ |
| 7826 | sector_t a = BB_OFFSET(p[hi]); |
| 7827 | sector_t e = a + BB_LEN(p[hi]); |
| 7828 | int ack = BB_ACK(p[hi]); |
| 7829 | if (a <= s + sectors) { |
| 7830 | /* merging is possible */ |
| 7831 | if (e <= s + sectors) { |
| 7832 | /* full overlap */ |
| 7833 | e = s + sectors; |
| 7834 | ack = acknowledged; |
| 7835 | } else |
| 7836 | ack = ack && acknowledged; |
| 7837 | |
| 7838 | a = s; |
| 7839 | if (e - a <= BB_MAX_LEN) { |
| 7840 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7841 | s = e; |
| 7842 | } else { |
| 7843 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7844 | s = a + BB_MAX_LEN; |
| 7845 | } |
| 7846 | sectors = e - s; |
| 7847 | lo = hi; |
| 7848 | hi++; |
| 7849 | } |
| 7850 | } |
| 7851 | if (sectors == 0 && hi < bb->count) { |
| 7852 | /* we might be able to combine lo and hi */ |
| 7853 | /* Note: 's' is at the end of 'lo' */ |
| 7854 | sector_t a = BB_OFFSET(p[hi]); |
| 7855 | int lolen = BB_LEN(p[lo]); |
| 7856 | int hilen = BB_LEN(p[hi]); |
| 7857 | int newlen = lolen + hilen - (s - a); |
| 7858 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7859 | /* yes, we can combine them */ |
| 7860 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7861 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7862 | memmove(p + hi, p + hi + 1, |
| 7863 | (bb->count - hi - 1) * 8); |
| 7864 | bb->count--; |
| 7865 | } |
| 7866 | } |
| 7867 | while (sectors) { |
| 7868 | /* didn't merge (it all). |
| 7869 | * Need to add a range just before 'hi' */ |
| 7870 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7871 | /* No room for more */ |
| 7872 | rv = 0; |
| 7873 | break; |
| 7874 | } else { |
| 7875 | int this_sectors = sectors; |
| 7876 | memmove(p + hi + 1, p + hi, |
| 7877 | (bb->count - hi) * 8); |
| 7878 | bb->count++; |
| 7879 | |
| 7880 | if (this_sectors > BB_MAX_LEN) |
| 7881 | this_sectors = BB_MAX_LEN; |
| 7882 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7883 | sectors -= this_sectors; |
| 7884 | s += this_sectors; |
| 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7889 | if (!acknowledged) |
| 7890 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7891 | write_sequnlock_irq(&bb->lock); |
| 7892 | |
| 7893 | return rv; |
| 7894 | } |
| 7895 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7896 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7897 | int acknowledged) |
| 7898 | { |
| 7899 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7900 | s + rdev->data_offset, sectors, acknowledged); |
| 7901 | if (rv) { |
| 7902 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 7903 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7904 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7905 | md_wakeup_thread(rdev->mddev->thread); |
| 7906 | } |
| 7907 | return rv; |
| 7908 | } |
| 7909 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7910 | |
| 7911 | /* |
| 7912 | * Remove a range of bad blocks from the table. |
| 7913 | * This may involve extending the table if we spilt a region, |
| 7914 | * but it must not fail. So if the table becomes full, we just |
| 7915 | * drop the remove request. |
| 7916 | */ |
| 7917 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7918 | { |
| 7919 | u64 *p; |
| 7920 | int lo, hi; |
| 7921 | sector_t target = s + sectors; |
| 7922 | int rv = 0; |
| 7923 | |
| 7924 | if (bb->shift > 0) { |
| 7925 | /* When clearing we round the start up and the end down. |
| 7926 | * This should not matter as the shift should align with |
| 7927 | * the block size and no rounding should ever be needed. |
| 7928 | * However it is better the think a block is bad when it |
| 7929 | * isn't than to think a block is not bad when it is. |
| 7930 | */ |
| 7931 | s += (1<<bb->shift) - 1; |
| 7932 | s >>= bb->shift; |
| 7933 | target >>= bb->shift; |
| 7934 | sectors = target - s; |
| 7935 | } |
| 7936 | |
| 7937 | write_seqlock_irq(&bb->lock); |
| 7938 | |
| 7939 | p = bb->page; |
| 7940 | lo = 0; |
| 7941 | hi = bb->count; |
| 7942 | /* Find the last range that starts before 'target' */ |
| 7943 | while (hi - lo > 1) { |
| 7944 | int mid = (lo + hi) / 2; |
| 7945 | sector_t a = BB_OFFSET(p[mid]); |
| 7946 | if (a < target) |
| 7947 | lo = mid; |
| 7948 | else |
| 7949 | hi = mid; |
| 7950 | } |
| 7951 | if (hi > lo) { |
| 7952 | /* p[lo] is the last range that could overlap the |
| 7953 | * current range. Earlier ranges could also overlap, |
| 7954 | * but only this one can overlap the end of the range. |
| 7955 | */ |
| 7956 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7957 | /* Partial overlap, leave the tail of this range */ |
| 7958 | int ack = BB_ACK(p[lo]); |
| 7959 | sector_t a = BB_OFFSET(p[lo]); |
| 7960 | sector_t end = a + BB_LEN(p[lo]); |
| 7961 | |
| 7962 | if (a < s) { |
| 7963 | /* we need to split this range */ |
| 7964 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7965 | rv = 0; |
| 7966 | goto out; |
| 7967 | } |
| 7968 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7969 | bb->count++; |
| 7970 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7971 | lo++; |
| 7972 | } |
| 7973 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7974 | /* there is no longer an overlap */ |
| 7975 | hi = lo; |
| 7976 | lo--; |
| 7977 | } |
| 7978 | while (lo >= 0 && |
| 7979 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7980 | /* This range does overlap */ |
| 7981 | if (BB_OFFSET(p[lo]) < s) { |
| 7982 | /* Keep the early parts of this range. */ |
| 7983 | int ack = BB_ACK(p[lo]); |
| 7984 | sector_t start = BB_OFFSET(p[lo]); |
| 7985 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7986 | /* now low doesn't overlap, so.. */ |
| 7987 | break; |
| 7988 | } |
| 7989 | lo--; |
| 7990 | } |
| 7991 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7992 | * anything between needs to be discarded |
| 7993 | */ |
| 7994 | if (hi - lo > 1) { |
| 7995 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7996 | bb->count -= (hi - lo - 1); |
| 7997 | } |
| 7998 | } |
| 7999 | |
| 8000 | bb->changed = 1; |
| 8001 | out: |
| 8002 | write_sequnlock_irq(&bb->lock); |
| 8003 | return rv; |
| 8004 | } |
| 8005 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8006 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8007 | { |
| 8008 | return md_clear_badblocks(&rdev->badblocks, |
| 8009 | s + rdev->data_offset, |
| 8010 | sectors); |
| 8011 | } |
| 8012 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8013 | |
| 8014 | /* |
| 8015 | * Acknowledge all bad blocks in a list. |
| 8016 | * This only succeeds if ->changed is clear. It is used by |
| 8017 | * in-kernel metadata updates |
| 8018 | */ |
| 8019 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8020 | { |
| 8021 | if (bb->page == NULL || bb->changed) |
| 8022 | /* no point even trying */ |
| 8023 | return; |
| 8024 | write_seqlock_irq(&bb->lock); |
| 8025 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8026 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8027 | u64 *p = bb->page; |
| 8028 | int i; |
| 8029 | for (i = 0; i < bb->count ; i++) { |
| 8030 | if (!BB_ACK(p[i])) { |
| 8031 | sector_t start = BB_OFFSET(p[i]); |
| 8032 | int len = BB_LEN(p[i]); |
| 8033 | p[i] = BB_MAKE(start, len, 1); |
| 8034 | } |
| 8035 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8036 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8037 | } |
| 8038 | write_sequnlock_irq(&bb->lock); |
| 8039 | } |
| 8040 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8041 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8042 | /* sysfs access to bad-blocks list. |
| 8043 | * We present two files. |
| 8044 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8045 | * are recorded as bad. The list is truncated to fit within |
| 8046 | * the one-page limit of sysfs. |
| 8047 | * Writing "sector length" to this file adds an acknowledged |
| 8048 | * bad block list. |
| 8049 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8050 | * been acknowledged. Writing to this file adds bad blocks |
| 8051 | * without acknowledging them. This is largely for testing. |
| 8052 | */ |
| 8053 | |
| 8054 | static ssize_t |
| 8055 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8056 | { |
| 8057 | size_t len; |
| 8058 | int i; |
| 8059 | u64 *p = bb->page; |
| 8060 | unsigned seq; |
| 8061 | |
| 8062 | if (bb->shift < 0) |
| 8063 | return 0; |
| 8064 | |
| 8065 | retry: |
| 8066 | seq = read_seqbegin(&bb->lock); |
| 8067 | |
| 8068 | len = 0; |
| 8069 | i = 0; |
| 8070 | |
| 8071 | while (len < PAGE_SIZE && i < bb->count) { |
| 8072 | sector_t s = BB_OFFSET(p[i]); |
| 8073 | unsigned int length = BB_LEN(p[i]); |
| 8074 | int ack = BB_ACK(p[i]); |
| 8075 | i++; |
| 8076 | |
| 8077 | if (unack && ack) |
| 8078 | continue; |
| 8079 | |
| 8080 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8081 | (unsigned long long)s << bb->shift, |
| 8082 | length << bb->shift); |
| 8083 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8084 | if (unack && len == 0) |
| 8085 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8086 | |
| 8087 | if (read_seqretry(&bb->lock, seq)) |
| 8088 | goto retry; |
| 8089 | |
| 8090 | return len; |
| 8091 | } |
| 8092 | |
| 8093 | #define DO_DEBUG 1 |
| 8094 | |
| 8095 | static ssize_t |
| 8096 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8097 | { |
| 8098 | unsigned long long sector; |
| 8099 | int length; |
| 8100 | char newline; |
| 8101 | #ifdef DO_DEBUG |
| 8102 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8103 | * Normally only a successful write may clear a badblock |
| 8104 | */ |
| 8105 | int clear = 0; |
| 8106 | if (page[0] == '-') { |
| 8107 | clear = 1; |
| 8108 | page++; |
| 8109 | } |
| 8110 | #endif /* DO_DEBUG */ |
| 8111 | |
| 8112 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8113 | case 3: |
| 8114 | if (newline != '\n') |
| 8115 | return -EINVAL; |
| 8116 | case 2: |
| 8117 | if (length <= 0) |
| 8118 | return -EINVAL; |
| 8119 | break; |
| 8120 | default: |
| 8121 | return -EINVAL; |
| 8122 | } |
| 8123 | |
| 8124 | #ifdef DO_DEBUG |
| 8125 | if (clear) { |
| 8126 | md_clear_badblocks(bb, sector, length); |
| 8127 | return len; |
| 8128 | } |
| 8129 | #endif /* DO_DEBUG */ |
| 8130 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8131 | return len; |
| 8132 | else |
| 8133 | return -ENOSPC; |
| 8134 | } |
| 8135 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8136 | static int md_notify_reboot(struct notifier_block *this, |
| 8137 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8138 | { |
| 8139 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8140 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8141 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8142 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8143 | for_each_mddev(mddev, tmp) { |
| 8144 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8145 | if (mddev->pers) |
| 8146 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8147 | mddev->safemode = 2; |
| 8148 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8149 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8150 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8151 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8152 | /* |
| 8153 | * certain more exotic SCSI devices are known to be |
| 8154 | * volatile wrt too early system reboots. While the |
| 8155 | * right place to handle this issue is the given |
| 8156 | * driver, we do want to have a safe RAID driver ... |
| 8157 | */ |
| 8158 | if (need_delay) |
| 8159 | mdelay(1000*1); |
| 8160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | return NOTIFY_DONE; |
| 8162 | } |
| 8163 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8164 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8165 | .notifier_call = md_notify_reboot, |
| 8166 | .next = NULL, |
| 8167 | .priority = INT_MAX, /* before any real devices */ |
| 8168 | }; |
| 8169 | |
| 8170 | static void md_geninit(void) |
| 8171 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8172 | 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] | 8173 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8174 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8175 | } |
| 8176 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8177 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8178 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8179 | int ret = -ENOMEM; |
| 8180 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8181 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8182 | if (!md_wq) |
| 8183 | goto err_wq; |
| 8184 | |
| 8185 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8186 | if (!md_misc_wq) |
| 8187 | goto err_misc_wq; |
| 8188 | |
| 8189 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8190 | goto err_md; |
| 8191 | |
| 8192 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8193 | goto err_mdp; |
| 8194 | mdp_major = ret; |
| 8195 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8196 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8197 | md_probe, NULL, NULL); |
| 8198 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8199 | md_probe, NULL, NULL); |
| 8200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8201 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8202 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8203 | |
| 8204 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8205 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8206 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8207 | err_mdp: |
| 8208 | unregister_blkdev(MD_MAJOR, "md"); |
| 8209 | err_md: |
| 8210 | destroy_workqueue(md_misc_wq); |
| 8211 | err_misc_wq: |
| 8212 | destroy_workqueue(md_wq); |
| 8213 | err_wq: |
| 8214 | return ret; |
| 8215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8216 | |
| 8217 | #ifndef MODULE |
| 8218 | |
| 8219 | /* |
| 8220 | * Searches all registered partitions for autorun RAID arrays |
| 8221 | * at boot time. |
| 8222 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8223 | |
| 8224 | static LIST_HEAD(all_detected_devices); |
| 8225 | struct detected_devices_node { |
| 8226 | struct list_head list; |
| 8227 | dev_t dev; |
| 8228 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8229 | |
| 8230 | void md_autodetect_dev(dev_t dev) |
| 8231 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8232 | struct detected_devices_node *node_detected_dev; |
| 8233 | |
| 8234 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8235 | if (node_detected_dev) { |
| 8236 | node_detected_dev->dev = dev; |
| 8237 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8238 | } else { |
| 8239 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8240 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | |
| 8245 | static void autostart_arrays(int part) |
| 8246 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8247 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8248 | struct detected_devices_node *node_detected_dev; |
| 8249 | dev_t dev; |
| 8250 | int i_scanned, i_passed; |
| 8251 | |
| 8252 | i_scanned = 0; |
| 8253 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | |
| 8255 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8256 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8257 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8258 | i_scanned++; |
| 8259 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8260 | struct detected_devices_node, list); |
| 8261 | list_del(&node_detected_dev->list); |
| 8262 | dev = node_detected_dev->dev; |
| 8263 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8264 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8265 | if (IS_ERR(rdev)) |
| 8266 | continue; |
| 8267 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8268 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8269 | MD_BUG(); |
| 8270 | continue; |
| 8271 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8272 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8273 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8274 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8276 | |
| 8277 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8278 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | |
| 8280 | autorun_devices(part); |
| 8281 | } |
| 8282 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8283 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8284 | |
| 8285 | static __exit void md_exit(void) |
| 8286 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8287 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8289 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8290 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8291 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8292 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8293 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8294 | unregister_blkdev(mdp_major, "mdp"); |
| 8295 | unregister_reboot_notifier(&md_notifier); |
| 8296 | unregister_sysctl_table(raid_table_header); |
| 8297 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8298 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8299 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8300 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8301 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8302 | destroy_workqueue(md_misc_wq); |
| 8303 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8304 | } |
| 8305 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8306 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8307 | module_exit(md_exit) |
| 8308 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8309 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8310 | { |
| 8311 | return sprintf(buffer, "%d", start_readonly); |
| 8312 | } |
| 8313 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8314 | { |
| 8315 | char *e; |
| 8316 | int num = simple_strtoul(val, &e, 10); |
| 8317 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8318 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8319 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8320 | } |
| 8321 | return -EINVAL; |
| 8322 | } |
| 8323 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8324 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8325 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8326 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8327 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8329 | EXPORT_SYMBOL(register_md_personality); |
| 8330 | EXPORT_SYMBOL(unregister_md_personality); |
| 8331 | EXPORT_SYMBOL(md_error); |
| 8332 | EXPORT_SYMBOL(md_done_sync); |
| 8333 | EXPORT_SYMBOL(md_write_start); |
| 8334 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8335 | EXPORT_SYMBOL(md_register_thread); |
| 8336 | EXPORT_SYMBOL(md_unregister_thread); |
| 8337 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8338 | EXPORT_SYMBOL(md_check_recovery); |
| 8339 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8340 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8341 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8342 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |